author | wenzelm |
Mon, 12 Jun 2006 21:19:00 +0200 | |
changeset 19861 | 620d90091788 |
parent 19835 | 81d6dc597559 |
child 19871 | 88e8f6173bab |
permissions | -rw-r--r-- |
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
1 |
(* Title: Provers/eqsubst.ML |
16434 | 2 |
ID: $Id$ |
18598 | 3 |
Author: Lucas Dixon, University of Edinburgh, lucas.dixon@ed.ac.uk |
15481 | 4 |
|
18598 | 5 |
A proof method to perform a substiution using an equation. |
6 |
*) |
|
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
7 |
|
18591 | 8 |
signature EQSUBST = |
15481 | 9 |
sig |
18708 | 10 |
val setup : theory -> theory |
15481 | 11 |
end; |
12 |
||
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
13 |
structure EqSubst |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
14 |
(* : EQSUBST *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
15 |
= struct |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
16 |
|
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
17 |
structure Z = Zipper; |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
18 |
|
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
19 |
(* changes object "=" to meta "==" which prepares a given rewrite rule *) |
18598 | 20 |
fun prep_meta_eq ctxt = |
21 |
let val (_, {mk_rews = {mk, ...}, ...}) = Simplifier.rep_ss (Simplifier.local_simpset_of ctxt) |
|
22 |
in mk #> map Drule.zero_var_indexes end; |
|
23 |
||
15481 | 24 |
|
15915
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
dixon
parents:
15855
diff
changeset
|
25 |
(* a type abriviation for match information *) |
16978 | 26 |
type match = |
27 |
((indexname * (sort * typ)) list (* type instantiations *) |
|
28 |
* (indexname * (typ * term)) list) (* term instantiations *) |
|
29 |
* (string * typ) list (* fake named type abs env *) |
|
30 |
* (string * typ) list (* type abs env *) |
|
31 |
* term (* outer term *) |
|
15550
806214035275
lucas - added more comments and an extra type to clarify the code.
dixon
parents:
15538
diff
changeset
|
32 |
|
16978 | 33 |
type searchinfo = |
18598 | 34 |
theory |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
35 |
* int (* maxidx *) |
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
36 |
* Zipper.T (* focusterm to search under *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
37 |
|
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
38 |
|
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
39 |
(* skipping non-empty sub-sequences but when we reach the end |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
40 |
of the seq, remembering how much we have left to skip. *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
41 |
datatype 'a skipseq = SkipMore of int |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
42 |
| SkipSeq of 'a Seq.seq Seq.seq; |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
43 |
(* given a seqseq, skip the first m non-empty seq's, note deficit *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
44 |
fun skipto_skipseq m s = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
45 |
let |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
46 |
fun skip_occs n sq = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
47 |
case Seq.pull sq of |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
48 |
NONE => SkipMore n |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
49 |
| SOME (h,t) => |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
50 |
(case Seq.pull h of NONE => skip_occs n t |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
51 |
| SOME _ => if n <= 1 then SkipSeq (Seq.cons h t) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
52 |
else skip_occs (n - 1) t) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
53 |
in (skip_occs m s) end; |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
54 |
|
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
55 |
(* note: outerterm is the taget with the match replaced by a bound |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
56 |
variable : ie: "P lhs" beocmes "%x. P x" |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
57 |
insts is the types of instantiations of vars in lhs |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
58 |
and typinsts is the type instantiations of types in the lhs |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
59 |
Note: Final rule is the rule lifted into the ontext of the |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
60 |
taget thm. *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
61 |
fun mk_foo_match mkuptermfunc Ts t = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
62 |
let |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
63 |
val ty = Term.type_of t |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
64 |
val bigtype = (rev (map snd Ts)) ---> ty |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
65 |
fun mk_foo 0 t = t |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
66 |
| mk_foo i t = mk_foo (i - 1) (t $ (Bound (i - 1))) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
67 |
val num_of_bnds = (length Ts) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
68 |
(* foo_term = "fooabs y0 ... yn" where y's are local bounds *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
69 |
val foo_term = mk_foo num_of_bnds (Bound num_of_bnds) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
70 |
in Abs("fooabs", bigtype, mkuptermfunc foo_term) end; |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
71 |
|
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
72 |
(* T is outer bound vars, n is number of locally bound vars *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
73 |
(* THINK: is order of Ts correct...? or reversed? *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
74 |
fun fakefree_badbounds Ts t = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
75 |
let val (FakeTs,Ts,newnames) = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
76 |
List.foldr (fn ((n,ty),(FakeTs,Ts,usednames)) => |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
77 |
let val newname = Term.variant usednames n |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
78 |
in ((RWTools.mk_fake_bound_name newname,ty)::FakeTs, |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
79 |
(newname,ty)::Ts, |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
80 |
newname::usednames) end) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
81 |
([],[],[]) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
82 |
Ts |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
83 |
in (FakeTs, Ts, Term.subst_bounds (map Free FakeTs, t)) end; |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
84 |
|
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
85 |
(* before matching we need to fake the bound vars that are missing an |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
86 |
abstraction. In this function we additionally construct the |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
87 |
abstraction environment, and an outer context term (with the focus |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
88 |
abstracted out) for use in rewriting with RWInst.rw *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
89 |
fun prep_zipper_match z = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
90 |
let |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
91 |
val t = Z.trm z |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
92 |
val c = Z.ctxt z |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
93 |
val Ts = Z.C.nty_ctxt c |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
94 |
val (FakeTs', Ts', t') = fakefree_badbounds Ts t |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
95 |
val absterm = mk_foo_match (Z.C.apply c) Ts' t' |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
96 |
in |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
97 |
(t', (FakeTs', Ts', absterm)) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
98 |
end; |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
99 |
|
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
100 |
(* Matching and Unification with exception handled *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
101 |
fun clean_match thy (a as (pat, t)) = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
102 |
let val (tyenv, tenv) = Pattern.match thy a (Vartab.empty, Vartab.empty) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
103 |
in SOME (Vartab.dest tyenv, Vartab.dest tenv) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
104 |
end handle Pattern.MATCH => NONE; |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
105 |
(* given theory, max var index, pat, tgt; returns Seq of instantiations *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
106 |
fun clean_unify sgn ix (a as (pat, tgt)) = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
107 |
let |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
108 |
(* type info will be re-derived, maybe this can be cached |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
109 |
for efficiency? *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
110 |
val pat_ty = Term.type_of pat; |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
111 |
val tgt_ty = Term.type_of tgt; |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
112 |
(* is it OK to ignore the type instantiation info? |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
113 |
or should I be using it? *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
114 |
val typs_unify = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
115 |
SOME (Sign.typ_unify sgn (pat_ty, tgt_ty) (Term.Vartab.empty, ix)) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
116 |
handle Type.TUNIFY => NONE; |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
117 |
in |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
118 |
case typs_unify of |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
119 |
SOME (typinsttab, ix2) => |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
120 |
let |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
121 |
(* is it right to throw away the flexes? |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
122 |
or should I be using them somehow? *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
123 |
fun mk_insts env = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
124 |
(Vartab.dest (Envir.type_env env), |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
125 |
Envir.alist_of env); |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
126 |
val initenv = Envir.Envir {asol = Vartab.empty, |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
127 |
iTs = typinsttab, maxidx = ix2}; |
19861 | 128 |
val useq = Unify.smash_unifiers sgn [a] initenv |
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
129 |
handle UnequalLengths => Seq.empty |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
130 |
| Term.TERM _ => Seq.empty; |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
131 |
fun clean_unify' useq () = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
132 |
(case (Seq.pull useq) of |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
133 |
NONE => NONE |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
134 |
| SOME (h,t) => SOME (mk_insts h, Seq.make (clean_unify' t))) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
135 |
handle UnequalLengths => NONE |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
136 |
| Term.TERM _ => NONE; |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
137 |
in |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
138 |
(Seq.make (clean_unify' useq)) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
139 |
end |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
140 |
| NONE => Seq.empty |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
141 |
end; |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
142 |
|
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
143 |
(* Matching and Unification for zippers *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
144 |
(* Note: Ts is a modified version of the original names of the outer |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
145 |
bound variables. New names have been introduced to make sure they are |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
146 |
unique w.r.t all names in the term and each other. usednames' is |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
147 |
oldnames + new names. *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
148 |
fun clean_match_z thy pat z = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
149 |
let val (t, (FakeTs,Ts,absterm)) = prep_zipper_match z in |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
150 |
case clean_match thy (pat, t) of |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
151 |
NONE => NONE |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
152 |
| SOME insts => SOME (insts, FakeTs, Ts, absterm) end; |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
153 |
(* ix = max var index *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
154 |
fun clean_unify_z sgn ix pat z = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
155 |
let val (t, (FakeTs, Ts,absterm)) = prep_zipper_match z in |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
156 |
Seq.map (fn insts => (insts, FakeTs, Ts, absterm)) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
157 |
(clean_unify sgn ix (t, pat)) end; |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
158 |
|
15550
806214035275
lucas - added more comments and an extra type to clarify the code.
dixon
parents:
15538
diff
changeset
|
159 |
|
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
160 |
(* FOR DEBUGGING... |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
161 |
type trace_subst_errT = int (* subgoal *) |
16978 | 162 |
* thm (* thm with all goals *) |
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
163 |
* (Thm.cterm list (* certified free var placeholders for vars *) |
16978 | 164 |
* thm) (* trivial thm of goal concl *) |
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
165 |
(* possible matches/unifiers *) |
16978 | 166 |
* thm (* rule *) |
167 |
* (((indexname * typ) list (* type instantiations *) |
|
168 |
* (indexname * term) list ) (* term instantiations *) |
|
169 |
* (string * typ) list (* Type abs env *) |
|
170 |
* term) (* outer term *); |
|
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
171 |
|
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
172 |
val trace_subst_err = (ref NONE : trace_subst_errT option ref); |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
173 |
val trace_subst_search = ref false; |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
174 |
exception trace_subst_exp of trace_subst_errT; |
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
175 |
*) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
176 |
|
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
177 |
|
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
178 |
fun bot_left_leaf_of (l $ r) = bot_left_leaf_of l |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
179 |
| bot_left_leaf_of (Abs(s,ty,t)) = bot_left_leaf_of t |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
180 |
| bot_left_leaf_of x = x; |
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
181 |
|
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
182 |
fun valid_match_start z = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
183 |
(case bot_left_leaf_of (Z.trm z) of |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
184 |
Const _ => true |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
185 |
| Free _ => true |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
186 |
| Abs _ => true (* allowed to look inside abs... search decides if we actually consider the abstraction itself *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
187 |
| _ => false); (* avoid vars - always suceeds uninterestingly. *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
188 |
|
15814
d65f461c8672
lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents:
15550
diff
changeset
|
189 |
(* search from top, left to right, then down *) |
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
190 |
val search_tlr_all = ZipperSearch.all_td_lr; |
15481 | 191 |
|
15814
d65f461c8672
lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents:
15550
diff
changeset
|
192 |
(* search from top, left to right, then down *) |
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
193 |
fun search_tlr_valid validf = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
194 |
let |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
195 |
fun sf_valid_td_lr z = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
196 |
let val here = if validf z then [Z.Here z] else [] in |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
197 |
case Z.trm z |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
198 |
of _ $ _ => here @ [Z.LookIn (Z.move_down_left z), |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
199 |
Z.LookIn (Z.move_down_right z)] |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
200 |
| Abs _ => here @ [Z.LookIn (Z.move_down_abs z)] |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
201 |
| _ => here |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
202 |
end; |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
203 |
in Z.lzy_search sf_valid_td_lr end; |
15814
d65f461c8672
lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents:
15550
diff
changeset
|
204 |
|
d65f461c8672
lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents:
15550
diff
changeset
|
205 |
(* search all unifications *) |
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
206 |
fun searchf_tlr_unify_all (sgn, maxidx, z) lhs = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
207 |
Seq.map (clean_unify_z sgn maxidx lhs) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
208 |
(Z.limit_apply search_tlr_all z); |
15481 | 209 |
|
15814
d65f461c8672
lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents:
15550
diff
changeset
|
210 |
(* search only for 'valid' unifiers (non abs subterms and non vars) *) |
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
211 |
fun searchf_tlr_unify_valid (sgn, maxidx, z) lhs = |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
212 |
Seq.map (clean_unify_z sgn maxidx lhs) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
213 |
(Z.limit_apply (search_tlr_valid valid_match_start) z); |
15929
68bd1e16552a
lucas - added option to select occurance to rewrite e.g. (occ 4)
dixon
parents:
15915
diff
changeset
|
214 |
|
15814
d65f461c8672
lucas - fixed a big with renaming of bound variables. Other small changes.
dixon
parents:
15550
diff
changeset
|
215 |
|
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
216 |
(* apply a substitution in the conclusion of the theorem th *) |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
217 |
(* cfvs are certified free var placeholders for goal params *) |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
218 |
(* conclthm is a theorem of for just the conclusion *) |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
219 |
(* m is instantiation/match information *) |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
220 |
(* rule is the equation for substitution *) |
16978 | 221 |
fun apply_subst_in_concl i th (cfvs, conclthm) rule m = |
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
222 |
(RWInst.rw m rule conclthm) |
15855 | 223 |
|> IsaND.unfix_frees cfvs |
15915
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
dixon
parents:
15855
diff
changeset
|
224 |
|> RWInst.beta_eta_contract |
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
225 |
|> (fn r => Tactic.rtac r i th); |
15481 | 226 |
|
227 |
(* substitute within the conclusion of goal i of gth, using a meta |
|
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
228 |
equation rule. Note that we assume rule has var indicies zero'd *) |
16978 | 229 |
fun prep_concl_subst i gth = |
230 |
let |
|
15481 | 231 |
val th = Thm.incr_indexes 1 gth; |
232 |
val tgt_term = Thm.prop_of th; |
|
233 |
||
234 |
val sgn = Thm.sign_of_thm th; |
|
235 |
val ctermify = Thm.cterm_of sgn; |
|
236 |
val trivify = Thm.trivial o ctermify; |
|
237 |
||
238 |
val (fixedbody, fvs) = IsaND.fix_alls_term i tgt_term; |
|
239 |
val cfvs = rev (map ctermify fvs); |
|
240 |
||
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
241 |
val conclterm = Logic.strip_imp_concl fixedbody; |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
242 |
val conclthm = trivify conclterm; |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
243 |
val maxidx = Term.maxidx_of_term conclterm; |
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
244 |
val ft = ((Z.move_down_right (* ==> *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
245 |
o Z.move_down_left (* Trueprop *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
246 |
o Z.mktop |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
247 |
o Thm.prop_of) conclthm) |
15481 | 248 |
in |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
249 |
((cfvs, conclthm), (sgn, maxidx, ft)) |
15481 | 250 |
end; |
251 |
||
252 |
(* substitute using an object or meta level equality *) |
|
18598 | 253 |
fun eqsubst_tac' ctxt searchf instepthm i th = |
16978 | 254 |
let |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
255 |
val (cvfsconclthm, searchinfo) = prep_concl_subst i th; |
18598 | 256 |
val stepthms = Seq.of_list (prep_meta_eq ctxt instepthm); |
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
257 |
fun rewrite_with_thm r = |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
258 |
let val (lhs,_) = Logic.dest_equals (Thm.concl_of r); |
18598 | 259 |
in searchf searchinfo lhs |
260 |
|> Seq.maps (apply_subst_in_concl i th cvfsconclthm r) end; |
|
261 |
in stepthms |> Seq.maps rewrite_with_thm end; |
|
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
262 |
|
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
263 |
|
19047 | 264 |
(* distinct subgoals *) |
15959
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
dixon
parents:
15936
diff
changeset
|
265 |
fun distinct_subgoals th = |
19047 | 266 |
the_default th (SINGLE distinct_subgoals_tac th); |
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
267 |
|
19047 | 268 |
(* General substitution of multiple occurances using one of |
15936
817ac93ee786
lucas - added ability to provide multiple replacements for subst: syntax is now: subst (1 3) myrule
dixon
parents:
15929
diff
changeset
|
269 |
the given theorems*) |
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
270 |
|
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
271 |
|
16978 | 272 |
exception eqsubst_occL_exp of |
273 |
string * (int list) * (thm list) * int * thm; |
|
274 |
fun skip_first_occs_search occ srchf sinfo lhs = |
|
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
275 |
case (skipto_skipseq occ (srchf sinfo lhs)) of |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
276 |
SkipMore _ => Seq.empty |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
277 |
| SkipSeq ss => Seq.flat ss; |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
278 |
|
18598 | 279 |
fun eqsubst_tac ctxt occL thms i th = |
15936
817ac93ee786
lucas - added ability to provide multiple replacements for subst: syntax is now: subst (1 3) myrule
dixon
parents:
15929
diff
changeset
|
280 |
let val nprems = Thm.nprems_of th in |
817ac93ee786
lucas - added ability to provide multiple replacements for subst: syntax is now: subst (1 3) myrule
dixon
parents:
15929
diff
changeset
|
281 |
if nprems < i then Seq.empty else |
16978 | 282 |
let val thmseq = (Seq.of_list thms) |
283 |
fun apply_occ occ th = |
|
18598 | 284 |
thmseq |> Seq.maps |
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
285 |
(fn r => eqsubst_tac' |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
286 |
ctxt |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
287 |
(skip_first_occs_search |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
288 |
occ searchf_tlr_unify_valid) r |
15936
817ac93ee786
lucas - added ability to provide multiple replacements for subst: syntax is now: subst (1 3) myrule
dixon
parents:
15929
diff
changeset
|
289 |
(i + ((Thm.nprems_of th) - nprems)) |
817ac93ee786
lucas - added ability to provide multiple replacements for subst: syntax is now: subst (1 3) myrule
dixon
parents:
15929
diff
changeset
|
290 |
th); |
16978 | 291 |
val sortedoccL = |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
292 |
Library.sort (Library.rev_order o Library.int_ord) occL; |
15936
817ac93ee786
lucas - added ability to provide multiple replacements for subst: syntax is now: subst (1 3) myrule
dixon
parents:
15929
diff
changeset
|
293 |
in |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
294 |
Seq.map distinct_subgoals (Seq.EVERY (map apply_occ sortedoccL) th) |
15936
817ac93ee786
lucas - added ability to provide multiple replacements for subst: syntax is now: subst (1 3) myrule
dixon
parents:
15929
diff
changeset
|
295 |
end |
15959
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
dixon
parents:
15936
diff
changeset
|
296 |
end |
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
dixon
parents:
15936
diff
changeset
|
297 |
handle THM _ => raise eqsubst_occL_exp ("THM",occL,thms,i,th); |
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
dixon
parents:
15936
diff
changeset
|
298 |
|
15481 | 299 |
|
300 |
(* inthms are the given arguments in Isar, and treated as eqstep with |
|
301 |
the first one, then the second etc *) |
|
18598 | 302 |
fun eqsubst_meth ctxt occL inthms = |
16978 | 303 |
Method.METHOD |
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
304 |
(fn facts => |
18598 | 305 |
HEADGOAL (Method.insert_tac facts THEN' eqsubst_tac ctxt occL inthms)); |
15481 | 306 |
|
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
307 |
(* apply a substitution inside assumption j, keeps asm in the same place *) |
16978 | 308 |
fun apply_subst_in_asm i th rule ((cfvs, j, ngoalprems, pth),m) = |
309 |
let |
|
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
310 |
val th2 = Thm.rotate_rule (j - 1) i th; (* put premice first *) |
16978 | 311 |
val preelimrule = |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
312 |
(RWInst.rw m rule pth) |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
313 |
|> (Seq.hd o Tactic.prune_params_tac) |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
314 |
|> Thm.permute_prems 0 ~1 (* put old asm first *) |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
315 |
|> IsaND.unfix_frees cfvs (* unfix any global params *) |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
316 |
|> RWInst.beta_eta_contract; (* normal form *) |
16978 | 317 |
(* val elimrule = |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
318 |
preelimrule |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
319 |
|> Tactic.make_elim (* make into elim rule *) |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
320 |
|> Thm.lift_rule (th2, i); (* lift into context *) |
16007
4dcccaa11a13
lucas - bugfix to subst in assumptions: fixed index error for conditional rules.
dixon
parents:
16004
diff
changeset
|
321 |
*) |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
322 |
in |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
323 |
(* ~j because new asm starts at back, thus we subtract 1 *) |
16007
4dcccaa11a13
lucas - bugfix to subst in assumptions: fixed index error for conditional rules.
dixon
parents:
16004
diff
changeset
|
324 |
Seq.map (Thm.rotate_rule (~j) ((Thm.nprems_of rule) + i)) |
4dcccaa11a13
lucas - bugfix to subst in assumptions: fixed index error for conditional rules.
dixon
parents:
16004
diff
changeset
|
325 |
(Tactic.dtac preelimrule i th2) |
4dcccaa11a13
lucas - bugfix to subst in assumptions: fixed index error for conditional rules.
dixon
parents:
16004
diff
changeset
|
326 |
|
16978 | 327 |
(* (Thm.bicompose |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
328 |
false (* use unification *) |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
329 |
(true, (* elim resolution *) |
16007
4dcccaa11a13
lucas - bugfix to subst in assumptions: fixed index error for conditional rules.
dixon
parents:
16004
diff
changeset
|
330 |
elimrule, (2 + (Thm.nprems_of rule)) - ngoalprems) |
4dcccaa11a13
lucas - bugfix to subst in assumptions: fixed index error for conditional rules.
dixon
parents:
16004
diff
changeset
|
331 |
i th2) *) |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
332 |
end; |
15481 | 333 |
|
334 |
||
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
335 |
(* prepare to substitute within the j'th premise of subgoal i of gth, |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
336 |
using a meta-level equation. Note that we assume rule has var indicies |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
337 |
zero'd. Note that we also assume that premt is the j'th premice of |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
338 |
subgoal i of gth. Note the repetition of work done for each |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
339 |
assumption, i.e. this can be made more efficient for search over |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
340 |
multiple assumptions. *) |
16978 | 341 |
fun prep_subst_in_asm i gth j = |
342 |
let |
|
15481 | 343 |
val th = Thm.incr_indexes 1 gth; |
344 |
val tgt_term = Thm.prop_of th; |
|
345 |
||
346 |
val sgn = Thm.sign_of_thm th; |
|
347 |
val ctermify = Thm.cterm_of sgn; |
|
348 |
val trivify = Thm.trivial o ctermify; |
|
349 |
||
350 |
val (fixedbody, fvs) = IsaND.fix_alls_term i tgt_term; |
|
351 |
val cfvs = rev (map ctermify fvs); |
|
352 |
||
18011
685d95c793ff
cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents:
17795
diff
changeset
|
353 |
val asmt = nth (Logic.strip_imp_prems fixedbody) (j - 1); |
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
354 |
val asm_nprems = length (Logic.strip_imp_prems asmt); |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
355 |
|
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
356 |
val pth = trivify asmt; |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
357 |
val maxidx = Term.maxidx_of_term asmt; |
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
358 |
|
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
359 |
val ft = ((Z.move_down_right (* trueprop *) |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
360 |
o Z.mktop |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
361 |
o Thm.prop_of) pth) |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
362 |
in ((cfvs, j, asm_nprems, pth), (sgn, maxidx, ft)) end; |
15481 | 363 |
|
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
364 |
(* prepare subst in every possible assumption *) |
16978 | 365 |
fun prep_subst_in_asms i gth = |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
366 |
map (prep_subst_in_asm i gth) |
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
367 |
((fn l => Library.upto (1, length l)) |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
368 |
(Logic.prems_of_goal (Thm.prop_of gth) i)); |
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
369 |
|
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
370 |
|
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
371 |
(* substitute in an assumption using an object or meta level equality *) |
18598 | 372 |
fun eqsubst_asm_tac' ctxt searchf skipocc instepthm i th = |
16978 | 373 |
let |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
374 |
val asmpreps = prep_subst_in_asms i th; |
18598 | 375 |
val stepthms = Seq.of_list (prep_meta_eq ctxt instepthm); |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
376 |
fun rewrite_with_thm r = |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
377 |
let val (lhs,_) = Logic.dest_equals (Thm.concl_of r) |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
378 |
fun occ_search occ [] = Seq.empty |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
379 |
| occ_search occ ((asminfo, searchinfo)::moreasms) = |
16978 | 380 |
(case searchf searchinfo occ lhs of |
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
381 |
SkipMore i => occ_search i moreasms |
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
382 |
| SkipSeq ss => |
19861 | 383 |
Seq.append (Seq.map (Library.pair asminfo) (Seq.flat ss)) |
384 |
(occ_search 1 moreasms)) |
|
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
385 |
(* find later substs also *) |
16978 | 386 |
in |
18598 | 387 |
occ_search skipocc asmpreps |> Seq.maps (apply_subst_in_asm i th r) |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
388 |
end; |
18598 | 389 |
in stepthms |> Seq.maps rewrite_with_thm end; |
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
390 |
|
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
391 |
|
16978 | 392 |
fun skip_first_asm_occs_search searchf sinfo occ lhs = |
19835
81d6dc597559
added updated version of IsaPlanner and substitution.
dixon
parents:
19473
diff
changeset
|
393 |
skipto_skipseq occ (searchf sinfo lhs); |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
394 |
|
18598 | 395 |
fun eqsubst_asm_tac ctxt occL thms i th = |
16978 | 396 |
let val nprems = Thm.nprems_of th |
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
397 |
in |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
398 |
if nprems < i then Seq.empty else |
16978 | 399 |
let val thmseq = (Seq.of_list thms) |
400 |
fun apply_occ occK th = |
|
18598 | 401 |
thmseq |> Seq.maps |
16978 | 402 |
(fn r => |
18598 | 403 |
eqsubst_asm_tac' ctxt (skip_first_asm_occs_search |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
404 |
searchf_tlr_unify_valid) occK r |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
405 |
(i + ((Thm.nprems_of th) - nprems)) |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
406 |
th); |
16978 | 407 |
val sortedoccs = |
16004
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
408 |
Library.sort (Library.rev_order o Library.int_ord) occL |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
409 |
in |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
410 |
Seq.map distinct_subgoals |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
411 |
(Seq.EVERY (map apply_occ sortedoccs) th) |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
412 |
end |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
413 |
end |
031f56012483
lucas - fixed subst in assumptions to count redexes from left to right.
dixon
parents:
15959
diff
changeset
|
414 |
handle THM _ => raise eqsubst_occL_exp ("THM",occL,thms,i,th); |
15481 | 415 |
|
416 |
(* inthms are the given arguments in Isar, and treated as eqstep with |
|
417 |
the first one, then the second etc *) |
|
18598 | 418 |
fun eqsubst_asm_meth ctxt occL inthms = |
16978 | 419 |
Method.METHOD |
15538
d8edf54cc28c
lucas - re-arranged code and added comments. Also added check to make sure the subgoal that we are being applied to exists. If it does not, empty seq is returned.
dixon
parents:
15486
diff
changeset
|
420 |
(fn facts => |
18598 | 421 |
HEADGOAL (Method.insert_tac facts THEN' eqsubst_asm_tac ctxt occL inthms )); |
15481 | 422 |
|
423 |
(* syntax for options, given "(asm)" will give back true, without |
|
424 |
gives back false *) |
|
425 |
val options_syntax = |
|
426 |
(Args.parens (Args.$$$ "asm") >> (K true)) || |
|
427 |
(Scan.succeed false); |
|
15936
817ac93ee786
lucas - added ability to provide multiple replacements for subst: syntax is now: subst (1 3) myrule
dixon
parents:
15929
diff
changeset
|
428 |
|
15929
68bd1e16552a
lucas - added option to select occurance to rewrite e.g. (occ 4)
dixon
parents:
15915
diff
changeset
|
429 |
val ith_syntax = |
15936
817ac93ee786
lucas - added ability to provide multiple replacements for subst: syntax is now: subst (1 3) myrule
dixon
parents:
15929
diff
changeset
|
430 |
(Args.parens (Scan.repeat Args.nat)) |
817ac93ee786
lucas - added ability to provide multiple replacements for subst: syntax is now: subst (1 3) myrule
dixon
parents:
15929
diff
changeset
|
431 |
|| (Scan.succeed [0]); |
15481 | 432 |
|
18598 | 433 |
(* combination method that takes a flag (true indicates that subst |
434 |
should be done to an assumption, false = apply to the conclusion of |
|
435 |
the goal) as well as the theorems to use *) |
|
436 |
fun subst_meth src = |
|
18988 | 437 |
Method.syntax ((Scan.lift options_syntax) -- (Scan.lift ith_syntax) -- Attrib.thms) src |
18598 | 438 |
#> (fn (ctxt, ((asmflag, occL), inthms)) => |
439 |
(if asmflag then eqsubst_asm_meth else eqsubst_meth) ctxt occL inthms); |
|
15481 | 440 |
|
18598 | 441 |
|
16978 | 442 |
val setup = |
18833 | 443 |
Method.add_method ("subst", subst_meth, "single-step substitution"); |
15481 | 444 |
|
16978 | 445 |
end; |