src/Pure/codegen.ML
author wenzelm
Sat Nov 24 16:55:56 2001 +0100 (2001-11-24)
changeset 12284 131e743d168a
parent 12123 739eba13e2cd
child 12311 ce5f9e61c037
permissions -rw-r--r--
added gen_merge_lists(') and merge_lists(');
removed obsolete generic_extend, generic_merge, extend_list;
     1 (*  Title:      Pure/codegen.ML
     2     ID:         $Id$
     3     Author:     Stefan Berghofer, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Generic code generator.
     7 *)
     8 
     9 signature CODEGEN =
    10 sig
    11   val quiet_mode : bool ref
    12   val message : string -> unit
    13 
    14   datatype mixfix =
    15       Arg
    16     | Ignore
    17     | Pretty of Pretty.T
    18     | Term of term;
    19 
    20   val add_codegen: string ->
    21     (theory -> (exn option * string) Graph.T -> string -> bool -> term ->
    22     ((exn option * string) Graph.T * Pretty.T) option) -> theory -> theory
    23   val print_codegens: theory -> unit
    24   val generate_code: theory -> (string * string) list -> string
    25   val generate_code_i: theory -> (string * term) list -> string
    26   val assoc_consts: (xstring * string option * mixfix list) list -> theory -> theory
    27   val assoc_consts_i: (xstring * typ option * mixfix list) list -> theory -> theory
    28   val assoc_types: (xstring * string) list -> theory -> theory
    29   val get_assoc_code: theory -> string -> typ -> mixfix list option
    30   val get_assoc_types: theory -> (string * string) list
    31   val invoke_codegen: theory -> (exn option * string) Graph.T ->
    32     string -> bool -> term -> (exn option * string) Graph.T * Pretty.T
    33   val mk_const_id: Sign.sg -> string -> string
    34   val mk_type_id: Sign.sg -> string -> string
    35   val rename_term: term -> term
    36   val get_defn: theory -> string -> typ -> ((term list * term) * int option) option
    37   val is_instance: theory -> typ -> typ -> bool
    38   val parens: Pretty.T -> Pretty.T
    39   val mk_app: bool -> Pretty.T -> Pretty.T list -> Pretty.T
    40   val eta_expand: term -> term list -> int -> term
    41   val parsers: OuterSyntax.parser list
    42   val setup: (theory -> theory) list
    43 end;
    44 
    45 structure Codegen : CODEGEN =
    46 struct
    47 
    48 val quiet_mode = ref true;
    49 fun message s = if !quiet_mode then () else writeln s;
    50 
    51 (**** Mixfix syntax ****)
    52 
    53 datatype mixfix =
    54     Arg
    55   | Ignore
    56   | Pretty of Pretty.T
    57   | Term of term;
    58 
    59 fun is_arg Arg = true
    60   | is_arg Ignore = true
    61   | is_arg _ = false;
    62 
    63 fun terms_of [] = []
    64   | terms_of (Term t :: ms) = t :: terms_of ms
    65   | terms_of (_ :: ms) = terms_of ms;
    66 
    67 val num_args = length o filter is_arg;
    68 
    69 
    70 (**** theory data ****)
    71 
    72 (* data kind 'Pure/codegen' *)
    73 
    74 structure CodegenArgs =
    75 struct
    76   val name = "Pure/codegen";
    77   type T =
    78     {codegens : (string * (theory -> (exn option * string) Graph.T -> string ->
    79        bool -> term -> ((exn option * string) Graph.T * Pretty.T) option)) list,
    80      consts : ((string * typ) * mixfix list) list,
    81      types : (string * string) list};
    82 
    83   val empty = {codegens = [], consts = [], types = []};
    84   val copy = I;
    85   val finish = I;
    86   val prep_ext = I;
    87 
    88   fun merge ({codegens = codegens1, consts = consts1, types = types1},
    89              {codegens = codegens2, consts = consts2, types = types2}) =
    90     {codegens = rev (merge_alists (rev codegens1) (rev codegens2)),
    91      consts   = merge_alists consts1 consts2,
    92      types    = merge_alists types1 types2};
    93 
    94   fun print sg (cs:T) = Pretty.writeln
    95     (Pretty.strs ("code generators:" :: map fst (#codegens cs)));
    96 end;
    97 
    98 structure CodegenData = TheoryDataFun(CodegenArgs);
    99 val print_codegens = CodegenData.print;
   100 
   101 
   102 (**** add new code generator to theory ****)
   103 
   104 fun add_codegen name f thy =
   105   let val {codegens, consts, types} = CodegenData.get thy
   106   in (case assoc (codegens, name) of
   107       None => CodegenData.put {codegens = (name, f)::codegens,
   108         consts = consts, types = types} thy
   109     | Some _ => error ("Code generator " ^ name ^ " already declared"))
   110   end;
   111 
   112 
   113 (**** associate constants with target language code ****)
   114 
   115 fun gen_assoc_consts prep_type xs thy = foldl (fn (thy, (s, tyopt, syn)) =>
   116   let
   117     val sg = sign_of thy;
   118     val {codegens, consts, types} = CodegenData.get thy;
   119     val cname = Sign.intern_const sg s;
   120   in
   121     (case Sign.const_type sg cname of
   122        Some T =>
   123          let val T' = (case tyopt of
   124                 None => T
   125               | Some ty =>
   126                   let val U = prep_type sg ty
   127                   in if Type.typ_instance (Sign.tsig_of sg, U, T) then U
   128                     else error ("Illegal type constraint for constant " ^ cname)
   129                   end)
   130          in (case assoc (consts, (cname, T')) of
   131              None => CodegenData.put {codegens = codegens,
   132                consts = ((cname, T'), syn) :: consts, types = types} thy
   133            | Some _ => error ("Constant " ^ cname ^ " already associated with code"))
   134          end
   135      | _ => error ("Not a constant: " ^ s))
   136   end) (thy, xs);
   137 
   138 val assoc_consts_i = gen_assoc_consts (K I);
   139 val assoc_consts = gen_assoc_consts (fn sg => typ_of o read_ctyp sg);
   140 
   141 (**** associate types with target language types ****)
   142 
   143 fun assoc_types xs thy = foldl (fn (thy, (s, syn)) =>
   144   let
   145     val {codegens, consts, types} = CodegenData.get thy;
   146     val tc = Sign.intern_tycon (sign_of thy) s
   147   in
   148     (case assoc (types, tc) of
   149        None => CodegenData.put {codegens = codegens, consts = consts,
   150          types = (tc, syn) :: types} thy
   151      | Some _ => error ("Type " ^ tc ^ " already associated with code"))
   152   end) (thy, xs);
   153 
   154 fun get_assoc_types thy = #types (CodegenData.get thy);
   155 
   156 
   157 (**** retrieve definition of constant ****)
   158 
   159 fun is_instance thy T1 T2 =
   160   Type.typ_instance (Sign.tsig_of (sign_of thy), T1, Type.varifyT T2);
   161 
   162 fun get_assoc_code thy s T = apsome snd (find_first (fn ((s', T'), _) =>
   163   s = s' andalso is_instance thy T T') (#consts (CodegenData.get thy)));
   164 
   165 fun get_defn thy s T =
   166   let
   167     val axms = flat (map (Symtab.dest o #axioms o Theory.rep_theory)
   168       (thy :: Theory.ancestors_of thy));
   169     val defs = mapfilter (fn (_, t) =>
   170       (let
   171          val (lhs, rhs) = Logic.dest_equals t;
   172          val (c, args) = strip_comb lhs;
   173          val (s', T') = dest_Const c
   174        in if s=s' then Some (T', (args, rhs)) else None end) handle TERM _ =>
   175          None) axms;
   176     val i = find_index (is_instance thy T o fst) defs
   177   in
   178     if i>=0 then Some (snd (nth_elem (i, defs)),
   179       if length defs = 1 then None else Some i)
   180     else None
   181   end;
   182 
   183 
   184 (**** make valid ML identifiers ****)
   185 
   186 fun gen_mk_id kind rename sg s =
   187   let
   188     val (xs as x::_) = explode (rename (space_implode "_"
   189       (NameSpace.unpack (Sign.cond_extern sg kind s))));
   190     fun check_str [] = ""
   191       | check_str (x::xs) =
   192           (if Symbol.is_letter x orelse Symbol.is_digit x orelse x="_" then x
   193            else "_" ^ string_of_int (ord x)) ^ check_str xs
   194   in
   195     (if not (Symbol.is_letter x) then "id" else "") ^ check_str xs
   196   end;
   197 
   198 val mk_const_id = gen_mk_id Sign.constK I;
   199 val mk_type_id = gen_mk_id Sign.typeK
   200   (fn s => if s mem ThmDatabase.ml_reserved then s ^ "_type" else s);
   201 
   202 fun rename_term t =
   203   let
   204     val names = add_term_names (t, map (fst o fst o dest_Var) (term_vars t));
   205     val clash = names inter ThmDatabase.ml_reserved;
   206     val ps = clash ~~ variantlist (clash, names);
   207 
   208     fun rename (Var ((a, i), T)) = Var ((if_none (assoc (ps, a)) a, i), T)
   209       | rename (Free (a, T)) = Free (if_none (assoc (ps, a)) a, T)
   210       | rename (Abs (s, T, t)) = Abs (s, T, rename t)
   211       | rename (t $ u) = rename t $ rename u
   212       | rename t = t;
   213   in
   214     rename t
   215   end;
   216 
   217 
   218 (**** invoke suitable code generator for term t ****)
   219 
   220 fun invoke_codegen thy gr dep brack t = (case get_first
   221    (fn (_, f) => f thy gr dep brack t) (#codegens (CodegenData.get thy)) of
   222       None => error ("Unable to generate code for term:\n" ^
   223         Sign.string_of_term (sign_of thy) t ^ "\nrequired by:\n" ^
   224         commas (Graph.all_succs gr [dep]))
   225     | Some x => x)
   226 
   227 
   228 (**** code generator for mixfix expressions ****)
   229 
   230 fun parens p = Pretty.block [Pretty.str "(", p, Pretty.str ")"];
   231 
   232 fun pretty_fn [] p = [p]
   233   | pretty_fn (x::xs) p = Pretty.str ("fn " ^ x ^ " =>") ::
   234       Pretty.brk 1 :: pretty_fn xs p;
   235 
   236 fun pretty_mixfix [] [] _ = []
   237   | pretty_mixfix (Arg :: ms) (p :: ps) qs = p :: pretty_mixfix ms ps qs
   238   | pretty_mixfix (Ignore :: ms) (p :: ps) qs = pretty_mixfix ms ps qs
   239   | pretty_mixfix (Pretty p :: ms) ps qs = p :: pretty_mixfix ms ps qs
   240   | pretty_mixfix (Term _ :: ms) ps (q :: qs) = q :: pretty_mixfix ms ps qs;
   241 
   242 
   243 (**** default code generator ****)
   244 
   245 fun eta_expand t ts i =
   246   let
   247     val (Ts, _) = strip_type (fastype_of t);
   248     val j = i - length ts
   249   in
   250     foldr (fn (T, t) => Abs ("x", T, t))
   251       (take (j, Ts), list_comb (t, ts @ map Bound (j-1 downto 0)))
   252   end;
   253 
   254 fun mk_app _ p [] = p
   255   | mk_app brack p ps = if brack then
   256        Pretty.block (Pretty.str "(" ::
   257          separate (Pretty.brk 1) (p :: ps) @ [Pretty.str ")"])
   258      else Pretty.block (separate (Pretty.brk 1) (p :: ps));
   259 
   260 fun new_names t xs = variantlist (xs,
   261   map (fst o fst o dest_Var) (term_vars t) union
   262   add_term_names (t, ThmDatabase.ml_reserved));
   263 
   264 fun new_name t x = hd (new_names t [x]);
   265 
   266 fun default_codegen thy gr dep brack t =
   267   let
   268     val (u, ts) = strip_comb t;
   269     fun mapcode brack' gr ts = foldl_map
   270       (fn (gr, t) => invoke_codegen thy gr dep brack' t) (gr, ts)
   271 
   272   in (case u of
   273       Var ((s, i), _) =>
   274         let val (gr', ps) = mapcode true gr ts
   275         in Some (gr', mk_app brack (Pretty.str (s ^
   276            (if i=0 then "" else string_of_int i))) ps)
   277         end
   278 
   279     | Free (s, _) =>
   280         let val (gr', ps) = mapcode true gr ts
   281         in Some (gr', mk_app brack (Pretty.str s) ps) end
   282 
   283     | Const (s, T) =>
   284       (case get_assoc_code thy s T of
   285          Some ms =>
   286            let val i = num_args ms
   287            in if length ts < i then
   288                default_codegen thy gr dep brack (eta_expand u ts i)
   289              else
   290                let
   291                  val ts1 = take (i, ts);
   292                  val ts2 = drop (i, ts);
   293                  val (gr1, ps1) = mapcode false gr ts1;
   294                  val (gr2, ps2) = mapcode true gr1 ts2;
   295                  val (gr3, ps3) = mapcode false gr2 (terms_of ms);
   296                in
   297                  Some (gr3, mk_app brack (Pretty.block (pretty_mixfix ms ps1 ps3)) ps2)
   298                end
   299            end
   300        | None => (case get_defn thy s T of
   301            None => None
   302          | Some ((args, rhs), k) =>
   303              let
   304                val id = mk_const_id (sign_of thy) s ^ (case k of
   305                  None => "" | Some i => "_def" ^ string_of_int i);
   306                val (gr', ps) = mapcode true gr ts;
   307              in
   308                Some (Graph.add_edge (id, dep) gr' handle Graph.UNDEF _ =>
   309                  let
   310                    val _ = message ("expanding definition of " ^ s);
   311                    val (Ts, _) = strip_type T;
   312                    val (args', rhs') =
   313                      if not (null args) orelse null Ts then (args, rhs) else
   314                        let val v = Free (new_name rhs "x", hd Ts)
   315                        in ([v], betapply (rhs, v)) end;
   316                    val (gr1, p) = invoke_codegen thy (Graph.add_edge (id, dep)
   317                      (Graph.new_node (id, (None, "")) gr')) id false rhs';
   318                    val (gr2, xs) = mapcode false gr1 args';
   319                  in Graph.map_node id (K (None, Pretty.string_of (Pretty.block
   320                    (Pretty.str (if null args' then "val " else "fun ") ::
   321                     separate (Pretty.brk 1) (Pretty.str id :: xs) @
   322                     [Pretty.str " =", Pretty.brk 1, p, Pretty.str ";"])) ^ "\n\n")) gr2
   323                  end, mk_app brack (Pretty.str id) ps)
   324              end))
   325 
   326     | Abs _ =>
   327       let
   328         val (bs, Ts) = ListPair.unzip (strip_abs_vars u);
   329         val t = strip_abs_body u
   330         val bs' = new_names t bs;
   331         val (gr1, ps) = mapcode true gr ts;
   332         val (gr2, p) = invoke_codegen thy gr1 dep false
   333           (subst_bounds (map Free (rev (bs' ~~ Ts)), t));
   334       in
   335         Some (gr2, mk_app brack (Pretty.block (Pretty.str "(" :: pretty_fn bs' p @
   336           [Pretty.str ")"])) ps)
   337       end
   338 
   339     | _ => None)
   340   end;
   341 
   342 
   343 fun output_code gr xs = implode (map (snd o Graph.get_node gr)
   344   (rev (Graph.all_preds gr xs)));
   345 
   346 fun gen_generate_code prep_term thy = Pretty.setmp_margin 80 (fn xs =>
   347   let
   348     val sg = sign_of thy;
   349     val gr = Graph.new_node ("<Top>", (None, "")) Graph.empty;
   350     val (gr', ps) = foldl_map (fn (gr, (s, t)) => apsnd (pair s)
   351       (invoke_codegen thy gr "<Top>" false t)) (gr, map (apsnd (prep_term sg)) xs)
   352   in
   353     "structure Generated =\nstruct\n\n" ^
   354     output_code gr' ["<Top>"] ^
   355     space_implode "\n\n" (map (fn (s', p) => Pretty.string_of (Pretty.block
   356       [Pretty.str ("val " ^ s' ^ " ="), Pretty.brk 1, p, Pretty.str ";"])) ps) ^
   357     "\n\nend;\n\nopen Generated;\n"
   358   end);
   359 
   360 val generate_code_i = gen_generate_code (K I);
   361 val generate_code = gen_generate_code
   362   (fn sg => term_of o read_cterm sg o rpair TypeInfer.logicT);
   363 
   364 fun parse_mixfix rd s =
   365   (case Scan.finite Symbol.stopper (Scan.repeat
   366      (   $$ "_" >> K Arg
   367       || $$ "?" >> K Ignore
   368       || $$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length)
   369       || $$ "{" |-- $$ "*" |-- Scan.repeat1
   370            (   $$ "'" |-- Scan.one Symbol.not_eof
   371             || Scan.unless ($$ "*" -- $$ "}") (Scan.one Symbol.not_eof)) --|
   372          $$ "*" --| $$ "}" >> (Term o rd o implode)
   373       || Scan.repeat1
   374            (   $$ "'" |-- Scan.one Symbol.not_eof
   375             || Scan.unless ($$ "_" || $$ "?" || $$ "/" || $$ "{" |-- $$ "*")
   376                  (Scan.one Symbol.not_eof)) >> (Pretty o Pretty.str o implode)))
   377        (Symbol.explode s) of
   378      (p, []) => p
   379    | _ => error ("Malformed annotation: " ^ quote s));
   380 
   381 structure P = OuterParse and K = OuterSyntax.Keyword;
   382 
   383 val assoc_typeP =
   384   OuterSyntax.command "types_code"
   385   "associate types with target language types" K.thy_decl
   386     (Scan.repeat1 (P.xname --| P.$$$ "(" -- P.string --| P.$$$ ")") >>
   387      (Toplevel.theory o assoc_types));
   388 
   389 val assoc_constP =
   390   OuterSyntax.command "consts_code"
   391   "associate constants with target language code" K.thy_decl
   392     (Scan.repeat1
   393        (P.xname -- (Scan.option (P.$$$ "::" |-- P.string)) --|
   394         P.$$$ "(" -- P.string --| P.$$$ ")") >>
   395      (fn xs => Toplevel.theory (fn thy => assoc_consts
   396        (map (fn ((name, optype), mfx) => (name, optype, parse_mixfix
   397          (term_of o read_cterm (sign_of thy) o rpair TypeInfer.logicT) mfx))
   398            xs) thy)));
   399 
   400 val generate_codeP =
   401   OuterSyntax.command "generate_code" "generates code for terms" K.thy_decl
   402     (Scan.option (P.$$$ "(" |-- P.string --| P.$$$ ")") --
   403      Scan.repeat1 (P.name --| P.$$$ "=" -- P.string) >>
   404      (fn (opt_fname, xs) => Toplevel.theory (fn thy =>
   405         ((case opt_fname of
   406             None => use_text Context.ml_output false
   407           | Some fname => File.write (Path.unpack fname))
   408               (generate_code thy xs); thy))));
   409 
   410 val parsers = [assoc_typeP, assoc_constP, generate_codeP];
   411 
   412 val setup = [CodegenData.init, add_codegen "default" default_codegen];
   413 
   414 end;
   415 
   416 OuterSyntax.add_parsers Codegen.parsers;