src/Pure/Tools/codegen_thingol.ML
changeset 19038 62c5f7591a43
parent 19025 596fb1eb7856
child 19042 630b8dd0b31a
equal deleted inserted replaced
19037:3be721728a6c 19038:62c5f7591a43
    16     | IVarT of vname * sort;
    16     | IVarT of vname * sort;
    17   datatype iexpr =
    17   datatype iexpr =
    18       IConst of (string * itype) * classlookup list list
    18       IConst of (string * itype) * classlookup list list
    19     | IVarE of vname * itype
    19     | IVarE of vname * itype
    20     | IApp of iexpr * iexpr
    20     | IApp of iexpr * iexpr
    21     | IAbs of (vname * itype) * iexpr
    21     | IAbs of iexpr * iexpr
    22     | ICase of iexpr * (iexpr * iexpr) list;
    22     | ICase of iexpr * (iexpr * iexpr) list;
    23   val mk_funs: itype list * itype -> itype;
    23   val mk_funs: itype list * itype -> itype;
    24   val mk_apps: iexpr * iexpr list -> iexpr;
    24   val mk_apps: iexpr * iexpr list -> iexpr;
    25   val mk_abss: (vname * itype) list * iexpr -> iexpr;
    25   val mk_abss: iexpr list * iexpr -> iexpr;
    26   val pretty_itype: itype -> Pretty.T;
    26   val pretty_itype: itype -> Pretty.T;
    27   val pretty_iexpr: iexpr -> Pretty.T;
    27   val pretty_iexpr: iexpr -> Pretty.T;
    28   val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list;
    28   val unfoldl: ('a -> ('a * 'b) option) -> 'a -> 'a * 'b list;
    29   val unfoldr: ('a -> ('b * 'a) option) -> 'a -> 'b list * 'a;
    29   val unfoldr: ('a -> ('b * 'a) option) -> 'a -> 'b list * 'a;
    30   val unfold_fun: itype -> itype list * itype;
    30   val unfold_fun: itype -> itype list * itype;
    31   val unfold_app: iexpr -> iexpr * iexpr list;
    31   val unfold_app: iexpr -> iexpr * iexpr list;
    32   val unfold_abs: iexpr -> (vname * itype) list * iexpr;
    32   val unfold_abs: iexpr -> iexpr list * iexpr;
    33   val unfold_let: iexpr -> (iexpr * iexpr) list * iexpr;
    33   val unfold_let: iexpr -> (iexpr * iexpr) list * iexpr;
    34   val unfold_const_app: iexpr ->
    34   val unfold_const_app: iexpr ->
    35     (((string * itype) * classlookup list list) * iexpr list) option;
    35     (((string * itype) * classlookup list list) * iexpr list) option;
    36   val ensure_pat: iexpr -> iexpr;
    36   val ensure_pat: iexpr -> iexpr;
    37   val itype_of_iexpr: iexpr -> itype;
    37   val itype_of_iexpr: iexpr -> itype;
    39   val `%% : string * itype list -> itype;
    39   val `%% : string * itype list -> itype;
    40   val `-> : itype * itype -> itype;
    40   val `-> : itype * itype -> itype;
    41   val `--> : itype list * itype -> itype;
    41   val `--> : itype list * itype -> itype;
    42   val `$ : iexpr * iexpr -> iexpr;
    42   val `$ : iexpr * iexpr -> iexpr;
    43   val `$$ : iexpr * iexpr list -> iexpr;
    43   val `$$ : iexpr * iexpr list -> iexpr;
    44   val `|-> : (vname * itype) * iexpr -> iexpr;
    44   val `|-> : iexpr * iexpr -> iexpr;
    45   val `|--> : (vname * itype) list * iexpr -> iexpr;
    45   val `|--> : iexpr list * iexpr -> iexpr;
    46 
    46 
    47   type funn = (iexpr list * iexpr) list * (ClassPackage.sortcontext * itype);
    47   type funn = (iexpr list * iexpr) list * (ClassPackage.sortcontext * itype);
    48   datatype prim =
    48   datatype prim =
    49       Pretty of Pretty.T
    49       Pretty of Pretty.T
    50     | Name;
    50     | Name;
    51   datatype def =
    51   datatype def =
    52       Undef
    52       Undef
    53     | Prim of (string * prim list) list
    53     | Prim of (string * prim list) list
    54     | Fun of funn
    54     | Fun of funn
    55     | Typesyn of (vname * string list) list * itype
    55     | Typesyn of (vname * sort) list * itype
    56     | Datatype of ((vname * string list) list * (string * itype list) list)
    56     | Datatype of (vname * sort) list * (string * itype list) list
    57         * string list
       
    58     | Datatypecons of string
    57     | Datatypecons of string
    59     | Class of (class list * (vname * (string * (ClassPackage.sortcontext * itype)) list))
    58     | Class of class list * (vname * (string * (ClassPackage.sortcontext * itype)) list)
    60         * string list
       
    61     | Classmember of class
    59     | Classmember of class
    62     | Classinst of ((class * (string * (vname * sort) list))
    60     | Classinst of ((class * (string * (vname * sort) list))
    63           * (class * classlookup list list) list)
    61           * (class * classlookup list list) list)
    64         * (string * funn) list;
    62         * (string * funn) list;
    65   type module;
    63   type module;
    89   val debug_level: int ref;
    87   val debug_level: int ref;
    90   val debug: int -> ('a -> string) -> 'a -> 'a;
    88   val debug: int -> ('a -> string) -> 'a -> 'a;
    91   val soft_exc: bool ref;
    89   val soft_exc: bool ref;
    92 
    90 
    93   val serialize:
    91   val serialize:
    94     ((string -> string) -> (string * def) list -> 'a option)
    92     ((string -> string -> string) -> string -> (string * def) list -> 'a option)
    95     -> (string list -> (string * string) * 'a list -> 'a option)
    93     -> (string list -> (string * string) * 'a list -> 'a option)
    96     -> (string -> string option)
    94     -> (string -> string option)
    97     -> (string * string -> string)
    95     -> (string * string -> string)
    98     -> string list list -> string -> module -> 'a option;
    96     -> string list list -> string -> module -> 'a option;
    99 end;
    97 end;
   159 
   157 
   160 datatype iexpr =
   158 datatype iexpr =
   161     IConst of (string * itype) * classlookup list list
   159     IConst of (string * itype) * classlookup list list
   162   | IVarE of vname * itype
   160   | IVarE of vname * itype
   163   | IApp of iexpr * iexpr
   161   | IApp of iexpr * iexpr
   164   | IAbs of (vname * itype) * iexpr
   162   | IAbs of iexpr * iexpr
   165   | ICase of iexpr * (iexpr * iexpr) list;
   163   | ICase of iexpr * (iexpr * iexpr) list;
   166 
   164 
   167 (*
   165 (*
   168   variable naming conventions
   166   variable naming conventions
   169 
   167 
   211       Pretty.block [Pretty.str (c ^ "::"), pretty_itype ty]
   209       Pretty.block [Pretty.str (c ^ "::"), pretty_itype ty]
   212   | pretty_iexpr (IVarE (v, ty)) =
   210   | pretty_iexpr (IVarE (v, ty)) =
   213       Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty]
   211       Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty]
   214   | pretty_iexpr (IApp (e1, e2)) =
   212   | pretty_iexpr (IApp (e1, e2)) =
   215       Pretty.enclose "(" ")" [pretty_iexpr e1, Pretty.brk 1, pretty_iexpr e2]
   213       Pretty.enclose "(" ")" [pretty_iexpr e1, Pretty.brk 1, pretty_iexpr e2]
   216   | pretty_iexpr (IAbs ((v, ty), e)) =
   214   | pretty_iexpr (IAbs (e1, e2)) =
   217       Pretty.enclose "(" ")" [Pretty.str ("?" ^ v ^ " |->"), Pretty.brk 1, pretty_iexpr e]
   215       Pretty.enclose "(" ")" [pretty_iexpr e1, Pretty.brk 1, Pretty.str "|->", Pretty.brk 1, pretty_iexpr e2]
   218   | pretty_iexpr (ICase (e, cs)) =
   216   | pretty_iexpr (ICase (e, cs)) =
   219       Pretty.enclose "(" ")" [
   217       Pretty.enclose "(" ")" [
   220         Pretty.str "case ",
   218         Pretty.str "case ",
   221         pretty_iexpr e,
   219         pretty_iexpr e,
   222         Pretty.enclose "(" ")" (map (fn (p, e) =>
   220         Pretty.enclose "(" ")" (map (fn (p, e) =>
   332   let
   330   let
   333     fun instant (IVarT x) = f x
   331     fun instant (IVarT x) = f x
   334       | instant y = map_itype instant y;
   332       | instant y = map_itype instant y;
   335   in map_itype instant end;
   333   in map_itype instant end;
   336 
   334 
   337 
       
   338 (* simple diagnosis *)
       
   339 
       
   340 fun pretty_itype (IType (tyco, tys)) =
       
   341       Pretty.enum "" "(" ")" (Pretty.str tyco :: map pretty_itype tys)
       
   342   | pretty_itype (IFun (ty1, ty2)) =
       
   343       Pretty.enum "" "(" ")" [pretty_itype ty1, Pretty.str "->", pretty_itype ty2]
       
   344   | pretty_itype (IVarT (v, sort)) =
       
   345       Pretty.str (v ^ enclose "|" "|" (space_implode "|" sort));
       
   346 
       
   347 fun pretty_iexpr (IConst ((c, ty), _)) =
       
   348       Pretty.block [Pretty.str (c ^ "::"), pretty_itype ty]
       
   349   | pretty_iexpr (IVarE (v, ty)) =
       
   350       Pretty.block [Pretty.str ("?" ^ v ^ "::"), pretty_itype ty]
       
   351   | pretty_iexpr (IApp (e1, e2)) =
       
   352       Pretty.enclose "(" ")" [pretty_iexpr e1, Pretty.brk 1, pretty_iexpr e2]
       
   353   | pretty_iexpr (IAbs ((v, ty), e)) =
       
   354       Pretty.enclose "(" ")" [Pretty.str ("?" ^ v ^ " |->"), Pretty.brk 1, pretty_iexpr e]
       
   355   | pretty_iexpr (ICase (e, cs)) =
       
   356       Pretty.enclose "(" ")" [
       
   357         Pretty.str "case ",
       
   358         pretty_iexpr e,
       
   359         Pretty.enclose "(" ")" (map (fn (p, e) =>
       
   360           Pretty.block [
       
   361             pretty_iexpr p,
       
   362             Pretty.str " => ",
       
   363             pretty_iexpr e
       
   364           ]
       
   365         ) cs)
       
   366       ];
       
   367 
       
   368 
       
   369 (* language auxiliary *)
       
   370 
       
   371 fun itype_of_iexpr (IConst ((_, ty), s)) = ty
   335 fun itype_of_iexpr (IConst ((_, ty), s)) = ty
   372   | itype_of_iexpr (IVarE (_, ty)) = ty
   336   | itype_of_iexpr (IVarE (_, ty)) = ty
   373   | itype_of_iexpr (e as IApp (e1, e2)) = (case itype_of_iexpr e1
   337   | itype_of_iexpr (e as IApp (e1, e2)) = (case itype_of_iexpr e1
   374       of (IFun (ty2, ty')) =>
   338       of (IFun (ty2, ty')) =>
   375             if ty2 = itype_of_iexpr e2
   339             if ty2 = itype_of_iexpr e2
   376             then ty'
   340             then ty'
   377             else error ("inconsistent application: in " ^ Pretty.output (pretty_iexpr e)
   341             else error ("inconsistent application: in " ^ Pretty.output (pretty_iexpr e)
   378               ^ ", " ^ (Pretty.output o pretty_itype) ty2
   342               ^ ", " ^ (Pretty.output o pretty_itype) ty2
   379               ^ " vs. " ^ (Pretty.output o pretty_itype o itype_of_iexpr) e2)
   343               ^ " vs. " ^ (Pretty.output o pretty_itype o itype_of_iexpr) e2)
   380        | _ => error ("expression is not a function: " ^ Pretty.output (pretty_iexpr e1)))
   344        | _ => error ("expression is not a function: " ^ Pretty.output (pretty_iexpr e1)))
   381   | itype_of_iexpr (IAbs ((_, ty1), e2)) = ty1 `-> itype_of_iexpr e2
   345   | itype_of_iexpr (IAbs (e1, e2)) = itype_of_iexpr e1 `-> itype_of_iexpr e2
   382   | itype_of_iexpr (ICase ((_, [(_, e)]))) = itype_of_iexpr e;
   346   | itype_of_iexpr (ICase ((_, [(_, e)]))) = itype_of_iexpr e;
   383 
   347 
   384 fun ensure_pat (e as IConst (_, [])) = e
   348 fun ensure_pat (e as IConst (_, [])) = e
   385   | ensure_pat (e as IVarE _) = e
   349   | ensure_pat (e as IVarE _) = e
   386   | ensure_pat (e as IApp (e1, e2)) =
   350   | ensure_pat (e as IApp (e1, e2)) =
   389       error ("illegal expression for pattern: " ^ (Pretty.output o pretty_iexpr) e);
   353       error ("illegal expression for pattern: " ^ (Pretty.output o pretty_iexpr) e);
   390 
   354 
   391 fun type_vnames ty = 
   355 fun type_vnames ty = 
   392   let
   356   let
   393     fun extr (IVarT (v, _)) =
   357     fun extr (IVarT (v, _)) =
   394       insert (op =) v
   358           insert (op =) v
       
   359       | extr _ = I;
   395   in fold_atype extr ty end;
   360   in fold_atype extr ty end;
   396 
   361 
   397 fun expr_names e =
   362 fun expr_names e =
   398   let
   363   let
   399     fun extr (IConst ((c, _), _)) =
   364     fun extr (IConst ((c, _), _)) =
   421 
   386 
   422 datatype def =
   387 datatype def =
   423     Undef
   388     Undef
   424   | Prim of (string * prim list) list
   389   | Prim of (string * prim list) list
   425   | Fun of funn
   390   | Fun of funn
   426   | Typesyn of (vname * string list) list * itype
   391   | Typesyn of (vname * sort) list * itype
   427   | Datatype of ((vname * string list) list * (string * itype list) list)
   392   | Datatype of (vname * sort) list * (string * itype list) list
   428       * string list
       
   429   | Datatypecons of string
   393   | Datatypecons of string
   430   | Class of (class list * (vname * (string * (ClassPackage.sortcontext * itype)) list))
   394   | Class of class list * (vname * (string * (ClassPackage.sortcontext * itype)) list)
   431       * string list
       
   432   | Classmember of class
   395   | Classmember of class
   433   | Classinst of ((class * (string * (vname * sort) list))
   396   | Classinst of ((class * (string * (vname * sort) list))
   434         * (class * classlookup list list) list)
   397         * (class * classlookup list list) list)
   435       * (string * funn) list;
   398       * (string * funn) list;
   436 
   399 
   466       Pretty.block [
   429       Pretty.block [
   467         Pretty.list "(" ")" (map (pretty_itype o IVarT) vs),
   430         Pretty.list "(" ")" (map (pretty_itype o IVarT) vs),
   468         Pretty.str " |=> ",
   431         Pretty.str " |=> ",
   469         pretty_itype ty
   432         pretty_itype ty
   470       ]
   433       ]
   471   | pretty_def (Datatype ((vs, cs), insts)) =
   434   | pretty_def (Datatype (vs, cs)) =
   472       Pretty.block [
   435       Pretty.block [
   473         Pretty.list "(" ")" (map (pretty_itype o IVarT) vs),
   436         Pretty.list "(" ")" (map (pretty_itype o IVarT) vs),
   474         Pretty.str " |=> ",
   437         Pretty.str " |=> ",
   475         Pretty.enum " |" "" ""
   438         Pretty.enum " |" "" ""
   476           (map (fn (c, tys) => (Pretty.block o Pretty.breaks)
   439           (map (fn (c, tys) => (Pretty.block o Pretty.breaks)
   477             (Pretty.str c :: map pretty_itype tys)) cs),
   440             (Pretty.str c :: map pretty_itype tys)) cs)
   478         Pretty.str ", instances ",
       
   479         Pretty.enum "," "[" "]" (map Pretty.str insts)
       
   480       ]
   441       ]
   481   | pretty_def (Datatypecons dtname) =
   442   | pretty_def (Datatypecons dtname) =
   482       Pretty.str ("cons " ^ dtname)
   443       Pretty.str ("cons " ^ dtname)
   483   | pretty_def (Class ((supcls, (v, mems)), insts)) =
   444   | pretty_def (Class (supcls, (v, mems))) =
   484       Pretty.block [
   445       Pretty.block [
   485         Pretty.str ("class var " ^ v ^ "extending "),
   446         Pretty.str ("class var " ^ v ^ "extending "),
   486         Pretty.enum "," "[" "]" (map Pretty.str supcls),
   447         Pretty.enum "," "[" "]" (map Pretty.str supcls),
   487         Pretty.str " with ",
   448         Pretty.str " with ",
   488         Pretty.enum "," "[" "]"
   449         Pretty.enum "," "[" "]"
   489           (map (fn (m, (_, ty)) => Pretty.block
   450           (map (fn (m, (_, ty)) => Pretty.block
   490             [Pretty.str (m ^ "::"), pretty_itype ty]) mems),
   451             [Pretty.str (m ^ "::"), pretty_itype ty]) mems)
   491         Pretty.str " instances ",
       
   492         Pretty.enum "," "[" "]" (map Pretty.str insts)
       
   493       ]
   452       ]
   494   | pretty_def (Classmember clsname) =
   453   | pretty_def (Classmember clsname) =
   495       Pretty.block [
   454       Pretty.block [
   496         Pretty.str "class member belonging to ",
   455         Pretty.str "class member belonging to ",
   497         Pretty.str clsname
   456         Pretty.str clsname
   607       val (ms, (r1, r2)) = get_prefix (op =) (m1, m2);
   566       val (ms, (r1, r2)) = get_prefix (op =) (m1, m2);
   608       val (ms, (s1::r1, s2::r2)) = get_prefix (op =) (m1, m2);
   567       val (ms, (s1::r1, s2::r2)) = get_prefix (op =) (m1, m2);
   609       val add_edge =
   568       val add_edge =
   610         if null r1 andalso null r2
   569         if null r1 andalso null r2
   611         then Graph.add_edge
   570         then Graph.add_edge
   612         else Graph.add_edge_acyclic
   571         else fn edge => (Graph.add_edge_acyclic edge
       
   572           handle Graph.CYCLES _ => error ("adding dependency "
       
   573             ^ quote name1 ^ " -> " ^ quote name2 ^ " would result in module dependency cycle"))
   613       fun add [] node =
   574       fun add [] node =
   614             node
   575             node
   615             |> add_edge (s1, s2)
   576             |> add_edge (s1, s2)
   616         | add (m::ms) node =
   577         | add (m::ms) node =
   617             node
   578             node
   774       d
   735       d
   775   | check_prep_def modl (Fun (eqs, d)) =
   736   | check_prep_def modl (Fun (eqs, d)) =
   776       Fun (check_funeqs eqs, d)
   737       Fun (check_funeqs eqs, d)
   777   | check_prep_def modl (d as Typesyn _) =
   738   | check_prep_def modl (d as Typesyn _) =
   778       d
   739       d
   779   | check_prep_def modl (d as Datatype (_, insts)) =
   740   | check_prep_def modl (d as Datatype _) =
   780       if null insts
   741       d
   781       then d
       
   782       else error "attempted to add datatype with bare instances"
       
   783   | check_prep_def modl (Datatypecons dtco) =
   742   | check_prep_def modl (Datatypecons dtco) =
   784       error "attempted to add bare datatype constructor"
   743       error "attempted to add bare datatype constructor"
   785   | check_prep_def modl (d as Class ((_, (v, membrs)), insts)) =
   744   | check_prep_def modl (d as Class _) =
   786       if null insts
   745       d
   787       then
       
   788         if member (op =) (map fst (Library.flat (map (fst o snd) membrs))) v
       
   789         then error "incorrectly abstracted class type variable"
       
   790         else d
       
   791       else error "attempted to add class with bare instances"
       
   792   | check_prep_def modl (Classmember _) =
   746   | check_prep_def modl (Classmember _) =
   793       error "attempted to add bare class member"
   747       error "attempted to add bare class member"
   794   | check_prep_def modl (Classinst ((d as ((class, (tyco, arity)), _), memdefs))) =
   748   | check_prep_def modl (Classinst ((d as ((class, (tyco, arity)), _), memdefs))) =
   795       let
   749       let
   796         val Class ((_, (v, membrs)), _) = get_def modl class;
   750         val Class (_, (v, membrs)) = get_def modl class;
   797         val _ = if length memdefs > length memdefs
   751         val _ = if length memdefs > length memdefs
   798           then error "too many member definitions given"
   752           then error "too many member definitions given"
   799           else ();
   753           else ();
   800         fun instant (w, ty) (var as (v, _)) =
   754         fun instant (w, ty) (var as (v, _)) =
   801           if v = w then ty else IVarT var;
   755           if v = w then ty else IVarT var;
   806                 if eq_itype (instant_itype (instant (v, tyco `%% map IVarT arity)) ty, ty')
   760                 if eq_itype (instant_itype (instant (v, tyco `%% map IVarT arity)) ty, ty')
   807                 then (m, (check_funeqs eqs, (ctxt', ty')))
   761                 then (m, (check_funeqs eqs, (ctxt', ty')))
   808                 else error ("inconsistent type for member definition " ^ quote m)
   762                 else error ("inconsistent type for member definition " ^ quote m)
   809       in Classinst (d, map mk_memdef membrs) end;
   763       in Classinst (d, map mk_memdef membrs) end;
   810 
   764 
   811 fun postprocess_def (name, Datatype ((_, constrs), _)) =
   765 fun postprocess_def (name, Datatype (_, constrs)) =
   812       (check_samemodule (name :: map fst constrs);
   766       (check_samemodule (name :: map fst constrs);
   813       fold (fn (co, _) =>
   767       fold (fn (co, _) =>
   814         ensure_def (co, Datatypecons name)
   768         ensure_def (co, Datatypecons name)
   815         #> add_dep (co, name)
   769         #> add_dep (co, name)
   816         #> add_dep (name, co)
   770         #> add_dep (name, co)
   817       ) constrs
   771       ) constrs
   818       )
   772       )
   819   | postprocess_def (name, Class ((_, (_, membrs)), _)) =
   773   | postprocess_def (name, Class (_, (_, membrs))) =
   820       (check_samemodule (name :: map fst membrs);
   774       (check_samemodule (name :: map fst membrs);
   821       fold (fn (m, _) =>
   775       fold (fn (m, _) =>
   822         ensure_def (m, Classmember name)
   776         ensure_def (m, Classmember name)
   823         #> add_dep (m, name)
   777         #> add_dep (m, name)
   824         #> add_dep (name, m)
   778         #> add_dep (name, m)
   825       ) membrs
   779       ) membrs
   826       )
   780       )
   827   | postprocess_def (name, Classinst (((class, (tyco, _)), _), _)) =
       
   828       map_def class (fn Datatype (d, insts) => Datatype (d, name::insts)
       
   829                       | d => d)
       
   830       #> map_def class (fn Class (d, insts) => Class (d, name::insts))
       
   831   | postprocess_def _ =
   781   | postprocess_def _ =
   832       I;
   782       I;
   833 
   783 
   834 fun succeed some (_, modl) = (Succeed some, modl);
   784 fun succeed some (_, modl) = (Succeed some, modl);
   835 fun fail msg (_, modl) = (Fail ([msg], NONE), modl);
   785 fun fail msg (_, modl) = (Fail ([msg], NONE), modl);
   940             val tys =
   890             val tys =
   941               (fst o unfold_fun) ty
   891               (fst o unfold_fun) ty
   942               |> curry Library.drop (length es)
   892               |> curry Library.drop (length es)
   943               |> curry Library.take add_n
   893               |> curry Library.take add_n
   944             val add_vars =
   894             val add_vars =
   945               Term.invent_names (fold expr_names es []) "x" add_n ~~ tys;
   895               map2 (curry IVarE) (Term.invent_names (fold expr_names es []) "x" add_n) tys;
   946           in
   896           in
   947             add_vars `|--> IConst ((f, ty), ls) `$$ es `$$ map IVarE add_vars
   897             add_vars `|--> IConst ((f, ty), ls) `$$ es `$$ add_vars
   948           end
   898           end
   949        | NONE => map_iexpr eta e;
   899        | NONE => map_iexpr eta e;
   950   in (map_defs o map_def_fun o map_def_fun_expr) eta end;
   900   in (map_defs o map_def_fun o map_def_fun_expr) eta end;
   951 
   901 
   952 val eta_expand_poly =
   902 val eta_expand_poly =
   954     fun eta (funn as ([([], e)], cty as (sortctxt, (ty as IFun (ty1, ty2))))) =
   904     fun eta (funn as ([([], e)], cty as (sortctxt, (ty as IFun (ty1, ty2))))) =
   955           if (not o null) sortctxt
   905           if (not o null) sortctxt
   956             orelse null (type_vnames ty [])
   906             orelse null (type_vnames ty [])
   957           then funn
   907           then funn
   958           else
   908           else
   959             let
   909             (case unfold_abs e
   960               val add_var = (hd (Term.invent_names (expr_names e []) "x" 1), ty1)
   910              of ([], e) =>
   961             in (([([IVarE add_var], add_var `|-> e)], cty)) end
   911                   let
       
   912                     val add_var = IVarE (hd (Term.invent_names (expr_names e []) "x" 1), ty1)
       
   913                   in (([([add_var], add_var `|-> e)], cty)) end
       
   914               | eq => 
       
   915                   (([eq], cty)))
   962       | eta funn = funn;
   916       | eta funn = funn;
   963   in (map_defs o map_def_fun) eta end;
   917   in (map_defs o map_def_fun) eta end;
   964 
   918 
   965 val unclash_vars_tvars = 
   919 val unclash_vars_tvars = 
   966   let
   920   let
  1073 (* serialization *)
  1027 (* serialization *)
  1074 
  1028 
  1075 fun serialize seri_defs seri_module validate postprocess nsp_conn name_root module =
  1029 fun serialize seri_defs seri_module validate postprocess nsp_conn name_root module =
  1076   let
  1030   let
  1077     val resolver = mk_deresolver module nsp_conn postprocess validate;
  1031     val resolver = mk_deresolver module nsp_conn postprocess validate;
       
  1032     fun sresolver s = (resolver o NameSpace.unpack) s
  1078     fun mk_name prfx name =
  1033     fun mk_name prfx name =
  1079       let
  1034       let
  1080         val name_qual = NameSpace.pack (prfx @ [name])
  1035         val name_qual = NameSpace.pack (prfx @ [name])
  1081       in (name_qual, resolver prfx name_qual) end;
  1036       in (name_qual, resolver prfx name_qual) end;
  1082     fun mk_contents prfx module =
  1037     fun mk_contents prfx module =
  1084         ((map (AList.make (Graph.get_node module)) o rev o Graph.strong_conn) module)
  1039         ((map (AList.make (Graph.get_node module)) o rev o Graph.strong_conn) module)
  1085     and seri prfx ([(name, Module modl)]) =
  1040     and seri prfx ([(name, Module modl)]) =
  1086           seri_module (map (resolver []) (imports_of module (prfx @ [name])))
  1041           seri_module (map (resolver []) (imports_of module (prfx @ [name])))
  1087             (mk_name prfx name, mk_contents (prfx @ [name]) modl)
  1042             (mk_name prfx name, mk_contents (prfx @ [name]) modl)
  1088       | seri prfx ds =
  1043       | seri prfx ds =
  1089           seri_defs (resolver prfx)
  1044           seri_defs sresolver (NameSpace.pack prfx)
  1090             (map (fn (name, Def def) => (fst (mk_name prfx name), def)) ds)
  1045             (map (fn (name, Def def) => (fst (mk_name prfx name), def)) ds)
  1091   in
  1046   in
  1092     seri_module (map (resolver []) (Graph.strong_conn module |> List.concat |> rev))
  1047     seri_module (map (resolver []) (Graph.strong_conn module |> List.concat |> rev))
  1093       (("", name_root), (mk_contents [] module))
  1048       (("", name_root), (mk_contents [] module))
  1094   end;
  1049   end;