src/Pure/type_infer.ML
changeset 24504 0edc609e36fd
parent 24485 687bbb686ef9
child 24682 29306b20079b
equal deleted inserted replaced
24503:2439587f516b 24504:0edc609e36fd
     9 sig
     9 sig
    10   val anyT: sort -> typ
    10   val anyT: sort -> typ
    11   val logicT: typ
    11   val logicT: typ
    12   val polymorphicT: typ -> typ
    12   val polymorphicT: typ -> typ
    13   val constrain: term -> typ -> term
    13   val constrain: term -> typ -> term
       
    14   val is_param: indexname -> bool
    14   val param: int -> string * sort -> typ
    15   val param: int -> string * sort -> typ
    15   val paramify_vars: typ -> typ
    16   val paramify_vars: typ -> typ
    16   val paramify_dummies: typ -> int -> typ * int
    17   val paramify_dummies: typ -> int -> typ * int
    17   val appl_error: Pretty.pp -> string -> term -> typ -> term -> typ -> string list
    18   val appl_error: Pretty.pp -> string -> term -> typ -> term -> typ -> string list
    18   val infer_types: Pretty.pp -> Type.tsig -> (typ list -> typ list) ->
    19   val infer_types: Pretty.pp -> Type.tsig -> (typ list -> typ list) ->
    37   else Const ("_type_constraint_", T --> T) $ t;
    38   else Const ("_type_constraint_", T --> T) $ t;
    38 
    39 
    39 
    40 
    40 (* user parameters *)
    41 (* user parameters *)
    41 
    42 
    42 fun is_param_default (x, _) = size x > 0 andalso ord x = ord "?";
    43 fun is_param (x, _: int) = String.isPrefix "?" x;
    43 fun param i (x, S) = TVar (("?" ^ x, i), S);
    44 fun param i (x, S) = TVar (("?" ^ x, i), S);
    44 
    45 
    45 val paramify_vars = Term.map_atyps (fn TVar ((x, i), S) => param i (x, S) | T => T);
    46 val paramify_vars = Term.map_atyps (fn TVar ((x, i), S) => param i (x, S) | T => T);
    46 
    47 
    47 val paramify_dummies =
    48 val paramify_dummies =
    98 
    99 
    99 (** raw typs/terms to pretyps/preterms **)
   100 (** raw typs/terms to pretyps/preterms **)
   100 
   101 
   101 (* pretyp_of *)
   102 (* pretyp_of *)
   102 
   103 
   103 fun pretyp_of is_param typ params =
   104 fun pretyp_of is_para typ params =
   104   let
   105   let
   105     val params' = fold_atyps
   106     val params' = fold_atyps
   106       (fn TVar (xi as (x, _), S) =>
   107       (fn TVar (xi as (x, _), S) =>
   107           (fn ps =>
   108           (fn ps =>
   108             if is_param xi andalso not (Vartab.defined ps xi)
   109             if is_para xi andalso not (Vartab.defined ps xi)
   109             then Vartab.update (xi, mk_param S) ps else ps)
   110             then Vartab.update (xi, mk_param S) ps else ps)
   110         | _ => I) typ params;
   111         | _ => I) typ params;
   111 
   112 
   112     fun pre_of (TVar (v as (xi, _))) =
   113     fun pre_of (TVar (v as (xi, _))) =
   113           (case Vartab.lookup params' xi of
   114           (case Vartab.lookup params' xi of
   121   in (pre_of typ, params') end;
   122   in (pre_of typ, params') end;
   122 
   123 
   123 
   124 
   124 (* preterm_of *)
   125 (* preterm_of *)
   125 
   126 
   126 fun preterm_of const_type is_param tm (vparams, params) =
   127 fun preterm_of const_type is_para tm (vparams, params) =
   127   let
   128   let
   128     fun add_vparm xi ps =
   129     fun add_vparm xi ps =
   129       if not (Vartab.defined ps xi) then
   130       if not (Vartab.defined ps xi) then
   130         Vartab.update (xi, mk_param []) ps
   131         Vartab.update (xi, mk_param []) ps
   131       else ps;
   132       else ps;
   136         | Free (x, _) => add_vparm (x, ~1)
   137         | Free (x, _) => add_vparm (x, ~1)
   137         | _ => I)
   138         | _ => I)
   138       tm vparams;
   139       tm vparams;
   139     fun var_param xi = the (Vartab.lookup vparams' xi);
   140     fun var_param xi = the (Vartab.lookup vparams' xi);
   140 
   141 
   141     val preT_of = pretyp_of is_param;
   142     val preT_of = pretyp_of is_para;
   142     fun polyT_of T = fst (pretyp_of (K true) T Vartab.empty);
   143     fun polyT_of T = fst (pretyp_of (K true) T Vartab.empty);
   143 
   144 
   144     fun constraint T t ps =
   145     fun constraint T t ps =
   145       if T = dummyT then (t, ps)
   146       if T = dummyT then (t, ps)
   146       else
   147       else
   396         | t => t);
   397         | t => t);
   397 
   398 
   398     (*convert to preterms/typs*)
   399     (*convert to preterms/typs*)
   399     val (Ts', Tps) = fold_map (pretyp_of (K true)) Ts Vartab.empty;
   400     val (Ts', Tps) = fold_map (pretyp_of (K true)) Ts Vartab.empty;
   400     val (ts', (vps, ps)) =
   401     val (ts', (vps, ps)) =
   401       fold_map (preterm_of const_type is_param_default o constrain_vars) ts (Vartab.empty, Tps);
   402       fold_map (preterm_of const_type is_param o constrain_vars) ts (Vartab.empty, Tps);
   402 
   403 
   403     (*run type inference*)
   404     (*run type inference*)
   404     val tTs' = ListPair.map Constraint (ts', Ts');
   405     val tTs' = ListPair.map Constraint (ts', Ts');
   405     val _ = List.app (fn t => (infer pp tsig t; ())) tTs';
   406     val _ = List.app (fn t => (infer pp tsig t; ())) tTs';
   406 
   407