author | Fabian Huch <huch@in.tum.de> |
Tue, 07 Jun 2022 17:20:25 +0200 | |
changeset 75521 | 7a289e681454 |
parent 74561 | 8e6c973003c8 |
child 78049 | d7395ef81292 |
permissions | -rw-r--r-- |
74287
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/ex/Def.thy |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
3 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
4 |
Primitive constant definition, without fact definition; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
5 |
automatic expansion via Simplifier (simproc). |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
6 |
*) |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
7 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
8 |
theory Def |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
9 |
imports Pure |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
10 |
keywords "def" :: thy_defn |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
11 |
begin |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
12 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
13 |
ML \<open> |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
14 |
signature DEF = |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
15 |
sig |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
16 |
val get_def: Proof.context -> cterm -> thm option |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
17 |
val def: (binding * typ option * mixfix) option -> |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
18 |
(binding * typ option * mixfix) list -> term -> local_theory -> term * local_theory |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
19 |
val def_cmd: (binding * string option * mixfix) option -> |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
20 |
(binding * string option * mixfix) list -> string -> local_theory -> term * local_theory |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
21 |
end; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
22 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
23 |
structure Def: DEF = |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
24 |
struct |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
25 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
26 |
(* context data *) |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
27 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
28 |
type def = {lhs: term, mk_eq: morphism -> thm}; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
29 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
30 |
val eq_def : def * def -> bool = op aconv o apply2 #lhs; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
31 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
32 |
fun transform_def phi ({lhs, mk_eq}: def) = |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
33 |
{lhs = Morphism.term phi lhs, mk_eq = Morphism.transform phi mk_eq}; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
34 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
35 |
structure Data = Generic_Data |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
36 |
( |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
37 |
type T = def Item_Net.T; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
38 |
val empty : T = Item_Net.init eq_def (single o #lhs); |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
39 |
val merge = Item_Net.merge; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
40 |
); |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
41 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
42 |
fun declare_def lhs eq lthy = |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
43 |
let |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
44 |
val eq0 = Thm.trim_context eq; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
45 |
val def: def = {lhs = lhs, mk_eq = fn phi => Morphism.thm phi eq0}; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
46 |
in |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
47 |
lthy |> Local_Theory.declaration {syntax = false, pervasive = true} |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
48 |
(fn phi => (Data.map o Item_Net.update) (transform_def phi def)) |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
49 |
end; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
50 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
51 |
fun get_def ctxt ct = |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
52 |
let |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
53 |
val thy = Proof_Context.theory_of ctxt; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
54 |
val data = Data.get (Context.Proof ctxt); |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
55 |
val t = Thm.term_of ct; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
56 |
fun match_def {lhs, mk_eq} = |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
57 |
if Pattern.matches thy (lhs, t) then |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
58 |
let |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
59 |
val inst = Thm.match (Thm.cterm_of ctxt lhs, ct); |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
60 |
val eq = |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
61 |
Morphism.form mk_eq |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
62 |
|> Thm.transfer thy |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
63 |
|> Thm.instantiate inst; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
64 |
in SOME eq end |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
65 |
else NONE; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
66 |
in Item_Net.retrieve_matching data t |> get_first match_def end; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
67 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
68 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
69 |
(* simproc setup *) |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
70 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
71 |
val _ = |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
72 |
(Theory.setup o Named_Target.theory_map) |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
73 |
(Simplifier.define_simproc \<^binding>\<open>expand_def\<close> |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
74 |
{lhss = [Free ("x", TFree ("'a", []))], proc = K get_def}); |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
75 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
76 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
77 |
(* Isar command *) |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
78 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
79 |
fun gen_def prep_spec raw_var raw_params raw_spec lthy = |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
80 |
let |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
81 |
val ((vars, xs, get_pos, spec), _) = lthy |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
82 |
|> prep_spec (the_list raw_var) raw_params [] raw_spec; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
83 |
val (((x, _), rhs), prove) = Local_Defs.derived_def lthy get_pos {conditional = false} spec; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
84 |
val _ = Name.reject_internal (x, []); |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
85 |
val (b, mx) = |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
86 |
(case (vars, xs) of |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
87 |
([], []) => (Binding.make (x, (case get_pos x of [] => Position.none | p :: _ => p)), NoSyn) |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
88 |
| ([(b, _, mx)], [y]) => |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
89 |
if x = y then (b, mx) |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
90 |
else |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
91 |
error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote y ^ |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
92 |
Position.here (Binding.pos_of b))); |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
93 |
val ((lhs, (_, eq)), lthy') = lthy |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
94 |
|> Local_Theory.define_internal ((b, mx), (Binding.empty_atts, rhs)); |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
95 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
96 |
(*sanity check for original specification*) |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
97 |
val _: thm = prove lthy' eq; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
98 |
in (lhs, declare_def lhs eq lthy') end; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
99 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
100 |
val def = gen_def Specification.check_spec_open; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
101 |
val def_cmd = gen_def Specification.read_spec_open; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
102 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
103 |
val _ = |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
104 |
Outer_Syntax.local_theory \<^command_keyword>\<open>def\<close> |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
105 |
"primitive constant definition, without fact definition" |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
106 |
(Scan.option Parse_Spec.constdecl -- Parse.prop -- Parse.for_fixes |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
107 |
>> (fn ((decl, spec), params) => #2 o def_cmd decl params spec)); |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
108 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
109 |
end; |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
110 |
\<close> |
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
111 |
|
f79dfc7656ae
miscellaneous examples and experiments for Isabelle/Pure;
wenzelm
parents:
diff
changeset
|
112 |
end |