1(*
2 * Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 *)
6
7signature OLD_DEFS=
8sig
9end
10
11structure Old_Defs : OLD_DEFS =
12struct
13
14fun read ctxt (b, str) =
15  Syntax.read_prop ctxt str handle ERROR msg =>
16    cat_error msg ("The error(s) above occurred in definition " ^ Binding.print b);
17
18fun add_defs ctxt ((unchecked, overloaded), args) thy =
19 (legacy_feature "Old 'defs' command -- use 'definition' (with 'overloading') instead";
20  thy |>
21    (if unchecked then Global_Theory.add_defs_unchecked else Global_Theory.add_defs)
22      overloaded
23      (map (fn ((b, ax), srcs) => ((b, read ctxt (b, ax)), map (Attrib.attribute_cmd ctxt) srcs)) args));
24
25val opt_unchecked_overloaded =
26  Scan.optional (@{keyword "("} |-- Parse.!!!
27    (((@{keyword "unchecked"} >> K true) --
28        Scan.optional (@{keyword "overloaded"} >> K true) false ||
29      @{keyword "overloaded"} >> K (false, true)) --| @{keyword ")"})) (false, false);
30
31fun syntax_alias global_alias local_alias b name =
32  Local_Theory.declaration {syntax = true, pervasive = true} (fn phi =>
33    let val b' = Morphism.binding phi b
34    in Context.mapping (global_alias b' name) (local_alias b' name) end);
35
36val const_alias = syntax_alias Sign.const_alias Proof_Context.const_alias;
37
38(* Proof_Context.fact_alias doesn't work here, so we need to play a few tricks to get the right fact name *)
39
40val _ =
41  Outer_Syntax.command @{command_keyword defs} "define constants"
42    (Parse.opt_target -- (opt_unchecked_overloaded --
43      Scan.repeat1 (Parse_Spec.thm_name ":" -- Parse.prop >> (fn ((x, y), z) => ((x, z), y))))
44      >> (fn (target, (b, args)) => Toplevel.local_theory NONE target (fn lthy =>
45          let
46               val args' = map (fn ((b, def), x) => ((Binding.suffix_name "__internal__" b, def), x)) args
47               val (thms, lthy') = Local_Theory.background_theory_result (add_defs lthy (b, args')) lthy;
48               val lthy'' = fold2 (fn ((b, _), _) => fn thm =>
49                  fn lthy =>
50                    let val (_, lthy') = Local_Theory.note ((b,[]), [thm]) lthy
51                    in lthy' end) args thms lthy';
52               val lthy''' = Local_Theory.raw_theory (fold (fn thm =>
53                    Global_Theory.hide_fact true (Thm.derivation_name thm)) thms) lthy''
54          in lthy''' end)));
55
56
57val _ =
58  Outer_Syntax.command @{command_keyword consts'} "localized consts declaration"
59    (Parse.opt_target -- Scan.repeat1 Parse.const_binding >>
60      (fn (target, bs) => Toplevel.local_theory NONE target (fn lthy =>
61         fold (fn (b, raw_typ, mixfix) => fn lthy =>
62           let val (Const (nm, _), lthy') = Local_Theory.background_theory_result
63             (Sign.declare_const lthy ((b, Syntax.read_typ lthy raw_typ), mixfix)) lthy;
64          in const_alias b nm lthy' end) bs lthy)))
65end
66