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