99 val subst_bounds: term list * term -> term |
99 val subst_bounds: term list * term -> term |
100 val subst_bound: term * term -> term |
100 val subst_bound: term * term -> term |
101 val betapply: term * term -> term |
101 val betapply: term * term -> term |
102 val betapplys: term * term list -> term |
102 val betapplys: term * term list -> term |
103 val eq_ix: indexname * indexname -> bool |
103 val eq_ix: indexname * indexname -> bool |
104 val ins_ix: indexname * indexname list -> indexname list |
|
105 val mem_ix: indexname * indexname list -> bool |
|
106 val mem_term: term * term list -> bool |
|
107 val ins_term: term * term list -> term list |
|
108 val could_unify: term * term -> bool |
104 val could_unify: term * term -> bool |
109 val subst_free: (term * term) list -> term -> term |
105 val subst_free: (term * term) list -> term -> term |
110 val xless: (string * int) * indexname -> bool |
106 val xless: (string * int) * indexname -> bool |
111 val abstract_over: term * term -> term |
107 val abstract_over: term * term -> term |
112 val lambda: term -> term -> term |
108 val lambda: term -> term -> term |
124 val subst_vars: (indexname * typ) list * (indexname * term) list -> term -> term |
120 val subst_vars: (indexname * typ) list * (indexname * term) list -> term -> term |
125 val is_first_order: string list -> term -> bool |
121 val is_first_order: string list -> term -> bool |
126 val maxidx_of_typ: typ -> int |
122 val maxidx_of_typ: typ -> int |
127 val maxidx_of_typs: typ list -> int |
123 val maxidx_of_typs: typ list -> int |
128 val maxidx_of_term: term -> int |
124 val maxidx_of_term: term -> int |
129 val variant: string list -> string -> string |
|
130 val variantlist: string list * string list -> string list |
|
131 (*note reversed order of args wrt. variant!*) |
|
132 val add_term_consts: term * string list -> string list |
125 val add_term_consts: term * string list -> string list |
133 val term_consts: term -> string list |
126 val term_consts: term -> string list |
134 val exists_subtype: (typ -> bool) -> typ -> bool |
127 val exists_subtype: (typ -> bool) -> typ -> bool |
135 val exists_subterm: (term -> bool) -> term -> bool |
128 val exists_subterm: (term -> bool) -> term -> bool |
136 val exists_Const: (string * typ -> bool) -> term -> bool |
129 val exists_Const: (string * typ -> bool) -> term -> bool |
184 val rename_abs: term -> term -> term -> term option |
177 val rename_abs: term -> term -> term -> term option |
185 val eq_tvar: (indexname * sort) * (indexname * sort) -> bool |
178 val eq_tvar: (indexname * sort) * (indexname * sort) -> bool |
186 val eq_var: (indexname * typ) * (indexname * typ) -> bool |
179 val eq_var: (indexname * typ) * (indexname * typ) -> bool |
187 val tvar_ord: (indexname * sort) * (indexname * sort) -> order |
180 val tvar_ord: (indexname * sort) * (indexname * sort) -> order |
188 val var_ord: (indexname * typ) * (indexname * typ) -> order |
181 val var_ord: (indexname * typ) * (indexname * typ) -> order |
189 val internal: string -> string |
|
190 val dest_internal: string -> string |
|
191 val skolem: string -> string |
|
192 val dest_skolem: string -> string |
|
193 val generalize: string list * string list -> int -> term -> term |
182 val generalize: string list * string list -> int -> term -> term |
194 val generalizeT: string list -> int -> typ -> typ |
183 val generalizeT: string list -> int -> typ -> typ |
195 val generalize_option: string list * string list -> int -> term -> term option |
184 val generalize_option: string list * string list -> int -> term -> term option |
196 val generalizeT_option: string list -> int -> typ -> typ option |
185 val generalizeT_option: string list -> int -> typ -> typ option |
197 val instantiate: ((indexname * sort) * typ) list * ((indexname * typ) * term) list |
186 val instantiate: ((indexname * sort) * typ) list * ((indexname * typ) * term) list |
201 -> term -> term option |
190 -> term -> term option |
202 val instantiateT_option: ((indexname * sort) * typ) list -> typ -> typ option |
191 val instantiateT_option: ((indexname * sort) * typ) list -> typ -> typ option |
203 val maxidx_typ: typ -> int -> int |
192 val maxidx_typ: typ -> int -> int |
204 val maxidx_typs: typ list -> int -> int |
193 val maxidx_typs: typ list -> int -> int |
205 val maxidx_term: term -> int -> int |
194 val maxidx_term: term -> int -> int |
206 val variant_name: (string -> bool) -> string -> string |
|
207 val invent_names: string list -> string -> int -> string list |
|
208 val dest_abs: string * typ * term -> string * term |
195 val dest_abs: string * typ * term -> string * term |
209 val bound: int -> string |
|
210 val is_bound: string -> bool |
|
211 val zero_var_indexesT: typ -> typ |
196 val zero_var_indexesT: typ -> typ |
212 val zero_var_indexes: term -> term |
197 val zero_var_indexes: term -> term |
213 val zero_var_indexes_inst: term -> |
198 val zero_var_indexes_inst: term -> |
214 ((indexname * sort) * typ) list * ((indexname * typ) * term) list |
199 ((indexname * sort) * typ) list * ((indexname * typ) * term) list |
215 val dummy_patternN: string |
200 val dummy_patternN: string |
828 val betapplys = Library.foldl betapply; |
813 val betapplys = Library.foldl betapply; |
829 |
814 |
830 |
815 |
831 (** Specialized equality, membership, insertion etc. **) |
816 (** Specialized equality, membership, insertion etc. **) |
832 |
817 |
833 (* indexnames *) |
818 (* variables *) |
834 |
819 |
835 fun eq_ix ((x, i): indexname, (y, j)) = i = j andalso x = y; |
820 fun eq_ix ((x, i): indexname, (y, j)) = i = j andalso x = y; |
836 |
|
837 fun mem_ix (_, []) = false |
|
838 | mem_ix (x, y :: ys) = eq_ix (x, y) orelse mem_ix (x, ys); |
|
839 |
|
840 fun ins_ix (x, xs) = if mem_ix (x, xs) then xs else x :: xs; |
|
841 |
|
842 |
|
843 (* variables *) |
|
844 |
821 |
845 fun eq_tvar ((xi, S: sort), (xi', S')) = eq_ix (xi, xi') andalso S = S'; |
822 fun eq_tvar ((xi, S: sort), (xi', S')) = eq_ix (xi, xi') andalso S = S'; |
846 fun eq_var ((xi, T: typ), (xi', T')) = eq_ix (xi, xi') andalso T = T'; |
823 fun eq_var ((xi, T: typ), (xi', T')) = eq_ix (xi, xi') andalso T = T'; |
847 |
824 |
848 val tvar_ord = prod_ord indexname_ord sort_ord; |
825 val tvar_ord = prod_ord indexname_ord sort_ord; |
849 val var_ord = prod_ord indexname_ord typ_ord; |
826 val var_ord = prod_ord indexname_ord typ_ord; |
850 |
|
851 |
|
852 (* terms *) |
|
853 |
|
854 fun mem_term (_, []) = false |
|
855 | mem_term (t, t'::ts) = t aconv t' orelse mem_term (t, ts); |
|
856 |
|
857 fun ins_term(t,ts) = if mem_term(t,ts) then ts else t :: ts; |
|
858 |
827 |
859 |
828 |
860 (*A fast unification filter: true unless the two terms cannot be unified. |
829 (*A fast unification filter: true unless the two terms cannot be unified. |
861 Terms must be NORMAL. Treats all Vars as distinct. *) |
830 Terms must be NORMAL. Treats all Vars as distinct. *) |
862 fun could_unify (t,u) = |
831 fun could_unify (t,u) = |
1014 in gen_typ ty end; |
974 in gen_typ ty end; |
1015 |
975 |
1016 fun generalize_same ([], []) _ _ = raise SAME |
976 fun generalize_same ([], []) _ _ = raise SAME |
1017 | generalize_same (tfrees, frees) idx tm = |
977 | generalize_same (tfrees, frees) idx tm = |
1018 let |
978 let |
1019 fun var ((x, i), T) = |
|
1020 (case try dest_internal x of |
|
1021 NONE => Var ((x, i), T) |
|
1022 | SOME x' => var ((x', i + 1), T)); |
|
1023 |
|
1024 val genT = generalizeT_same tfrees idx; |
979 val genT = generalizeT_same tfrees idx; |
1025 fun gen (Free (x, T)) = |
980 fun gen (Free (x, T)) = |
1026 if member (op =) frees x then var ((x, idx), genT T handle SAME => T) |
981 if member (op =) frees x then |
|
982 Var (Name.clean_index (x, idx), genT T handle SAME => T) |
1027 else Free (x, genT T) |
983 else Free (x, genT T) |
1028 | gen (Var (xi, T)) = Var (xi, genT T) |
984 | gen (Var (xi, T)) = Var (xi, genT T) |
1029 | gen (Const (c, T)) = Const (c, genT T) |
985 | gen (Const (c, T)) = Const (c, genT T) |
1030 | gen (Bound _) = raise SAME |
986 | gen (Bound _) = raise SAME |
1031 | gen (Abs (x, T, t)) = |
987 | gen (Abs (x, T, t)) = |
1135 |
1091 |
1136 |
1092 |
1137 |
1093 |
1138 (**** Syntax-related declarations ****) |
1094 (**** Syntax-related declarations ****) |
1139 |
1095 |
1140 (*** Printing ***) |
|
1141 |
|
1142 (*Makes a variant of a name distinct from already used names. First |
|
1143 attaches the suffix and then increments this; preserves a suffix of |
|
1144 underscores "_". *) |
|
1145 fun variant_name used name = |
|
1146 let |
|
1147 val (c, u) = pairself implode (Library.take_suffix (fn s => s = "_") (Symbol.explode name)); |
|
1148 fun vary2 c = if used (c ^ u) then vary2 (Symbol.bump_string c) else c; |
|
1149 fun vary1 c = if used (c ^ u) then vary2 (Symbol.bump_init c) else c; |
|
1150 in vary1 (if c = "" then "u" else c) ^ u end; |
|
1151 |
|
1152 fun variant used_names = variant_name (member (op =) used_names); |
|
1153 |
|
1154 (*Create variants of the list of names, with priority to the first ones*) |
|
1155 fun variantlist ([], used) = [] |
|
1156 | variantlist(b::bs, used) = |
|
1157 let val b' = variant used b |
|
1158 in b' :: variantlist (bs, b'::used) end; |
|
1159 |
|
1160 (*Invent fresh names*) |
|
1161 fun invent_names _ _ 0 = [] |
|
1162 | invent_names used a n = |
|
1163 let val b = Symbol.bump_string a in |
|
1164 if a mem_string used then invent_names used b n |
|
1165 else a :: invent_names used b (n - 1) |
|
1166 end; |
|
1167 |
|
1168 |
|
1169 (* substructure *) |
1096 (* substructure *) |
1170 |
1097 |
1171 fun exists_subtype P = |
1098 fun exists_subtype P = |
1172 let |
1099 let |
1173 fun ex ty = P ty orelse |
1100 fun ex ty = P ty orelse |
1246 fun term_tvars t = add_term_tvars(t,[]); |
1173 fun term_tvars t = add_term_tvars(t,[]); |
1247 |
1174 |
1248 (*special code to enforce left-to-right collection of TVar-indexnames*) |
1175 (*special code to enforce left-to-right collection of TVar-indexnames*) |
1249 |
1176 |
1250 fun add_typ_ixns(ixns,Type(_,Ts)) = Library.foldl add_typ_ixns (ixns,Ts) |
1177 fun add_typ_ixns(ixns,Type(_,Ts)) = Library.foldl add_typ_ixns (ixns,Ts) |
1251 | add_typ_ixns(ixns,TVar(ixn,_)) = if mem_ix (ixn, ixns) then ixns |
1178 | add_typ_ixns(ixns,TVar(ixn,_)) = if member (op =) ixns ixn then ixns |
1252 else ixns@[ixn] |
1179 else ixns@[ixn] |
1253 | add_typ_ixns(ixns,TFree(_)) = ixns; |
1180 | add_typ_ixns(ixns,TFree(_)) = ixns; |
1254 |
1181 |
1255 fun add_term_tvar_ixns(Const(_,T),ixns) = add_typ_ixns(ixns,T) |
1182 fun add_term_tvar_ixns(Const(_,T),ixns) = add_typ_ixns(ixns,T) |
1256 | add_term_tvar_ixns(Free(_,T),ixns) = add_typ_ixns(ixns,T) |
1183 | add_term_tvar_ixns(Free(_,T),ixns) = add_typ_ixns(ixns,T) |
1283 fun term_frees t = add_term_frees(t,[]); |
1210 fun term_frees t = add_term_frees(t,[]); |
1284 |
1211 |
1285 (*Given an abstraction over P, replaces the bound variable by a Free variable |
1212 (*Given an abstraction over P, replaces the bound variable by a Free variable |
1286 having a unique name -- SLOW!*) |
1213 having a unique name -- SLOW!*) |
1287 fun variant_abs (a,T,P) = |
1214 fun variant_abs (a,T,P) = |
1288 let val b = variant (add_term_names(P,[])) a |
1215 let val b = Name.variant (add_term_names(P,[])) a |
1289 in (b, subst_bound (Free(b,T), P)) end; |
1216 in (b, subst_bound (Free(b,T), P)) end; |
1290 |
1217 |
1291 fun dest_abs (x, T, body) = |
1218 fun dest_abs (x, T, body) = |
1292 let |
1219 let |
1293 fun name_clash (Free (y, _)) = (x = y) |
1220 fun name_clash (Free (y, _)) = (x = y) |
1294 | name_clash (t $ u) = name_clash t orelse name_clash u |
1221 | name_clash (t $ u) = name_clash t orelse name_clash u |
1295 | name_clash (Abs (_, _, t)) = name_clash t |
1222 | name_clash (Abs (_, _, t)) = name_clash t |
1296 | name_clash _ = false; |
1223 | name_clash _ = false; |
1297 in |
1224 in |
1298 if name_clash body then |
1225 if name_clash body then |
1299 dest_abs (variant [x] x, T, body) (*potentially slow, but rarely happens*) |
1226 dest_abs (Name.variant [x] x, T, body) (*potentially slow, but rarely happens*) |
1300 else (x, subst_bound (Free (x, T), body)) |
1227 else (x, subst_bound (Free (x, T), body)) |
1301 end; |
1228 end; |
1302 |
|
1303 (*names for numbered variables -- |
|
1304 preserves order wrt. int_ord vs. string_ord, avoids allocating new strings*) |
|
1305 local |
|
1306 val small_int = Vector.tabulate (1000, fn i => |
|
1307 let val leading = if i < 10 then "00" else if i < 100 then "0" else "" |
|
1308 in ":" ^ leading ^ string_of_int i end); |
|
1309 in |
|
1310 fun bound n = |
|
1311 if n < 1000 then Vector.sub (small_int, n) |
|
1312 else ":" ^ bound (n div 1000) ^ Vector.sub (small_int, n mod 1000); |
|
1313 end; |
|
1314 |
|
1315 val is_bound = String.isPrefix ":"; |
|
1316 |
1229 |
1317 (* renames and reverses the strings in vars away from names *) |
1230 (* renames and reverses the strings in vars away from names *) |
1318 fun rename_aTs names vars : (string*typ)list = |
1231 fun rename_aTs names vars : (string*typ)list = |
1319 let fun rename_aT (vars,(a,T)) = |
1232 let fun rename_aT (vars,(a,T)) = |
1320 (variant (map #1 vars @ names) a, T) :: vars |
1233 (Name.variant (map #1 vars @ names) a, T) :: vars |
1321 in Library.foldl rename_aT ([],vars) end; |
1234 in Library.foldl rename_aT ([],vars) end; |
1322 |
1235 |
1323 fun rename_wrt_term t = rename_aTs (add_term_names(t,[])); |
1236 fun rename_wrt_term t = rename_aTs (add_term_names(t,[])); |
1324 |
1237 |
1325 |
1238 |
1326 (* zero var indexes *) |
1239 (* zero var indexes *) |
1327 |
1240 |
1328 fun zero_var_inst vars = |
1241 fun zero_var_inst vars = |
1329 fold (fn v as ((x, i), X) => fn (used, inst) => |
1242 fold (fn v as ((x, i), X) => fn (used, inst) => |
1330 let |
1243 let |
1331 val x' = variant used (if is_bound x then "u" else x); |
1244 val x' = Name.variant used (if Name.is_bound x then "u" else x); |
1332 val used' = x' :: used; |
1245 val used' = x' :: used; |
1333 in if x = x' andalso i = 0 then (used', inst) else (used', (v, ((x', 0), X)) :: inst) end) |
1246 in if x = x' andalso i = 0 then (used', inst) else (used', (v, ((x', 0), X)) :: inst) end) |
1334 vars ([], []) |> #2; |
1247 vars ([], []) |> #2; |
1335 |
1248 |
1336 fun zero_var_indexesT ty = |
1249 fun zero_var_indexesT ty = |