39 val rem_sorts: typ -> typ |
39 val rem_sorts: typ -> typ |
40 val cert_typ: type_sig -> typ -> typ |
40 val cert_typ: type_sig -> typ -> typ |
41 val norm_typ: type_sig -> typ -> typ |
41 val norm_typ: type_sig -> typ -> typ |
42 val freeze: (indexname -> bool) -> term -> term |
42 val freeze: (indexname -> bool) -> term -> term |
43 val freeze_vars: typ -> typ |
43 val freeze_vars: typ -> typ |
44 val infer_types: type_sig * typ Symtab.table * (indexname -> typ option) * |
44 val infer_types: type_sig * (string -> typ option) * (indexname -> typ option) * |
45 (indexname -> sort option) * typ * term -> term * (indexname * typ) list |
45 (indexname -> sort option) * typ * term -> term * (indexname * typ) list |
46 val inst_term_tvars: type_sig * (indexname * typ) list -> term -> term |
46 val inst_term_tvars: type_sig * (indexname * typ) list -> term -> term |
47 val thaw_vars: typ -> typ |
47 val thaw_vars: typ -> typ |
48 val typ_errors: type_sig -> typ * string list -> string list |
48 val typ_errors: type_sig -> typ * string list -> string list |
49 val typ_instance: type_sig * typ * typ -> bool |
49 val typ_instance: type_sig * typ * typ -> bool |
78 |
78 |
79 |
79 |
80 (* print sorts and arities *) |
80 (* print sorts and arities *) |
81 |
81 |
82 fun str_of_sort [c] = c |
82 fun str_of_sort [c] = c |
83 | str_of_sort cs = parents "{" "}" (commas cs); |
83 | str_of_sort cs = enclose "{" "}" (commas cs); |
84 |
84 |
85 fun str_of_dom dom = parents "(" ")" (commas (map str_of_sort dom)); |
85 fun str_of_dom dom = enclose "(" ")" (commas (map str_of_sort dom)); |
86 |
86 |
87 fun str_of_arity (t, [], S) = t ^ " :: " ^ str_of_sort S |
87 fun str_of_arity (t, [], S) = t ^ " :: " ^ str_of_sort S |
88 | str_of_arity (t, SS, S) = |
88 | str_of_arity (t, SS, S) = |
89 t ^ " :: " ^ str_of_dom SS ^ " " ^ str_of_sort S; |
89 t ^ " :: " ^ str_of_dom SS ^ " " ^ str_of_sort S; |
90 |
90 |
767 |
767 |
768 During type inference all TVars in the term have negative index. This keeps |
768 During type inference all TVars in the term have negative index. This keeps |
769 them apart from normal TVars, which is essential, because at the end the type |
769 them apart from normal TVars, which is essential, because at the end the type |
770 of the term is unified with the expected type, which contains normal TVars. |
770 of the term is unified with the expected type, which contains normal TVars. |
771 |
771 |
772 1. Add initial type information to the term (add_types). |
772 1. Add initial type information to the term (attach_types). |
773 This freezes (freeze_vars) TVars in explicitly provided types (eg |
773 This freezes (freeze_vars) TVars in explicitly provided types (eg |
774 constraints or defaults) by turning them into TFrees. |
774 constraints or defaults) by turning them into TFrees. |
775 2. Carry out type inference, possibly introducing new negative TVars. |
775 2. Carry out type inference, possibly introducing new negative TVars. |
776 3. Unify actual and expected type. |
776 3. Unify actual and expected type. |
777 4. Turn all (negative) TVars into unique new TFrees (freeze). |
777 4. Turn all (negative) TVars into unique new TFrees (freeze). |
910 |
910 |
911 (*Find type of ident. If not in table then use ident's name for tyvar |
911 (*Find type of ident. If not in table then use ident's name for tyvar |
912 to get consistent typing.*) |
912 to get consistent typing.*) |
913 fun new_id_type a = TVar(("'"^a, new_tvar_inx()), []); |
913 fun new_id_type a = TVar(("'"^a, new_tvar_inx()), []); |
914 fun type_of_ixn(types, ixn as (a, _)) = |
914 fun type_of_ixn(types, ixn as (a, _)) = |
915 case types ixn of Some T => freeze_vars T | None => TVar(("'"^a, ~1), []); |
915 case types ixn of Some T => freeze_vars T | None => TVar(("'"^a, ~1), []); |
916 |
916 |
917 fun constrain(term, T) = Const(Syntax.constrainC, T-->T) $ term; |
917 fun constrain (term, T) = Const (Syntax.constrainC, T --> T) $ term; |
918 fun constrainAbs(Abs(a, _, body), T) = Abs(a, T, body); |
918 |
919 |
919 fun constrainAbs (Abs (a, _, body), T) = Abs (a, T, body) |
|
920 | constrainAbs _ = sys_error "constrainAbs"; |
|
921 |
|
922 |
|
923 (* attach_types *) |
920 |
924 |
921 (* |
925 (* |
922 Attach types to a term. Input is a "parse tree" containing dummy types. |
926 Attach types to a term. Input is a "parse tree" containing dummy types. |
923 Type constraints are translated and checked for validity wrt tsig. TVars in |
927 Type constraints are translated and checked for validity wrt tsig. TVars in |
924 constraints are frozen. |
928 constraints are frozen. |
937 T is either a type constraint or TVar (("'" ^ a, i), []), where i is |
941 T is either a type constraint or TVar (("'" ^ a, i), []), where i is |
938 generated by new_tvar_inx(). Thus different abstractions can have the |
942 generated by new_tvar_inx(). Thus different abstractions can have the |
939 bound variables of the same name but different types. |
943 bound variables of the same name but different types. |
940 *) |
944 *) |
941 |
945 |
942 (* FIXME replace const_tab by (const_typ: string -> typ option) (?) *) |
946 (* FIXME consitency of sort_env / sorts (!?) *) |
943 (* FIXME improve handling of sort constraints *) |
947 |
944 |
948 (* FIXME check *) |
945 fun add_types (tsig, const_tab, types, sorts) = |
949 |
|
950 fun attach_types (tsig, const_type, types, sorts) tm = |
946 let |
951 let |
947 val S0 = defaultS tsig; |
952 val sort_env = Syntax.raw_term_sorts tm; |
948 fun defS0 ixn = if_none (sorts ixn) S0; |
953 fun def_sort xi = if_none (sorts xi) (defaultS tsig); |
949 |
954 |
950 fun prepareT typ = |
955 fun prepareT t = |
951 freeze_vars (cert_typ tsig (Syntax.typ_of_term defS0 typ)); |
956 freeze_vars (cert_typ tsig (Syntax.typ_of_term sort_env def_sort t)); |
952 |
957 |
953 fun add (Const (a, _)) = |
958 fun add (Const (a, _)) = |
954 (case Symtab.lookup (const_tab, a) of |
959 (case const_type a of |
955 Some T => type_const (a, T) |
960 Some T => type_const (a, T) |
956 | None => raise_type ("No such constant: " ^ quote a) [] []) |
961 | None => raise_type ("No such constant: " ^ quote a) [] []) |
957 | add (Bound i) = Bound i |
|
958 | add (Free (a, _)) = |
962 | add (Free (a, _)) = |
959 (case Symtab.lookup (const_tab, a) of |
963 (case const_type a of |
960 Some T => type_const (a, T) |
964 Some T => type_const (a, T) |
961 | None => Free (a, type_of_ixn (types, (a, ~1)))) |
965 | None => Free (a, type_of_ixn (types, (a, ~1)))) |
962 | add (Var (ixn, _)) = Var (ixn, type_of_ixn (types, ixn)) |
966 | add (Var (ixn, _)) = Var (ixn, type_of_ixn (types, ixn)) |
|
967 | add (Bound i) = Bound i |
963 | add (Abs (a, _, body)) = Abs (a, new_id_type a, add body) |
968 | add (Abs (a, _, body)) = Abs (a, new_id_type a, add body) |
964 | add ((f as Const (a, _) $ t1) $ t2) = |
969 | add ((f as Const (a, _) $ t1) $ t2) = |
965 if a = Syntax.constrainC then |
970 if a = Syntax.constrainC then |
966 constrain (add t1, prepareT t2) |
971 constrain (add t1, prepareT t2) |
967 else if a = Syntax.constrainAbsC then |
972 else if a = Syntax.constrainAbsC then |
968 constrainAbs (add t1, prepareT t2) |
973 constrainAbs (add t1, prepareT t2) |
969 else add f $ add t2 |
974 else add f $ add t2 |
970 | add (f $ t) = add f $ add t; |
975 | add (f $ t) = add f $ add t; |
971 in add end; |
976 in add tm end; |
972 |
977 |
973 |
978 |
974 (* Post-Processing *) |
979 (* Post-Processing *) |
975 |
|
976 |
980 |
977 (*Instantiation of type variables in terms*) |
981 (*Instantiation of type variables in terms*) |
978 fun inst_types tye = map_term_types (inst_typ tye); |
982 fun inst_types tye = map_term_types (inst_typ tye); |
979 |
983 |
980 (*Delete explicit constraints -- occurrences of "_constrain" *) |
984 (*Delete explicit constraints -- occurrences of "_constrain" *) |
1012 in foldl clean ([], tye) end |
1016 in foldl clean ([], tye) end |
1013 |
1017 |
1014 |
1018 |
1015 (*Infer types for term t using tables. Check that t's type and T unify *) |
1019 (*Infer types for term t using tables. Check that t's type and T unify *) |
1016 |
1020 |
1017 fun infer_term (tsig, const_tab, types, sorts, T, t) = |
1021 fun infer_term (tsig, const_type, types, sorts, T, t) = |
1018 let val u = add_types (tsig, const_tab, types, sorts) t; |
1022 let |
1019 val (U, tye) = infer tsig ([], u, []); |
1023 val u = attach_types (tsig, const_type, types, sorts) t; |
1020 val uu = unconstrain u; |
1024 val (U, tye) = infer tsig ([], u, []); |
1021 val tye' = unify tsig ((T, U), tye) handle TUNIFY => raise TYPE |
1025 val uu = unconstrain u; |
1022 ("Term does not have expected type", [T, U], [inst_types tye uu]) |
1026 val tye' = unify tsig ((T, U), tye) handle TUNIFY => raise TYPE |
1023 val Ttye = restrict tye' (* restriction to TVars in T *) |
1027 ("Term does not have expected type", [T, U], [inst_types tye uu]) |
1024 val all = Const("", Type("", map snd Ttye)) $ (inst_types tye' uu) |
1028 val Ttye = restrict tye' (*restriction to TVars in T*) |
1025 (* all is a dummy term which contains all exported TVars *) |
1029 val all = Const("", Type("", map snd Ttye)) $ (inst_types tye' uu) |
1026 val Const(_, Type(_, Ts)) $ u'' = |
1030 (*all is a dummy term which contains all exported TVars*) |
1027 map_term_types thaw_vars (freeze (fn (_, i) => i<0) all) |
1031 val Const(_, Type(_, Ts)) $ u'' = |
1028 (* turn all internally generated TVars into TFrees |
1032 map_term_types thaw_vars (freeze (fn (_, i) => i < 0) all) |
1029 and thaw all initially frozen TVars *) |
1033 (*turn all internally generated TVars into TFrees |
1030 in (u'', (map fst Ttye) ~~ Ts) end; |
1034 and thaw all initially frozen TVars*) |
|
1035 in |
|
1036 (u'', (map fst Ttye) ~~ Ts) |
|
1037 end; |
1031 |
1038 |
1032 fun infer_types args = (tyinit(); infer_term args); |
1039 fun infer_types args = (tyinit(); infer_term args); |
1033 |
1040 |
1034 |
1041 |
1035 (* Turn TFrees into TVars to allow types & axioms to be written without "?" *) |
1042 (* Turn TFrees into TVars to allow types & axioms to be written without "?" *) |