src/Pure/type_infer.ML
author wenzelm
Mon Sep 13 12:42:08 2010 +0200 (2010-09-13)
changeset 39295 6e8b0672c6a2
parent 39294 27fae73fe769
child 39296 e275d581a218
permissions -rw-r--r--
Type_Infer.finish: index 0 -- freshness supposedly via Name.invents;
Type_Infer.fixate_params: full Proof.context;
wenzelm@2957
     1
(*  Title:      Pure/type_infer.ML
wenzelm@2957
     2
    Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
wenzelm@2957
     3
wenzelm@22698
     4
Simple type inference.
wenzelm@2957
     5
*)
wenzelm@2957
     6
wenzelm@2957
     7
signature TYPE_INFER =
wenzelm@2957
     8
sig
wenzelm@8087
     9
  val anyT: sort -> typ
wenzelm@24504
    10
  val is_param: indexname -> bool
wenzelm@39294
    11
  val is_paramT: typ -> bool
wenzelm@14788
    12
  val param: int -> string * sort -> typ
wenzelm@22771
    13
  val paramify_vars: typ -> typ
wenzelm@18339
    14
  val paramify_dummies: typ -> int -> typ * int
wenzelm@39295
    15
  val fixate_params: Proof.context -> term list -> term list
wenzelm@39290
    16
  val infer_types: Proof.context -> (string -> typ option) -> (indexname -> typ option) ->
wenzelm@27263
    17
    term list -> term list
wenzelm@2957
    18
end;
wenzelm@2957
    19
wenzelm@37145
    20
structure Type_Infer: TYPE_INFER =
wenzelm@2957
    21
struct
wenzelm@2957
    22
wenzelm@2957
    23
wenzelm@22698
    24
(** type parameters and constraints **)
wenzelm@2957
    25
wenzelm@22698
    26
fun anyT S = TFree ("'_dummy_", S);
wenzelm@2957
    27
wenzelm@22698
    28
wenzelm@39286
    29
(* type inference parameters -- may get instantiated *)
wenzelm@2957
    30
wenzelm@24504
    31
fun is_param (x, _: int) = String.isPrefix "?" x;
wenzelm@39294
    32
wenzelm@39294
    33
fun is_paramT (TVar (xi, _)) = is_param xi
wenzelm@39294
    34
  | is_paramT _ = false;
wenzelm@39294
    35
wenzelm@22698
    36
fun param i (x, S) = TVar (("?" ^ x, i), S);
wenzelm@22698
    37
wenzelm@39294
    38
fun mk_param i S = TVar (("?'a", i), S);
wenzelm@39294
    39
wenzelm@32002
    40
val paramify_vars =
wenzelm@32146
    41
  Same.commit
wenzelm@32146
    42
    (Term_Subst.map_atypsT_same
wenzelm@32146
    43
      (fn TVar ((x, i), S) => (param i (x, S)) | _ => raise Same.SAME));
wenzelm@22771
    44
wenzelm@22698
    45
val paramify_dummies =
wenzelm@22698
    46
  let
wenzelm@22698
    47
    fun dummy S maxidx = (param (maxidx + 1) ("'dummy", S), maxidx + 1);
wenzelm@2957
    48
wenzelm@22698
    49
    fun paramify (TFree ("'_dummy_", S)) maxidx = dummy S maxidx
wenzelm@22698
    50
      | paramify (Type ("dummy", _)) maxidx = dummy [] maxidx
wenzelm@22698
    51
      | paramify (Type (a, Ts)) maxidx =
wenzelm@22698
    52
          let val (Ts', maxidx') = fold_map paramify Ts maxidx
wenzelm@22698
    53
          in (Type (a, Ts'), maxidx') end
wenzelm@22698
    54
      | paramify T maxidx = (T, maxidx);
wenzelm@22698
    55
  in paramify end;
wenzelm@2957
    56
wenzelm@39295
    57
fun fixate_params ctxt ts =
wenzelm@24764
    58
  let
wenzelm@24764
    59
    fun subst_param (xi, S) (inst, used) =
wenzelm@24764
    60
      if is_param xi then
wenzelm@24764
    61
        let
wenzelm@24848
    62
          val [a] = Name.invents used Name.aT 1;
wenzelm@24764
    63
          val used' = Name.declare a used;
wenzelm@24764
    64
        in (((xi, S), TFree (a, S)) :: inst, used') end
wenzelm@24764
    65
      else (inst, used);
wenzelm@39295
    66
    val used = (fold o fold_types) Term.declare_typ_names ts (Variable.names_of ctxt);
wenzelm@39295
    67
    val (inst, _) = fold_rev subst_param (fold Term.add_tvars ts []) ([], used);
wenzelm@31977
    68
  in (map o map_types) (Term_Subst.instantiateT inst) ts end;
wenzelm@24764
    69
wenzelm@2957
    70
wenzelm@2957
    71
wenzelm@39294
    72
(** prepare types/terms: create inference parameters **)
wenzelm@2957
    73
wenzelm@39294
    74
(* prepare_typ *)
wenzelm@2957
    75
wenzelm@39294
    76
fun prepare_typ typ params_idx =
wenzelm@2957
    77
  let
berghofe@32141
    78
    val (params', idx) = fold_atyps
wenzelm@20668
    79
      (fn TVar (xi as (x, _), S) =>
berghofe@32141
    80
          (fn ps_idx as (ps, idx) =>
wenzelm@39287
    81
            if is_param xi andalso not (Vartab.defined ps xi)
wenzelm@39294
    82
            then (Vartab.update (xi, mk_param idx S) ps, idx + 1) else ps_idx)
berghofe@32141
    83
        | _ => I) typ params_idx;
wenzelm@2957
    84
wenzelm@39294
    85
    fun prepare (T as Type (a, Ts)) idx =
wenzelm@39294
    86
          if T = dummyT then (mk_param idx [], idx + 1)
wenzelm@39294
    87
          else
wenzelm@39294
    88
            let val (Ts', idx') = fold_map prepare Ts idx
wenzelm@39294
    89
            in (Type (a, Ts'), idx') end
wenzelm@39294
    90
      | prepare (T as TVar (xi, _)) idx =
wenzelm@20735
    91
          (case Vartab.lookup params' xi of
wenzelm@39294
    92
            NONE => T
berghofe@32141
    93
          | SOME p => p, idx)
wenzelm@39294
    94
      | prepare (TFree ("'_dummy_", S)) idx = (mk_param idx S, idx + 1)
wenzelm@39294
    95
      | prepare (T as TFree _) idx = (T, idx);
berghofe@32141
    96
wenzelm@39294
    97
    val (typ', idx') = prepare typ idx;
wenzelm@39294
    98
  in (typ', (params', idx')) end;
wenzelm@2957
    99
wenzelm@2957
   100
wenzelm@39294
   101
(* prepare_term *)
wenzelm@2957
   102
wenzelm@39294
   103
fun prepare_term const_type tm (vparams, params, idx) =
wenzelm@2957
   104
  let
berghofe@32141
   105
    fun add_vparm xi (ps_idx as (ps, idx)) =
wenzelm@20735
   106
      if not (Vartab.defined ps xi) then
wenzelm@39294
   107
        (Vartab.update (xi, mk_param idx []) ps, idx + 1)
berghofe@32141
   108
      else ps_idx;
wenzelm@2957
   109
berghofe@32141
   110
    val (vparams', idx') = fold_aterms
wenzelm@20668
   111
      (fn Var (_, Type ("_polymorphic_", _)) => I
wenzelm@20668
   112
        | Var (xi, _) => add_vparm xi
wenzelm@20668
   113
        | Free (x, _) => add_vparm (x, ~1)
wenzelm@20668
   114
        | _ => I)
berghofe@32141
   115
      tm (vparams, idx);
wenzelm@20735
   116
    fun var_param xi = the (Vartab.lookup vparams' xi);
wenzelm@2957
   117
wenzelm@39294
   118
    fun polyT_of T idx = apsnd snd (prepare_typ (paramify_vars T) (Vartab.empty, idx));
wenzelm@2957
   119
wenzelm@22698
   120
    fun constraint T t ps =
wenzelm@20668
   121
      if T = dummyT then (t, ps)
wenzelm@2957
   122
      else
wenzelm@39294
   123
        let val (T', ps') = prepare_typ T ps
wenzelm@39294
   124
        in (Type.constraint T' t, ps') end;
wenzelm@2957
   125
wenzelm@39294
   126
    fun prepare (Const ("_type_constraint_", T) $ t) ps_idx =
wenzelm@39294
   127
          let
wenzelm@39294
   128
            val (T', ps_idx') = prepare_typ T ps_idx;
wenzelm@39294
   129
            val (t', ps_idx'') = prepare t ps_idx';
wenzelm@39294
   130
          in (Const ("_type_constraint_", T') $ t', ps_idx'') end
wenzelm@39294
   131
      | prepare (Const (c, T)) (ps, idx) =
wenzelm@2957
   132
          (case const_type c of
berghofe@32141
   133
            SOME U =>
wenzelm@39294
   134
              let val (U', idx') = polyT_of U idx
wenzelm@39294
   135
              in constraint T (Const (c, U')) (ps, idx') end
wenzelm@39291
   136
          | NONE => error ("Undeclared constant: " ^ quote c))
wenzelm@39294
   137
      | prepare (Var (xi, Type ("_polymorphic_", [T]))) (ps, idx) =
wenzelm@39294
   138
          let val (T', idx') = polyT_of T idx
wenzelm@39294
   139
          in (Var (xi, T'), (ps, idx')) end
wenzelm@39294
   140
      | prepare (Var (xi, T)) ps_idx = constraint T (Var (xi, var_param xi)) ps_idx
wenzelm@39294
   141
      | prepare (Free (x, T)) ps_idx = constraint T (Free (x, var_param (x, ~1))) ps_idx
wenzelm@39294
   142
      | prepare (Bound i) ps_idx = (Bound i, ps_idx)
wenzelm@39294
   143
      | prepare (Abs (x, T, t)) ps_idx =
wenzelm@39292
   144
          let
wenzelm@39294
   145
            val (T', ps_idx') = prepare_typ T ps_idx;
wenzelm@39294
   146
            val (t', ps_idx'') = prepare t ps_idx';
wenzelm@39294
   147
          in (Abs (x, T', t'), ps_idx'') end
wenzelm@39294
   148
      | prepare (t $ u) ps_idx =
wenzelm@2957
   149
          let
wenzelm@39294
   150
            val (t', ps_idx') = prepare t ps_idx;
wenzelm@39294
   151
            val (u', ps_idx'') = prepare u ps_idx';
wenzelm@39294
   152
          in (t' $ u', ps_idx'') end;
wenzelm@2957
   153
wenzelm@39294
   154
    val (tm', (params', idx'')) = prepare tm (params, idx');
berghofe@32141
   155
  in (tm', (vparams', params', idx'')) end;
wenzelm@2957
   156
wenzelm@2957
   157
wenzelm@2957
   158
wenzelm@39294
   159
(** finish types/terms: standardize remaining parameters **)
wenzelm@2957
   160
wenzelm@39294
   161
(* dereferenced views *)
wenzelm@2957
   162
wenzelm@39294
   163
fun deref tye (T as TVar (xi, _)) =
wenzelm@39294
   164
      (case Vartab.lookup tye xi of
wenzelm@39294
   165
        NONE => T
wenzelm@39294
   166
      | SOME U => deref tye U)
wenzelm@39294
   167
  | deref tye T = T;
wenzelm@39294
   168
wenzelm@39294
   169
fun add_parms tye T =
wenzelm@32146
   170
  (case deref tye T of
wenzelm@39294
   171
    Type (_, Ts) => fold (add_parms tye) Ts
wenzelm@39294
   172
  | TVar (xi, _) => if is_param xi then insert (op =) xi else I
wenzelm@32146
   173
  | _ => I);
wenzelm@2957
   174
wenzelm@39294
   175
fun add_names tye T =
wenzelm@32146
   176
  (case deref tye T of
wenzelm@39294
   177
    Type (_, Ts) => fold (add_names tye) Ts
wenzelm@39294
   178
  | TFree (x, _) => Name.declare x
wenzelm@39294
   179
  | TVar ((x, i), _) => if is_param (x, i) then I else Name.declare x);
wenzelm@2957
   180
wenzelm@2957
   181
wenzelm@39294
   182
(* finish *)
wenzelm@2957
   183
wenzelm@39294
   184
fun finish ctxt tye (Ts, ts) =
wenzelm@39294
   185
  let
wenzelm@39294
   186
    val used =
wenzelm@39294
   187
      (fold o fold_types) (add_names tye) ts (fold (add_names tye) Ts (Variable.names_of ctxt));
wenzelm@39294
   188
    val parms = rev ((fold o fold_types) (add_parms tye) ts (fold (add_parms tye) Ts []));
wenzelm@39294
   189
    val names = Name.invents used ("?" ^ Name.aT) (length parms);
wenzelm@39294
   190
    val tab = Vartab.make (parms ~~ names);
wenzelm@2957
   191
wenzelm@39294
   192
    fun finish_typ T =
wenzelm@39294
   193
      (case deref tye T of
wenzelm@39294
   194
        Type (a, Ts) => Type (a, map finish_typ Ts)
wenzelm@39294
   195
      | U as TFree _ => U
wenzelm@39294
   196
      | U as TVar (xi, S) =>
wenzelm@39294
   197
          (case Vartab.lookup tab xi of
wenzelm@39294
   198
            NONE => U
wenzelm@39295
   199
          | SOME a => TVar ((a, 0), S)));
wenzelm@39294
   200
  in (map finish_typ Ts, map (Type.strip_constraints o Term.map_types finish_typ) ts) end;
wenzelm@2957
   201
wenzelm@2957
   202
wenzelm@2957
   203
berghofe@32141
   204
(** order-sorted unification of types **)
wenzelm@2957
   205
wenzelm@39294
   206
exception NO_UNIFIER of string * typ Vartab.table;
wenzelm@2957
   207
wenzelm@39290
   208
fun unify ctxt pp =
wenzelm@2957
   209
  let
wenzelm@39290
   210
    val thy = ProofContext.theory_of ctxt;
wenzelm@39290
   211
    val arity_sorts = Type.arity_sorts pp (Sign.tsig_of thy);
wenzelm@39290
   212
wenzelm@39290
   213
wenzelm@2957
   214
    (* adjust sorts of parameters *)
wenzelm@2957
   215
wenzelm@19465
   216
    fun not_of_sort x S' S =
wenzelm@39290
   217
      "Variable " ^ x ^ "::" ^ Syntax.string_of_sort ctxt S' ^ " not of sort " ^
wenzelm@39290
   218
        Syntax.string_of_sort ctxt S;
wenzelm@2957
   219
berghofe@32141
   220
    fun meet (_, []) tye_idx = tye_idx
wenzelm@39294
   221
      | meet (Type (a, Ts), S) (tye_idx as (tye, _)) =
wenzelm@39291
   222
          meets (Ts, arity_sorts a S handle ERROR msg => raise NO_UNIFIER (msg, tye)) tye_idx
wenzelm@39294
   223
      | meet (TFree (x, S'), S) (tye_idx as (tye, _)) =
wenzelm@39290
   224
          if Sign.subsort thy (S', S) then tye_idx
berghofe@32141
   225
          else raise NO_UNIFIER (not_of_sort x S' S, tye)
wenzelm@39294
   226
      | meet (TVar (xi, S'), S) (tye_idx as (tye, idx)) =
wenzelm@39290
   227
          if Sign.subsort thy (S', S) then tye_idx
wenzelm@39294
   228
          else if is_param xi then
wenzelm@39294
   229
            (Vartab.update_new (xi, mk_param idx (Sign.inter_sort thy (S', S))) tye, idx + 1)
berghofe@32141
   230
          else raise NO_UNIFIER (not_of_sort (Term.string_of_vname xi) S' S, tye)
berghofe@32141
   231
    and meets (T :: Ts, S :: Ss) (tye_idx as (tye, _)) =
berghofe@32141
   232
          meets (Ts, Ss) (meet (deref tye T, S) tye_idx)
berghofe@32141
   233
      | meets _ tye_idx = tye_idx;
wenzelm@2957
   234
wenzelm@2957
   235
wenzelm@35013
   236
    (* occurs check and assignment *)
wenzelm@2957
   237
wenzelm@39294
   238
    fun occurs_check tye xi (TVar (xi', S)) =
wenzelm@39294
   239
          if xi = xi' then raise NO_UNIFIER ("Occurs check!", tye)
wenzelm@32146
   240
          else
wenzelm@39294
   241
            (case Vartab.lookup tye xi' of
berghofe@32141
   242
              NONE => ()
wenzelm@39294
   243
            | SOME T => occurs_check tye xi T)
wenzelm@39294
   244
      | occurs_check tye xi (Type (_, Ts)) = List.app (occurs_check tye xi) Ts
berghofe@32141
   245
      | occurs_check _ _ _ = ();
wenzelm@2957
   246
wenzelm@39294
   247
    fun assign xi (T as TVar (xi', _)) S env =
wenzelm@39294
   248
          if xi = xi' then env
wenzelm@39294
   249
          else env |> meet (T, S) |>> Vartab.update_new (xi, T)
wenzelm@39294
   250
      | assign xi T S (env as (tye, _)) =
wenzelm@39294
   251
          (occurs_check tye xi T; env |> meet (T, S) |>> Vartab.update_new (xi, T));
wenzelm@2957
   252
wenzelm@2957
   253
wenzelm@2957
   254
    (* unification *)
wenzelm@2957
   255
wenzelm@39286
   256
    fun show_tycon (a, Ts) =
wenzelm@39290
   257
      quote (Syntax.string_of_typ ctxt (Type (a, replicate (length Ts) dummyT)));
wenzelm@39286
   258
wenzelm@39294
   259
    fun unif (T1, T2) (env as (tye, _)) =
wenzelm@39294
   260
      (case pairself (`is_paramT o deref tye) (T1, T2) of
wenzelm@39294
   261
        ((true, TVar (xi, S)), (_, T)) => assign xi T S env
wenzelm@39294
   262
      | ((_, T), (true, TVar (xi, S))) => assign xi T S env
wenzelm@39294
   263
      | ((_, Type (a, Ts)), (_, Type (b, Us))) =>
wenzelm@2979
   264
          if a <> b then
wenzelm@39286
   265
            raise NO_UNIFIER
wenzelm@39286
   266
              ("Clash of types " ^ show_tycon (a, Ts) ^ " and " ^ show_tycon (b, Us), tye)
wenzelm@39294
   267
          else fold unif (Ts ~~ Us) env
wenzelm@39294
   268
      | ((_, T), (_, U)) => if T = U then env else raise NO_UNIFIER ("", tye));
wenzelm@2957
   269
wenzelm@2957
   270
  in unif end;
wenzelm@2957
   271
wenzelm@2957
   272
wenzelm@2957
   273
wenzelm@2957
   274
(** type inference **)
wenzelm@2957
   275
berghofe@32141
   276
(* infer *)
wenzelm@2957
   277
wenzelm@39290
   278
fun infer ctxt =
wenzelm@2957
   279
  let
wenzelm@39290
   280
    val pp = Syntax.pp ctxt;
wenzelm@39290
   281
wenzelm@39290
   282
wenzelm@2979
   283
    (* errors *)
wenzelm@2957
   284
berghofe@32141
   285
    fun prep_output tye bs ts Ts =
wenzelm@2957
   286
      let
wenzelm@39294
   287
        val (Ts_bTs', ts') = finish ctxt tye (Ts @ map snd bs, ts);
wenzelm@19012
   288
        val (Ts', Ts'') = chop (length Ts) Ts_bTs';
wenzelm@27263
   289
        fun prep t =
wenzelm@27263
   290
          let val xs = rev (Term.variant_frees t (rev (map fst bs ~~ Ts'')))
wenzelm@27263
   291
          in Term.subst_bounds (map Syntax.mark_boundT xs, t) end;
wenzelm@27263
   292
      in (map prep ts', Ts') end;
wenzelm@2979
   293
wenzelm@39291
   294
    fun err_loose i = error ("Loose bound variable: B." ^ string_of_int i);
wenzelm@2979
   295
wenzelm@39289
   296
    fun unif_failed msg =
wenzelm@39289
   297
      "Type unification failed" ^ (if msg = "" then "" else ": " ^ msg) ^ "\n\n";
wenzelm@39289
   298
berghofe@32141
   299
    fun err_appl msg tye bs t T u U =
wenzelm@39291
   300
      let val ([t', u'], [T', U']) = prep_output tye bs [t, u] [T, U]
wenzelm@39291
   301
      in error (unif_failed msg ^ Type.appl_error pp t' T' u' U' ^ "\n") end;
wenzelm@2979
   302
wenzelm@2979
   303
wenzelm@2979
   304
    (* main *)
wenzelm@2979
   305
wenzelm@39294
   306
    fun inf _ (Const (_, T)) tye_idx = (T, tye_idx)
wenzelm@39294
   307
      | inf _ (Free (_, T)) tye_idx = (T, tye_idx)
wenzelm@39294
   308
      | inf _ (Var (_, T)) tye_idx = (T, tye_idx)
wenzelm@39294
   309
      | inf bs (Bound i) tye_idx =
berghofe@32141
   310
          (snd (nth bs i handle Subscript => err_loose i), tye_idx)
wenzelm@39294
   311
      | inf bs (Abs (x, T, t)) tye_idx =
berghofe@32141
   312
          let val (U, tye_idx') = inf ((x, T) :: bs) t tye_idx
wenzelm@39294
   313
          in (T --> U, tye_idx') end
wenzelm@39294
   314
      | inf bs (t $ u) tye_idx =
wenzelm@2957
   315
          let
berghofe@32141
   316
            val (T, tye_idx') = inf bs t tye_idx;
berghofe@32141
   317
            val (U, (tye, idx)) = inf bs u tye_idx';
wenzelm@39294
   318
            val V = mk_param idx [];
wenzelm@39294
   319
            val tye_idx'' = unify ctxt pp (U --> V, T) (tye, idx + 1)
berghofe@32141
   320
              handle NO_UNIFIER (msg, tye') => err_appl msg tye' bs t T u U;
wenzelm@39292
   321
          in (V, tye_idx'') end;
wenzelm@2957
   322
wenzelm@2957
   323
  in inf [] end;
wenzelm@2957
   324
wenzelm@2957
   325
wenzelm@22698
   326
(* infer_types *)
wenzelm@2957
   327
wenzelm@39290
   328
fun infer_types ctxt const_type var_type raw_ts =
wenzelm@2957
   329
  let
wenzelm@22698
   330
    (*constrain vars*)
wenzelm@22698
   331
    val get_type = the_default dummyT o var_type;
wenzelm@22698
   332
    val constrain_vars = Term.map_aterms
wenzelm@39288
   333
      (fn Free (x, T) => Type.constraint T (Free (x, get_type (x, ~1)))
wenzelm@39288
   334
        | Var (xi, T) => Type.constraint T (Var (xi, get_type xi))
wenzelm@22698
   335
        | t => t);
wenzelm@22698
   336
wenzelm@27263
   337
    (*convert to preterms*)
wenzelm@39290
   338
    val ts = burrow_types (Syntax.check_typs ctxt) raw_ts;
berghofe@32141
   339
    val (ts', (_, _, idx)) =
wenzelm@39294
   340
      fold_map (prepare_term const_type o constrain_vars) ts
berghofe@32141
   341
      (Vartab.empty, Vartab.empty, 0);
wenzelm@2957
   342
wenzelm@27263
   343
    (*do type inference*)
wenzelm@39294
   344
    val (tye, _) = fold (snd oo infer ctxt) ts' (Vartab.empty, idx);
wenzelm@39294
   345
  in #2 (finish ctxt tye ([], ts')) end;
wenzelm@14788
   346
wenzelm@2957
   347
end;