Type inference (isolated from type.ML, completely reimplemented).
authorwenzelm
Wed Apr 16 18:15:32 1997 +0200 (1997-04-16)
changeset 2957d35fca99b3be
parent 2956 d128ae3e7421
child 2958 7837471d2f27
Type inference (isolated from type.ML, completely reimplemented).
src/Pure/type_infer.ML
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/type_infer.ML	Wed Apr 16 18:15:32 1997 +0200
     1.3 @@ -0,0 +1,380 @@
     1.4 +(*  Title:      Pure/type_infer.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
     1.7 +
     1.8 +Type inference.
     1.9 +*)
    1.10 +
    1.11 +signature TYPE_INFER =
    1.12 +sig
    1.13 +  val infer_types: (string -> typ option) -> Sorts.classrel -> Sorts.arities
    1.14 +    -> string list -> bool -> (indexname -> bool) -> term list -> typ list
    1.15 +    -> term list * typ list * (indexname * typ) list
    1.16 +end;
    1.17 +
    1.18 +structure TypeInfer: TYPE_INFER =
    1.19 +struct
    1.20 +
    1.21 +
    1.22 +(** generic utils **)
    1.23 +
    1.24 +fun seq2 _ [] [] = ()
    1.25 +  | seq2 f (x :: xs) (y :: ys) = (f x y; seq2 f xs ys)
    1.26 +  | seq2 _ _ _ = raise LIST "seq2";
    1.27 +
    1.28 +fun scan _ (xs, []) = (xs, [])
    1.29 +  | scan f (xs, y :: ys) =
    1.30 +      let
    1.31 +        val (xs', y') = f (xs, y);
    1.32 +        val (xs'', ys') = scan f (xs', ys);
    1.33 +      in (xs'', y' :: ys') end;
    1.34 +
    1.35 +
    1.36 +
    1.37 +(** term encodings **)
    1.38 +
    1.39 +(*
    1.40 +  Flavours of term encodings:
    1.41 +
    1.42 +    parse trees (type term):
    1.43 +      A very complicated structure produced by the syntax module's
    1.44 +      read functions.  Encodes types and sorts as terms; may contain
    1.45 +      explicit constraints and partial typing information (where
    1.46 +      dummyT serves as wildcard).
    1.47 +
    1.48 +      Parse trees are INTERNAL! Users should never encounter them,
    1.49 +      except in parse / print translation functions.
    1.50 +
    1.51 +    raw terms (type term):
    1.52 +      Provide the user interface to type inferences.  They may contain
    1.53 +      partial type information (dummyT is wildcard) or explicit type
    1.54 +      constraints (introduced via constrain: term -> typ -> term).
    1.55 +
    1.56 +      The type inference function also lets users specify a certain
    1.57 +      subset of TVars to be treated as non-rigid inference parameters.
    1.58 +
    1.59 +    preterms (type preterm):
    1.60 +      The internal representation for type inference.
    1.61 +
    1.62 +    well-typed term (type term):
    1.63 +      Fully typed lambda terms to be accepted by appropriate
    1.64 +      certification functions.
    1.65 +*)
    1.66 +
    1.67 +
    1.68 +
    1.69 +(** pretyps and preterms **)
    1.70 +
    1.71 +(*links to parameters may get instantiated, anything else is rigid*)
    1.72 +datatype pretyp =
    1.73 +  PType of string * pretyp list |
    1.74 +  PTFree of string * sort |
    1.75 +  PTVar of indexname * sort |
    1.76 +  Param of sort |
    1.77 +  Link of pretyp ref;
    1.78 +
    1.79 +datatype preterm =
    1.80 +  PConst of string * pretyp |
    1.81 +  PFree of string * pretyp |
    1.82 +  PVar of indexname * pretyp |
    1.83 +  PBound of int |
    1.84 +  PAbs of string * pretyp * preterm |
    1.85 +  PAppl of preterm * preterm |
    1.86 +  Constraint of preterm * pretyp;
    1.87 +
    1.88 +
    1.89 +(* utils *)
    1.90 +
    1.91 +val mk_param = Link o ref o Param;
    1.92 +
    1.93 +fun deref (T as Link (ref (Param _))) = T
    1.94 +  | deref (Link (ref T)) = deref T
    1.95 +  | deref T = T;
    1.96 +
    1.97 +fun foldl_pretyps f (x, PConst (_, T)) = f (x, T)
    1.98 +  | foldl_pretyps f (x, PFree (_, T)) = f (x, T)
    1.99 +  | foldl_pretyps f (x, PVar (_, T)) = f (x, T)
   1.100 +  | foldl_pretyps _ (x, PBound _) = x
   1.101 +  | foldl_pretyps f (x, PAbs (_, T, t)) = foldl_pretyps f (f (x, T), t)
   1.102 +  | foldl_pretyps f (x, PAppl (t, u)) = foldl_pretyps f (foldl_pretyps f (x, t), u)
   1.103 +  | foldl_pretyps f (x, Constraint (t, T)) = f (foldl_pretyps f (x, t), T);
   1.104 +
   1.105 +
   1.106 +
   1.107 +(** raw typs/terms to pretyps/preterms **)
   1.108 +
   1.109 +(* pretyp(s)_of *)
   1.110 +
   1.111 +fun pretyp_of is_param (params, typ) =
   1.112 +  let
   1.113 +    fun add_parms (ps, TVar (xi as (x, _), S)) =
   1.114 +          if is_param xi andalso is_none (assoc (ps, xi))
   1.115 +          then (xi, mk_param S) :: ps else ps
   1.116 +      | add_parms (ps, TFree _) = ps
   1.117 +      | add_parms (ps, Type (_, Ts)) = foldl add_parms (ps, Ts);
   1.118 +
   1.119 +    val params' = add_parms (params, typ);
   1.120 +
   1.121 +    fun pre_of (TVar (v as (xi, _))) =
   1.122 +          (case assoc (params', xi) of
   1.123 +            None => PTVar v
   1.124 +          | Some p => p)
   1.125 +      | pre_of (TFree v) = PTFree v
   1.126 +      | pre_of (T as Type (a, Ts)) =
   1.127 +          if T = dummyT then mk_param []
   1.128 +          else PType (a, map pre_of Ts);
   1.129 +  in (params', pre_of typ) end;
   1.130 +
   1.131 +fun pretyps_of is_param = scan (pretyp_of is_param);
   1.132 +
   1.133 +
   1.134 +(* preterm(s)_of *)
   1.135 +
   1.136 +fun preterm_of const_type is_param ((vparams, params), tm) =
   1.137 +  let
   1.138 +    fun add_vparm (ps, xi) =
   1.139 +      if is_none (assoc (ps, xi)) then
   1.140 +        (xi, mk_param []) :: ps
   1.141 +      else ps;
   1.142 +
   1.143 +    fun add_vparms (ps, Var (xi, _)) = add_vparm (ps, xi)
   1.144 +      | add_vparms (ps, Free (x, _)) = add_vparm (ps, (x, ~1))
   1.145 +      | add_vparms (ps, Abs (_, _, t)) = add_vparms (ps, t)
   1.146 +      | add_vparms (ps, t $ u) = add_vparms (add_vparms (ps, t), u)
   1.147 +      | add_vparms (ps, _) = ps;
   1.148 +
   1.149 +    val vparams' = add_vparms (vparams, tm);
   1.150 +    fun var_param xi = the (assoc (vparams', xi));
   1.151 +
   1.152 +
   1.153 +    val preT_of = pretyp_of is_param;
   1.154 +
   1.155 +    fun constrain (ps, t) T =
   1.156 +      if T = dummyT then (ps, t)
   1.157 +      else
   1.158 +        let val (ps', T') = preT_of (ps, T) in
   1.159 +          (ps', Constraint (t, T'))
   1.160 +        end;
   1.161 +
   1.162 +    fun pre_of (ps, Const (c, T)) =
   1.163 +          (case const_type c of
   1.164 +            Some U => constrain (ps, PConst (c, snd (pretyp_of (K true) ([], U)))) T
   1.165 +          | None => raise_type ("No such constant: " ^ quote c) [] [])
   1.166 +      | pre_of (ps, Free (x, T)) = constrain (ps, PFree (x, var_param (x, ~1))) T
   1.167 +      | pre_of (ps, Var (xi, T)) = constrain (ps, PVar (xi, var_param xi)) T
   1.168 +      | pre_of (ps, Const ("_type_constraint_", T) $ t) = constrain (pre_of (ps, t)) T
   1.169 +      | pre_of (ps, Bound i) = (ps, PBound i)
   1.170 +      | pre_of (ps, Abs (x, T, t)) =
   1.171 +          let
   1.172 +            val (ps', T') = preT_of (ps, T);
   1.173 +            val (ps'', t') = pre_of (ps', t);
   1.174 +          in (ps'', PAbs (x, T', t')) end
   1.175 +      | pre_of (ps, t $ u) =
   1.176 +          let
   1.177 +            val (ps', t') = pre_of (ps, t);
   1.178 +            val (ps'', u') = pre_of (ps', u);
   1.179 +          in (ps'', PAppl (t', u')) end;
   1.180 +
   1.181 +
   1.182 +    val (params', tm') = pre_of (params, tm);
   1.183 +  in
   1.184 +    ((vparams', params'), tm')
   1.185 +  end;
   1.186 +
   1.187 +fun preterms_of const_type is_param = scan (preterm_of const_type is_param);
   1.188 +
   1.189 +
   1.190 +
   1.191 +(** pretyps/terms to typs/terms **)
   1.192 +
   1.193 +(* add_parms *)
   1.194 +
   1.195 +fun add_parmsT (rs, PType (_, Ts)) = foldl add_parmsT (rs, Ts)
   1.196 +  | add_parmsT (rs, Link (r as ref (Param _))) = r ins rs
   1.197 +  | add_parmsT (rs, Link (ref T)) = add_parmsT (rs, T)
   1.198 +  | add_parmsT (rs, _) = rs;
   1.199 +
   1.200 +val add_parms = foldl_pretyps add_parmsT;
   1.201 +
   1.202 +
   1.203 +(* add_names *)
   1.204 +
   1.205 +fun add_namesT (xs, PType (_, Ts)) = foldl add_namesT (xs, Ts)
   1.206 +  | add_namesT (xs, PTFree (x, _)) = x ins xs
   1.207 +  | add_namesT (xs, PTVar ((x, _), _)) = x ins xs
   1.208 +  | add_namesT (xs, Link (ref T)) = add_namesT (xs, T)
   1.209 +  | add_namesT (xs, Param _) = xs;
   1.210 +
   1.211 +val add_names = foldl_pretyps add_namesT;
   1.212 +
   1.213 +
   1.214 +(* simple_typ/term_of *)
   1.215 +
   1.216 +(*deref links, fail on params*)
   1.217 +fun simple_typ_of (PType (a, Ts)) = Type (a, map simple_typ_of Ts)
   1.218 +  | simple_typ_of (PTFree v) = TFree v
   1.219 +  | simple_typ_of (PTVar v) = TVar v
   1.220 +  | simple_typ_of (Link (ref T)) = simple_typ_of T
   1.221 +  | simple_typ_of (Param _) = sys_error "simple_typ_of: illegal Param";
   1.222 +
   1.223 +(*convert types, drop constraints*)
   1.224 +fun simple_term_of (PConst (c, T)) = Const (c, simple_typ_of T)
   1.225 +  | simple_term_of (PFree (x, T)) = Free (x, simple_typ_of T)
   1.226 +  | simple_term_of (PVar (xi, T)) = Var (xi, simple_typ_of T)
   1.227 +  | simple_term_of (PBound i) = Bound i
   1.228 +  | simple_term_of (PAbs (x, T, t)) = Abs (x, simple_typ_of T, simple_term_of t)
   1.229 +  | simple_term_of (PAppl (t, u)) = simple_term_of t $ simple_term_of u
   1.230 +  | simple_term_of (Constraint (t, _)) = simple_term_of t;
   1.231 +
   1.232 +
   1.233 +(* typs_terms_of *)                             (*DESTRUCTIVE*)
   1.234 +
   1.235 +fun typs_terms_of used mk_var prfx (Ts, ts) =
   1.236 +  let
   1.237 +    fun elim (r as ref (Param S)) x = r := mk_var (x, S)
   1.238 +      | elim _ _ = ();
   1.239 +
   1.240 +    val used' = foldl add_names (foldl add_namesT (used, Ts), ts);
   1.241 +    val parms = rev (foldl add_parms (foldl add_parmsT ([], Ts), ts));
   1.242 +    val pre_names = replicate (length parms) (prfx ^ "'");
   1.243 +    val names = variantlist (pre_names, prfx ^ "'" :: used');
   1.244 +  in
   1.245 +    seq2 elim parms names;
   1.246 +    (map simple_typ_of Ts, map simple_term_of ts)
   1.247 +  end;
   1.248 +
   1.249 +
   1.250 +
   1.251 +(** order-sorted unification of types **)       (*DESTRUCTIVE*)
   1.252 +
   1.253 +exception NO_UNIFIER of string;
   1.254 +
   1.255 +
   1.256 +fun unify classrel arities =
   1.257 +  let
   1.258 +
   1.259 +    (* adjust sorts of parameters *)
   1.260 +
   1.261 +    fun not_in_sort x S' S =
   1.262 +      "Type variable " ^ x ^ "::" ^ Sorts.str_of_sort S' ^ " not in sort " ^
   1.263 +        Sorts.str_of_sort S;
   1.264 +
   1.265 +    fun meet _ [] = ()
   1.266 +      | meet (Link (r as (ref (Param S')))) S =
   1.267 +          if Sorts.sort_le classrel (S', S) then ()
   1.268 +          else r := mk_param (Sorts.inter_sort classrel (S', S))
   1.269 +      | meet (Link (ref T)) S = meet T S
   1.270 +      | meet (PType (a, Ts)) S =
   1.271 +          seq2 meet Ts (Sorts.mg_domain classrel arities a S
   1.272 +            handle TYPE (msg, _, _) => raise NO_UNIFIER msg)
   1.273 +      | meet (PTFree (x, S')) S =
   1.274 +          if Sorts.sort_le classrel (S', S) then ()
   1.275 +          else raise NO_UNIFIER (not_in_sort x S' S)
   1.276 +      | meet (PTVar (xi, S')) S =
   1.277 +          if Sorts.sort_le classrel (S', S) then ()
   1.278 +          else raise NO_UNIFIER (not_in_sort (Syntax.string_of_vname xi) S' S)
   1.279 +      | meet (Param _) _ = sys_error "meet";
   1.280 +
   1.281 +
   1.282 +    (* occurs check and assigment *)
   1.283 +
   1.284 +    fun occurs_check r (Link (r' as ref T)) =
   1.285 +          if r = r' then raise NO_UNIFIER "Occurs check!"
   1.286 +          else occurs_check r T
   1.287 +      | occurs_check r (PType (_, Ts)) = seq (occurs_check r) Ts
   1.288 +      | occurs_check _ _ = ();
   1.289 +
   1.290 +    fun assign r T S =
   1.291 +      (case deref T of
   1.292 +        T' as Link (r' as ref (Param _)) =>
   1.293 +          if r = r' then () else (r := T'; meet T' S)
   1.294 +      | T' => (occurs_check r T'; r := T'; meet T' S));
   1.295 +
   1.296 +
   1.297 +    (* unification *)
   1.298 +
   1.299 +    fun unif (Link (r as ref (Param S))) T = assign r T S
   1.300 +      | unif T (Link (r as ref (Param S))) = assign r T S
   1.301 +      | unif (Link (ref T)) U = unif T U
   1.302 +      | unif T (Link (ref U)) = unif T U
   1.303 +      | unif (PType (a, Ts)) (PType (b, Us)) =
   1.304 +          if a <> b then raise NO_UNIFIER ("Clash of " ^ a ^ ", " ^ b ^ "!")
   1.305 +          else seq2 unif Ts Us
   1.306 +      | unif T U = if T = U then () else raise NO_UNIFIER "Unification failed!";
   1.307 +
   1.308 +  in unif end;
   1.309 +
   1.310 +
   1.311 +
   1.312 +(** type inference **)
   1.313 +
   1.314 +(* infer *)                                     (*DESTRUCTIVE*)
   1.315 +
   1.316 +fun infer classrel arities =
   1.317 +  let
   1.318 +    val unif = unify classrel arities;
   1.319 +
   1.320 +    fun err msg1 msg2 bs ts Ts =
   1.321 +      let
   1.322 +        val (Ts_bTs', ts') = typs_terms_of [] PTFree "??" (Ts @ map snd bs, ts);
   1.323 +        val len = length Ts;
   1.324 +        val Ts' = take (len, Ts_bTs');
   1.325 +        val xs = map Free (map fst bs ~~ drop (len, Ts_bTs'));
   1.326 +        val ts'' = map (fn t => subst_bounds (xs, t)) ts';
   1.327 +      in
   1.328 +        raise_type (msg1 ^ " " ^ msg2) Ts' ts''
   1.329 +      end;
   1.330 +
   1.331 +    fun inf _ (PConst (_, T)) = T
   1.332 +      | inf _ (PFree (_, T)) = T
   1.333 +      | inf _ (PVar (_, T)) = T
   1.334 +      | inf bs (PBound i) = snd (nth_elem (i, bs)
   1.335 +          handle LIST _ => raise_type "Loose bound variable" [] [Bound i])
   1.336 +      | inf bs (PAbs (x, T, t)) = PType ("fun", [T, inf ((x, T) :: bs) t])
   1.337 +      | inf bs (PAppl (t, u)) =
   1.338 +          let
   1.339 +            val T = inf bs t;
   1.340 +            val U = inf bs u;
   1.341 +            val V = mk_param [];
   1.342 +            val U_to_V = PType ("fun", [U, V]);
   1.343 +            val _ = unif U_to_V T handle NO_UNIFIER msg =>
   1.344 +              err msg "Bad function application." bs [PAppl (t, u)] [U_to_V, U];
   1.345 +          in V end
   1.346 +      | inf bs (Constraint (t, U)) =
   1.347 +          let val T = inf bs t in
   1.348 +            unif T U handle NO_UNIFIER msg =>
   1.349 +              err msg "Cannot meet type constraint." bs [t] [T, U];
   1.350 +            T
   1.351 +          end;
   1.352 +
   1.353 +  in inf [] end;
   1.354 +
   1.355 +
   1.356 +(* infer_types *)
   1.357 +
   1.358 +fun infer_types const_type classrel arities used freeze is_param ts Ts =
   1.359 +  let
   1.360 +    (*convert to preterms/typs*)
   1.361 +    val (Tps, Ts') = pretyps_of (K true) ([], Ts);
   1.362 +    val ((vps, ps), ts') = preterms_of const_type is_param (([], Tps), ts);
   1.363 +
   1.364 +    (*run type inference*)
   1.365 +    val tTs' = ListPair.map Constraint (ts', Ts');
   1.366 +    val _ = seq (fn t => (infer classrel arities t; ())) tTs';
   1.367 +
   1.368 +    (*collect result unifier*)
   1.369 +    fun ch_var (xi, Link (r as ref (Param S))) = (r := PTVar (xi, S); None)
   1.370 +      | ch_var xi_T = Some xi_T;
   1.371 +    val env = mapfilter ch_var Tps;
   1.372 +
   1.373 +    (*convert back to terms/typs*)
   1.374 +    val mk_var =
   1.375 +      if freeze then PTFree
   1.376 +      else (fn (x, S) => PTVar ((x, 0), S));
   1.377 +    val (final_Ts, final_ts) = typs_terms_of used mk_var "" (Ts', ts');
   1.378 +    val final_env = map (apsnd simple_typ_of) env;
   1.379 +  in
   1.380 +    (final_ts, final_Ts, final_env)
   1.381 +  end;
   1.382 +
   1.383 +end;