18744
|
1 |
(* Title: Pure/Isar/local_theory.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Makarius
|
|
4 |
|
|
5 |
Local theory operations, with optional target locale.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature LOCAL_THEORY =
|
|
9 |
sig
|
|
10 |
val map_theory: (theory -> theory) -> Proof.context -> Proof.context
|
|
11 |
val map_theory_result: (theory -> 'a * theory) -> Proof.context -> 'a * Proof.context
|
|
12 |
val init: string option -> theory -> Proof.context
|
|
13 |
val exit: Proof.context -> theory
|
|
14 |
val params_of: Proof.context -> (string * typ) list
|
|
15 |
val consts: (string * typ * mixfix) list -> Proof.context ->
|
|
16 |
((string * typ) list * term list) * Proof.context
|
|
17 |
end;
|
|
18 |
|
|
19 |
structure LocalTheory: LOCAL_THEORY =
|
|
20 |
struct
|
|
21 |
|
|
22 |
(* local context data *)
|
|
23 |
|
|
24 |
structure Data = ProofDataFun
|
|
25 |
(
|
|
26 |
val name = "Isar/local_theory";
|
|
27 |
type T =
|
|
28 |
{locale: string option,
|
|
29 |
params: (string * typ) list,
|
|
30 |
hyps: term list,
|
|
31 |
exit: theory -> theory};
|
|
32 |
fun init _ = {locale = NONE, params = [], hyps = [], exit = I};
|
|
33 |
fun print _ _ = ();
|
|
34 |
);
|
|
35 |
|
|
36 |
val _ = Context.add_setup Data.init;
|
|
37 |
|
|
38 |
|
|
39 |
(* theory *)
|
|
40 |
|
|
41 |
fun map_theory f ctxt =
|
|
42 |
ProofContext.transfer (f (ProofContext.theory_of ctxt)) ctxt;
|
|
43 |
|
|
44 |
fun map_theory_result f ctxt =
|
|
45 |
let val (res, thy') = f (ProofContext.theory_of ctxt)
|
|
46 |
in (res, ProofContext.transfer thy' ctxt) end;
|
|
47 |
|
|
48 |
fun init NONE thy = ProofContext.init thy
|
|
49 |
| init (SOME loc) thy =
|
|
50 |
thy
|
|
51 |
|> Locale.init loc
|
|
52 |
|> (fn ctxt => Data.put
|
|
53 |
{locale = SOME loc,
|
|
54 |
params = Locale.the_params thy loc,
|
|
55 |
hyps = ProofContext.assms_of ctxt, (* FIXME query locale!! *)
|
|
56 |
exit = Sign.restore_naming thy} ctxt)
|
|
57 |
|> map_theory (Sign.add_path loc #> Sign.no_base_names);
|
|
58 |
|
|
59 |
fun exit ctxt = #exit (Data.get ctxt) (ProofContext.theory_of ctxt);
|
|
60 |
|
|
61 |
|
|
62 |
(* local theory *)
|
|
63 |
|
|
64 |
val params_of = #params o Data.get;
|
|
65 |
|
|
66 |
fun consts decls ctxt =
|
|
67 |
let
|
|
68 |
val thy = ProofContext.theory_of ctxt;
|
|
69 |
val params = params_of ctxt;
|
|
70 |
val ps = map Free params;
|
|
71 |
val Us = map snd params;
|
|
72 |
|
|
73 |
val xs = decls |> map (fn (c, _, mx) => Syntax.const_name c mx);
|
|
74 |
val Ts = map #2 decls;
|
|
75 |
fun const x T = Term.list_comb (Const (Sign.full_name thy x, Us ---> T), ps);
|
|
76 |
in
|
|
77 |
ctxt
|
|
78 |
|> map_theory (Sign.add_consts_i (decls |> map (fn (c, T, mx) => (c, Us ---> T, mx))))
|
|
79 |
|> ProofContext.add_fixes_i (decls |> map (fn (c, T, mx) => (c, SOME T, mx)))
|
|
80 |
|-> (fn xs' => pair ((xs' ~~ Ts), map2 const xs Ts))
|
|
81 |
end;
|
|
82 |
|
|
83 |
end;
|