39 val subsort: type_sig -> sort * sort -> bool |
39 val subsort: type_sig -> sort * sort -> bool |
40 val norm_sort: type_sig -> sort -> sort |
40 val norm_sort: type_sig -> sort -> sort |
41 val rem_sorts: typ -> typ |
41 val rem_sorts: typ -> typ |
42 val cert_typ: type_sig -> typ -> typ |
42 val cert_typ: type_sig -> typ -> typ |
43 val norm_typ: type_sig -> typ -> typ |
43 val norm_typ: type_sig -> typ -> typ |
44 val freeze: (indexname -> bool) -> term -> term |
44 val freeze: term -> term |
45 val freeze_vars: typ -> typ |
45 val freeze_vars: typ -> typ |
46 val infer_types: type_sig * (string -> typ option) * (indexname -> typ option) * |
46 val infer_types: type_sig * (string -> typ option) * |
47 (indexname -> sort option) * typ * term -> term * (indexname * typ) list |
47 (indexname -> typ option) * (indexname -> sort option) * |
|
48 string list * bool * typ * term |
|
49 -> term * (indexname * typ) list |
48 val inst_term_tvars: type_sig * (indexname * typ) list -> term -> term |
50 val inst_term_tvars: type_sig * (indexname * typ) list -> term -> term |
49 val thaw_vars: typ -> typ |
51 val thaw_vars: typ -> typ |
50 val typ_errors: type_sig -> typ * string list -> string list |
52 val typ_errors: type_sig -> typ * string list -> string list |
51 val typ_instance: type_sig * typ * typ -> bool |
53 val typ_instance: type_sig * typ * typ -> bool |
52 val typ_match: type_sig -> (indexname * typ) list * (typ * typ) |
54 val typ_match: type_sig -> (indexname * typ) list * (typ * typ) |
325 else raise TYPE("Type not of sort " ^ (str_of_sort S), [T], []) |
321 else raise TYPE("Type not of sort " ^ (str_of_sort S), [T], []) |
326 |
322 |
327 |
323 |
328 (*Instantiation of type variables in types *) |
324 (*Instantiation of type variables in types *) |
329 fun inst_typ_tvars(tsig, tye) = |
325 fun inst_typ_tvars(tsig, tye) = |
330 let fun inst(Type(a, Ts)) = Type(a, map inst Ts) |
326 let fun inst(var as (v, S)) = case assoc(tye, v) of |
331 | inst(T as TFree _) = T |
327 Some U => (check_has_sort(tsig, U, S); U) |
332 | inst(T as TVar(v, S)) = (case assoc(tye, v) of |
328 | None => TVar(var) |
333 None => T | Some(U) => (check_has_sort(tsig, U, S); U)) |
329 in map_type_tvar inst end; |
334 in inst end; |
|
335 |
330 |
336 (*Instantiation of type variables in terms *) |
331 (*Instantiation of type variables in terms *) |
337 fun inst_term_tvars(tsig, tye) = map_term_types (inst_typ_tvars(tsig, tye)); |
332 fun inst_term_tvars(tsig, tye) = map_term_types (inst_typ_tvars(tsig, tye)); |
338 |
333 |
339 |
334 |
1011 if a=Syntax.constrainC then unconstrain t |
1005 if a=Syntax.constrainC then unconstrain t |
1012 else unconstrain f $ unconstrain t |
1006 else unconstrain f $ unconstrain t |
1013 | unconstrain (f$t) = unconstrain f $ unconstrain t |
1007 | unconstrain (f$t) = unconstrain f $ unconstrain t |
1014 | unconstrain (t) = t; |
1008 | unconstrain (t) = t; |
1015 |
1009 |
1016 |
1010 fun nextname(pref,c) = if c="z" then (pref^"a", "a") else (pref,chr(ord(c)+1)); |
1017 (* Turn all TVars which satisfy p into new TFrees *) |
1011 |
1018 fun freeze p t = |
1012 fun newtvars used = |
1019 let val fs = add_term_tfree_names(t, []); |
1013 let fun new([],_,vmap) = vmap |
1020 val inxs = filter p (add_term_tvar_ixns(t, [])); |
1014 | new(ixn::ixns,p as (pref,c),vmap) = |
1021 val vmap = inxs ~~ variantlist(map #1 inxs, fs); |
1015 let val nm = pref ^ c |
1022 fun free(Type(a, Ts)) = Type(a, map free Ts) |
1016 in if nm mem used then new(ixn::ixns,nextname p, vmap) |
1023 | free(T as TVar(v, S)) = |
1017 else new(ixns, nextname p, (ixn,nm)::vmap) |
1024 (case assoc(vmap, v) of None => T | Some(a) => TFree(a, S)) |
1018 end |
1025 | free(T as TFree _) = T |
1019 in new end; |
1026 in map_term_types free t end; |
1020 |
|
1021 (* |
|
1022 Turn all TVars which satisfy p into new (if freeze then TFrees else TVars). |
|
1023 Note that if t contains frozen TVars there is the possibility that a TVar is |
|
1024 turned into one of those. This is sound but not complete. |
|
1025 *) |
|
1026 fun convert used freeze p t = |
|
1027 let val used = if freeze then add_term_tfree_names(t, used) |
|
1028 else used union |
|
1029 (map #1 (filter_out p (add_term_tvar_ixns(t, [])))) |
|
1030 val ixns = filter p (add_term_tvar_ixns(t, [])); |
|
1031 val vmap = newtvars used (ixns,("'","a"),[]); |
|
1032 fun conv(var as (ixn,S)) = case assoc(vmap,ixn) of |
|
1033 None => TVar(var) | |
|
1034 Some(a) => if freeze then TFree(a,S) else TVar((a,0),S); |
|
1035 in map_term_types (map_type_tvar conv) t end; |
|
1036 |
|
1037 fun freeze t = convert (add_term_tfree_names(t,[])) true (K true) t; |
1027 |
1038 |
1028 (* Thaw all TVars that were frozen in freeze_vars *) |
1039 (* Thaw all TVars that were frozen in freeze_vars *) |
1029 fun thaw_vars(Type(a, Ts)) = Type(a, map thaw_vars Ts) |
1040 val thaw_vars = |
1030 | thaw_vars(T as TFree(a, S)) = (case explode a of |
1041 let fun thaw(f as (a, S)) = (case explode a of |
1031 "?"::"'"::vn => let val ((b, i), _) = Syntax.scan_varname vn |
1042 "?"::"'"::vn => let val ((b, i), _) = Syntax.scan_varname vn |
1032 in TVar(("'"^b, i), S) end |
1043 in TVar(("'"^b, i), S) end |
1033 | _ => T) |
1044 | _ => TFree f) |
1034 | thaw_vars(T) = T; |
1045 in map_type_tfree thaw end; |
1035 |
1046 |
1036 |
1047 |
1037 fun restrict tye = |
1048 fun restrict tye = |
1038 let fun clean(tye1, ((a, i), T)) = |
1049 let fun clean(tye1, ((a, i), T)) = |
1039 if i < 0 then tye1 else ((a, i), inst_typ tye T) :: tye1 |
1050 if i < 0 then tye1 else ((a, i), inst_typ tye T) :: tye1 |
1040 in foldl clean ([], tye) end |
1051 in foldl clean ([], tye) end |
1041 |
1052 |
1042 |
1053 |
1043 (*Infer types for term t using tables. Check that t's type and T unify *) |
1054 (*Infer types for term t using tables. Check that t's type and T unify *) |
1044 |
1055 (*freeze determines if internal TVars are turned into TFrees or TVars*) |
1045 fun infer_term (tsig, const_type, types, sorts, T, t) = |
1056 fun infer_term (tsig, const_type, types, sorts, used, freeze, T, t) = |
1046 let |
1057 let |
1047 val u = attach_types (tsig, const_type, types, sorts) t; |
1058 val u = attach_types (tsig, const_type, types, sorts) t; |
1048 val (U, tye) = infer tsig ([], u, []); |
1059 val (U, tye) = infer tsig ([], u, []); |
1049 val uu = unconstrain u; |
1060 val uu = unconstrain u; |
1050 val tye' = unify tsig ((T, U), tye) handle TUNIFY => raise TYPE |
1061 val tye' = unify tsig ((T, U), tye) handle TUNIFY => raise TYPE |
1051 ("Term does not have expected type", [T, U], [inst_types tye uu]) |
1062 ("Term does not have expected type", [T, U], [inst_types tye uu]) |
1052 val Ttye = restrict tye' (*restriction to TVars in T*) |
1063 val Ttye = restrict tye' (*restriction to TVars in T*) |
1053 val all = Const("", Type("", map snd Ttye)) $ (inst_types tye' uu) |
1064 val all = Const("", Type("", map snd Ttye)) $ (inst_types tye' uu) |
1054 (*all is a dummy term which contains all exported TVars*) |
1065 (*all is a dummy term which contains all exported TVars*) |
1055 val Const(_, Type(_, Ts)) $ u'' = |
1066 val Const(_, Type(_, Ts)) $ u'' = |
1056 map_term_types thaw_vars (freeze (fn (_, i) => i < 0) all) |
1067 map_term_types thaw_vars (convert used freeze (fn (_, i) => i < 0) all) |
1057 (*turn all internally generated TVars into TFrees |
1068 (*convert all internally generated TVars into TFrees or TVars |
1058 and thaw all initially frozen TVars*) |
1069 and thaw all initially frozen TVars*) |
1059 in |
1070 in |
1060 (u'', (map fst Ttye) ~~ Ts) |
1071 (u'', (map fst Ttye) ~~ Ts) |
1061 end; |
1072 end; |
1062 |
1073 |
1063 fun infer_types args = (tyinit (); infer_term args); |
1074 fun infer_types args = (tyinit (); infer_term args); |
1064 |
1075 |
1065 |
1076 |
1066 end; |
1077 end; |
1067 |
|