src/Pure/type_infer_context.ML
author wenzelm
Tue Apr 19 20:47:02 2011 +0200 (2011-04-19)
changeset 42405 13ecdb3057d8
child 43278 1fbdcebb364b
permissions -rw-r--r--
split Type_Infer into early and late part, after Proof_Context;
added Type_Infer_Context.const_sorts option, which allows NBE to use regular Syntax.check_term;
     1 (*  Title:      Pure/type_infer_context.ML
     2     Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
     3 
     4 Type-inference preparation and standard type inference.
     5 *)
     6 
     7 signature TYPE_INFER_CONTEXT =
     8 sig
     9   val const_sorts: bool Config.T
    10   val prepare: Proof.context -> term list -> int * term list
    11   val infer_types: Proof.context -> term list -> term list
    12 end;
    13 
    14 structure Type_Infer_Context: TYPE_INFER_CONTEXT =
    15 struct
    16 
    17 (** prepare types/terms: create inference parameters **)
    18 
    19 (* constraints *)
    20 
    21 val const_sorts = Config.bool (Config.declare "const_sorts" (K (Config.Bool true)));
    22 
    23 fun const_type ctxt =
    24   try ((not (Config.get ctxt const_sorts) ? Type.strip_sorts) o
    25     Consts.the_constraint (Proof_Context.consts_of ctxt));
    26 
    27 fun var_type ctxt = the_default dummyT o Proof_Context.def_type ctxt;
    28 
    29 
    30 (* prepare_typ *)
    31 
    32 fun prepare_typ typ params_idx =
    33   let
    34     val (params', idx) = fold_atyps
    35       (fn TVar (xi, S) =>
    36           (fn ps_idx as (ps, idx) =>
    37             if Type_Infer.is_param xi andalso not (Vartab.defined ps xi)
    38             then (Vartab.update (xi, Type_Infer.mk_param idx S) ps, idx + 1) else ps_idx)
    39         | _ => I) typ params_idx;
    40 
    41     fun prepare (T as Type (a, Ts)) idx =
    42           if T = dummyT then (Type_Infer.mk_param idx [], idx + 1)
    43           else
    44             let val (Ts', idx') = fold_map prepare Ts idx
    45             in (Type (a, Ts'), idx') end
    46       | prepare (T as TVar (xi, _)) idx =
    47           (case Vartab.lookup params' xi of
    48             NONE => T
    49           | SOME p => p, idx)
    50       | prepare (TFree ("'_dummy_", S)) idx = (Type_Infer.mk_param idx S, idx + 1)
    51       | prepare (T as TFree _) idx = (T, idx);
    52 
    53     val (typ', idx') = prepare typ idx;
    54   in (typ', (params', idx')) end;
    55 
    56 
    57 (* prepare_term *)
    58 
    59 fun prepare_term ctxt tm (vparams, params, idx) =
    60   let
    61     fun add_vparm xi (ps_idx as (ps, idx)) =
    62       if not (Vartab.defined ps xi) then
    63         (Vartab.update (xi, Type_Infer.mk_param idx []) ps, idx + 1)
    64       else ps_idx;
    65 
    66     val (vparams', idx') = fold_aterms
    67       (fn Var (_, Type ("_polymorphic_", _)) => I
    68         | Var (xi, _) => add_vparm xi
    69         | Free (x, _) => add_vparm (x, ~1)
    70         | _ => I)
    71       tm (vparams, idx);
    72     fun var_param xi = the (Vartab.lookup vparams' xi);
    73 
    74     fun polyT_of T idx =
    75       apsnd snd (prepare_typ (Type_Infer.paramify_vars T) (Vartab.empty, idx));
    76 
    77     fun constraint T t ps =
    78       if T = dummyT then (t, ps)
    79       else
    80         let val (T', ps') = prepare_typ T ps
    81         in (Type.constraint T' t, ps') end;
    82 
    83     fun prepare (Const ("_type_constraint_", T) $ t) ps_idx =
    84           let
    85             fun err () =
    86               error ("Malformed internal type constraint: " ^ Syntax.string_of_typ ctxt T);
    87             val A = (case T of Type ("fun", [A, B]) => if A = B then A else err () | _ => err ());
    88             val (A', ps_idx') = prepare_typ A ps_idx;
    89             val (t', ps_idx'') = prepare t ps_idx';
    90           in (Const ("_type_constraint_", A' --> A') $ t', ps_idx'') end
    91       | prepare (Const (c, T)) (ps, idx) =
    92           (case const_type ctxt c of
    93             SOME U =>
    94               let val (U', idx') = polyT_of U idx
    95               in constraint T (Const (c, U')) (ps, idx') end
    96           | NONE => error ("Undeclared constant: " ^ quote c))
    97       | prepare (Var (xi, Type ("_polymorphic_", [T]))) (ps, idx) =
    98           let val (T', idx') = polyT_of T idx
    99           in (Var (xi, T'), (ps, idx')) end
   100       | prepare (Var (xi, T)) ps_idx = constraint T (Var (xi, var_param xi)) ps_idx
   101       | prepare (Free (x, T)) ps_idx = constraint T (Free (x, var_param (x, ~1))) ps_idx
   102       | prepare (Bound i) ps_idx = (Bound i, ps_idx)
   103       | prepare (Abs (x, T, t)) ps_idx =
   104           let
   105             val (T', ps_idx') = prepare_typ T ps_idx;
   106             val (t', ps_idx'') = prepare t ps_idx';
   107           in (Abs (x, T', t'), ps_idx'') end
   108       | prepare (t $ u) ps_idx =
   109           let
   110             val (t', ps_idx') = prepare t ps_idx;
   111             val (u', ps_idx'') = prepare u ps_idx';
   112           in (t' $ u', ps_idx'') end;
   113 
   114     val (tm', (params', idx'')) = prepare tm (params, idx');
   115   in (tm', (vparams', params', idx'')) end;
   116 
   117 
   118 
   119 (** order-sorted unification of types **)
   120 
   121 exception NO_UNIFIER of string * typ Vartab.table;
   122 
   123 fun unify ctxt =
   124   let
   125     val thy = Proof_Context.theory_of ctxt;
   126     val arity_sorts = Type.arity_sorts (Context.pretty ctxt) (Sign.tsig_of thy);
   127 
   128 
   129     (* adjust sorts of parameters *)
   130 
   131     fun not_of_sort x S' S =
   132       "Variable " ^ x ^ "::" ^ Syntax.string_of_sort ctxt S' ^ " not of sort " ^
   133         Syntax.string_of_sort ctxt S;
   134 
   135     fun meet (_, []) tye_idx = tye_idx
   136       | meet (Type (a, Ts), S) (tye_idx as (tye, _)) =
   137           meets (Ts, arity_sorts a S handle ERROR msg => raise NO_UNIFIER (msg, tye)) tye_idx
   138       | meet (TFree (x, S'), S) (tye_idx as (tye, _)) =
   139           if Sign.subsort thy (S', S) then tye_idx
   140           else raise NO_UNIFIER (not_of_sort x S' S, tye)
   141       | meet (TVar (xi, S'), S) (tye_idx as (tye, idx)) =
   142           if Sign.subsort thy (S', S) then tye_idx
   143           else if Type_Infer.is_param xi then
   144             (Vartab.update_new
   145               (xi, Type_Infer.mk_param idx (Sign.inter_sort thy (S', S))) tye, idx + 1)
   146           else raise NO_UNIFIER (not_of_sort (Term.string_of_vname xi) S' S, tye)
   147     and meets (T :: Ts, S :: Ss) (tye_idx as (tye, _)) =
   148           meets (Ts, Ss) (meet (Type_Infer.deref tye T, S) tye_idx)
   149       | meets _ tye_idx = tye_idx;
   150 
   151 
   152     (* occurs check and assignment *)
   153 
   154     fun occurs_check tye xi (TVar (xi', _)) =
   155           if xi = xi' then raise NO_UNIFIER ("Occurs check!", tye)
   156           else
   157             (case Vartab.lookup tye xi' of
   158               NONE => ()
   159             | SOME T => occurs_check tye xi T)
   160       | occurs_check tye xi (Type (_, Ts)) = List.app (occurs_check tye xi) Ts
   161       | occurs_check _ _ _ = ();
   162 
   163     fun assign xi (T as TVar (xi', _)) S env =
   164           if xi = xi' then env
   165           else env |> meet (T, S) |>> Vartab.update_new (xi, T)
   166       | assign xi T S (env as (tye, _)) =
   167           (occurs_check tye xi T; env |> meet (T, S) |>> Vartab.update_new (xi, T));
   168 
   169 
   170     (* unification *)
   171 
   172     fun show_tycon (a, Ts) =
   173       quote (Syntax.string_of_typ ctxt (Type (a, replicate (length Ts) dummyT)));
   174 
   175     fun unif (T1, T2) (env as (tye, _)) =
   176       (case pairself (`Type_Infer.is_paramT o Type_Infer.deref tye) (T1, T2) of
   177         ((true, TVar (xi, S)), (_, T)) => assign xi T S env
   178       | ((_, T), (true, TVar (xi, S))) => assign xi T S env
   179       | ((_, Type (a, Ts)), (_, Type (b, Us))) =>
   180           if a <> b then
   181             raise NO_UNIFIER
   182               ("Clash of types " ^ show_tycon (a, Ts) ^ " and " ^ show_tycon (b, Us), tye)
   183           else fold unif (Ts ~~ Us) env
   184       | ((_, T), (_, U)) => if T = U then env else raise NO_UNIFIER ("", tye));
   185 
   186   in unif end;
   187 
   188 
   189 
   190 (** simple type inference **)
   191 
   192 (* infer *)
   193 
   194 fun infer ctxt =
   195   let
   196     (* errors *)
   197 
   198     fun prep_output tye bs ts Ts =
   199       let
   200         val (Ts_bTs', ts') = Type_Infer.finish ctxt tye (Ts @ map snd bs, ts);
   201         val (Ts', Ts'') = chop (length Ts) Ts_bTs';
   202         fun prep t =
   203           let val xs = rev (Term.variant_frees t (rev (map fst bs ~~ Ts'')))
   204           in Term.subst_bounds (map Syntax_Trans.mark_boundT xs, t) end;
   205       in (map prep ts', Ts') end;
   206 
   207     fun err_loose i = error ("Loose bound variable: B." ^ string_of_int i);
   208 
   209     fun unif_failed msg =
   210       "Type unification failed" ^ (if msg = "" then "" else ": " ^ msg) ^ "\n\n";
   211 
   212     fun err_appl msg tye bs t T u U =
   213       let val ([t', u'], [T', U']) = prep_output tye bs [t, u] [T, U]
   214       in error (unif_failed msg ^ Type.appl_error ctxt t' T' u' U' ^ "\n") end;
   215 
   216 
   217     (* main *)
   218 
   219     fun inf _ (Const (_, T)) tye_idx = (T, tye_idx)
   220       | inf _ (Free (_, T)) tye_idx = (T, tye_idx)
   221       | inf _ (Var (_, T)) tye_idx = (T, tye_idx)
   222       | inf bs (Bound i) tye_idx =
   223           (snd (nth bs i handle Subscript => err_loose i), tye_idx)
   224       | inf bs (Abs (x, T, t)) tye_idx =
   225           let val (U, tye_idx') = inf ((x, T) :: bs) t tye_idx
   226           in (T --> U, tye_idx') end
   227       | inf bs (t $ u) tye_idx =
   228           let
   229             val (T, tye_idx') = inf bs t tye_idx;
   230             val (U, (tye, idx)) = inf bs u tye_idx';
   231             val V = Type_Infer.mk_param idx [];
   232             val tye_idx'' = unify ctxt (U --> V, T) (tye, idx + 1)
   233               handle NO_UNIFIER (msg, tye') => err_appl msg tye' bs t T u U;
   234           in (V, tye_idx'') end;
   235 
   236   in inf [] end;
   237 
   238 
   239 (* main interfaces *)
   240 
   241 fun prepare ctxt raw_ts =
   242   let
   243     val constrain_vars = Term.map_aterms
   244       (fn Free (x, T) => Type.constraint T (Free (x, var_type ctxt (x, ~1)))
   245         | Var (xi, T) => Type.constraint T (Var (xi, var_type ctxt xi))
   246         | t => t);
   247 
   248     val ts = burrow_types (Syntax.check_typs ctxt) raw_ts;
   249     val idx = Type_Infer.param_maxidx_of ts + 1;
   250     val (ts', (_, _, idx')) =
   251       fold_map (prepare_term ctxt o constrain_vars) ts
   252         (Vartab.empty, Vartab.empty, idx);
   253   in (idx', ts') end;
   254 
   255 fun infer_types ctxt raw_ts =
   256   let
   257     val (idx, ts) = prepare ctxt raw_ts;
   258     val (tye, _) = fold (snd oo infer ctxt) ts (Vartab.empty, idx);
   259     val (_, ts') = Type_Infer.finish ctxt tye ([], ts);
   260   in ts' end;
   261 
   262 val _ =
   263   Context.>>
   264     (Syntax.add_term_check 0 "standard"
   265       (fn ctxt => infer_types ctxt #> map (Proof_Context.expand_abbrevs ctxt)));
   266 
   267 end;