18830
|
1 |
(* Title: Pure/Isar/local_defs.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Makarius
|
|
4 |
|
|
5 |
Basic operations on local definitions.
|
|
6 |
*)
|
|
7 |
|
|
8 |
signature LOCAL_DEFS =
|
|
9 |
sig
|
|
10 |
val mk_def: ProofContext.context -> (string * term) list -> term list
|
|
11 |
val cert_def: ProofContext.context -> term -> string * term
|
|
12 |
val abs_def: term -> (string * typ) * term
|
|
13 |
val derived_def: ProofContext.context -> term ->
|
|
14 |
((string * typ) * term) * (ProofContext.context -> term -> thm -> thm)
|
|
15 |
val def_export: ProofContext.export
|
|
16 |
val add_def: string * term -> ProofContext.context ->
|
|
17 |
((string * typ) * thm) * ProofContext.context
|
|
18 |
end;
|
|
19 |
|
|
20 |
structure LocalDefs: LOCAL_DEFS =
|
|
21 |
struct
|
|
22 |
|
|
23 |
(* prepare defs *)
|
|
24 |
|
|
25 |
fun mk_def ctxt args =
|
|
26 |
let
|
|
27 |
val (xs, rhss) = split_list args;
|
|
28 |
val (bind, _) = ProofContext.bind_fixes xs ctxt;
|
|
29 |
val lhss = map (fn (x, rhs) => bind (Free (x, Term.fastype_of rhs))) args;
|
|
30 |
in map Logic.mk_equals (lhss ~~ rhss) end;
|
|
31 |
|
|
32 |
fun cert_def ctxt eq =
|
|
33 |
let
|
|
34 |
fun err msg = cat_error msg
|
|
35 |
("The error(s) above occurred in definition: " ^ ProofContext.string_of_term ctxt eq);
|
|
36 |
|
|
37 |
val (lhs, rhs) = Logic.dest_equals (Term.strip_all_body eq)
|
|
38 |
handle TERM _ => err "Not a meta-equality (==)";
|
|
39 |
val (f, xs) = Term.strip_comb (Pattern.beta_eta_contract lhs);
|
|
40 |
val (c, _) = Term.dest_Free f handle TERM _ =>
|
|
41 |
err "Head of lhs must be a free/fixed variable";
|
|
42 |
|
|
43 |
fun check_arg (Bound _) = true
|
|
44 |
| check_arg (Free (x, _)) = not (ProofContext.is_fixed ctxt x)
|
|
45 |
| check_arg t = (case try Logic.dest_type t of SOME (TFree _) => true | _ => false);
|
|
46 |
fun close_arg (Free (x, T)) t = Term.all T $ Term.absfree (x, T, t)
|
|
47 |
| close_arg (Const ("TYPE", T)) t = Term.all T $ Term.absdummy (T, t)
|
|
48 |
| close_arg _ t = t;
|
|
49 |
|
|
50 |
val extra_frees = Term.fold_aterms (fn v as Free (x, _) =>
|
|
51 |
if ProofContext.is_fixed ctxt x orelse member (op aconv) xs v then I
|
|
52 |
else insert (op =) x | _ => I) rhs [];
|
|
53 |
in
|
|
54 |
if not (forall check_arg xs) orelse has_duplicates (op =) xs then
|
|
55 |
err "Arguments of lhs must be distinct free/bound variables"
|
|
56 |
else if not (null extra_frees) then
|
|
57 |
err ("Extra free variables on rhs: " ^ commas_quote extra_frees)
|
|
58 |
else if Term.exists_subterm (fn t => t = f) rhs then
|
|
59 |
err "Element to be defined occurs on rhs"
|
|
60 |
else (c, fold_rev close_arg xs eq)
|
|
61 |
end;
|
|
62 |
|
|
63 |
fun abs_def eq =
|
|
64 |
let
|
|
65 |
val body = Term.strip_all_body eq;
|
|
66 |
val vars = map Free (Term.rename_wrt_term body (Term.strip_all_vars eq));
|
|
67 |
val (lhs, rhs) = Logic.dest_equals (Term.subst_bounds (vars, body));
|
|
68 |
val (f, xs) = Term.strip_comb (Pattern.beta_eta_contract lhs);
|
|
69 |
val eq' = Term.list_abs_free (map Term.dest_Free xs, rhs);
|
|
70 |
in (Term.dest_Free f, eq') end;
|
|
71 |
|
|
72 |
|
|
73 |
(* derived defs -- potentially within the object-logic *)
|
|
74 |
|
|
75 |
fun derived_def ctxt prop =
|
|
76 |
let
|
|
77 |
val thy = ProofContext.theory_of ctxt;
|
|
78 |
val ((c, T), rhs) = prop
|
|
79 |
|> Thm.cterm_of thy
|
|
80 |
|> ObjectLogic.meta_rewrite_cterm
|
|
81 |
|> (snd o Logic.dest_equals o Thm.prop_of)
|
|
82 |
|> Logic.strip_imp_concl
|
|
83 |
|> (snd o cert_def ctxt)
|
|
84 |
|> abs_def;
|
|
85 |
fun prove ctxt' t def =
|
|
86 |
let
|
|
87 |
val thy' = ProofContext.theory_of ctxt';
|
|
88 |
val prop' = Term.subst_atomic [(Free (c, T), t)] prop;
|
|
89 |
val frees = Term.fold_aterms (fn Free (x, _) =>
|
|
90 |
if ProofContext.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop' [];
|
|
91 |
in
|
|
92 |
Goal.prove thy' frees [] prop' (K (ALLGOALS
|
|
93 |
(ObjectLogic.meta_rewrite_tac THEN'
|
|
94 |
Tactic.rewrite_goal_tac [def] THEN'
|
|
95 |
Tactic.resolve_tac [Drule.reflexive_thm])))
|
|
96 |
handle ERROR msg => cat_error msg "Failed to prove definitional specification."
|
|
97 |
end;
|
|
98 |
in (((c, T), rhs), prove) end;
|
|
99 |
|
|
100 |
|
|
101 |
(* export defs *)
|
|
102 |
|
|
103 |
fun head_of_def cprop =
|
|
104 |
#1 (Term.strip_comb (#1 (Logic.dest_equals (Term.strip_all_body (Thm.term_of cprop)))))
|
|
105 |
|> Thm.cterm_of (Thm.theory_of_cterm cprop);
|
|
106 |
|
|
107 |
fun def_export _ cprops thm =
|
|
108 |
thm
|
|
109 |
|> Drule.implies_intr_list cprops
|
|
110 |
|> Drule.forall_intr_list (map head_of_def cprops)
|
|
111 |
|> Drule.forall_elim_vars 0
|
|
112 |
|> RANGE (replicate (length cprops) (Tactic.rtac Drule.reflexive_thm)) 1;
|
|
113 |
|
|
114 |
|
|
115 |
(* add defs *)
|
|
116 |
|
|
117 |
fun add_def (x, t) ctxt =
|
|
118 |
let
|
|
119 |
val [eq] = mk_def ctxt [(x, t)];
|
|
120 |
val x' = Term.dest_Free (fst (Logic.dest_equals eq));
|
|
121 |
in
|
|
122 |
ctxt
|
|
123 |
|> ProofContext.add_fixes_i [(x, NONE, NoSyn)] |> snd
|
|
124 |
|> ProofContext.add_assms_i def_export [(("", []), [(eq, ([], []))])]
|
|
125 |
|>> (fn [(_, [th])] => (x', th))
|
|
126 |
end;
|
|
127 |
|
|
128 |
end;
|