src/Pure/Tools/codegen_thingol.ML
changeset 22305 0e56750a092b
parent 22185 24bf0e403526
child 22799 ed7d53db2170
equal deleted inserted replaced
22304:ba3d6b76a627 22305:0e56750a092b
    21     | DictVar of string list * (vname * (int * int));
    21     | DictVar of string list * (vname * (int * int));
    22   datatype itype =
    22   datatype itype =
    23       `%% of string * itype list
    23       `%% of string * itype list
    24     | ITyVar of vname;
    24     | ITyVar of vname;
    25   datatype iterm =
    25   datatype iterm =
    26       IConst of string * (dict list list * itype)
    26       IConst of string * (dict list list * itype list (*types of arguments*))
    27     | IVar of vname
    27     | IVar of vname
    28     | `$ of iterm * iterm
    28     | `$ of iterm * iterm
    29     | `|-> of (vname * itype) * iterm
    29     | `|-> of (vname * itype) * iterm
    30     | INum of IntInf.int
    30     | INum of IntInf.int
    31     | IChar of string (*length one!*)
    31     | IChar of string (*length one!*)
    49   val split_abs: iterm -> (((vname * iterm option) * itype) * iterm) option;
    49   val split_abs: iterm -> (((vname * iterm option) * itype) * iterm) option;
    50   val unfold_abs: iterm -> ((vname * iterm option) * itype) list * iterm;
    50   val unfold_abs: iterm -> ((vname * iterm option) * itype) list * iterm;
    51   val split_let: iterm -> (((iterm * itype) * iterm) * iterm) option;
    51   val split_let: iterm -> (((iterm * itype) * iterm) * iterm) option;
    52   val unfold_let: iterm -> ((iterm * itype) * iterm) list * iterm;
    52   val unfold_let: iterm -> ((iterm * itype) * iterm) list * iterm;
    53   val unfold_const_app: iterm ->
    53   val unfold_const_app: iterm ->
    54     ((string * (dict list list * itype)) * iterm list) option;
    54     ((string * (dict list list * itype list)) * iterm list) option;
    55   val eta_expand: (string * (dict list list * itype)) * iterm list -> int -> iterm;
    55   val eta_expand: (string * (dict list list * itype list)) * iterm list -> int -> iterm;
    56   val fold_constnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
    56   val fold_constnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
    57   val fold_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
    57   val fold_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
    58   val fold_unbound_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
    58   val fold_unbound_varnames: (string -> 'a -> 'a) -> iterm -> 'a -> 'a;
    59 
    59 
    60   datatype def =
    60   datatype def =
   122 datatype itype =
   122 datatype itype =
   123     `%% of string * itype list
   123     `%% of string * itype list
   124   | ITyVar of vname;
   124   | ITyVar of vname;
   125 
   125 
   126 datatype iterm =
   126 datatype iterm =
   127     IConst of string * (dict list list * itype)
   127     IConst of string * (dict list list * itype list)
   128   | IVar of vname
   128   | IVar of vname
   129   | `$ of iterm * iterm
   129   | `$ of iterm * iterm
   130   | `|-> of (vname * itype) * iterm
   130   | `|-> of (vname * itype) * iterm
   131   | INum of IntInf.int
   131   | INum of IntInf.int
   132   | IChar of string
   132   | IChar of string
   234           I
   234           I
   235       | add vs (ICase ((td, _), bs)) =
   235       | add vs (ICase ((td, _), bs)) =
   236           add vs td #> fold (fn (p, t) => add vs p #> add vs t) bs;
   236           add vs td #> fold (fn (p, t) => add vs p #> add vs t) bs;
   237   in add [] end;
   237   in add [] end;
   238 
   238 
   239 fun eta_expand (c as (_, (_, ty)), ts) k =
   239 fun eta_expand (c as (_, (_, tys)), ts) k =
   240   let
   240   let
   241     val j = length ts;
   241     val j = length ts;
   242     val l = k - j;
   242     val l = k - j;
   243     val tys = (curry Library.take l o curry Library.drop j o fst o unfold_fun) ty;
       
   244     val ctxt = (fold o fold_varnames) Name.declare ts Name.context;
   243     val ctxt = (fold o fold_varnames) Name.declare ts Name.context;
   245     val vs_tys = Name.names ctxt "a" tys;
   244     val vs_tys = Name.names ctxt "a" ((curry Library.take l o curry Library.drop j) tys);
   246   in vs_tys `|--> IConst c `$$ ts @ map (fn (v, _) => IVar v) vs_tys end;
   245   in vs_tys `|--> IConst c `$$ ts @ map (fn (v, _) => IVar v) vs_tys end;
   247 
   246 
   248 
   247 
   249 (** definitions, transactions **)
   248 (** definitions, transactions **)
   250 
   249 
   408           else (Bot, code);
   407           else (Bot, code);
   409   in
   408   in
   410     code
   409     code
   411     |> (if can (get_def code) name
   410     |> (if can (get_def code) name
   412         then
   411         then
   413           tracing (fn _ => "asserting definition " ^ labelled_name name)
   412           add_dp dep
       
   413         else
       
   414           ensure_bot name
   414           #> add_dp dep
   415           #> add_dp dep
   415         else
       
   416           tracing (fn _ => "allocating definition " ^ labelled_name name
       
   417             ^ (if strict then " (strict)" else " (non-strict)"))
       
   418           #> ensure_bot name
       
   419           #> add_dp dep
       
   420           #> tracing (fn _ => "creating definition " ^ labelled_name name)
       
   421           #> invoke_generator name defgen
   416           #> invoke_generator name defgen
   422           #-> (fn def => prep_def def)
   417           #-> (fn def => prep_def def)
   423           #-> (fn def =>
   418           #-> (fn def =>
   424              tracing (fn _ => "adding")
   419              add_def_incr strict (name, def)
   425           #> add_def_incr strict (name, def)
       
   426           #> tracing (fn _ => "postprocessing")
       
   427           #> postprocess_def (name, def)
   420           #> postprocess_def (name, def)
   428           #> tracing (fn _ => "adding done")
       
   429        ))
   421        ))
   430     |> pair dep
   422     |> pair dep
   431   end;
   423   end;
   432 
   424 
   433 fun succeed some (_, code) = (some, code);
   425 fun succeed some (_, code) = (some, code);