| author | wenzelm | 
| Fri, 28 Jun 2024 13:20:18 +0200 | |
| changeset 80443 | ab0dd21dd0ca | 
| parent 80294 | eec06d39e5fa | 
| child 80560 | 960b866b1117 | 
| permissions | -rw-r--r-- | 
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 1 | (* Title: Pure/zterm.ML | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 2 | Author: Makarius | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 3 | |
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 4 | Tight representation of types / terms / proof terms, notably for proof recording. | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 5 | *) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 6 | |
| 79124 | 7 | (*** global ***) | 
| 8 | ||
| 9 | (* types and terms *) | |
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 10 | |
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 11 | datatype ztyp = | 
| 79119 | 12 | ZTVar of indexname * sort (*free: index ~1*) | 
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 13 | | ZFun of ztyp * ztyp | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 14 | | ZProp | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 15 | | ZType0 of string (*type constant*) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 16 | | ZType1 of string * ztyp (*type constructor: 1 argument*) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 17 | | ZType of string * ztyp list (*type constructor: >= 2 arguments*) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 18 | |
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 19 | datatype zterm = | 
| 79119 | 20 | ZVar of indexname * ztyp (*free: index ~1*) | 
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 21 | | ZBound of int | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 22 | | ZConst0 of string (*monomorphic constant*) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 23 | | ZConst1 of string * ztyp (*polymorphic constant: 1 type argument*) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 24 | | ZConst of string * ztyp list (*polymorphic constant: >= 2 type arguments*) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 25 | | ZAbs of string * ztyp * zterm | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 26 | | ZApp of zterm * zterm | 
| 80293 | 27 | | OFCLASS of ztyp * class | 
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 28 | |
| 79124 | 29 | structure ZTerm = | 
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 30 | struct | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 31 | |
| 79119 | 32 | (* fold *) | 
| 33 | ||
| 34 | fun fold_tvars f (ZTVar v) = f v | |
| 35 | | fold_tvars f (ZFun (T, U)) = fold_tvars f T #> fold_tvars f U | |
| 36 | | fold_tvars f (ZType1 (_, T)) = fold_tvars f T | |
| 37 | | fold_tvars f (ZType (_, Ts)) = fold (fold_tvars f) Ts | |
| 38 | | fold_tvars _ _ = I; | |
| 39 | ||
| 40 | fun fold_aterms f (ZApp (t, u)) = fold_aterms f t #> fold_aterms f u | |
| 41 | | fold_aterms f (ZAbs (_, _, t)) = fold_aterms f t | |
| 42 | | fold_aterms f a = f a; | |
| 43 | ||
| 80266 | 44 | fun fold_vars f = fold_aterms (fn ZVar v => f v | _ => I); | 
| 45 | ||
| 79119 | 46 | fun fold_types f (ZVar (_, T)) = f T | 
| 47 | | fold_types f (ZConst1 (_, T)) = f T | |
| 48 | | fold_types f (ZConst (_, As)) = fold f As | |
| 49 | | fold_types f (ZAbs (_, T, b)) = f T #> fold_types f b | |
| 50 | | fold_types f (ZApp (t, u)) = fold_types f t #> fold_types f u | |
| 80293 | 51 | | fold_types f (OFCLASS (T, _)) = f T | 
| 79119 | 52 | | fold_types _ _ = I; | 
| 53 | ||
| 54 | ||
| 79264 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 55 | (* type ordering *) | 
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 56 | |
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 57 | local | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 58 | |
| 79119 | 59 | fun cons_nr (ZTVar _) = 0 | 
| 60 | | cons_nr (ZFun _) = 1 | |
| 61 | | cons_nr ZProp = 2 | |
| 79420 
4c3346b4b100
clarified datatype ztyp: omit special case that rarely occurs (thanks to ZClass and ZClassp);
 wenzelm parents: 
79419diff
changeset | 62 | | cons_nr (ZType0 _) = 3 | 
| 
4c3346b4b100
clarified datatype ztyp: omit special case that rarely occurs (thanks to ZClass and ZClassp);
 wenzelm parents: 
79419diff
changeset | 63 | | cons_nr (ZType1 _) = 4 | 
| 
4c3346b4b100
clarified datatype ztyp: omit special case that rarely occurs (thanks to ZClass and ZClassp);
 wenzelm parents: 
79419diff
changeset | 64 | | cons_nr (ZType _) = 5; | 
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 65 | |
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 66 | val fast_indexname_ord = Term_Ord.fast_indexname_ord; | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 67 | val sort_ord = Term_Ord.sort_ord; | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 68 | |
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 69 | in | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 70 | |
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 71 | fun ztyp_ord TU = | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 72 | if pointer_eq TU then EQUAL | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 73 | else | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 74 | (case TU of | 
| 79119 | 75 | (ZTVar (a, A), ZTVar (b, B)) => | 
| 76 | (case fast_indexname_ord (a, b) of EQUAL => sort_ord (A, B) | ord => ord) | |
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 77 | | (ZFun (T, T'), ZFun (U, U')) => | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 78 | (case ztyp_ord (T, U) of EQUAL => ztyp_ord (T', U') | ord => ord) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 79 | | (ZProp, ZProp) => EQUAL | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 80 | | (ZType0 a, ZType0 b) => fast_string_ord (a, b) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 81 | | (ZType1 (a, T), ZType1 (b, U)) => | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 82 | (case fast_string_ord (a, b) of EQUAL => ztyp_ord (T, U) | ord => ord) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 83 | | (ZType (a, Ts), ZType (b, Us)) => | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 84 | (case fast_string_ord (a, b) of EQUAL => dict_ord ztyp_ord (Ts, Us) | ord => ord) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 85 | | (T, U) => int_ord (cons_nr T, cons_nr U)); | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 86 | |
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 87 | end; | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 88 | |
| 79264 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 89 | |
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 90 | (* term ordering and alpha-conversion *) | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 91 | |
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 92 | local | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 93 | |
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 94 | fun cons_nr (ZVar _) = 0 | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 95 | | cons_nr (ZBound _) = 1 | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 96 | | cons_nr (ZConst0 _) = 2 | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 97 | | cons_nr (ZConst1 _) = 3 | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 98 | | cons_nr (ZConst _) = 4 | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 99 | | cons_nr (ZAbs _) = 5 | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 100 | | cons_nr (ZApp _) = 6 | 
| 80293 | 101 | | cons_nr (OFCLASS _) = 7; | 
| 79264 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 102 | |
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 103 | fun struct_ord tu = | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 104 | if pointer_eq tu then EQUAL | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 105 | else | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 106 | (case tu of | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 107 | (ZAbs (_, _, t), ZAbs (_, _, u)) => struct_ord (t, u) | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 108 | | (ZApp (t1, t2), ZApp (u1, u2)) => | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 109 | (case struct_ord (t1, u1) of EQUAL => struct_ord (t2, u2) | ord => ord) | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 110 | | (t, u) => int_ord (cons_nr t, cons_nr u)); | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 111 | |
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 112 | fun atoms_ord tu = | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 113 | if pointer_eq tu then EQUAL | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 114 | else | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 115 | (case tu of | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 116 | (ZAbs (_, _, t), ZAbs (_, _, u)) => atoms_ord (t, u) | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 117 | | (ZApp (t1, t2), ZApp (u1, u2)) => | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 118 | (case atoms_ord (t1, u1) of EQUAL => atoms_ord (t2, u2) | ord => ord) | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 119 | | (ZConst0 a, ZConst0 b) => fast_string_ord (a, b) | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 120 | | (ZConst1 (a, _), ZConst1 (b, _)) => fast_string_ord (a, b) | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 121 | | (ZConst (a, _), ZConst (b, _)) => fast_string_ord (a, b) | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 122 | | (ZVar (xi, _), ZVar (yj, _)) => Term_Ord.fast_indexname_ord (xi, yj) | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 123 | | (ZBound i, ZBound j) => int_ord (i, j) | 
| 80293 | 124 | | (OFCLASS (_, a), OFCLASS (_, b)) => fast_string_ord (a, b) | 
| 79264 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 125 | | _ => EQUAL); | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 126 | |
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 127 | fun types_ord tu = | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 128 | if pointer_eq tu then EQUAL | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 129 | else | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 130 | (case tu of | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 131 | (ZAbs (_, T, t), ZAbs (_, U, u)) => | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 132 | (case ztyp_ord (T, U) of EQUAL => types_ord (t, u) | ord => ord) | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 133 | | (ZApp (t1, t2), ZApp (u1, u2)) => | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 134 | (case types_ord (t1, u1) of EQUAL => types_ord (t2, u2) | ord => ord) | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 135 | | (ZConst1 (_, T), ZConst1 (_, U)) => ztyp_ord (T, U) | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 136 | | (ZConst (_, Ts), ZConst (_, Us)) => dict_ord ztyp_ord (Ts, Us) | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 137 | | (ZVar (_, T), ZVar (_, U)) => ztyp_ord (T, U) | 
| 80293 | 138 | | (OFCLASS (T, _), OFCLASS (U, _)) => ztyp_ord (T, U) | 
| 79264 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 139 | | _ => EQUAL); | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 140 | |
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 141 | in | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 142 | |
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 143 | val fast_zterm_ord = struct_ord ||| atoms_ord ||| types_ord; | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 144 | |
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 145 | end; | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 146 | |
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 147 | fun aconv_zterm (tm1, tm2) = | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 148 | pointer_eq (tm1, tm2) orelse | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 149 | (case (tm1, tm2) of | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 150 | (ZApp (t1, u1), ZApp (t2, u2)) => aconv_zterm (t1, t2) andalso aconv_zterm (u1, u2) | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 151 | | (ZAbs (_, T1, t1), ZAbs (_, T2, t2)) => aconv_zterm (t1, t2) andalso T1 = T2 | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 152 | | (a1, a2) => a1 = a2); | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 153 | |
| 79124 | 154 | end; | 
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 155 | |
| 79124 | 156 | |
| 79264 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 157 | (* tables and term items *) | 
| 79124 | 158 | |
| 79163 | 159 | structure ZTypes = Table(type key = ztyp val ord = ZTerm.ztyp_ord); | 
| 79264 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 160 | structure ZTerms = Table(type key = zterm val ord = ZTerm.fast_zterm_ord); | 
| 79163 | 161 | |
| 79124 | 162 | structure ZTVars: | 
| 163 | sig | |
| 164 | include TERM_ITEMS | |
| 165 | val add_tvarsT: ztyp -> set -> set | |
| 166 | val add_tvars: zterm -> set -> set | |
| 167 | end = | |
| 168 | struct | |
| 169 | open TVars; | |
| 170 | val add_tvarsT = ZTerm.fold_tvars add_set; | |
| 171 | val add_tvars = ZTerm.fold_types add_tvarsT; | |
| 172 | end; | |
| 173 | ||
| 174 | structure ZVars: | |
| 175 | sig | |
| 176 | include TERM_ITEMS | |
| 177 | val add_vars: zterm -> set -> set | |
| 178 | end = | |
| 179 | struct | |
| 180 | ||
| 181 | structure Term_Items = Term_Items | |
| 182 | ( | |
| 183 | type key = indexname * ztyp; | |
| 184 | val ord = pointer_eq_ord (prod_ord Term_Ord.fast_indexname_ord ZTerm.ztyp_ord); | |
| 185 | ); | |
| 186 | open Term_Items; | |
| 187 | ||
| 80266 | 188 | val add_vars = ZTerm.fold_vars add_set; | 
| 79124 | 189 | |
| 190 | end; | |
| 191 | ||
| 192 | ||
| 193 | (* proofs *) | |
| 194 | ||
| 79126 | 195 | datatype zproof_name = | 
| 196 | ZAxiom of string | |
| 197 | | ZOracle of string | |
| 80289 | 198 |   | ZThm of {theory_name: string, thm_name: Thm_Name.P, serial: serial};
 | 
| 79126 | 199 | |
| 80264 | 200 | type zproof_const = (zproof_name * zterm) * (ztyp ZTVars.table * zterm ZVars.table); | 
| 201 | ||
| 79124 | 202 | datatype zproof = | 
| 203 | ZDummy (*dummy proof*) | |
| 80264 | 204 | | ZConstp of zproof_const | 
| 79212 | 205 | | ZBoundp of int | 
| 79124 | 206 | | ZAbst of string * ztyp * zproof | 
| 79212 | 207 | | ZAbsp of string * zterm * zproof | 
| 79124 | 208 | | ZAppt of zproof * zterm | 
| 79212 | 209 | | ZAppp of zproof * zproof | 
| 79476 | 210 | | ZHyp of zterm | 
| 80293 | 211 | | OFCLASSp of ztyp * class; (*OFCLASS proof from sorts algebra*) | 
| 79124 | 212 | |
| 213 | ||
| 214 | ||
| 215 | (*** local ***) | |
| 216 | ||
| 217 | signature ZTERM = | |
| 218 | sig | |
| 219 | datatype ztyp = datatype ztyp | |
| 220 | datatype zterm = datatype zterm | |
| 221 | datatype zproof = datatype zproof | |
| 79265 | 222 | exception ZTERM of string * ztyp list * zterm list * zproof list | 
| 79124 | 223 | val fold_tvars: (indexname * sort -> 'a -> 'a) -> ztyp -> 'a -> 'a | 
| 224 | val fold_aterms: (zterm -> 'a -> 'a) -> zterm -> 'a -> 'a | |
| 80266 | 225 | val fold_vars: (indexname * ztyp -> 'a -> 'a) -> zterm -> 'a -> 'a | 
| 79124 | 226 | val fold_types: (ztyp -> 'a -> 'a) -> zterm -> 'a -> 'a | 
| 79264 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 227 | val ztyp_ord: ztyp ord | 
| 
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
 wenzelm parents: 
79263diff
changeset | 228 | val fast_zterm_ord: zterm ord | 
| 79124 | 229 | val aconv_zterm: zterm * zterm -> bool | 
| 79212 | 230 | val ZAbsts: (string * ztyp) list -> zproof -> zproof | 
| 231 | val ZAbsps: zterm list -> zproof -> zproof | |
| 232 | val ZAppts: zproof * zterm list -> zproof | |
| 233 | val ZAppps: zproof * zproof list -> zproof | |
| 79200 | 234 | val incr_bv_same: int -> int -> zterm Same.operation | 
| 79283 | 235 | val incr_proof_bv_same: int -> int -> int -> int -> zproof Same.operation | 
| 79200 | 236 | val incr_bv: int -> int -> zterm -> zterm | 
| 237 | val incr_boundvars: int -> zterm -> zterm | |
| 79283 | 238 | val incr_proof_bv: int -> int -> int -> int -> zproof -> zproof | 
| 239 | val incr_proof_boundvars: int -> int -> zproof -> zproof | |
| 240 | val subst_term_bound_same: zterm -> int -> zterm Same.operation | |
| 241 | val subst_proof_bound_same: zterm -> int -> zproof Same.operation | |
| 242 | val subst_proof_boundp_same: zproof -> int -> int -> zproof Same.operation | |
| 243 | val subst_term_bound: zterm -> zterm -> zterm | |
| 244 | val subst_proof_bound: zterm -> zproof -> zproof | |
| 245 | val subst_proof_boundp: zproof -> zproof -> zproof | |
| 79388 | 246 | val subst_type_same: (indexname * sort, ztyp) Same.function -> ztyp Same.operation | 
| 247 | val subst_term_same: | |
| 248 | ztyp Same.operation -> (indexname * ztyp, zterm) Same.function -> zterm Same.operation | |
| 79318 | 249 | exception BAD_INST of ((indexname * ztyp) * zterm) list | 
| 79418 | 250 |   val map_proof: {hyps: bool} -> ztyp Same.operation -> zterm Same.operation -> zproof -> zproof
 | 
| 251 |   val map_proof_types: {hyps: bool} -> ztyp Same.operation -> zproof -> zproof
 | |
| 79414 | 252 | val map_class_proof: (ztyp * class, zproof) Same.function -> zproof -> zproof | 
| 79124 | 253 | val ztyp_of: typ -> ztyp | 
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 254 |   val zterm_cache: theory -> {zterm: term -> zterm, ztyp: typ -> ztyp}
 | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 255 | val zterm_of: theory -> term -> zterm | 
| 79477 | 256 | val typ_of_tvar: indexname * sort -> typ | 
| 79124 | 257 | val typ_of: ztyp -> typ | 
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 258 | val term_of: theory -> zterm -> term | 
| 79298 | 259 | val beta_norm_term_same: zterm Same.operation | 
| 260 | val beta_norm_proof_same: zproof Same.operation | |
| 79302 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 261 | val beta_norm_prooft_same: zproof -> zproof | 
| 79298 | 262 | val beta_norm_term: zterm -> zterm | 
| 263 | val beta_norm_proof: zproof -> zproof | |
| 79302 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 264 | val beta_norm_prooft: zproof -> zproof | 
| 79200 | 265 | val norm_type: Type.tyenv -> ztyp -> ztyp | 
| 266 | val norm_term: theory -> Envir.env -> zterm -> zterm | |
| 79270 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 267 | val norm_proof: theory -> Envir.env -> term list -> zproof -> zproof | 
| 80267 | 268 | val zproof_const_typargs: zproof_const -> ((indexname * sort) * ztyp) list | 
| 269 | val zproof_const_args: zproof_const -> ((indexname * ztyp) * zterm) list | |
| 79311 
e4a9a861856b
omit pointless future: proof terms are built sequentially;
 wenzelm parents: 
79303diff
changeset | 270 | type zbox = serial * (zterm * zproof) | 
| 79279 | 271 | val zbox_ord: zbox ord | 
| 272 | type zboxes = zbox Ord_List.T | |
| 79265 | 273 | val union_zboxes: zboxes -> zboxes -> zboxes | 
| 79279 | 274 | val unions_zboxes: zboxes list -> zboxes | 
| 79425 | 275 |   val add_box_proof: {unconstrain: bool} -> theory ->
 | 
| 276 | term list -> term -> zproof -> zboxes -> zproof * zboxes | |
| 80289 | 277 | val thm_proof: theory -> Thm_Name.P -> term list -> term -> zproof -> zbox * zproof | 
| 79126 | 278 | val axiom_proof: theory -> string -> term -> zproof | 
| 279 | val oracle_proof: theory -> string -> term -> zproof | |
| 79124 | 280 | val assume_proof: theory -> term -> zproof | 
| 281 | val trivial_proof: theory -> term -> zproof | |
| 79414 | 282 | val implies_intr_proof': zterm -> zproof -> zproof | 
| 79124 | 283 | val implies_intr_proof: theory -> term -> zproof -> zproof | 
| 284 | val forall_intr_proof: theory -> typ -> string * term -> zproof -> zproof | |
| 285 | val forall_elim_proof: theory -> term -> zproof -> zproof | |
| 79128 | 286 | val of_class_proof: typ * class -> zproof | 
| 79124 | 287 | val reflexive_proof: theory -> typ -> term -> zproof | 
| 288 | val symmetric_proof: theory -> typ -> term -> term -> zproof -> zproof | |
| 289 | val transitive_proof: theory -> typ -> term -> term -> term -> zproof -> zproof -> zproof | |
| 290 | val equal_intr_proof: theory -> term -> term -> zproof -> zproof -> zproof | |
| 291 | val equal_elim_proof: theory -> term -> term -> zproof -> zproof -> zproof | |
| 292 | val abstract_rule_proof: theory -> typ -> typ -> string * term -> term -> term -> zproof -> zproof | |
| 293 | val combination_proof: theory -> typ -> typ -> term -> term -> term -> term -> | |
| 294 | zproof -> zproof -> zproof | |
| 79133 | 295 | val generalize_proof: Names.set * Names.set -> int -> zproof -> zproof | 
| 79149 | 296 | val instantiate_proof: theory -> | 
| 297 | ((indexname * sort) * typ) list * ((indexname * typ) * term) list -> zproof -> zproof | |
| 79135 | 298 | val varifyT_proof: ((string * sort) * (indexname * sort)) list -> zproof -> zproof | 
| 79152 | 299 | val legacy_freezeT_proof: term -> zproof -> zproof | 
| 79155 | 300 | val rotate_proof: theory -> term list -> term -> (string * typ) list -> | 
| 301 | term list -> int -> zproof -> zproof | |
| 302 | val permute_prems_proof: theory -> term list -> int -> int -> zproof -> zproof | |
| 79234 | 303 | val incr_indexes_proof: int -> zproof -> zproof | 
| 79237 | 304 | val lift_proof: theory -> term -> int -> term list -> zproof -> zproof | 
| 79270 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 305 | val assumption_proof: theory -> Envir.env -> term list -> term -> int -> term list -> | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 306 | zproof -> zproof | 
| 79261 | 307 | val bicompose_proof: theory -> Envir.env -> int -> bool -> term list -> term list -> | 
| 79270 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 308 | term option -> term list -> int -> int -> term list -> zproof -> zproof -> zproof | 
| 79425 | 309 | type sorts_proof = (class * class -> zproof) * (string * class list list * class -> zproof) | 
| 310 | val of_sort_proof: Sorts.algebra -> sorts_proof -> (typ * class -> zproof) -> | |
| 79405 | 311 | typ * sort -> zproof list | 
| 79425 | 312 | val unconstrainT_proof: theory -> sorts_proof -> Logic.unconstrain_context -> zproof -> zproof | 
| 79124 | 313 | end; | 
| 314 | ||
| 315 | structure ZTerm: ZTERM = | |
| 316 | struct | |
| 317 | ||
| 318 | datatype ztyp = datatype ztyp; | |
| 319 | datatype zterm = datatype zterm; | |
| 320 | datatype zproof = datatype zproof; | |
| 321 | ||
| 79265 | 322 | exception ZTERM of string * ztyp list * zterm list * zproof list; | 
| 323 | ||
| 79124 | 324 | open ZTerm; | 
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 325 | |
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 326 | |
| 79155 | 327 | (* derived operations *) | 
| 328 | ||
| 79234 | 329 | val ZFuns = Library.foldr ZFun; | 
| 330 | ||
| 79212 | 331 | val ZAbsts = fold_rev (fn (x, T) => fn prf => ZAbst (x, T, prf)); | 
| 332 | val ZAbsps = fold_rev (fn t => fn prf => ZAbsp ("H", t, prf));
 | |
| 79155 | 333 | |
| 79212 | 334 | val ZAppts = Library.foldl ZAppt; | 
| 335 | val ZAppps = Library.foldl ZAppp; | |
| 79155 | 336 | |
| 79234 | 337 | fun combound (t, n, k) = | 
| 338 | if k > 0 then ZApp (combound (t, n + 1, k - 1), ZBound n) else t; | |
| 339 | ||
| 79155 | 340 | |
| 79283 | 341 | (* increment bound variables *) | 
| 79200 | 342 | |
| 343 | fun incr_bv_same inc = | |
| 344 | if inc = 0 then fn _ => Same.same | |
| 345 | else | |
| 346 | let | |
| 79285 | 347 | fun term lev (ZBound i) = | 
| 348 | if i >= lev then ZBound (i + inc) else raise Same.SAME | |
| 79200 | 349 | | term lev (ZAbs (a, T, t)) = ZAbs (a, T, term (lev + 1) t) | 
| 350 | | term lev (ZApp (t, u)) = | |
| 79285 | 351 | (ZApp (term lev t, Same.commit (term lev) u) | 
| 352 | handle Same.SAME => ZApp (t, term lev u)) | |
| 79200 | 353 | | term _ _ = raise Same.SAME; | 
| 354 | in term end; | |
| 355 | ||
| 79283 | 356 | fun incr_proof_bv_same incp inct = | 
| 357 | if incp = 0 andalso inct = 0 then fn _ => fn _ => Same.same | |
| 358 | else | |
| 359 | let | |
| 360 | val term = incr_bv_same inct; | |
| 79200 | 361 | |
| 79283 | 362 | fun proof plev _ (ZBoundp i) = | 
| 363 | if i >= plev then ZBoundp (i + incp) else raise Same.SAME | |
| 364 | | proof plev tlev (ZAbsp (a, t, p)) = | |
| 365 | (ZAbsp (a, term tlev t, Same.commit (proof (plev + 1) tlev) p) | |
| 366 | handle Same.SAME => ZAbsp (a, t, proof (plev + 1) tlev p)) | |
| 367 | | proof plev tlev (ZAbst (a, T, p)) = ZAbst (a, T, proof plev (tlev + 1) p) | |
| 368 | | proof plev tlev (ZAppp (p, q)) = | |
| 369 | (ZAppp (proof plev tlev p, Same.commit (proof plev tlev) q) | |
| 370 | handle Same.SAME => ZAppp (p, proof plev tlev q)) | |
| 371 | | proof plev tlev (ZAppt (p, t)) = | |
| 372 | (ZAppt (proof plev tlev p, Same.commit (term tlev) t) | |
| 373 | handle Same.SAME => ZAppt (p, term tlev t)) | |
| 374 | | proof _ _ _ = raise Same.SAME; | |
| 375 | in proof end; | |
| 376 | ||
| 377 | fun incr_bv inc lev = Same.commit (incr_bv_same inc lev); | |
| 79200 | 378 | fun incr_boundvars inc = incr_bv inc 0; | 
| 379 | ||
| 79283 | 380 | fun incr_proof_bv incp inct plev tlev = Same.commit (incr_proof_bv_same incp inct plev tlev); | 
| 381 | fun incr_proof_boundvars incp inct = incr_proof_bv incp inct 0 0; | |
| 382 | ||
| 383 | ||
| 384 | (* substitution of bound variables *) | |
| 385 | ||
| 79281 
28342f38da5c
clarified signature, following Term.subst_bounds_same;
 wenzelm parents: 
79279diff
changeset | 386 | fun subst_term_bound_same arg = | 
| 79200 | 387 | let | 
| 388 | fun term lev (ZBound i) = | |
| 389 | if i < lev then raise Same.SAME (*var is locally bound*) | |
| 390 | else if i = lev then incr_boundvars lev arg | |
| 391 | else ZBound (i - 1) (*loose: change it*) | |
| 392 | | term lev (ZAbs (a, T, t)) = ZAbs (a, T, term (lev + 1) t) | |
| 393 | | term lev (ZApp (t, u)) = | |
| 394 | (ZApp (term lev t, Same.commit (term lev) u) | |
| 395 | handle Same.SAME => ZApp (t, term lev u)) | |
| 396 | | term _ _ = raise Same.SAME; | |
| 79281 
28342f38da5c
clarified signature, following Term.subst_bounds_same;
 wenzelm parents: 
79279diff
changeset | 397 | in term end; | 
| 
28342f38da5c
clarified signature, following Term.subst_bounds_same;
 wenzelm parents: 
79279diff
changeset | 398 | |
| 79283 | 399 | fun subst_proof_bound_same arg = | 
| 400 | let | |
| 401 | val term = subst_term_bound_same arg; | |
| 402 | ||
| 403 | fun proof lev (ZAbsp (a, t, p)) = | |
| 404 | (ZAbsp (a, term lev t, Same.commit (proof lev) p) | |
| 405 | handle Same.SAME => ZAbsp (a, t, proof lev p)) | |
| 406 | | proof lev (ZAbst (a, T, p)) = ZAbst (a, T, proof (lev + 1) p) | |
| 407 | | proof lev (ZAppp (p, q)) = | |
| 408 | (ZAppp (proof lev p, Same.commit (proof lev) q) | |
| 409 | handle Same.SAME => ZAppp (p, proof lev q)) | |
| 410 | | proof lev (ZAppt (p, t)) = | |
| 411 | (ZAppt (proof lev p, Same.commit (term lev) t) | |
| 412 | handle Same.SAME => ZAppt (p, term lev t)) | |
| 413 | | proof _ _ = raise Same.SAME; | |
| 414 | in proof end; | |
| 415 | ||
| 416 | fun subst_proof_boundp_same arg = | |
| 417 | let | |
| 418 | fun proof plev tlev (ZBoundp i) = | |
| 419 | if i < plev then raise Same.SAME (*var is locally bound*) | |
| 420 | else if i = plev then incr_proof_boundvars plev tlev arg | |
| 421 | else ZBoundp (i - 1) (*loose: change it*) | |
| 422 | | proof plev tlev (ZAbsp (a, t, p)) = ZAbsp (a, t, proof (plev + 1) tlev p) | |
| 423 | | proof plev tlev (ZAbst (a, T, p)) = ZAbst (a, T, proof plev (tlev + 1) p) | |
| 424 | | proof plev tlev (ZAppp (p, q)) = | |
| 425 | (ZAppp (proof plev tlev p, Same.commit (proof plev tlev) q) | |
| 426 | handle Same.SAME => ZAppp (p, proof plev tlev q)) | |
| 427 | | proof plev tlev (ZAppt (p, t)) = ZAppt (proof plev tlev p, t) | |
| 428 | | proof _ _ _ = raise Same.SAME; | |
| 429 | in proof end; | |
| 430 | ||
| 431 | fun subst_term_bound arg body = Same.commit (subst_term_bound_same arg 0) body; | |
| 432 | fun subst_proof_bound arg body = Same.commit (subst_proof_bound_same arg 0) body; | |
| 433 | fun subst_proof_boundp arg body = Same.commit (subst_proof_boundp_same arg 0 0) body; | |
| 79200 | 434 | |
| 435 | ||
| 79268 | 436 | (* substitution of free or schematic variables *) | 
| 79132 | 437 | |
| 438 | fun subst_type_same tvar = | |
| 439 | let | |
| 440 | fun typ (ZTVar x) = tvar x | |
| 79326 | 441 | | typ (ZFun (T, U)) = | 
| 442 | (ZFun (typ T, Same.commit typ U) | |
| 443 | handle Same.SAME => ZFun (T, typ U)) | |
| 79132 | 444 | | typ ZProp = raise Same.SAME | 
| 445 | | typ (ZType0 _) = raise Same.SAME | |
| 446 | | typ (ZType1 (a, T)) = ZType1 (a, typ T) | |
| 447 | | typ (ZType (a, Ts)) = ZType (a, Same.map typ Ts); | |
| 448 | in typ end; | |
| 449 | ||
| 450 | fun subst_term_same typ var = | |
| 451 | let | |
| 452 | fun term (ZVar (x, T)) = | |
| 453 | let val (T', same) = Same.commit_id typ T in | |
| 454 | (case Same.catch var (x, T') of | |
| 455 | NONE => if same then raise Same.SAME else ZVar (x, T') | |
| 456 | | SOME t' => t') | |
| 457 | end | |
| 458 | | term (ZBound _) = raise Same.SAME | |
| 459 | | term (ZConst0 _) = raise Same.SAME | |
| 460 | | term (ZConst1 (a, T)) = ZConst1 (a, typ T) | |
| 461 | | term (ZConst (a, Ts)) = ZConst (a, Same.map typ Ts) | |
| 462 | | term (ZAbs (a, T, t)) = | |
| 79326 | 463 | (ZAbs (a, typ T, Same.commit term t) | 
| 464 | handle Same.SAME => ZAbs (a, T, term t)) | |
| 79132 | 465 | | term (ZApp (t, u)) = | 
| 79326 | 466 | (ZApp (term t, Same.commit term u) | 
| 467 | handle Same.SAME => ZApp (t, term u)) | |
| 80293 | 468 | | term (OFCLASS (T, c)) = OFCLASS (typ T, c); | 
| 79132 | 469 | in term end; | 
| 470 | ||
| 79270 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 471 | fun instantiate_type_same instT = | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 472 | if ZTVars.is_empty instT then Same.same | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 473 | else ZTypes.unsynchronized_cache (subst_type_same (Same.function (ZTVars.lookup instT))); | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 474 | |
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 475 | fun instantiate_term_same typ inst = | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 476 | subst_term_same typ (Same.function (ZVars.lookup inst)); | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 477 | |
| 79268 | 478 | |
| 79475 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 479 | (* fold types/terms within proof *) | 
| 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 480 | |
| 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 481 | fun fold_proof {hyps} typ term =
 | 
| 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 482 | let | 
| 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 483 | fun proof ZDummy = I | 
| 80262 | 484 | | proof (ZConstp (_, (instT, inst))) = | 
| 79475 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 485 | ZTVars.fold (typ o #2) instT #> ZVars.fold (term o #2) inst | 
| 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 486 | | proof (ZBoundp _) = I | 
| 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 487 | | proof (ZAbst (_, T, p)) = typ T #> proof p | 
| 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 488 | | proof (ZAbsp (_, t, p)) = term t #> proof p | 
| 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 489 | | proof (ZAppt (p, t)) = proof p #> term t | 
| 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 490 | | proof (ZAppp (p, q)) = proof p #> proof q | 
| 79476 | 491 | | proof (ZHyp h) = hyps ? term h | 
| 80293 | 492 | | proof (OFCLASSp (T, _)) = hyps ? typ T; | 
| 79475 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 493 | in proof end; | 
| 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 494 | |
| 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 495 | fun fold_proof_types hyps typ = | 
| 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 496 | fold_proof hyps typ (fold_types typ); | 
| 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 497 | |
| 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 498 | |
| 79268 | 499 | (* map types/terms within proof *) | 
| 500 | ||
| 79318 | 501 | exception BAD_INST of ((indexname * ztyp) * zterm) list | 
| 502 | ||
| 503 | fun make_inst inst = | |
| 79320 
bbac3e8a5070
more permissive: allow collapse of term variables for equal results, e.g. relevant for metis (line 1882 of "~~/src/HOL/List.thy");
 wenzelm parents: 
79318diff
changeset | 504 | ZVars.build (inst |> fold (ZVars.insert (op aconv_zterm))) | 
| 
bbac3e8a5070
more permissive: allow collapse of term variables for equal results, e.g. relevant for metis (line 1882 of "~~/src/HOL/List.thy");
 wenzelm parents: 
79318diff
changeset | 505 | handle ZVars.DUP _ => raise BAD_INST inst; | 
| 79318 | 506 | |
| 79145 | 507 | fun map_insts_same typ term (instT, inst) = | 
| 508 | let | |
| 509 | val changed = Unsynchronized.ref false; | |
| 79146 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 510 | fun apply f x = | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 511 | (case Same.catch f x of | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 512 | NONE => NONE | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 513 | | some => (changed := true; some)); | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 514 | |
| 79145 | 515 | val instT' = | 
| 516 | (instT, instT) |-> ZTVars.fold (fn (v, T) => | |
| 79146 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 517 | (case apply typ T of | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 518 | NONE => I | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 519 | | SOME T' => ZTVars.update (v, T'))); | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 520 | |
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 521 | val vars' = | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 522 | (inst, ZVars.empty) |-> ZVars.fold (fn ((v, T), _) => | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 523 | (case apply typ T of | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 524 | NONE => I | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 525 | | SOME T' => ZVars.add ((v, T), (v, T')))); | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 526 | |
| 79145 | 527 | val inst' = | 
| 79146 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 528 | if ZVars.is_empty vars' then | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 529 | (inst, inst) |-> ZVars.fold (fn (v, t) => | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 530 | (case apply term t of | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 531 | NONE => I | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 532 | | SOME t' => ZVars.update (v, t'))) | 
| 
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
 wenzelm parents: 
79145diff
changeset | 533 | else | 
| 79325 | 534 | build (inst |> ZVars.fold_rev (fn (v, t) => | 
| 535 | cons (the_default v (ZVars.lookup vars' v), the_default t (apply term t)))) | |
| 79318 | 536 | |> make_inst; | 
| 79145 | 537 | in if ! changed then (instT', inst') else raise Same.SAME end; | 
| 538 | ||
| 79418 | 539 | fun map_proof_same {hyps} typ term =
 | 
| 79132 | 540 | let | 
| 541 | fun proof ZDummy = raise Same.SAME | |
| 80262 | 542 | | proof (ZConstp (a, (instT, inst))) = | 
| 543 | ZConstp (a, map_insts_same typ term (instT, inst)) | |
| 79212 | 544 | | proof (ZBoundp _) = raise Same.SAME | 
| 79132 | 545 | | proof (ZAbst (a, T, p)) = | 
| 79326 | 546 | (ZAbst (a, typ T, Same.commit proof p) | 
| 547 | handle Same.SAME => ZAbst (a, T, proof p)) | |
| 79212 | 548 | | proof (ZAbsp (a, t, p)) = | 
| 79326 | 549 | (ZAbsp (a, term t, Same.commit proof p) | 
| 550 | handle Same.SAME => ZAbsp (a, t, proof p)) | |
| 79132 | 551 | | proof (ZAppt (p, t)) = | 
| 79326 | 552 | (ZAppt (proof p, Same.commit term t) | 
| 553 | handle Same.SAME => ZAppt (p, term t)) | |
| 79212 | 554 | | proof (ZAppp (p, q)) = | 
| 79326 | 555 | (ZAppp (proof p, Same.commit proof q) | 
| 556 | handle Same.SAME => ZAppp (p, proof q)) | |
| 79476 | 557 | | proof (ZHyp h) = if hyps then ZHyp (term h) else raise Same.SAME | 
| 80293 | 558 | | proof (OFCLASSp (T, c)) = if hyps then OFCLASSp (typ T, c) else raise Same.SAME; | 
| 79132 | 559 | in proof end; | 
| 560 | ||
| 79418 | 561 | fun map_proof hyps typ term = Same.commit (map_proof_same hyps typ term); | 
| 562 | fun map_proof_types hyps typ = map_proof hyps typ (subst_term_same typ Same.same); | |
| 79144 | 563 | |
| 79124 | 564 | |
| 79414 | 565 | (* map class proofs *) | 
| 566 | ||
| 567 | fun map_class_proof class = | |
| 568 | let | |
| 80293 | 569 | fun proof (OFCLASSp C) = class C | 
| 79414 | 570 | | proof (ZAbst (a, T, p)) = ZAbst (a, T, proof p) | 
| 571 | | proof (ZAbsp (a, t, p)) = ZAbsp (a, t, proof p) | |
| 572 | | proof (ZAppt (p, t)) = ZAppt (proof p, t) | |
| 573 | | proof (ZAppp (p, q)) = | |
| 574 | (ZAppp (proof p, Same.commit proof q) | |
| 575 | handle Same.SAME => ZAppp (p, proof q)) | |
| 576 | | proof _ = raise Same.SAME; | |
| 577 | in Same.commit proof end; | |
| 578 | ||
| 579 | ||
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 580 | (* convert ztyp / zterm vs. regular typ / term *) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 581 | |
| 79119 | 582 | fun ztyp_of (TFree (a, S)) = ZTVar ((a, ~1), S) | 
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 583 | | ztyp_of (TVar v) = ZTVar v | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 584 |   | ztyp_of (Type ("fun", [T, U])) = ZFun (ztyp_of T, ztyp_of U)
 | 
| 79421 | 585 |   | ztyp_of (Type ("prop", [])) = ZProp
 | 
| 586 | | ztyp_of (Type (c, [])) = ZType0 c | |
| 79420 
4c3346b4b100
clarified datatype ztyp: omit special case that rarely occurs (thanks to ZClass and ZClassp);
 wenzelm parents: 
79419diff
changeset | 587 | | ztyp_of (Type (c, [T])) = ZType1 (c, ztyp_of T) | 
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 588 | | ztyp_of (Type (c, ts)) = ZType (c, map ztyp_of ts); | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 589 | |
| 79163 | 590 | fun ztyp_cache () = Typtab.unsynchronized_cache ztyp_of; | 
| 591 | ||
| 79226 | 592 | fun zterm_cache thy = | 
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 593 | let | 
| 79226 | 594 | val typargs = Consts.typargs (Sign.consts_of thy); | 
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 595 | |
| 79163 | 596 | val ztyp = ztyp_cache (); | 
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 597 | |
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 598 | fun zterm (Free (x, T)) = ZVar ((x, ~1), ztyp T) | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 599 | | zterm (Var (xi, T)) = ZVar (xi, ztyp T) | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 600 | | zterm (Bound i) = ZBound i | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 601 | | zterm (Const (c, T)) = | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 602 | (case typargs (c, T) of | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 603 | [] => ZConst0 c | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 604 | | [T] => ZConst1 (c, ztyp T) | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 605 | | Ts => ZConst (c, map ztyp Ts)) | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 606 | | zterm (Abs (a, T, b)) = ZAbs (a, ztyp T, zterm b) | 
| 80294 | 607 | | zterm (tm as t $ u) = | 
| 608 | (case try Logic.match_of_class tm of | |
| 609 | SOME (T, c) => OFCLASS (ztyp T, c) | |
| 610 | | NONE => ZApp (zterm t, zterm u)); | |
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 611 |   in {ztyp = ztyp, zterm = zterm} end;
 | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 612 | |
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 613 | val zterm_of = #zterm o zterm_cache; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 614 | |
| 79477 | 615 | fun typ_of_tvar ((a, ~1), S) = TFree (a, S) | 
| 616 | | typ_of_tvar v = TVar v; | |
| 617 | ||
| 618 | fun typ_of (ZTVar v) = typ_of_tvar v | |
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 619 | | typ_of (ZFun (T, U)) = typ_of T --> typ_of U | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 620 | | typ_of ZProp = propT | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 621 | | typ_of (ZType0 c) = Type (c, []) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 622 | | typ_of (ZType1 (c, T)) = Type (c, [typ_of T]) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 623 | | typ_of (ZType (c, Ts)) = Type (c, map typ_of Ts); | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 624 | |
| 79201 | 625 | fun typ_cache () = ZTypes.unsynchronized_cache typ_of; | 
| 626 | ||
| 79226 | 627 | fun term_of thy = | 
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 628 | let | 
| 79226 | 629 | val instance = Consts.instance (Sign.consts_of thy); | 
| 630 | ||
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 631 | fun const (c, Ts) = Const (c, instance (c, Ts)); | 
| 79226 | 632 | |
| 79119 | 633 | fun term (ZVar ((x, ~1), T)) = Free (x, typ_of T) | 
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 634 | | term (ZVar (xi, T)) = Var (xi, typ_of T) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 635 | | term (ZBound i) = Bound i | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 636 | | term (ZConst0 c) = const (c, []) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 637 | | term (ZConst1 (c, T)) = const (c, [typ_of T]) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 638 | | term (ZConst (c, Ts)) = const (c, map typ_of Ts) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 639 | | term (ZAbs (a, T, b)) = Abs (a, typ_of T, term b) | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 640 | | term (ZApp (t, u)) = term t $ term u | 
| 80293 | 641 | | term (OFCLASS (T, c)) = Logic.mk_of_class (typ_of T, c); | 
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 642 | in term end; | 
| 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 643 | |
| 79119 | 644 | |
| 79298 | 645 | (* beta contraction *) | 
| 79200 | 646 | |
| 79286 | 647 | val beta_norm_term_same = | 
| 79271 | 648 | let | 
| 79299 | 649 | fun norm (ZAbs (a, T, t)) = ZAbs (a, T, Same.commit norm t) | 
| 650 | | norm (ZApp (ZAbs (_, _, body), t)) = Same.commit norm (subst_term_bound t body) | |
| 79271 | 651 | | norm (ZApp (f, t)) = | 
| 652 | ((case norm f of | |
| 79286 | 653 | ZAbs (_, _, body) => Same.commit norm (subst_term_bound t body) | 
| 79271 | 654 | | nf => ZApp (nf, Same.commit norm t)) | 
| 655 | handle Same.SAME => ZApp (f, norm t)) | |
| 656 | | norm _ = raise Same.SAME; | |
| 657 | in norm end; | |
| 658 | ||
| 79302 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 659 | val beta_norm_prooft_same = | 
| 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 660 | let | 
| 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 661 | val term = beta_norm_term_same; | 
| 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 662 | |
| 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 663 | fun norm (ZAbst (a, T, p)) = ZAbst (a, T, norm p) | 
| 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 664 | | norm (ZAppt (ZAbst (_, _, body), t)) = Same.commit norm (subst_proof_bound t body) | 
| 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 665 | | norm (ZAppt (f, t)) = | 
| 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 666 | ((case norm f of | 
| 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 667 | ZAbst (_, _, body) => Same.commit norm (subst_proof_bound t body) | 
| 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 668 | | nf => ZAppt (nf, Same.commit term t)) | 
| 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 669 | handle Same.SAME => ZAppt (f, term t)) | 
| 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 670 | | norm _ = raise Same.SAME; | 
| 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 671 | in norm end; | 
| 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 672 | |
| 79286 | 673 | val beta_norm_proof_same = | 
| 674 | let | |
| 675 | val term = beta_norm_term_same; | |
| 676 | ||
| 79300 
236fa4b32afb
more thorough beta contraction, following Envir.norm_term;
 wenzelm parents: 
79299diff
changeset | 677 | fun norm (ZAbst (a, T, p)) = ZAbst (a, T, norm p) | 
| 
236fa4b32afb
more thorough beta contraction, following Envir.norm_term;
 wenzelm parents: 
79299diff
changeset | 678 | | norm (ZAbsp (a, t, p)) = | 
| 
236fa4b32afb
more thorough beta contraction, following Envir.norm_term;
 wenzelm parents: 
79299diff
changeset | 679 | (ZAbsp (a, term t, Same.commit norm p) | 
| 
236fa4b32afb
more thorough beta contraction, following Envir.norm_term;
 wenzelm parents: 
79299diff
changeset | 680 | handle Same.SAME => ZAbsp (a, t, norm p)) | 
| 
236fa4b32afb
more thorough beta contraction, following Envir.norm_term;
 wenzelm parents: 
79299diff
changeset | 681 | | norm (ZAppt (ZAbst (_, _, body), t)) = Same.commit norm (subst_proof_bound t body) | 
| 79301 | 682 | | norm (ZAppp (ZAbsp (_, _, body), p)) = Same.commit norm (subst_proof_boundp p body) | 
| 79286 | 683 | | norm (ZAppt (f, t)) = | 
| 684 | ((case norm f of | |
| 685 | ZAbst (_, _, body) => Same.commit norm (subst_proof_bound t body) | |
| 686 | | nf => ZAppt (nf, Same.commit term t)) | |
| 687 | handle Same.SAME => ZAppt (f, term t)) | |
| 688 | | norm (ZAppp (f, p)) = | |
| 689 | ((case norm f of | |
| 690 | ZAbsp (_, _, body) => Same.commit norm (subst_proof_boundp p body) | |
| 691 | | nf => ZAppp (nf, Same.commit norm p)) | |
| 692 | handle Same.SAME => ZAppp (f, norm p)) | |
| 693 | | norm _ = raise Same.SAME; | |
| 694 | in norm end; | |
| 695 | ||
| 79298 | 696 | val beta_norm_term = Same.commit beta_norm_term_same; | 
| 697 | val beta_norm_proof = Same.commit beta_norm_proof_same; | |
| 79302 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 698 | val beta_norm_prooft = Same.commit beta_norm_prooft_same; | 
| 79298 | 699 | |
| 700 | ||
| 701 | (* normalization wrt. environment and beta contraction *) | |
| 702 | ||
| 79200 | 703 | fun norm_type_same ztyp tyenv = | 
| 704 | if Vartab.is_empty tyenv then Same.same | |
| 705 | else | |
| 706 | let | |
| 707 | fun norm (ZTVar v) = | |
| 708 | (case Type.lookup tyenv v of | |
| 709 | SOME U => Same.commit norm (ztyp U) | |
| 710 | | NONE => raise Same.SAME) | |
| 711 | | norm (ZFun (T, U)) = | |
| 712 | (ZFun (norm T, Same.commit norm U) | |
| 713 | handle Same.SAME => ZFun (T, norm U)) | |
| 714 | | norm ZProp = raise Same.SAME | |
| 715 | | norm (ZType0 _) = raise Same.SAME | |
| 716 | | norm (ZType1 (a, T)) = ZType1 (a, norm T) | |
| 717 | | norm (ZType (a, Ts)) = ZType (a, Same.map norm Ts); | |
| 718 | in norm end; | |
| 719 | ||
| 79269 | 720 | fun norm_term_same {ztyp, zterm, typ} (envir as Envir.Envir {tyenv, tenv, ...}) =
 | 
| 721 | let | |
| 722 | val lookup = | |
| 723 | if Vartab.is_empty tenv then K NONE | |
| 724 | else ZVars.unsynchronized_cache (apsnd typ #> Envir.lookup envir #> Option.map zterm); | |
| 79200 | 725 | |
| 79269 | 726 | val normT = norm_type_same ztyp tyenv; | 
| 79200 | 727 | |
| 79269 | 728 | fun norm (ZVar (xi, T)) = | 
| 729 | (case lookup (xi, T) of | |
| 730 | SOME u => Same.commit norm u | |
| 731 | | NONE => ZVar (xi, normT T)) | |
| 732 | | norm (ZBound _) = raise Same.SAME | |
| 733 | | norm (ZConst0 _) = raise Same.SAME | |
| 734 | | norm (ZConst1 (a, T)) = ZConst1 (a, normT T) | |
| 735 | | norm (ZConst (a, Ts)) = ZConst (a, Same.map normT Ts) | |
| 736 | | norm (ZAbs (a, T, t)) = | |
| 737 | (ZAbs (a, normT T, Same.commit norm t) | |
| 738 | handle Same.SAME => ZAbs (a, T, norm t)) | |
| 79281 
28342f38da5c
clarified signature, following Term.subst_bounds_same;
 wenzelm parents: 
79279diff
changeset | 739 | | norm (ZApp (ZAbs (_, _, body), t)) = Same.commit norm (subst_term_bound t body) | 
| 79269 | 740 | | norm (ZApp (f, t)) = | 
| 741 | ((case norm f of | |
| 79281 
28342f38da5c
clarified signature, following Term.subst_bounds_same;
 wenzelm parents: 
79279diff
changeset | 742 | ZAbs (_, _, body) => Same.commit norm (subst_term_bound t body) | 
| 79269 | 743 | | nf => ZApp (nf, Same.commit norm t)) | 
| 744 | handle Same.SAME => ZApp (f, norm t)) | |
| 745 | | norm _ = raise Same.SAME; | |
| 79286 | 746 | in fn t => if Envir.is_empty envir then beta_norm_term_same t else norm t end; | 
| 79200 | 747 | |
| 79270 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 748 | fun norm_proof_cache (cache as {ztyp, ...}) (envir as Envir.Envir {tyenv, ...}) =
 | 
| 79269 | 749 | let | 
| 79270 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 750 | val norm_tvar = ZTVar #> Same.commit (norm_type_same ztyp tyenv); | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 751 | val norm_var = ZVar #> Same.commit (norm_term_same cache envir); | 
| 79269 | 752 | in | 
| 79270 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 753 | fn visible => fn prf => | 
| 79302 
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
 wenzelm parents: 
79301diff
changeset | 754 | if Envir.is_empty envir orelse null visible then beta_norm_prooft prf | 
| 79270 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 755 | else | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 756 | let | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 757 | fun add_tvar v tab = | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 758 | if ZTVars.defined tab v then tab else ZTVars.update (v, norm_tvar v) tab; | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 759 | |
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 760 | val inst_typ = | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 761 | if Vartab.is_empty tyenv then Same.same | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 762 | else | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 763 | ZTVars.build (visible |> (fold o Term.fold_types o Term.fold_atyps) | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 764 | (fn TVar v => add_tvar v | _ => I)) | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 765 | |> instantiate_type_same; | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 766 | |
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 767 | fun add_var (a, T) tab = | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 768 | let val v = (a, Same.commit inst_typ (ztyp T)) | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 769 | in if ZVars.defined tab v then tab else ZVars.update (v, norm_var v) tab end; | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 770 | |
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 771 | val inst_term = | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 772 | ZVars.build (visible |> (fold o Term.fold_aterms) (fn Var v => add_var v | _ => I)) | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 773 | |> instantiate_term_same inst_typ; | 
| 79272 
899f37f6d218
proper beta_norm after instantiation (amending 90c5aadcc4b2);
 wenzelm parents: 
79271diff
changeset | 774 | |
| 79286 | 775 | val norm_term = Same.compose beta_norm_term_same inst_term; | 
| 79418 | 776 |         in beta_norm_prooft (map_proof {hyps = false} inst_typ norm_term prf) end
 | 
| 79269 | 777 | end; | 
| 79210 | 778 | |
| 779 | fun norm_cache thy = | |
| 79205 | 780 | let | 
| 79226 | 781 |     val {ztyp, zterm} = zterm_cache thy;
 | 
| 79205 | 782 | val typ = typ_cache (); | 
| 783 |   in {ztyp = ztyp, zterm = zterm, typ = typ} end;
 | |
| 784 | ||
| 79214 | 785 | fun norm_type tyenv = Same.commit (norm_type_same (ztyp_cache ()) tyenv); | 
| 786 | fun norm_term thy envir = Same.commit (norm_term_same (norm_cache thy) envir); | |
| 79270 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 787 | fun norm_proof thy envir = norm_proof_cache (norm_cache thy) envir; | 
| 79200 | 788 | |
| 789 | ||
| 79119 | 790 | |
| 791 | (** proof construction **) | |
| 79113 
5109e4b2a292
pro-forma support for optional zproof: no proper content yet;
 wenzelm parents: 
79098diff
changeset | 792 | |
| 79161 | 793 | (* constants *) | 
| 79124 | 794 | |
| 80262 | 795 | fun zproof_const (a, A) : zproof_const = | 
| 79119 | 796 | let | 
| 79154 | 797 | val instT = | 
| 79263 | 798 | ZTVars.build (A |> (fold_types o fold_tvars) (fn v => fn tab => | 
| 79154 | 799 | if ZTVars.defined tab v then tab else ZTVars.update (v, ZTVar v) tab)); | 
| 800 | val inst = | |
| 80266 | 801 | ZVars.build (A |> fold_vars (fn v => fn tab => | 
| 802 | if ZVars.defined tab v then tab else ZVars.update (v, ZVar v) tab)); | |
| 80262 | 803 | in ((a, A), (instT, inst)) end; | 
| 79126 | 804 | |
| 80267 | 805 | fun zproof_const_typargs (((_, A), (instT, _)): zproof_const) = | 
| 806 | ZTVars.build (A |> ZTVars.add_tvars) |> ZTVars.list_set | |
| 807 | |> map (fn v => (v, the_default (ZTVar v) (ZTVars.lookup instT v))); | |
| 808 | ||
| 809 | fun zproof_const_args (((_, A), (_, inst)): zproof_const) = | |
| 810 | ZVars.build (A |> ZVars.add_vars) |> ZVars.list_set | |
| 811 | |> map (fn v => (v, the_default (ZVar v) (ZVars.lookup inst v))); | |
| 812 | ||
| 80264 | 813 | fun make_const_proof (f, g) ((a, insts): zproof_const) = | 
| 79263 | 814 | let | 
| 79417 
a4eae462f224
proper support for complex types, not just type variables (amending 623789141e39);
 wenzelm parents: 
79416diff
changeset | 815 | val typ = subst_type_same (Same.function (fn ((x, _), _) => try f x)); | 
| 79416 
623789141e39
proper instantiation for make_const_proof, notably change of types for term variables;
 wenzelm parents: 
79415diff
changeset | 816 | val term = Same.function (fn ZVar ((x, _), _) => try g x | _ => NONE); | 
| 80262 | 817 | in ZConstp (a, Same.commit (map_insts_same typ term) insts) end; | 
| 79161 | 818 | |
| 819 | ||
| 79265 | 820 | (* closed proof boxes *) | 
| 821 | ||
| 79311 
e4a9a861856b
omit pointless future: proof terms are built sequentially;
 wenzelm parents: 
79303diff
changeset | 822 | type zbox = serial * (zterm * zproof); | 
| 79279 | 823 | val zbox_ord: zbox ord = fn ((i, _), (j, _)) => int_ord (j, i); | 
| 79265 | 824 | |
| 79279 | 825 | type zboxes = zbox Ord_List.T; | 
| 826 | val union_zboxes = Ord_List.union zbox_ord; | |
| 827 | val unions_zboxes = Ord_List.unions zbox_ord; | |
| 828 | val add_zboxes = Ord_List.insert zbox_ord; | |
| 79265 | 829 | |
| 830 | local | |
| 831 | ||
| 79419 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 832 | fun close_prop prems prop = | 
| 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 833 | fold_rev (fn A => fn B => ZApp (ZApp (ZConst0 "Pure.imp", A), B)) prems prop; | 
| 79265 | 834 | |
| 79428 
4cd892d1a676
omit syntactic of_class check, which is in conflict with sort constraints within the logic;
 wenzelm parents: 
79427diff
changeset | 835 | fun close_proof prems prf = | 
| 79265 | 836 | let | 
| 79419 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 837 | val m = length prems - 1; | 
| 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 838 | val bounds = ZTerms.build (prems |> fold_index (fn (i, h) => ZTerms.update (h, m - i))); | 
| 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 839 | fun get_bound lev t = ZTerms.lookup bounds t |> Option.map (fn i => ZBoundp (lev + i)); | 
| 79265 | 840 | |
| 841 | fun proof lev (ZHyp t) = | |
| 79419 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 842 | (case get_bound lev t of | 
| 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 843 | SOME p => p | 
| 79428 
4cd892d1a676
omit syntactic of_class check, which is in conflict with sort constraints within the logic;
 wenzelm parents: 
79427diff
changeset | 844 |           | NONE => raise ZTERM ("Loose bound in proof term", [], [t], [prf]))
 | 
| 80293 | 845 | | proof lev (OFCLASSp C) = | 
| 846 | (case get_bound lev (OFCLASS C) of | |
| 79419 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 847 | SOME p => p | 
| 79428 
4cd892d1a676
omit syntactic of_class check, which is in conflict with sort constraints within the logic;
 wenzelm parents: 
79427diff
changeset | 848 | | NONE => raise Same.SAME) | 
| 79265 | 849 | | proof lev (ZAbst (x, T, p)) = ZAbst (x, T, proof lev p) | 
| 850 | | proof lev (ZAbsp (x, t, p)) = ZAbsp (x, t, proof (lev + 1) p) | |
| 851 | | proof lev (ZAppt (p, t)) = ZAppt (proof lev p, t) | |
| 852 | | proof lev (ZAppp (p, q)) = | |
| 79326 | 853 | (ZAppp (proof lev p, Same.commit (proof lev) q) | 
| 854 | handle Same.SAME => ZAppp (p, proof lev q)) | |
| 79265 | 855 | | proof _ _ = raise Same.SAME; | 
| 79419 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 856 | in ZAbsps prems (Same.commit (proof 0) prf) end; | 
| 79265 | 857 | |
| 80286 | 858 | fun box_proof {unconstrain} thy thm_name hyps concl prf =
 | 
| 79265 | 859 | let | 
| 79428 
4cd892d1a676
omit syntactic of_class check, which is in conflict with sort constraints within the logic;
 wenzelm parents: 
79427diff
changeset | 860 |     val {zterm, ...} = zterm_cache thy;
 | 
| 79419 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 861 | |
| 79477 | 862 | val present_set_prf = | 
| 863 |       ZTVars.build ((fold_proof_types {hyps = true} o fold_tvars) ZTVars.add_set prf);
 | |
| 79475 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 864 | val present_set = | 
| 
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
 wenzelm parents: 
79474diff
changeset | 865 | Types.build (Types.add_atyps concl #> fold Types.add_atyps hyps #> | 
| 79478 | 866 | ZTVars.fold (Types.add_set o typ_of_tvar o #1) present_set_prf); | 
| 79429 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 867 |     val ucontext as {constraints, outer_constraints, ...} =
 | 
| 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 868 | Logic.unconstrain_context [] present_set; | 
| 79419 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 869 | |
| 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 870 |     val typ_operation = #typ_operation ucontext {strip_sorts = true};
 | 
| 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 871 | val unconstrain_typ = Same.commit typ_operation; | 
| 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 872 | val unconstrain_ztyp = | 
| 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 873 | ZTypes.unsynchronized_cache (Same.function_eq (op =) (typ_of #> unconstrain_typ #> ztyp_of)); | 
| 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 874 | val unconstrain_zterm = zterm o Term.map_types typ_operation; | 
| 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 875 |     val unconstrain_proof = Same.commit (map_proof_types {hyps = true} unconstrain_ztyp);
 | 
| 79265 | 876 | |
| 79419 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 877 | val constrain_instT = | 
| 79429 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 878 | if unconstrain then ZTVars.empty | 
| 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 879 | else | 
| 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 880 | ZTVars.build (present_set |> Types.fold (fn (T, _) => | 
| 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 881 | let | 
| 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 882 | val ZTVar v = ztyp_of (unconstrain_typ T) and U = ztyp_of T; | 
| 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 883 | val equal = (case U of ZTVar u => u = v | _ => false); | 
| 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 884 | in not equal ? ZTVars.add (v, U) end)); | 
| 79419 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 885 | val constrain_proof = | 
| 79426 | 886 | if ZTVars.is_empty constrain_instT then I | 
| 887 | else | |
| 888 |         map_proof_types {hyps = true}
 | |
| 889 | (subst_type_same (Same.function (ZTVars.lookup constrain_instT))); | |
| 79419 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 890 | |
| 79429 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 891 | val hyps' = map unconstrain_zterm hyps; | 
| 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 892 | val prems = map (zterm o #2) constraints @ hyps'; | 
| 79419 
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
 wenzelm parents: 
79418diff
changeset | 893 | val prop' = beta_norm_term (close_prop prems (unconstrain_zterm concl)); | 
| 79428 
4cd892d1a676
omit syntactic of_class check, which is in conflict with sort constraints within the logic;
 wenzelm parents: 
79427diff
changeset | 894 | val prf' = beta_norm_prooft (close_proof prems (unconstrain_proof prf)); | 
| 79265 | 895 | |
| 896 | val i = serial (); | |
| 79312 | 897 | val zbox: zbox = (i, (prop', prf')); | 
| 79429 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 898 | |
| 80286 | 899 | val proof_name = | 
| 900 |       ZThm {theory_name = Context.theory_long_name thy, thm_name = thm_name, serial = i};
 | |
| 901 | ||
| 902 | val const = constrain_proof (ZConstp (zproof_const (proof_name, prop'))); | |
| 79429 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 903 | val args1 = | 
| 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 904 | outer_constraints |> map (fn (T, c) => | 
| 80293 | 905 | OFCLASSp (ztyp_of (if unconstrain then unconstrain_typ T else T), c)); | 
| 79429 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 906 | val args2 = if unconstrain then map ZHyp hyps' else map (ZHyp o zterm) hyps; | 
| 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 907 | in (zbox, ZAppps (ZAppps (const, args1), args2)) end; | 
| 79312 | 908 | |
| 79313 
3b03af5125de
use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
 wenzelm parents: 
79312diff
changeset | 909 | in | 
| 
3b03af5125de
use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
 wenzelm parents: 
79312diff
changeset | 910 | |
| 80286 | 911 | val thm_proof = box_proof {unconstrain = false};
 | 
| 79313 
3b03af5125de
use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
 wenzelm parents: 
79312diff
changeset | 912 | |
| 79429 
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
 wenzelm parents: 
79428diff
changeset | 913 | fun add_box_proof unconstrain thy hyps concl prf zboxes = | 
| 79422 | 914 | let | 
| 80286 | 915 | val (zbox, prf') = box_proof unconstrain thy Thm_Name.none hyps concl prf; | 
| 79422 | 916 | val zboxes' = add_zboxes zbox zboxes; | 
| 917 | in (prf', zboxes') end; | |
| 79265 | 918 | |
| 919 | end; | |
| 920 | ||
| 921 | ||
| 79161 | 922 | (* basic logic *) | 
| 923 | ||
| 80265 | 924 | fun zproof_axiom thy name A = zproof_const (ZAxiom name, zterm_of thy A); | 
| 925 | val axiom_proof = ZConstp ooo zproof_axiom; | |
| 79161 | 926 | |
| 79263 | 927 | fun oracle_proof thy name A = | 
| 80262 | 928 | ZConstp (zproof_const (ZOracle name, zterm_of thy A)); | 
| 79119 | 929 | |
| 930 | fun assume_proof thy A = | |
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 931 | ZHyp (zterm_of thy A); | 
| 79119 | 932 | |
| 933 | fun trivial_proof thy A = | |
| 79212 | 934 |   ZAbsp ("H", zterm_of thy A, ZBoundp 0);
 | 
| 79119 | 935 | |
| 79414 | 936 | fun implies_intr_proof' h prf = | 
| 79119 | 937 | let | 
| 79277 | 938 | fun proof lev (ZHyp t) = if aconv_zterm (h, t) then ZBoundp lev else raise Same.SAME | 
| 939 | | proof lev (ZAbst (x, T, p)) = ZAbst (x, T, proof lev p) | |
| 940 | | proof lev (ZAbsp (x, t, p)) = ZAbsp (x, t, proof (lev + 1) p) | |
| 941 | | proof lev (ZAppt (p, t)) = ZAppt (proof lev p, t) | |
| 942 | | proof lev (ZAppp (p, q)) = | |
| 79326 | 943 | (ZAppp (proof lev p, Same.commit (proof lev) q) | 
| 944 | handle Same.SAME => ZAppp (p, proof lev q)) | |
| 79157 | 945 | | proof _ _ = raise Same.SAME; | 
| 79212 | 946 |   in ZAbsp ("H", h, Same.commit (proof 0) prf) end;
 | 
| 79119 | 947 | |
| 79414 | 948 | fun implies_intr_proof thy = implies_intr_proof' o zterm_of thy; | 
| 949 | ||
| 79124 | 950 | fun forall_intr_proof thy T (a, x) prf = | 
| 79119 | 951 | let | 
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 952 |     val {ztyp, zterm} = zterm_cache thy;
 | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 953 | val Z = ztyp T; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 954 | val z = zterm x; | 
| 79119 | 955 | |
| 79157 | 956 | fun term i b = | 
| 79119 | 957 | if aconv_zterm (b, z) then ZBound i | 
| 958 | else | |
| 959 | (case b of | |
| 79157 | 960 | ZAbs (x, T, t) => ZAbs (x, T, term (i + 1) t) | 
| 79156 | 961 | | ZApp (t, u) => | 
| 79326 | 962 | (ZApp (term i t, Same.commit (term i) u) | 
| 963 | handle Same.SAME => ZApp (t, term i u)) | |
| 79156 | 964 | | _ => raise Same.SAME); | 
| 79119 | 965 | |
| 79157 | 966 | fun proof i (ZAbst (x, T, prf)) = ZAbst (x, T, proof (i + 1) prf) | 
| 79212 | 967 | | proof i (ZAbsp (x, t, prf)) = | 
| 79326 | 968 | (ZAbsp (x, term i t, Same.commit (proof i) prf) | 
| 969 | handle Same.SAME => ZAbsp (x, t, proof i prf)) | |
| 79157 | 970 | | proof i (ZAppt (p, t)) = | 
| 79326 | 971 | (ZAppt (proof i p, Same.commit (term i) t) | 
| 972 | handle Same.SAME => ZAppt (p, term i t)) | |
| 79212 | 973 | | proof i (ZAppp (p, q)) = | 
| 79326 | 974 | (ZAppp (proof i p, Same.commit (proof i) q) | 
| 975 | handle Same.SAME => ZAppp (p, proof i q)) | |
| 79157 | 976 | | proof _ _ = raise Same.SAME; | 
| 79119 | 977 | |
| 79157 | 978 | in ZAbst (a, Z, Same.commit (proof 0) prf) end; | 
| 79119 | 979 | |
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 980 | fun forall_elim_proof thy t p = ZAppt (p, zterm_of thy t); | 
| 79119 | 981 | |
| 80293 | 982 | fun of_class_proof (T, c) = OFCLASSp (ztyp_of T, c); | 
| 79128 | 983 | |
| 79124 | 984 | |
| 985 | (* equality *) | |
| 986 | ||
| 987 | local | |
| 988 | ||
| 989 | val thy0 = | |
| 990 | Context.the_global_context () | |
| 991 | |> Sign.add_types_global [(Binding.name "fun", 2, NoSyn), (Binding.name "prop", 0, NoSyn)] | |
| 992 | |> Sign.local_path | |
| 993 | |> Sign.add_consts | |
| 994 | [(Binding.name "all", (Term.aT [] --> propT) --> propT, NoSyn), | |
| 995 | (Binding.name "imp", propT --> propT --> propT, NoSyn), | |
| 996 | (Binding.name "eq", Term.aT [] --> Term.aT [] --> propT, NoSyn)]; | |
| 997 | ||
| 998 | val [reflexive_axiom, symmetric_axiom, transitive_axiom, equal_intr_axiom, equal_elim_axiom, | |
| 999 | abstract_rule_axiom, combination_axiom] = | |
| 80265 | 1000 | Theory.equality_axioms |> map (fn (b, t) => zproof_axiom thy0 (Sign.full_name thy0 b) t); | 
| 79124 | 1001 | |
| 1002 | in | |
| 1003 | ||
| 1004 | val is_reflexive_proof = | |
| 80262 | 1005 | fn ZConstp ((ZAxiom "Pure.reflexive", _), _) => true | _ => false; | 
| 79124 | 1006 | |
| 1007 | fun reflexive_proof thy T t = | |
| 1008 | let | |
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1009 |     val {ztyp, zterm} = zterm_cache thy;
 | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1010 | val A = ztyp T; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1011 | val x = zterm t; | 
| 79263 | 1012 | in make_const_proof (fn "'a" => A, fn "x" => x) reflexive_axiom end; | 
| 79124 | 1013 | |
| 1014 | fun symmetric_proof thy T t u prf = | |
| 1015 | if is_reflexive_proof prf then prf | |
| 1016 | else | |
| 1017 | let | |
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1018 |       val {ztyp, zterm} = zterm_cache thy;
 | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1019 | val A = ztyp T; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1020 | val x = zterm t; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1021 | val y = zterm u; | 
| 79263 | 1022 | val ax = make_const_proof (fn "'a" => A, fn "x" => x | "y" => y) symmetric_axiom; | 
| 79212 | 1023 | in ZAppp (ax, prf) end; | 
| 79124 | 1024 | |
| 1025 | fun transitive_proof thy T t u v prf1 prf2 = | |
| 1026 | if is_reflexive_proof prf1 then prf2 | |
| 1027 | else if is_reflexive_proof prf2 then prf1 | |
| 1028 | else | |
| 1029 | let | |
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1030 |       val {ztyp, zterm} = zterm_cache thy;
 | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1031 | val A = ztyp T; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1032 | val x = zterm t; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1033 | val y = zterm u; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1034 | val z = zterm v; | 
| 79263 | 1035 | val ax = make_const_proof (fn "'a" => A, fn "x" => x | "y" => y | "z" => z) transitive_axiom; | 
| 79212 | 1036 | in ZAppp (ZAppp (ax, prf1), prf2) end; | 
| 79124 | 1037 | |
| 1038 | fun equal_intr_proof thy t u prf1 prf2 = | |
| 1039 | let | |
| 79160 | 1040 |     val {zterm, ...} = zterm_cache thy;
 | 
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1041 | val A = zterm t; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1042 | val B = zterm u; | 
| 79263 | 1043 | val ax = make_const_proof (undefined, fn "A" => A | "B" => B) equal_intr_axiom; | 
| 79212 | 1044 | in ZAppp (ZAppp (ax, prf1), prf2) end; | 
| 79124 | 1045 | |
| 1046 | fun equal_elim_proof thy t u prf1 prf2 = | |
| 1047 | let | |
| 79160 | 1048 |     val {zterm, ...} = zterm_cache thy;
 | 
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1049 | val A = zterm t; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1050 | val B = zterm u; | 
| 79263 | 1051 | val ax = make_const_proof (undefined, fn "A" => A | "B" => B) equal_elim_axiom; | 
| 79212 | 1052 | in ZAppp (ZAppp (ax, prf1), prf2) end; | 
| 79124 | 1053 | |
| 1054 | fun abstract_rule_proof thy T U x t u prf = | |
| 1055 | let | |
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1056 |     val {ztyp, zterm} = zterm_cache thy;
 | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1057 | val A = ztyp T; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1058 | val B = ztyp U; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1059 | val f = zterm t; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1060 | val g = zterm u; | 
| 79126 | 1061 | val ax = | 
| 79263 | 1062 | make_const_proof (fn "'a" => A | "'b" => B, fn "f" => f | "g" => g) | 
| 79126 | 1063 | abstract_rule_axiom; | 
| 79212 | 1064 | in ZAppp (ax, forall_intr_proof thy T x prf) end; | 
| 79124 | 1065 | |
| 1066 | fun combination_proof thy T U f g t u prf1 prf2 = | |
| 1067 | let | |
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1068 |     val {ztyp, zterm} = zterm_cache thy;
 | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1069 | val A = ztyp T; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1070 | val B = ztyp U; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1071 | val f' = zterm f; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1072 | val g' = zterm g; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1073 | val x = zterm t; | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1074 | val y = zterm u; | 
| 79124 | 1075 | val ax = | 
| 79263 | 1076 | make_const_proof (fn "'a" => A | "'b" => B, fn "f" => f' | "g" => g' | "x" => x | "y" => y) | 
| 79124 | 1077 | combination_axiom; | 
| 79212 | 1078 | in ZAppp (ZAppp (ax, prf1), prf2) end; | 
| 79124 | 1079 | |
| 79098 
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
 wenzelm parents: diff
changeset | 1080 | end; | 
| 79124 | 1081 | |
| 79133 | 1082 | |
| 1083 | (* substitution *) | |
| 1084 | ||
| 1085 | fun generalize_proof (tfrees, frees) idx prf = | |
| 1086 | let | |
| 1087 | val typ = | |
| 79326 | 1088 | if Names.is_empty tfrees then Same.same | 
| 1089 | else | |
| 79163 | 1090 | ZTypes.unsynchronized_cache | 
| 1091 | (subst_type_same (fn ((a, i), S) => | |
| 1092 | if i = ~1 andalso Names.defined tfrees a then ZTVar ((a, idx), S) | |
| 1093 | else raise Same.SAME)); | |
| 79136 | 1094 | val term = | 
| 79147 | 1095 | subst_term_same typ (fn ((x, i), T) => | 
| 1096 | if i = ~1 andalso Names.defined frees x then ZVar ((x, idx), T) | |
| 1097 | else raise Same.SAME); | |
| 79418 | 1098 |   in map_proof {hyps = false} typ term prf end;
 | 
| 79133 | 1099 | |
| 79149 | 1100 | fun instantiate_proof thy (Ts, ts) prf = | 
| 1101 | let | |
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1102 |     val {ztyp, zterm} = zterm_cache thy;
 | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1103 | val instT = ZTVars.build (Ts |> fold (fn (v, T) => ZTVars.add (v, ztyp T))); | 
| 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1104 | val inst = ZVars.build (ts |> fold (fn ((v, T), t) => ZVars.add ((v, ztyp T), zterm t))); | 
| 79270 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 1105 | val typ = instantiate_type_same instT; | 
| 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 1106 | val term = instantiate_term_same typ inst; | 
| 79418 | 1107 |   in map_proof {hyps = false} typ term prf end;
 | 
| 79149 | 1108 | |
| 79135 | 1109 | fun varifyT_proof names prf = | 
| 1110 | if null names then prf | |
| 1111 | else | |
| 1112 | let | |
| 1113 | val tab = ZTVars.build (names |> fold (fn ((a, S), b) => ZTVars.add (((a, ~1), S), b))); | |
| 79136 | 1114 | val typ = | 
| 79163 | 1115 | ZTypes.unsynchronized_cache (subst_type_same (fn v => | 
| 79136 | 1116 | (case ZTVars.lookup tab v of | 
| 1117 | NONE => raise Same.SAME | |
| 79163 | 1118 | | SOME w => ZTVar w))); | 
| 79418 | 1119 |     in map_proof_types {hyps = false} typ prf end;
 | 
| 79135 | 1120 | |
| 79152 | 1121 | fun legacy_freezeT_proof t prf = | 
| 1122 | (case Type.legacy_freezeT t of | |
| 1123 | NONE => prf | |
| 1124 | | SOME f => | |
| 1125 | let | |
| 1126 | val tvar = ztyp_of o Same.function f; | |
| 79163 | 1127 | val typ = ZTypes.unsynchronized_cache (subst_type_same tvar); | 
| 79418 | 1128 |       in map_proof_types {hyps = false} typ prf end);
 | 
| 79152 | 1129 | |
| 79155 | 1130 | |
| 1131 | (* permutations *) | |
| 1132 | ||
| 1133 | fun rotate_proof thy Bs Bi' params asms m prf = | |
| 1134 | let | |
| 79158 
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
 wenzelm parents: 
79157diff
changeset | 1135 |     val {ztyp, zterm} = zterm_cache thy;
 | 
| 79155 | 1136 | val i = length asms; | 
| 1137 | val j = length Bs; | |
| 1138 | in | |
| 79212 | 1139 | ZAbsps (map zterm Bs @ [zterm Bi']) (ZAppps (prf, map ZBoundp | 
| 1140 | (j downto 1) @ [ZAbsts (map (apsnd ztyp) params) (ZAbsps (map zterm asms) | |
| 1141 | (ZAppps (ZAppts (ZBoundp i, map ZBound ((length params - 1) downto 0)), | |
| 79326 | 1142 | map ZBoundp (((i - m - 1) downto 0) @ ((i - 1) downto (i - m))))))])) | 
| 79155 | 1143 | end; | 
| 1144 | ||
| 1145 | fun permute_prems_proof thy prems' j k prf = | |
| 1146 | let | |
| 79160 | 1147 |     val {zterm, ...} = zterm_cache thy;
 | 
| 79155 | 1148 | val n = length prems'; | 
| 1149 | in | |
| 79212 | 1150 | ZAbsps (map zterm prems') | 
| 79326 | 1151 | (ZAppps (prf, map ZBoundp ((n - 1 downto n - j) @ (k - 1 downto 0) @ (n - j - 1 downto k)))) | 
| 79155 | 1152 | end; | 
| 1153 | ||
| 79211 | 1154 | |
| 79234 | 1155 | (* lifting *) | 
| 1156 | ||
| 1157 | fun incr_tvar_same inc = | |
| 1158 | if inc = 0 then Same.same | |
| 1159 | else | |
| 1160 | let fun tvar ((a, i), S) = if i >= 0 then ZTVar ((a, i + inc), S) else raise Same.SAME | |
| 1161 | in ZTypes.unsynchronized_cache (subst_type_same tvar) end; | |
| 1162 | ||
| 1163 | fun incr_indexes_proof inc prf = | |
| 1164 | let | |
| 1165 | val typ = incr_tvar_same inc; | |
| 1166 | fun var ((x, i), T) = if i >= 0 then ZVar ((x, i + inc), T) else raise Same.SAME; | |
| 1167 | val term = subst_term_same typ var; | |
| 79418 | 1168 |   in map_proof {hyps = false} typ term prf end;
 | 
| 79234 | 1169 | |
| 1170 | fun lift_proof thy gprop inc prems prf = | |
| 1171 | let | |
| 1172 |     val {ztyp, zterm} = zterm_cache thy;
 | |
| 1173 | ||
| 1174 | val typ = incr_tvar_same inc; | |
| 1175 | ||
| 1176 | fun term Ts lev = | |
| 1177 | if null Ts andalso inc = 0 then Same.same | |
| 1178 | else | |
| 1179 | let | |
| 1180 | val n = length Ts; | |
| 1181 | fun incr lev (ZVar ((x, i), T)) = | |
| 1182 | if i >= 0 then combound (ZVar ((x, i + inc), ZFuns (Ts, Same.commit typ T)), lev, n) | |
| 1183 | else raise Same.SAME | |
| 1184 | | incr _ (ZBound _) = raise Same.SAME | |
| 1185 | | incr _ (ZConst0 _) = raise Same.SAME | |
| 1186 | | incr _ (ZConst1 (c, T)) = ZConst1 (c, typ T) | |
| 1187 | | incr _ (ZConst (c, Ts)) = ZConst (c, Same.map typ Ts) | |
| 1188 | | incr lev (ZAbs (x, T, t)) = | |
| 1189 | (ZAbs (x, typ T, Same.commit (incr (lev + 1)) t) | |
| 1190 | handle Same.SAME => ZAbs (x, T, incr (lev + 1) t)) | |
| 1191 | | incr lev (ZApp (t, u)) = | |
| 1192 | (ZApp (incr lev t, Same.commit (incr lev) u) | |
| 1193 | handle Same.SAME => ZApp (t, incr lev u)) | |
| 80293 | 1194 | | incr _ (OFCLASS (T, c)) = OFCLASS (typ T, c); | 
| 79234 | 1195 | in incr lev end; | 
| 1196 | ||
| 1197 | fun proof Ts lev (ZAbst (a, T, p)) = | |
| 1198 | (ZAbst (a, typ T, Same.commit (proof Ts (lev + 1)) p) | |
| 1199 | handle Same.SAME => ZAbst (a, T, proof Ts (lev + 1) p)) | |
| 1200 | | proof Ts lev (ZAbsp (a, t, p)) = | |
| 1201 | (ZAbsp (a, term Ts lev t, Same.commit (proof Ts lev) p) | |
| 1202 | handle Same.SAME => ZAbsp (a, t, proof Ts lev p)) | |
| 1203 | | proof Ts lev (ZAppt (p, t)) = | |
| 1204 | (ZAppt (proof Ts lev p, Same.commit (term Ts lev) t) | |
| 1205 | handle Same.SAME => ZAppt (p, term Ts lev t)) | |
| 1206 | | proof Ts lev (ZAppp (p, q)) = | |
| 1207 | (ZAppp (proof Ts lev p, Same.commit (proof Ts lev) q) | |
| 1208 | handle Same.SAME => ZAppp (p, proof Ts lev q)) | |
| 80262 | 1209 | | proof Ts lev (ZConstp (a, insts)) = | 
| 1210 | ZConstp (a, map_insts_same typ (term Ts lev) insts) | |
| 80293 | 1211 | | proof _ _ (OFCLASSp (T, c)) = OFCLASSp (typ T, c) | 
| 79234 | 1212 | | proof _ _ _ = raise Same.SAME; | 
| 1213 | ||
| 1214 | val k = length prems; | |
| 1215 | ||
| 1216 | fun mk_app b (i, j, p) = | |
| 1217 | if b then (i - 1, j, ZAppp (p, ZBoundp i)) else (i, j - 1, ZAppt (p, ZBound j)); | |
| 1218 | ||
| 1219 |     fun lift Ts bs i j (Const ("Pure.imp", _) $ A $ B) =
 | |
| 1220 |           ZAbsp ("H", Same.commit (term Ts 0) (zterm A), lift Ts (true :: bs) (i + 1) j B)
 | |
| 1221 |       | lift Ts bs i j (Const ("Pure.all", _) $ Abs (a, T, t)) =
 | |
| 1222 | let val T' = ztyp T | |
| 1223 | in ZAbst (a, T', lift (T' :: Ts) (false :: bs) i (j + 1) t) end | |
| 1224 | | lift Ts bs i j _ = | |
| 1225 | ZAppps (Same.commit (proof (rev Ts) 0) prf, | |
| 1226 | map (fn k => (#3 (fold_rev mk_app bs (i - 1, j - 1, ZBoundp k)))) (i + k - 1 downto i)); | |
| 79237 | 1227 | in ZAbsps (map zterm prems) (lift [] [] 0 0 gprop) end; | 
| 79234 | 1228 | |
| 1229 | ||
| 79211 | 1230 | (* higher-order resolution *) | 
| 1231 | ||
| 1232 | fun mk_asm_prf C i m = | |
| 1233 | let | |
| 79212 | 1234 | fun imp _ i 0 = ZBoundp i | 
| 1235 |       | imp (ZApp (ZApp (ZConst0 "Pure.imp", A), B)) i m = ZAbsp ("H", A, imp B (i + 1) (m - 1))
 | |
| 1236 | | imp _ i _ = ZBoundp i; | |
| 79211 | 1237 |     fun all (ZApp (ZConst1 ("Pure.all", _), ZAbs (a, T, t))) = ZAbst (a, T, all t)
 | 
| 1238 | | all t = imp t (~ i) m | |
| 1239 | in all C end; | |
| 1240 | ||
| 79270 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 1241 | fun assumption_proof thy envir Bs Bi n visible prf = | 
| 79211 | 1242 | let | 
| 1243 |     val cache as {zterm, ...} = norm_cache thy;
 | |
| 1244 | val normt = zterm #> Same.commit (norm_term_same cache envir); | |
| 1245 | in | |
| 79212 | 1246 | ZAbsps (map normt Bs) | 
| 1247 | (ZAppps (prf, map ZBoundp (length Bs - 1 downto 0) @ [mk_asm_prf (normt Bi) n ~1])) | |
| 79270 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 1248 | |> norm_proof_cache cache envir visible | 
| 79211 | 1249 | end; | 
| 1250 | ||
| 79261 | 1251 | fun flatten_params_proof i j n (ZApp (ZApp (ZConst0 "Pure.imp", A), B), k) = | 
| 1252 |       ZAbsp ("H", A, flatten_params_proof (i + 1) j n (B, k))
 | |
| 1253 |   | flatten_params_proof i j n (ZApp (ZConst1 ("Pure.all", _), ZAbs (a, T, t)), k) =
 | |
| 1254 | ZAbst (a, T, flatten_params_proof i (j + 1) n (t, k)) | |
| 1255 | | flatten_params_proof i j n (_, k) = | |
| 1256 | ZAppps (ZAppts (ZBoundp (k + i), | |
| 1257 | map ZBound (j - 1 downto 0)), map ZBoundp (remove (op =) (i - n) (i - 1 downto 0))); | |
| 1258 | ||
| 79270 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 1259 | fun bicompose_proof thy env smax flatten Bs As A oldAs n m visible rprf sprf = | 
| 79261 | 1260 | let | 
| 1261 |     val cache as {zterm, ...} = norm_cache thy;
 | |
| 1262 | val normt = zterm #> Same.commit (norm_term_same cache env); | |
| 79270 
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
 wenzelm parents: 
79269diff
changeset | 1263 | val normp = norm_proof_cache cache env visible; | 
| 79261 | 1264 | |
| 1265 | val lb = length Bs; | |
| 1266 | val la = length As; | |
| 1267 | ||
| 1268 | fun proof p q = | |
| 1269 | ZAbsps (map normt (Bs @ As)) | |
| 1270 | (ZAppp (ZAppps (q, map ZBoundp (lb + la - 1 downto la)), | |
| 1271 | ZAppps (p, (if n > 0 then [mk_asm_prf (normt (the A)) n m] else []) @ | |
| 1272 | map (if flatten then flatten_params_proof 0 0 n else ZBoundp o snd) | |
| 1273 | (map normt oldAs ~~ (la - 1 downto 0))))); | |
| 1274 | in | |
| 1275 | if Envir.is_empty env then proof rprf sprf | |
| 1276 | else proof (normp rprf) (if Envir.above env smax then sprf else normp sprf) | |
| 1277 | end; | |
| 1278 | ||
| 79405 | 1279 | |
| 1280 | (* sort constraints within the logic *) | |
| 1281 | ||
| 79425 | 1282 | type sorts_proof = (class * class -> zproof) * (string * class list list * class -> zproof); | 
| 79405 | 1283 | |
| 1284 | fun of_sort_proof algebra (classrel_proof, arity_proof) hyps = | |
| 1285 | Sorts.of_sort_derivation algebra | |
| 1286 |    {class_relation = fn _ => fn _ => fn (prf, c1) => fn c2 =>
 | |
| 1287 | if c1 = c2 then prf else ZAppp (classrel_proof (c1, c2), prf), | |
| 1288 | type_constructor = fn (a, _) => fn dom => fn c => | |
| 1289 | let val Ss = map (map snd) dom and prfs = maps (map fst) dom | |
| 1290 | in ZAppps (arity_proof (a, Ss, c), prfs) end, | |
| 1291 | type_variable = fn typ => map (fn c => (hyps (typ, c), c)) (Type.sort_of_atyp typ)}; | |
| 1292 | ||
| 79414 | 1293 | fun unconstrainT_proof thy sorts_proof (ucontext: Logic.unconstrain_context) = | 
| 1294 | let | |
| 1295 | val cache = norm_cache thy; | |
| 1296 | val algebra = Sign.classes_of thy; | |
| 1297 | ||
| 1298 | val typ = | |
| 1299 | ZTypes.unsynchronized_cache | |
| 1300 |         (typ_of #> #typ_operation ucontext {strip_sorts = true} #> ztyp_of);
 | |
| 1301 | ||
| 1302 |     val typ_sort = #typ_operation ucontext {strip_sorts = false};
 | |
| 1303 | ||
| 1304 | fun hyps T = | |
| 1305 | (case AList.lookup (op =) (#constraints ucontext) T of | |
| 1306 | SOME t => ZHyp (#zterm cache t) | |
| 1307 | | NONE => raise Fail "unconstrainT_proof: missing constraint"); | |
| 1308 | ||
| 1309 | fun class (T, c) = | |
| 1310 | let val T' = Same.commit typ_sort (#typ cache T) | |
| 1311 | in the_single (of_sort_proof algebra sorts_proof hyps (T', [c])) end; | |
| 1312 | in | |
| 79418 | 1313 |     map_proof_types {hyps = false} typ #> map_class_proof class #> beta_norm_prooft
 | 
| 79414 | 1314 | #> fold_rev (implies_intr_proof' o #zterm cache o #2) (#constraints ucontext) | 
| 1315 | end; | |
| 1316 | ||
| 79124 | 1317 | end; |