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