src/Pure/type.ML
changeset 949 83c588d6fee9
parent 621 9d8791da0208
child 963 7a78fda77104
equal deleted inserted replaced
948:3647161d15d3 949:83c588d6fee9
    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)
    70 fun no_tvars T =
    72 fun no_tvars T =
    71   if null (typ_tvars T) then T
    73   if null (typ_tvars T) then T
    72   else raise_type "Illegal schematic type variable(s)" [T] [];
    74   else raise_type "Illegal schematic type variable(s)" [T] [];
    73 
    75 
    74 (*turn TFrees into TVars to allow types & axioms to be written without "?"*)
    76 (*turn TFrees into TVars to allow types & axioms to be written without "?"*)
    75 fun varifyT (Type (a, Ts)) = Type (a, map varifyT Ts)
    77 val varifyT = map_type_tfree (fn (a, S) => TVar((a, 0), S));
    76   | varifyT (TFree (a, S)) = TVar ((a, 0), S)
       
    77   | varifyT T = T;
       
    78 
    78 
    79 (*inverse of varifyT*)
    79 (*inverse of varifyT*)
    80 fun unvarifyT (Type (a, Ts)) = Type (a, map unvarifyT Ts)
    80 fun unvarifyT (Type (a, Ts)) = Type (a, map unvarifyT Ts)
    81   | unvarifyT (TVar ((a, 0), S)) = TFree (a, S)
    81   | unvarifyT (TVar ((a, 0), S)) = TFree (a, S)
    82   | unvarifyT T = T;
    82   | unvarifyT T = T;
    85 fun varify (t, fixed) =
    85 fun varify (t, fixed) =
    86   let
    86   let
    87     val fs = add_term_tfree_names (t, []) \\ fixed;
    87     val fs = add_term_tfree_names (t, []) \\ fixed;
    88     val ixns = add_term_tvar_ixns (t, []);
    88     val ixns = add_term_tvar_ixns (t, []);
    89     val fmap = fs ~~ variantlist (fs, map #1 ixns)
    89     val fmap = fs ~~ variantlist (fs, map #1 ixns)
    90     fun thaw (Type(a, Ts)) = Type (a, map thaw Ts)
    90     fun thaw(f as (a,S)) = case assoc (fmap, a) of
    91       | thaw (T as TVar _) = T
    91                              None => TFree(f)
    92       | thaw (T as TFree(a, S)) =
    92                            | Some b => TVar((b, 0), S)
    93           (case assoc (fmap, a) of None => T | Some b => TVar((b, 0), S))
    93   in  map_term_types (map_type_tfree thaw) t  end;
    94   in
       
    95     map_term_types thaw t
       
    96   end;
       
    97 
    94 
    98 
    95 
    99 
    96 
   100 (*** type classes and sorts ***)
    97 (*** type classes and sorts ***)
   101 
    98 
   296 
   293 
   297 
   294 
   298 (*Instantiation of type variables in types*)
   295 (*Instantiation of type variables in types*)
   299 (*Pre: instantiations obey restrictions! *)
   296 (*Pre: instantiations obey restrictions! *)
   300 fun inst_typ tye =
   297 fun inst_typ tye =
   301   let fun inst(Type(a, Ts)) = Type(a, map inst Ts)
   298   let fun inst(var as (v, _)) = case assoc(tye, v) of
   302         | inst(T as TFree _) = T
   299                                   Some U => inst_typ tye U
   303         | inst(T as TVar(v, _)) =
   300                                 | None => TVar(var)
   304             (case assoc(tye, v) of Some U => inst U | None => T)
   301   in map_type_tvar inst end;
   305   in inst end;
       
   306 
   302 
   307 (* 'least_sort' returns for a given type its maximum sort:
   303 (* 'least_sort' returns for a given type its maximum sort:
   308    - type variables, free types: the sort brought with
   304    - type variables, free types: the sort brought with
   309    - type constructors: recursive determination of the maximum sort of the
   305    - type constructors: recursive determination of the maximum sort of the
   310                     arguments if the type is declared in 'coreg' of the
   306                     arguments if the type is declared in 'coreg' of the
   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 
   925                    end
   920                    end
   926                | _ => err"rator must have function type"
   921                | _ => err"rator must have function type"
   927            end
   922            end
   928   in inf end;
   923   in inf end;
   929 
   924 
   930 fun freeze_vars(Type(a, Ts)) = Type(a, map freeze_vars Ts)
   925 val freeze_vars =
   931   | freeze_vars(T as TFree _) = T
   926       map_type_tvar (fn (v, S) => TFree(Syntax.string_of_vname v, S));
   932   | freeze_vars(TVar(v, S)) = TFree(Syntax.string_of_vname v, S);
       
   933 
   927 
   934 (* Attach a type to a constant *)
   928 (* Attach a type to a constant *)
   935 fun type_const (a, T) = Const(a, incr_tvar (new_tvar_inx()) T);
   929 fun type_const (a, T) = Const(a, incr_tvar (new_tvar_inx()) T);
   936 
   930 
   937 (*Find type of ident.  If not in table then use ident's name for tyvar
   931 (*Find type of ident.  If not in table then use ident's name for tyvar
  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