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