author | krauss |
Wed, 18 Apr 2007 11:37:43 +0200 | |
changeset 22725 | 83099f0a9d8d |
parent 21237 | b803f9870e97 |
child 23819 | 2040846d1bbe |
permissions | -rw-r--r-- |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
1 |
(* Title: HOL/Tools/function_package/context_tree.ML |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
2 |
ID: $Id$ |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
3 |
Author: Alexander Krauss, TU Muenchen |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
4 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
5 |
A package for general recursive function definitions. |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
6 |
Builds and traverses trees of nested contexts along a term. |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
7 |
*) |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
8 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
9 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
10 |
signature FUNDEF_CTXTREE = |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
11 |
sig |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
12 |
type ctx_tree |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
13 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
14 |
(* FIXME: This interface is a mess and needs to be cleaned up! *) |
19612 | 15 |
val cong_deps : thm -> int IntGraph.T |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
16 |
val add_congs : thm list |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
17 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
18 |
val mk_tree: (thm * FundefCommon.depgraph) list -> |
21100 | 19 |
(string * typ) -> term -> Proof.context -> term -> FundefCommon.ctx_tree |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
20 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
21 |
val inst_tree: theory -> term -> term -> FundefCommon.ctx_tree |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
22 |
-> FundefCommon.ctx_tree |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
23 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
24 |
val add_context_varnames : FundefCommon.ctx_tree -> string list -> string list |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
25 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
26 |
val export_term : (string * typ) list * term list -> term -> term |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
27 |
val export_thm : theory -> (string * typ) list * term list -> thm -> thm |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
28 |
val import_thm : theory -> (string * typ) list * thm list -> thm -> thm |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
29 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
30 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
31 |
val traverse_tree : |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
32 |
((string * typ) list * thm list -> term -> |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
33 |
(((string * typ) list * thm list) * thm) list -> |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
34 |
(((string * typ) list * thm list) * thm) list * 'b -> |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
35 |
(((string * typ) list * thm list) * thm) list * 'b) |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
36 |
-> FundefCommon.ctx_tree -> 'b -> 'b |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
37 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
38 |
val rewrite_by_tree : theory -> term -> thm -> (thm * thm) list -> FundefCommon.ctx_tree -> thm * (thm * thm) list |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
39 |
end |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
40 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
41 |
structure FundefCtxTree : FUNDEF_CTXTREE = |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
42 |
struct |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
43 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
44 |
open FundefCommon |
21051
c49467a9c1e1
Switched function package to use the new package for inductive predicates.
krauss
parents:
20854
diff
changeset
|
45 |
open FundefLib |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
46 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
47 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
48 |
(* Maps "Trueprop A = B" to "A" *) |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
49 |
val rhs_of = snd o HOLogic.dest_eq o HOLogic.dest_Trueprop |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
50 |
(* Maps "A == B" to "B" *) |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
51 |
val meta_rhs_of = snd o Logic.dest_equals |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
52 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
53 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
54 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
55 |
(*** Dependency analysis for congruence rules ***) |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
56 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
57 |
fun branch_vars t = |
21237 | 58 |
let |
21188 | 59 |
val t' = snd (dest_all_all t) |
60 |
val assumes = Logic.strip_imp_prems t' |
|
61 |
val concl = Logic.strip_imp_concl t' |
|
62 |
in (fold (curry add_term_vars) assumes [], term_vars concl) |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
63 |
end |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
64 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
65 |
fun cong_deps crule = |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
66 |
let |
21237 | 67 |
val branches = map branch_vars (prems_of crule) |
68 |
val num_branches = (1 upto (length branches)) ~~ branches |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
69 |
in |
21237 | 70 |
IntGraph.empty |
71 |
|> fold (fn (i,_)=> IntGraph.new_node (i,i)) num_branches |
|
72 |
|> fold (fn ((i,(c1,_)),(j,(_, t2))) => if i = j orelse null (c1 inter t2) then I else IntGraph.add_edge_acyclic (i,j)) |
|
73 |
(product num_branches num_branches) |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
74 |
end |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
75 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
76 |
val add_congs = map (fn c => c RS eq_reflection) [cong, ext] |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
77 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
78 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
79 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
80 |
(* Called on the INSTANTIATED branches of the congruence rule *) |
19922 | 81 |
fun mk_branch ctx t = |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
82 |
let |
21237 | 83 |
val (ctx', fixes, impl) = dest_all_all_ctx ctx t |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
84 |
in |
21188 | 85 |
(ctx', fixes, Logic.strip_imp_prems impl, rhs_of (Logic.strip_imp_concl impl)) |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
86 |
end |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
87 |
|
21100 | 88 |
fun find_cong_rule ctx fvar h ((r,dep)::rs) t = |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
89 |
(let |
21100 | 90 |
val thy = ProofContext.theory_of ctx |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
91 |
|
21100 | 92 |
val tt' = Logic.mk_equals (Pattern.rewrite_term thy [(Free fvar, h)] [] t, t) |
93 |
val (c, subs) = (concl_of r, prems_of r) |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
94 |
|
21100 | 95 |
val subst = Pattern.match (ProofContext.theory_of ctx) (c, tt') (Vartab.empty, Vartab.empty) |
96 |
val branches = map (mk_branch ctx o Envir.beta_norm o Envir.subst_vars subst) subs |
|
97 |
val inst = map (fn v => (cterm_of thy (Var v), cterm_of thy (Envir.subst_vars subst (Var v)))) (Term.add_vars c []) |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
98 |
in |
21237 | 99 |
(cterm_instantiate inst r, dep, branches) |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
100 |
end |
21100 | 101 |
handle Pattern.MATCH => find_cong_rule ctx fvar h rs t) |
102 |
| find_cong_rule _ _ _ [] _ = sys_error "function_package/context_tree.ML: No cong rule found!" |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
103 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
104 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
105 |
fun matchcall fvar (a $ b) = if a = Free fvar then SOME b else NONE |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
106 |
| matchcall fvar _ = NONE |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
107 |
|
21100 | 108 |
fun mk_tree congs fvar h ctx t = |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
109 |
case matchcall fvar t of |
21100 | 110 |
SOME arg => RCall (t, mk_tree congs fvar h ctx arg) |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
111 |
| NONE => |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
112 |
if not (exists_subterm (fn Free v => v = fvar | _ => false) t) then Leaf t |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
113 |
else |
21237 | 114 |
let val (r, dep, branches) = find_cong_rule ctx fvar h congs t in |
115 |
Cong (t, r, dep, |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
116 |
map (fn (ctx', fixes, assumes, st) => |
21237 | 117 |
(fixes, map (assume o cterm_of (ProofContext.theory_of ctx)) assumes, |
21100 | 118 |
mk_tree congs fvar h ctx' st)) branches) |
21237 | 119 |
end |
120 |
||
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
121 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
122 |
fun inst_tree thy fvar f tr = |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
123 |
let |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
124 |
val cfvar = cterm_of thy fvar |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
125 |
val cf = cterm_of thy f |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
126 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
127 |
fun inst_term t = |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
128 |
subst_bound(f, abstract_over (fvar, t)) |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
129 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
130 |
val inst_thm = forall_elim cf o forall_intr cfvar |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
131 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
132 |
fun inst_tree_aux (Leaf t) = Leaf t |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
133 |
| inst_tree_aux (Cong (t, crule, deps, branches)) = |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
134 |
Cong (inst_term t, inst_thm crule, deps, map inst_branch branches) |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
135 |
| inst_tree_aux (RCall (t, str)) = |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
136 |
RCall (inst_term t, inst_tree_aux str) |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
137 |
and inst_branch (fxs, assms, str) = |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
138 |
(fxs, map (assume o cterm_of thy o inst_term o prop_of) assms, inst_tree_aux str) |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
139 |
in |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
140 |
inst_tree_aux tr |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
141 |
end |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
142 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
143 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
144 |
|
21237 | 145 |
(* FIXME: remove *) |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
146 |
fun add_context_varnames (Leaf _) = I |
20854 | 147 |
| add_context_varnames (Cong (_, _, _, sub)) = fold (fn (fs, _, st) => fold (insert (op =) o fst) fs o add_context_varnames st) sub |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
148 |
| add_context_varnames (RCall (_,st)) = add_context_varnames st |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
149 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
150 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
151 |
(* Poor man's contexts: Only fixes and assumes *) |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
152 |
fun compose (fs1, as1) (fs2, as2) = (fs1 @ fs2, as1 @ as2) |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
153 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
154 |
fun export_term (fixes, assumes) = |
19922 | 155 |
fold_rev (curry Logic.mk_implies) assumes #> fold_rev (mk_forall o Free) fixes |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
156 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
157 |
fun export_thm thy (fixes, assumes) = |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
158 |
fold_rev (implies_intr o cterm_of thy) assumes |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
159 |
#> fold_rev (forall_intr o cterm_of thy o Free) fixes |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
160 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
161 |
fun import_thm thy (fixes, athms) = |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
162 |
fold (forall_elim o cterm_of thy o Free) fixes |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
163 |
#> fold implies_elim_swp athms |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
164 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
165 |
fun assume_in_ctxt thy (fixes, athms) prop = |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
166 |
let |
21237 | 167 |
val global_assum = export_term (fixes, map prop_of athms) prop |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
168 |
in |
21237 | 169 |
(global_assum, |
170 |
assume (cterm_of thy global_assum) |> import_thm thy (fixes, athms)) |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
171 |
end |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
172 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
173 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
174 |
(* folds in the order of the dependencies of a graph. *) |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
175 |
fun fold_deps G f x = |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
176 |
let |
21237 | 177 |
fun fill_table i (T, x) = |
178 |
case Inttab.lookup T i of |
|
179 |
SOME _ => (T, x) |
|
180 |
| NONE => |
|
181 |
let |
|
182 |
val (T', x') = fold fill_table (IntGraph.imm_succs G i) (T, x) |
|
183 |
val (v, x'') = f (the o Inttab.lookup T') i x |
|
184 |
in |
|
185 |
(Inttab.update (i, v) T', x'') |
|
186 |
end |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
187 |
|
21237 | 188 |
val (T, x) = fold fill_table (IntGraph.keys G) (Inttab.empty, x) |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
189 |
in |
21237 | 190 |
(Inttab.fold (cons o snd) T [], x) |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
191 |
end |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
192 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
193 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
194 |
fun flatten xss = fold_rev append xss [] |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
195 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
196 |
fun traverse_tree rcOp tr x = |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
197 |
let |
21237 | 198 |
fun traverse_help ctx (Leaf _) u x = ([], x) |
199 |
| traverse_help ctx (RCall (t, st)) u x = |
|
200 |
rcOp ctx t u (traverse_help ctx st u x) |
|
201 |
| traverse_help ctx (Cong (t, crule, deps, branches)) u x = |
|
202 |
let |
|
203 |
fun sub_step lu i x = |
|
204 |
let |
|
205 |
val (fixes, assumes, subtree) = nth branches (i - 1) |
|
206 |
val used = fold_rev (append o lu) (IntGraph.imm_succs deps i) u |
|
207 |
val (subs, x') = traverse_help (compose ctx (fixes, assumes)) subtree used x |
|
208 |
val exported_subs = map (apfst (compose (fixes, assumes))) subs |
|
209 |
in |
|
210 |
(exported_subs, x') |
|
211 |
end |
|
212 |
in |
|
213 |
fold_deps deps sub_step x |
|
214 |
|> apfst flatten |
|
215 |
end |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
216 |
in |
21237 | 217 |
snd (traverse_help ([], []) tr [] x) |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
218 |
end |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
219 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
220 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
221 |
fun is_refl thm = let val (l,r) = Logic.dest_equals (prop_of thm) in l = r end |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
222 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20289
diff
changeset
|
223 |
fun rewrite_by_tree thy h ih x tr = |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
224 |
let |
21237 | 225 |
fun rewrite_help fix f_as h_as x (Leaf t) = (reflexive (cterm_of thy t), x) |
226 |
| rewrite_help fix f_as h_as x (RCall (_ $ arg, st)) = |
|
227 |
let |
|
228 |
val (inner, (lRi,ha)::x') = rewrite_help fix f_as h_as x st |
|
229 |
||
230 |
(* Need not use the simplifier here. Can use primitive steps! *) |
|
231 |
val rew_ha = if is_refl inner then I else simplify (HOL_basic_ss addsimps [inner]) |
|
232 |
||
233 |
val h_a_eq_h_a' = combination (reflexive (cterm_of thy h)) inner |
|
234 |
val iha = import_thm thy (fix, h_as) ha (* (a', h a') : G *) |
|
235 |
|> rew_ha |
|
236 |
||
237 |
val inst_ih = instantiate' [] [SOME (cterm_of thy arg)] ih |
|
238 |
val eq = implies_elim (implies_elim inst_ih lRi) iha |
|
239 |
||
240 |
val h_a'_eq_f_a' = eq RS eq_reflection |
|
241 |
val result = transitive h_a_eq_h_a' h_a'_eq_f_a' |
|
242 |
in |
|
243 |
(result, x') |
|
244 |
end |
|
245 |
| rewrite_help fix f_as h_as x (Cong (t, crule, deps, branches)) = |
|
246 |
let |
|
247 |
fun sub_step lu i x = |
|
248 |
let |
|
249 |
val (fixes, assumes, st) = nth branches (i - 1) |
|
250 |
val used = fold_rev (cons o lu) (IntGraph.imm_succs deps i) [] |
|
251 |
val used_rev = map (fn u_eq => (u_eq RS sym) RS eq_reflection) used |
|
252 |
val assumes' = map (simplify (HOL_basic_ss addsimps (filter_out is_refl used_rev))) assumes |
|
253 |
||
254 |
val (subeq, x') = rewrite_help (fix @ fixes) (f_as @ assumes) (h_as @ assumes') x st |
|
255 |
val subeq_exp = export_thm thy (fixes, map prop_of assumes) (subeq RS meta_eq_to_obj_eq) |
|
256 |
in |
|
257 |
(subeq_exp, x') |
|
258 |
end |
|
259 |
||
260 |
val (subthms, x') = fold_deps deps sub_step x |
|
261 |
in |
|
262 |
(fold_rev (curry op COMP) subthms crule, x') |
|
263 |
end |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
264 |
in |
21237 | 265 |
rewrite_help [] [] [] x tr |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
266 |
end |
21237 | 267 |
|
19612 | 268 |
end |