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