src/Pure/type_infer_context.ML
author wenzelm
Wed Dec 12 23:36:07 2012 +0100 (2012-12-12 ago)
changeset 50499 f496b2b7bafb
parent 49660 de49d9b4d7bc
child 56438 7f6b2634d853
permissions -rw-r--r--
rendering of selected dialog_result as active_result_color, depending on dynamic command status in output panel, but not static popups etc.;
     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 const_type: Proof.context -> string -> typ option
    11   val prepare_positions: Proof.context -> term list -> term list * (Position.T * typ) list
    12   val prepare: Proof.context -> term list -> int * term list
    13   val infer_types: Proof.context -> term list -> term list
    14 end;
    15 
    16 structure Type_Infer_Context: TYPE_INFER_CONTEXT =
    17 struct
    18 
    19 (** prepare types/terms: create inference parameters **)
    20 
    21 (* constraints *)
    22 
    23 val const_sorts = Config.bool (Config.declare "const_sorts" (K (Config.Bool true)));
    24 
    25 fun const_type ctxt =
    26   try ((not (Config.get ctxt const_sorts) ? Type.strip_sorts) o
    27     Consts.the_constraint (Proof_Context.consts_of ctxt));
    28 
    29 fun var_type ctxt = the_default dummyT o Proof_Context.def_type ctxt;
    30 
    31 
    32 (* prepare_typ *)
    33 
    34 fun prepare_typ typ params_idx =
    35   let
    36     val (params', idx) = fold_atyps
    37       (fn TVar (xi, S) =>
    38           (fn ps_idx as (ps, idx) =>
    39             if Type_Infer.is_param xi andalso not (Vartab.defined ps xi)
    40             then (Vartab.update (xi, Type_Infer.mk_param idx S) ps, idx + 1) else ps_idx)
    41         | _ => I) typ params_idx;
    42 
    43     fun prepare (T as Type (a, Ts)) idx =
    44           if T = dummyT then (Type_Infer.mk_param idx [], idx + 1)
    45           else
    46             let val (Ts', idx') = fold_map prepare Ts idx
    47             in (Type (a, Ts'), idx') end
    48       | prepare (T as TVar (xi, _)) idx =
    49           (case Vartab.lookup params' xi of
    50             NONE => T
    51           | SOME p => p, idx)
    52       | prepare (TFree ("'_dummy_", S)) idx = (Type_Infer.mk_param idx S, idx + 1)
    53       | prepare (T as TFree _) idx = (T, idx);
    54 
    55     val (typ', idx') = prepare typ idx;
    56   in (typ', (params', idx')) end;
    57 
    58 
    59 (* prepare_term *)
    60 
    61 fun prepare_term ctxt tm (vparams, params, idx) =
    62   let
    63     fun add_vparm xi (ps_idx as (ps, idx)) =
    64       if not (Vartab.defined ps xi) then
    65         (Vartab.update (xi, Type_Infer.mk_param idx []) ps, idx + 1)
    66       else ps_idx;
    67 
    68     val (vparams', idx') = fold_aterms
    69       (fn Var (_, Type ("_polymorphic_", _)) => I
    70         | Var (xi, _) => add_vparm xi
    71         | Free (x, _) => add_vparm (x, ~1)
    72         | _ => I)
    73       tm (vparams, idx);
    74     fun var_param xi = the (Vartab.lookup vparams' xi);
    75 
    76     fun polyT_of T idx =
    77       apsnd snd (prepare_typ (Type_Infer.paramify_vars T) (Vartab.empty, idx));
    78 
    79     fun constraint T t ps =
    80       if T = dummyT then (t, ps)
    81       else
    82         let val (T', ps') = prepare_typ T ps
    83         in (Type.constraint T' t, ps') end;
    84 
    85     fun prepare (Const ("_type_constraint_", T) $ t) ps_idx =
    86           let
    87             val A = Type.constraint_type ctxt T;
    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 (* prepare_positions *)
   119 
   120 fun prepare_positions ctxt tms =
   121   let
   122     fun prepareT (Type (a, Ts)) ps_idx =
   123           let val (Ts', ps_idx') = fold_map prepareT Ts ps_idx
   124           in (Type (a, Ts'), ps_idx') end
   125       | prepareT T (ps, idx) =
   126           (case Term_Position.decode_positionT T of
   127             SOME pos =>
   128               let val U = Type_Infer.mk_param idx []
   129               in (U, ((pos, U) :: ps, idx + 1)) end
   130           | NONE => (T, (ps, idx)));
   131 
   132     fun prepare (Const ("_type_constraint_", T)) ps_idx =
   133           let
   134             val A = Type.constraint_type ctxt T;
   135             val (A', ps_idx') = prepareT A ps_idx;
   136           in (Const ("_type_constraint_", A' --> A'), ps_idx') end
   137       | prepare (Const (c, T)) ps_idx =
   138           let val (T', ps_idx') = prepareT T ps_idx
   139           in (Const (c, T'), ps_idx') end
   140       | prepare (Free (x, T)) ps_idx =
   141           let val (T', ps_idx') = prepareT T ps_idx
   142           in (Free (x, T'), ps_idx') end
   143       | prepare (Var (xi, T)) ps_idx =
   144           let val (T', ps_idx') = prepareT T ps_idx
   145           in (Var (xi, T'), ps_idx') end
   146       | prepare (t as Bound _) ps_idx = (t, ps_idx)
   147       | prepare (Abs (x, T, t)) ps_idx =
   148           let
   149             val (T', ps_idx') = prepareT T ps_idx;
   150             val (t', ps_idx'') = prepare t ps_idx';
   151           in (Abs (x, T', t'), ps_idx'') end
   152       | prepare (t $ u) ps_idx =
   153           let
   154             val (t', ps_idx') = prepare t ps_idx;
   155             val (u', ps_idx'') = prepare u ps_idx';
   156           in (t' $ u', ps_idx'') end;
   157 
   158     val idx = Type_Infer.param_maxidx_of tms + 1;
   159     val (tms', (ps, _)) = fold_map prepare tms ([], idx);
   160   in (tms', ps) end;
   161 
   162 
   163 
   164 (** order-sorted unification of types **)
   165 
   166 exception NO_UNIFIER of string * typ Vartab.table;
   167 
   168 fun unify ctxt =
   169   let
   170     val thy = Proof_Context.theory_of ctxt;
   171     val arity_sorts = Type.arity_sorts (Context.pretty ctxt) (Sign.tsig_of thy);
   172 
   173 
   174     (* adjust sorts of parameters *)
   175 
   176     fun not_of_sort x S' S =
   177       "Variable " ^ x ^ "::" ^ Syntax.string_of_sort ctxt S' ^ " not of sort " ^
   178         Syntax.string_of_sort ctxt S;
   179 
   180     fun meet (_, []) tye_idx = tye_idx
   181       | meet (Type (a, Ts), S) (tye_idx as (tye, _)) =
   182           meets (Ts, arity_sorts a S handle ERROR msg => raise NO_UNIFIER (msg, tye)) tye_idx
   183       | meet (TFree (x, S'), S) (tye_idx as (tye, _)) =
   184           if Sign.subsort thy (S', S) then tye_idx
   185           else raise NO_UNIFIER (not_of_sort x S' S, tye)
   186       | meet (TVar (xi, S'), S) (tye_idx as (tye, idx)) =
   187           if Sign.subsort thy (S', S) then tye_idx
   188           else if Type_Infer.is_param xi then
   189             (Vartab.update_new
   190               (xi, Type_Infer.mk_param idx (Sign.inter_sort thy (S', S))) tye, idx + 1)
   191           else raise NO_UNIFIER (not_of_sort (Term.string_of_vname xi) S' S, tye)
   192     and meets (T :: Ts, S :: Ss) (tye_idx as (tye, _)) =
   193           meets (Ts, Ss) (meet (Type_Infer.deref tye T, S) tye_idx)
   194       | meets _ tye_idx = tye_idx;
   195 
   196 
   197     (* occurs check and assignment *)
   198 
   199     fun occurs_check tye xi (TVar (xi', _)) =
   200           if xi = xi' then raise NO_UNIFIER ("Occurs check!", tye)
   201           else
   202             (case Vartab.lookup tye xi' of
   203               NONE => ()
   204             | SOME T => occurs_check tye xi T)
   205       | occurs_check tye xi (Type (_, Ts)) = List.app (occurs_check tye xi) Ts
   206       | occurs_check _ _ _ = ();
   207 
   208     fun assign xi (T as TVar (xi', _)) S env =
   209           if xi = xi' then env
   210           else env |> meet (T, S) |>> Vartab.update_new (xi, T)
   211       | assign xi T S (env as (tye, _)) =
   212           (occurs_check tye xi T; env |> meet (T, S) |>> Vartab.update_new (xi, T));
   213 
   214 
   215     (* unification *)
   216 
   217     fun show_tycon (a, Ts) =
   218       quote (Syntax.string_of_typ ctxt (Type (a, replicate (length Ts) dummyT)));
   219 
   220     fun unif (T1, T2) (env as (tye, _)) =
   221       (case pairself (`Type_Infer.is_paramT o Type_Infer.deref tye) (T1, T2) of
   222         ((true, TVar (xi, S)), (_, T)) => assign xi T S env
   223       | ((_, T), (true, TVar (xi, S))) => assign xi T S env
   224       | ((_, Type (a, Ts)), (_, Type (b, Us))) =>
   225           if a <> b then
   226             raise NO_UNIFIER
   227               ("Clash of types " ^ show_tycon (a, Ts) ^ " and " ^ show_tycon (b, Us), tye)
   228           else fold unif (Ts ~~ Us) env
   229       | ((_, T), (_, U)) => if T = U then env else raise NO_UNIFIER ("", tye));
   230 
   231   in unif end;
   232 
   233 
   234 
   235 (** simple type inference **)
   236 
   237 (* infer *)
   238 
   239 fun infer ctxt =
   240   let
   241     (* errors *)
   242 
   243     fun prep_output tye bs ts Ts =
   244       let
   245         val (Ts_bTs', ts') = Type_Infer.finish ctxt tye (Ts @ map snd bs, ts);
   246         val (Ts', Ts'') = chop (length Ts) Ts_bTs';
   247         fun prep t =
   248           let val xs = rev (Term.variant_frees t (rev (map fst bs ~~ Ts'')))
   249           in Term.subst_bounds (map Syntax_Trans.mark_bound_abs xs, t) end;
   250       in (map prep ts', Ts') end;
   251 
   252     fun err_loose i = error ("Loose bound variable: B." ^ string_of_int i);
   253 
   254     fun unif_failed msg =
   255       "Type unification failed" ^ (if msg = "" then "" else ": " ^ msg) ^ "\n\n";
   256 
   257     fun err_appl msg tye bs t T u U =
   258       let val ([t', u'], [T', U']) = prep_output tye bs [t, u] [T, U]
   259       in error (unif_failed msg ^ Type.appl_error ctxt t' T' u' U' ^ "\n") end;
   260 
   261 
   262     (* main *)
   263 
   264     fun inf _ (Const (_, T)) tye_idx = (T, tye_idx)
   265       | inf _ (Free (_, T)) tye_idx = (T, tye_idx)
   266       | inf _ (Var (_, T)) tye_idx = (T, tye_idx)
   267       | inf bs (Bound i) tye_idx =
   268           (snd (nth bs i handle General.Subscript => err_loose i), tye_idx)
   269       | inf bs (Abs (x, T, t)) tye_idx =
   270           let val (U, tye_idx') = inf ((x, T) :: bs) t tye_idx
   271           in (T --> U, tye_idx') end
   272       | inf bs (t $ u) tye_idx =
   273           let
   274             val (T, tye_idx') = inf bs t tye_idx;
   275             val (U, (tye, idx)) = inf bs u tye_idx';
   276             val V = Type_Infer.mk_param idx [];
   277             val tye_idx'' = unify ctxt (U --> V, T) (tye, idx + 1)
   278               handle NO_UNIFIER (msg, tye') => err_appl msg tye' bs t T u U;
   279           in (V, tye_idx'') end;
   280 
   281   in inf [] end;
   282 
   283 
   284 (* main interfaces *)
   285 
   286 fun prepare ctxt raw_ts =
   287   let
   288     val constrain_vars = Term.map_aterms
   289       (fn Free (x, T) => Type.constraint T (Free (x, var_type ctxt (x, ~1)))
   290         | Var (xi, T) => Type.constraint T (Var (xi, var_type ctxt xi))
   291         | t => t);
   292 
   293     val ts = burrow_types (Syntax.check_typs ctxt) raw_ts;
   294     val idx = Type_Infer.param_maxidx_of ts + 1;
   295     val (ts', (_, _, idx')) =
   296       fold_map (prepare_term ctxt o constrain_vars) ts
   297         (Vartab.empty, Vartab.empty, idx);
   298   in (idx', ts') end;
   299 
   300 fun infer_types ctxt raw_ts =
   301   let
   302     val (idx, ts) = prepare ctxt raw_ts;
   303     val (tye, _) = fold (snd oo infer ctxt) ts (Vartab.empty, idx);
   304     val (_, ts') = Type_Infer.finish ctxt tye ([], ts);
   305   in ts' end;
   306 
   307 end;