src/Pure/Tools/codegen_thingol.ML
changeset 18963 3adfc9dfb30a
parent 18919 340ffeaaaede
child 19025 596fb1eb7856
equal deleted inserted replaced
18962:d6ecc5828b14 18963:3adfc9dfb30a
    45   val `|--> : (vname * itype) list * iexpr -> iexpr;
    45   val `|--> : (vname * itype) 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 of string;
    50     | Name;
    51   datatype def =
    51   datatype def =
    52       Undef
    52       Undef
    53     | Prim of (string * Pretty.T option) 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 * string list) list * itype
    56     | Datatype of ((vname * string list) list * (string * itype list) list)
    56     | Datatype of ((vname * string list) list * (string * itype list) list)
    57         * string list
    57         * string list
    58     | Datatypecons of string
    58     | Datatypecons of string
    68   type gen_defgen = string -> transact -> def transact_fin;
    68   type gen_defgen = string -> transact -> def transact_fin;
    69   val pretty_def: def -> Pretty.T;
    69   val pretty_def: def -> Pretty.T;
    70   val pretty_module: module -> Pretty.T; 
    70   val pretty_module: module -> Pretty.T; 
    71   val pretty_deps: module -> Pretty.T;
    71   val pretty_deps: module -> Pretty.T;
    72   val empty_module: module;
    72   val empty_module: module;
    73   val add_prim: string -> string list -> (string * Pretty.T) -> module -> module;
    73   val add_prim: string -> (string * prim list) -> module -> module;
    74   val ensure_prim: string -> string -> module -> module;
    74   val ensure_prim: string -> string -> module -> module;
    75   val get_def: module -> string -> def;
    75   val get_def: module -> string -> def;
    76   val merge_module: module * module -> module;
    76   val merge_module: module * module -> module;
    77   val partof: string list -> module -> module;
    77   val partof: string list -> module -> module;
    78   val has_nsp: string -> string -> bool;
    78   val has_nsp: string -> string -> bool;
    79   val succeed: 'a -> transact -> 'a transact_fin;
    79   val succeed: 'a -> transact -> 'a transact_fin;
    80   val fail: string -> transact -> 'a transact_fin;
    80   val fail: string -> transact -> 'a transact_fin;
    81   val gen_ensure_def: (string * gen_defgen) list -> string
    81   val gen_ensure_def: (string * gen_defgen) list -> string
    82     -> string -> transact -> transact;
    82     -> string -> transact -> transact;
    83   val start_transact: (transact -> 'a * transact) -> module -> 'a * module;
    83   val start_transact: string option -> (transact -> 'a * transact) -> module -> 'a * module;
    84 
    84 
    85   val eta_expand: (string -> int) -> module -> module;
    85   val eta_expand: (string -> int) -> module -> module;
    86   val eta_expand_poly: module -> module;
    86   val eta_expand_poly: module -> module;
    87   val unclash_vars: module -> module;
    87   val unclash_vars_tvars: module -> module;
    88 
    88 
    89   val debug_level: int ref;
    89   val debug_level: int ref;
    90   val debug: int -> ('a -> string) -> 'a -> 'a;
    90   val debug: int -> ('a -> string) -> 'a -> 'a;
    91   val soft_exc: bool ref;
    91   val soft_exc: bool ref;
    92 
    92 
   415 
   415 
   416 type funn = (iexpr list * iexpr) list * (ClassPackage.sortcontext * itype);
   416 type funn = (iexpr list * iexpr) list * (ClassPackage.sortcontext * itype);
   417 
   417 
   418 datatype prim =
   418 datatype prim =
   419     Pretty of Pretty.T
   419     Pretty of Pretty.T
   420   | Name of string;
   420   | Name;
   421 
   421 
   422 datatype def =
   422 datatype def =
   423     Undef
   423     Undef
   424   | Prim of (string * Pretty.T option) list
   424   | Prim of (string * prim list) list
   425   | Fun of funn
   425   | Fun of funn
   426   | Typesyn of (vname * string list) list * itype
   426   | Typesyn of (vname * string list) list * itype
   427   | Datatype of ((vname * string list) list * (string * itype list) list)
   427   | Datatype of ((vname * string list) list * (string * itype list) list)
   428       * string list
   428       * string list
   429   | Datatypecons of string
   429   | Datatypecons of string
   658         | SOME Undef => map_def name (K def) module
   658         | SOME Undef => map_def name (K def) module
   659         | SOME def' => if eq_def (def, def')
   659         | SOME def' => if eq_def (def, def')
   660             then module
   660             then module
   661             else error ("tried to overwrite definition " ^ name));
   661             else error ("tried to overwrite definition " ^ name));
   662 
   662 
   663 fun add_prim name deps (target, primdef) =
   663 fun add_prim name (target, primdef as _::_) =
   664   let
   664   let
   665     val (modl, base) = dest_name name;
   665     val (modl, base) = dest_name name;
   666     fun add [] module =
   666     fun add [] module =
   667           (case try (Graph.get_node module) base
   667           (case try (Graph.get_node module) base
   668            of NONE =>
   668            of NONE =>
   669                 module
   669                 module
   670                 |> Graph.new_node (base, (Def o Prim) [(target, SOME primdef)])
   670                 |> Graph.new_node (base, (Def o Prim) [(target, primdef)])
   671             | SOME (Def (Prim prim)) =>
   671             | SOME (Def (Prim prim)) =>
   672                 if AList.defined (op =) prim target
   672                 if AList.defined (op =) prim target
   673                 then error ("already primitive definition (" ^ target
   673                 then error ("already primitive definition (" ^ target
   674                   ^ ") present for " ^ name)
   674                   ^ ") present for " ^ name)
   675                 else
   675                 else
   676                   module
   676                   module
   677                   |> Graph.map_node base ((K o Def o Prim) (AList.update (op =)
   677                   |> Graph.map_node base ((K o Def o Prim) (AList.update (op =)
   678                        (target, SOME primdef) prim))
   678                        (target, primdef) prim))
   679             | _ => error ("already non-primitive definition present for " ^ name))
   679             | _ => error ("already non-primitive definition present for " ^ name))
   680       | add (m::ms) module =
   680       | add (m::ms) module =
   681           module
   681           module
   682           |> Graph.default_node (m, Module empty_module)
   682           |> Graph.default_node (m, Module empty_module)
   683           |> Graph.map_node m (Module o add ms o dest_modl)
   683           |> Graph.map_node m (Module o add ms o dest_modl)
   684   in
   684   in add modl end;
   685     add modl
       
   686     #> fold (curry add_dep name) deps
       
   687   end;
       
   688 
   685 
   689 fun ensure_prim name target =
   686 fun ensure_prim name target =
   690   let
   687   let
   691     val (modl, base) = dest_name name;
   688     val (modl, base) = dest_name name;
   692     fun ensure [] module =
   689     fun ensure [] module =
   693           (case try (Graph.get_node module) base
   690           (case try (Graph.get_node module) base
   694            of NONE =>
   691            of NONE =>
   695                 module
   692                 module
   696                 |> Graph.new_node (base, (Def o Prim) [(target, NONE)])
   693                 |> Graph.new_node (base, (Def o Prim) [(target, [])])
   697             | SOME (Def (Prim prim)) =>
   694             | SOME (Def (Prim prim)) =>
   698                 module
   695                 module
   699                 |> Graph.map_node base ((K o Def o Prim) (AList.default (op =)
   696                 |> Graph.map_node base ((K o Def o Prim) (AList.default (op =)
   700                      (target, NONE) prim))
   697                      (target, []) prim))
   701             | _ => module)
   698             | _ => module)
   702       | ensure (m::ms) module =
   699       | ensure (m::ms) module =
   703           module
   700           module
   704           |> Graph.default_node (m, Module empty_module)
   701           |> Graph.default_node (m, Module empty_module)
   705           |> Graph.map_node m (Module o ensure ms o dest_modl)
   702           |> Graph.map_node m (Module o ensure ms o dest_modl)
   906           #> debug 10 (fn _ => "adding done")
   903           #> debug 10 (fn _ => "adding done")
   907        ))
   904        ))
   908     |> pair dep
   905     |> pair dep
   909   end;
   906   end;
   910 
   907 
   911 fun start_transact f modl =
   908 fun start_transact init f modl =
   912   let
   909   let
   913     fun handle_fail f modl =
   910     fun handle_fail f x =
   914       (((NONE, modl) |> f)
   911       (f x
   915       handle FAIL (msgs, NONE) =>
   912       handle FAIL (msgs, NONE) =>
   916         (error o cat_lines) ("code generation failed, while:" :: msgs))
   913         (error o cat_lines) ("code generation failed, while:" :: msgs))
   917       handle FAIL (msgs, SOME e) =>
   914       handle FAIL (msgs, SOME e) =>
   918         ((writeln o cat_lines) ("code generation failed, while:" :: msgs); raise e);
   915         ((writeln o cat_lines) ("code generation failed, while:" :: msgs); raise e);
   919   in
   916   in
   920     modl
   917     (init, modl)
   921     |> handle_fail f
   918     |> handle_fail f
   922     |-> (fn x => fn (_, module) => (x, module))
   919     |-> (fn x => fn (_, module) => (x, module))
   923   end;
   920   end;
   924 
   921 
   925 
   922 
   964               val add_var = (hd (Term.invent_names (expr_names e []) "x" 1), ty1)
   961               val add_var = (hd (Term.invent_names (expr_names e []) "x" 1), ty1)
   965             in (([([IVarE add_var], add_var `|-> e)], cty)) end
   962             in (([([IVarE add_var], add_var `|-> e)], cty)) end
   966       | eta funn = funn;
   963       | eta funn = funn;
   967   in (map_defs o map_def_fun) eta end;
   964   in (map_defs o map_def_fun) eta end;
   968 
   965 
   969 val unclash_vars = 
   966 val unclash_vars_tvars = 
   970   let
   967   let
   971     fun unclash (eqs, (sortctxt, ty)) =
   968     fun unclash (eqs, (sortctxt, ty)) =
   972       let
   969       let
   973         val used_expr =
   970         val used_expr =
   974           fold (fn (pats, rhs) => fold expr_names pats #> expr_names rhs) eqs [];
   971           fold (fn (pats, rhs) => fold expr_names pats #> expr_names rhs) eqs [];
   984         (map rename_eq eqs, (map rename sortctxt, rename_typ ty))
   981         (map rename_eq eqs, (map rename sortctxt, rename_typ ty))
   985       end;
   982       end;
   986   in (map_defs o map_def_fun) unclash end;
   983   in (map_defs o map_def_fun) unclash end;
   987 
   984 
   988 
   985 
   989 
       
   990 (** generic serialization **)
   986 (** generic serialization **)
   991 
   987 
   992 (* resolving *)
   988 (* resolving *)
   993 
   989 
   994 structure NameMangler = NameManglerFun (
   990 structure NameMangler = NameManglerFun (
   995   type ctxt = (string * string -> string) * (string -> string option);
   991   type ctxt = (string * string -> string) * (string -> string option);
   996   type src = string * string;
   992   type src = string * string;
   997   val ord = prod_ord string_ord string_ord;
   993   val ord = prod_ord string_ord string_ord;
   998   fun mk (preprocess, validate) ((shallow, name), 0) =
   994   fun mk (postprocess, validate) ((shallow, name), 0) =
   999         (case validate (preprocess (shallow, name))
   995         let
  1000          of NONE => name
   996           val name' = postprocess (shallow, name);
  1001           | _ => mk (preprocess, validate) ((shallow, name), 1))
   997         in case validate name'
  1002     | mk (preprocess, validate) (("", name), i) =
   998          of NONE => name'
  1003         preprocess ("", name ^ "_" ^ string_of_int (i+1))
   999           | _ => mk (postprocess, validate) ((shallow, name), 1)
       
  1000         end
       
  1001     | mk (postprocess, validate) (("", name), i) =
       
  1002         postprocess ("", name ^ "_" ^ string_of_int (i+1))
  1004         |> perhaps validate
  1003         |> perhaps validate
  1005     | mk (preprocess, validate) ((shallow, name), i) =
  1004     | mk (postprocess, validate) ((shallow, name), i) =
  1006         preprocess (shallow, shallow ^ "_" ^ name ^ "_" ^ string_of_int (i+1))
  1005         postprocess (shallow, shallow ^ "_" ^ name ^ "_" ^ string_of_int (i+1))
  1007         |> perhaps validate;
  1006         |> perhaps validate;
  1008   fun is_valid _ _ = true;
  1007   fun is_valid _ _ = true;
  1009   fun maybe_unique _ _ = NONE;
  1008   fun maybe_unique _ _ = NONE;
  1010   fun re_mangle _ dst = error ("no such definition name: " ^ quote dst);
  1009   fun re_mangle _ dst = error ("no such definition name: " ^ quote dst);
  1011 );
  1010 );
  1012 
  1011 
  1013 fun mk_deresolver module nsp_conn preprocess validate =
  1012 fun mk_deresolver module nsp_conn postprocess validate =
  1014   let
  1013   let
  1015     datatype tabnode = N of string * tabnode Symtab.table option;
  1014     datatype tabnode = N of string * tabnode Symtab.table option;
  1016     fun mk module manglers tab =
  1015     fun mk module manglers tab =
  1017       let
  1016       let
  1018         fun mk_name name =
  1017         fun mk_name name =
  1022         fun in_conn (shallow, conn) =
  1021         fun in_conn (shallow, conn) =
  1023           member (op = : string * string -> bool) conn shallow;
  1022           member (op = : string * string -> bool) conn shallow;
  1024         fun add_name name =
  1023         fun add_name name =
  1025           let
  1024           let
  1026             val n as (shallow, _) = mk_name name;
  1025             val n as (shallow, _) = mk_name name;
  1027             fun diag (nm as (name, n')) = (writeln ("resolving " ^ quote name ^ " to " ^ quote n'); nm);
       
  1028           in
  1026           in
  1029             AList.map_entry_yield in_conn shallow (
  1027             AList.map_entry_yield in_conn shallow (
  1030               NameMangler.declare (preprocess, validate) n
  1028               NameMangler.declare (postprocess, validate) n
  1031               #-> (fn n' => pair (name, n'))
  1029               #-> (fn n' => pair (name, n'))
  1032             ) #> apfst the #> apfst diag
  1030             ) #> apfst the
  1033           end;
  1031           end;
  1034         val (renamings, manglers') =
  1032         val (renamings, manglers') =
  1035           fold_map add_name (Graph.keys module) manglers;
  1033           fold_map add_name (Graph.keys module) manglers;
  1036         fun extend_tab (n, n') =
  1034         fun extend_tab (n, n') =
  1037           if (length o NameSpace.unpack) n = 1
  1035           if (length o NameSpace.unpack) n = 1
  1073   in deresolv (mk module (AList.make (K NameMangler.empty) nsp_conn) Symtab.empty) end;
  1071   in deresolv (mk module (AList.make (K NameMangler.empty) nsp_conn) Symtab.empty) end;
  1074 
  1072 
  1075 
  1073 
  1076 (* serialization *)
  1074 (* serialization *)
  1077 
  1075 
  1078 fun serialize seri_defs seri_module validate preprocess nsp_conn name_root module =
  1076 fun serialize seri_defs seri_module validate postprocess nsp_conn name_root module =
  1079   let
  1077   let
  1080     val resolver = mk_deresolver module nsp_conn preprocess validate;
  1078     val resolver = mk_deresolver module nsp_conn postprocess validate;
  1081     fun mk_name prfx name =
  1079     fun mk_name prfx name =
  1082       let
  1080       let
  1083         val name_qual = NameSpace.pack (prfx @ [name])
  1081         val name_qual = NameSpace.pack (prfx @ [name])
  1084       in (name_qual, resolver prfx name_qual) end;
  1082       in (name_qual, resolver prfx name_qual) end;
  1085     fun mk_contents prfx module =
  1083     fun mk_contents prfx module =