src/Pure/codegen.ML
author berghofe
Fri Apr 16 18:45:56 2004 +0200 (2004-04-16)
changeset 14598 7009f59711e3
parent 14197 3d7c6fc7c66e
child 14769 b698d0b243dc
permissions -rw-r--r--
Replaced quote by Library.quote, since quote now refers to Symbol.quote
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@13753
    13
  val mode : string list ref
berghofe@13886
    14
  val margin : int ref
berghofe@11520
    15
berghofe@12452
    16
  datatype 'a mixfix =
berghofe@11520
    17
      Arg
berghofe@11520
    18
    | Ignore
berghofe@11520
    19
    | Pretty of Pretty.T
berghofe@12452
    20
    | Quote of 'a;
berghofe@11520
    21
berghofe@12452
    22
  type 'a codegen
berghofe@12452
    23
berghofe@12452
    24
  val add_codegen: string -> term codegen -> theory -> theory
berghofe@12452
    25
  val add_tycodegen: string -> typ codegen -> theory -> theory
berghofe@14197
    26
  val add_attribute: string -> (Args.T list -> theory attribute * Args.T list) -> theory -> theory
berghofe@11520
    27
  val print_codegens: theory -> unit
berghofe@11520
    28
  val generate_code: theory -> (string * string) list -> string
berghofe@11520
    29
  val generate_code_i: theory -> (string * term) list -> string
berghofe@12452
    30
  val assoc_consts: (xstring * string option * term mixfix list) list -> theory -> theory
berghofe@12452
    31
  val assoc_consts_i: (xstring * typ option * term mixfix list) list -> theory -> theory
berghofe@12452
    32
  val assoc_types: (xstring * typ mixfix list) list -> theory -> theory
berghofe@12452
    33
  val get_assoc_code: theory -> string -> typ -> term mixfix list option
berghofe@12452
    34
  val get_assoc_type: theory -> string -> typ mixfix list option
berghofe@12452
    35
  val invoke_codegen: theory -> string -> bool ->
berghofe@12452
    36
    (exn option * string) Graph.T * term -> (exn option * string) Graph.T * Pretty.T
berghofe@12452
    37
  val invoke_tycodegen: theory -> string -> bool ->
berghofe@12452
    38
    (exn option * string) Graph.T * typ -> (exn option * string) Graph.T * Pretty.T
berghofe@11520
    39
  val mk_const_id: Sign.sg -> string -> string
berghofe@11520
    40
  val mk_type_id: Sign.sg -> string -> string
berghofe@11520
    41
  val rename_term: term -> term
berghofe@11520
    42
  val get_defn: theory -> string -> typ -> ((term list * term) * int option) option
berghofe@11520
    43
  val is_instance: theory -> typ -> typ -> bool
berghofe@11520
    44
  val parens: Pretty.T -> Pretty.T
berghofe@11520
    45
  val mk_app: bool -> Pretty.T -> Pretty.T list -> Pretty.T
berghofe@11520
    46
  val eta_expand: term -> term list -> int -> term
berghofe@14105
    47
  val strip_tname: string -> string
berghofe@13753
    48
  val mk_type: bool -> typ -> Pretty.T
berghofe@13753
    49
  val mk_term_of: Sign.sg -> bool -> typ -> Pretty.T
berghofe@14105
    50
  val mk_gen: Sign.sg -> bool -> string list -> string -> typ -> Pretty.T
berghofe@14105
    51
  val test_fn: (int -> (string * term) list option) ref
berghofe@14105
    52
  val test_term: theory -> int -> int -> term -> (string * term) list option
berghofe@12452
    53
  val parse_mixfix: (string -> 'a) -> string -> 'a mixfix list
berghofe@11520
    54
  val parsers: OuterSyntax.parser list
berghofe@11520
    55
  val setup: (theory -> theory) list
berghofe@11520
    56
end;
berghofe@11520
    57
berghofe@11520
    58
structure Codegen : CODEGEN =
berghofe@11520
    59
struct
berghofe@11520
    60
berghofe@11520
    61
val quiet_mode = ref true;
berghofe@11520
    62
fun message s = if !quiet_mode then () else writeln s;
berghofe@11520
    63
berghofe@13753
    64
val mode = ref ([] : string list);
berghofe@13753
    65
berghofe@13886
    66
val margin = ref 80;
berghofe@13886
    67
berghofe@11520
    68
(**** Mixfix syntax ****)
berghofe@11520
    69
berghofe@12452
    70
datatype 'a mixfix =
berghofe@11520
    71
    Arg
berghofe@11520
    72
  | Ignore
berghofe@11520
    73
  | Pretty of Pretty.T
berghofe@12452
    74
  | Quote of 'a;
berghofe@11520
    75
berghofe@11520
    76
fun is_arg Arg = true
berghofe@11520
    77
  | is_arg Ignore = true
berghofe@11520
    78
  | is_arg _ = false;
berghofe@11520
    79
berghofe@12452
    80
fun quotes_of [] = []
berghofe@12452
    81
  | quotes_of (Quote q :: ms) = q :: quotes_of ms
berghofe@12452
    82
  | quotes_of (_ :: ms) = quotes_of ms;
berghofe@12452
    83
berghofe@12452
    84
fun args_of [] xs = ([], xs)
berghofe@12452
    85
  | args_of (Arg :: ms) (x :: xs) = apfst (cons x) (args_of ms xs)
berghofe@12452
    86
  | args_of (Ignore :: ms) (_ :: xs) = args_of ms xs
berghofe@12452
    87
  | args_of (_ :: ms) xs = args_of ms xs;
berghofe@11520
    88
wenzelm@12490
    89
fun num_args x = length (filter is_arg x);
berghofe@11520
    90
berghofe@11520
    91
berghofe@11520
    92
(**** theory data ****)
berghofe@11520
    93
berghofe@14105
    94
(* type of code generators *)
berghofe@11520
    95
berghofe@12452
    96
type 'a codegen = theory -> (exn option * string) Graph.T ->
berghofe@12452
    97
  string -> bool -> 'a -> ((exn option * string) Graph.T * Pretty.T) option;
berghofe@12452
    98
berghofe@14105
    99
(* parameters for random testing *)
berghofe@14105
   100
berghofe@14105
   101
type test_params =
berghofe@14105
   102
  {size: int, iterations: int, default_type: typ option};
berghofe@14105
   103
berghofe@14105
   104
fun merge_test_params
berghofe@14105
   105
  {size = size1, iterations = iterations1, default_type = default_type1}
berghofe@14105
   106
  {size = size2, iterations = iterations2, default_type = default_type2} =
berghofe@14105
   107
  {size = Int.max (size1, size2),
berghofe@14105
   108
   iterations = Int.max (iterations1, iterations2),
berghofe@14105
   109
   default_type = case default_type1 of
berghofe@14105
   110
       None => default_type2
berghofe@14105
   111
     | _ => default_type1};
berghofe@14105
   112
berghofe@14105
   113
val default_test_params : test_params =
berghofe@14105
   114
  {size = 10, iterations = 100, default_type = None};
berghofe@14105
   115
berghofe@14105
   116
fun set_size size ({iterations, default_type, ...} : test_params) =
berghofe@14105
   117
  {size = size, iterations = iterations, default_type = default_type};
berghofe@14105
   118
berghofe@14105
   119
fun set_iterations iterations ({size, default_type, ...} : test_params) =
berghofe@14105
   120
  {size = size, iterations = iterations, default_type = default_type};
berghofe@14105
   121
berghofe@14105
   122
fun set_default_type s sg ({size, iterations, ...} : test_params) =
berghofe@14105
   123
  {size = size, iterations = iterations,
berghofe@14105
   124
   default_type = Some (typ_of (read_ctyp sg s))};
berghofe@14105
   125
berghofe@14105
   126
(* data kind 'Pure/codegen' *)
berghofe@14105
   127
 
berghofe@11520
   128
structure CodegenArgs =
berghofe@11520
   129
struct
berghofe@11520
   130
  val name = "Pure/codegen";
berghofe@11520
   131
  type T =
berghofe@12452
   132
    {codegens : (string * term codegen) list,
berghofe@12452
   133
     tycodegens : (string * typ codegen) list,
berghofe@12452
   134
     consts : ((string * typ) * term mixfix list) list,
berghofe@12555
   135
     types : (string * typ mixfix list) list,
berghofe@14197
   136
     attrs: (string * (Args.T list -> theory attribute * Args.T list)) list,
berghofe@14105
   137
     test_params: test_params};
berghofe@11520
   138
berghofe@12555
   139
  val empty =
berghofe@14105
   140
    {codegens = [], tycodegens = [], consts = [], types = [], attrs = [],
berghofe@14105
   141
     test_params = default_test_params};
berghofe@11520
   142
  val copy = I;
berghofe@11520
   143
  val prep_ext = I;
berghofe@11520
   144
berghofe@12555
   145
  fun merge
berghofe@12555
   146
    ({codegens = codegens1, tycodegens = tycodegens1,
berghofe@14105
   147
      consts = consts1, types = types1, attrs = attrs1,
berghofe@14105
   148
      test_params = test_params1},
berghofe@12555
   149
     {codegens = codegens2, tycodegens = tycodegens2,
berghofe@14105
   150
      consts = consts2, types = types2, attrs = attrs2,
berghofe@14105
   151
      test_params = test_params2}) =
berghofe@11520
   152
    {codegens = rev (merge_alists (rev codegens1) (rev codegens2)),
berghofe@12452
   153
     tycodegens = rev (merge_alists (rev tycodegens1) (rev tycodegens2)),
berghofe@12555
   154
     consts = merge_alists consts1 consts2,
berghofe@12555
   155
     types = merge_alists types1 types2,
berghofe@14105
   156
     attrs = merge_alists attrs1 attrs2,
berghofe@14105
   157
     test_params = merge_test_params test_params1 test_params2};
berghofe@11520
   158
berghofe@12452
   159
  fun print sg ({codegens, tycodegens, ...} : T) =
berghofe@12452
   160
    Pretty.writeln (Pretty.chunks
berghofe@12452
   161
      [Pretty.strs ("term code generators:" :: map fst codegens),
berghofe@12452
   162
       Pretty.strs ("type code generators:" :: map fst tycodegens)]);
berghofe@11520
   163
end;
berghofe@11520
   164
berghofe@11520
   165
structure CodegenData = TheoryDataFun(CodegenArgs);
berghofe@11520
   166
val print_codegens = CodegenData.print;
berghofe@11520
   167
berghofe@11520
   168
berghofe@14105
   169
(**** access parameters for random testing ****)
berghofe@14105
   170
berghofe@14105
   171
fun get_test_params thy = #test_params (CodegenData.get thy);
berghofe@14105
   172
berghofe@14105
   173
fun map_test_params f thy =
berghofe@14105
   174
  let val {codegens, tycodegens, consts, types, attrs, test_params} =
berghofe@14105
   175
    CodegenData.get thy;
berghofe@14105
   176
  in CodegenData.put {codegens = codegens, tycodegens = tycodegens,
berghofe@14105
   177
    consts = consts, types = types, attrs = attrs,
berghofe@14105
   178
    test_params = f test_params} thy
berghofe@14105
   179
  end;
berghofe@14105
   180
berghofe@14105
   181
berghofe@12452
   182
(**** add new code generators to theory ****)
berghofe@11520
   183
berghofe@11520
   184
fun add_codegen name f thy =
berghofe@14105
   185
  let val {codegens, tycodegens, consts, types, attrs, test_params} =
berghofe@14105
   186
    CodegenData.get thy
berghofe@11520
   187
  in (case assoc (codegens, name) of
berghofe@12452
   188
      None => CodegenData.put {codegens = (name, f) :: codegens,
berghofe@12555
   189
        tycodegens = tycodegens, consts = consts, types = types,
berghofe@14105
   190
        attrs = attrs, test_params = test_params} thy
berghofe@12452
   191
    | Some _ => error ("Code generator " ^ name ^ " already declared"))
berghofe@12452
   192
  end;
berghofe@12452
   193
berghofe@12452
   194
fun add_tycodegen name f thy =
berghofe@14105
   195
  let val {codegens, tycodegens, consts, types, attrs, test_params} =
berghofe@14105
   196
    CodegenData.get thy
berghofe@12452
   197
  in (case assoc (tycodegens, name) of
berghofe@12452
   198
      None => CodegenData.put {tycodegens = (name, f) :: tycodegens,
berghofe@12555
   199
        codegens = codegens, consts = consts, types = types,
berghofe@14105
   200
        attrs = attrs, test_params = test_params} thy
berghofe@11520
   201
    | Some _ => error ("Code generator " ^ name ^ " already declared"))
berghofe@11520
   202
  end;
berghofe@11520
   203
berghofe@11520
   204
berghofe@12555
   205
(**** code attribute ****)
berghofe@12555
   206
berghofe@12555
   207
fun add_attribute name att thy =
berghofe@14105
   208
  let val {codegens, tycodegens, consts, types, attrs, test_params} =
berghofe@14105
   209
    CodegenData.get thy
berghofe@12555
   210
  in (case assoc (attrs, name) of
berghofe@12555
   211
      None => CodegenData.put {tycodegens = tycodegens,
berghofe@12555
   212
        codegens = codegens, consts = consts, types = types,
berghofe@14105
   213
        attrs = (name, att) :: attrs, test_params = test_params} thy
berghofe@12555
   214
    | Some _ => error ("Code attribute " ^ name ^ " already declared"))
berghofe@12555
   215
  end;
berghofe@12555
   216
berghofe@14197
   217
fun mk_parser (a, p) = (if a = "" then Scan.succeed "" else Args.$$$ a) |-- p;
berghofe@14197
   218
berghofe@12555
   219
val code_attr =
berghofe@14197
   220
  Attrib.syntax (Scan.depend (fn thy => foldr op || (map mk_parser
berghofe@14197
   221
    (#attrs (CodegenData.get thy)), Scan.fail) >> pair thy));
berghofe@12555
   222
berghofe@12555
   223
berghofe@11520
   224
(**** associate constants with target language code ****)
berghofe@11520
   225
berghofe@11520
   226
fun gen_assoc_consts prep_type xs thy = foldl (fn (thy, (s, tyopt, syn)) =>
berghofe@11520
   227
  let
berghofe@11520
   228
    val sg = sign_of thy;
berghofe@14105
   229
    val {codegens, tycodegens, consts, types, attrs, test_params} =
berghofe@14105
   230
      CodegenData.get thy;
berghofe@11520
   231
    val cname = Sign.intern_const sg s;
berghofe@11520
   232
  in
berghofe@11520
   233
    (case Sign.const_type sg cname of
berghofe@11520
   234
       Some T =>
berghofe@11520
   235
         let val T' = (case tyopt of
berghofe@11520
   236
                None => T
berghofe@11520
   237
              | Some ty =>
berghofe@11520
   238
                  let val U = prep_type sg ty
berghofe@11520
   239
                  in if Type.typ_instance (Sign.tsig_of sg, U, T) then U
berghofe@11520
   240
                    else error ("Illegal type constraint for constant " ^ cname)
berghofe@11520
   241
                  end)
berghofe@11520
   242
         in (case assoc (consts, (cname, T')) of
berghofe@11520
   243
             None => CodegenData.put {codegens = codegens,
berghofe@12452
   244
               tycodegens = tycodegens,
berghofe@12555
   245
               consts = ((cname, T'), syn) :: consts,
berghofe@14105
   246
               types = types, attrs = attrs, test_params = test_params} thy
berghofe@11520
   247
           | Some _ => error ("Constant " ^ cname ^ " already associated with code"))
berghofe@11520
   248
         end
berghofe@11520
   249
     | _ => error ("Not a constant: " ^ s))
berghofe@11520
   250
  end) (thy, xs);
berghofe@11520
   251
berghofe@11520
   252
val assoc_consts_i = gen_assoc_consts (K I);
berghofe@11520
   253
val assoc_consts = gen_assoc_consts (fn sg => typ_of o read_ctyp sg);
berghofe@11520
   254
berghofe@11520
   255
(**** associate types with target language types ****)
berghofe@11520
   256
berghofe@11520
   257
fun assoc_types xs thy = foldl (fn (thy, (s, syn)) =>
berghofe@11520
   258
  let
berghofe@14105
   259
    val {codegens, tycodegens, consts, types, attrs, test_params} =
berghofe@14105
   260
      CodegenData.get thy;
berghofe@11520
   261
    val tc = Sign.intern_tycon (sign_of thy) s
berghofe@11520
   262
  in
berghofe@11520
   263
    (case assoc (types, tc) of
berghofe@12452
   264
       None => CodegenData.put {codegens = codegens,
berghofe@12452
   265
         tycodegens = tycodegens, consts = consts,
berghofe@14105
   266
         types = (tc, syn) :: types, attrs = attrs,
berghofe@14105
   267
         test_params = test_params} thy
berghofe@11520
   268
     | Some _ => error ("Type " ^ tc ^ " already associated with code"))
berghofe@11520
   269
  end) (thy, xs);
berghofe@11520
   270
berghofe@12452
   271
fun get_assoc_type thy s = assoc (#types (CodegenData.get thy), s);
wenzelm@11546
   272
berghofe@11520
   273
berghofe@11520
   274
(**** make valid ML identifiers ****)
berghofe@11520
   275
berghofe@11520
   276
fun gen_mk_id kind rename sg s =
berghofe@11520
   277
  let
berghofe@11520
   278
    val (xs as x::_) = explode (rename (space_implode "_"
berghofe@11520
   279
      (NameSpace.unpack (Sign.cond_extern sg kind s))));
berghofe@11520
   280
    fun check_str [] = ""
berghofe@12555
   281
      | check_str (" " :: xs) = "_" ^ check_str xs
berghofe@12555
   282
      | check_str (x :: xs) =
berghofe@12824
   283
          (if Symbol.is_letdig x then x
berghofe@11520
   284
           else "_" ^ string_of_int (ord x)) ^ check_str xs
berghofe@11520
   285
  in
berghofe@11520
   286
    (if not (Symbol.is_letter x) then "id" else "") ^ check_str xs
berghofe@11520
   287
  end;
berghofe@11520
   288
berghofe@13073
   289
val mk_const_id = gen_mk_id Sign.constK
berghofe@13073
   290
  (fn s => if s mem ThmDatabase.ml_reserved then s ^ "_const" else s);
berghofe@13073
   291
berghofe@11520
   292
val mk_type_id = gen_mk_id Sign.typeK
berghofe@11520
   293
  (fn s => if s mem ThmDatabase.ml_reserved then s ^ "_type" else s);
berghofe@11520
   294
berghofe@13731
   295
fun rename_terms ts =
berghofe@11520
   296
  let
berghofe@13731
   297
    val names = foldr add_term_names
berghofe@13731
   298
      (ts, map (fst o fst) (Drule.vars_of_terms ts));
berghofe@11520
   299
    val clash = names inter ThmDatabase.ml_reserved;
berghofe@11520
   300
    val ps = clash ~~ variantlist (clash, names);
berghofe@11520
   301
berghofe@11520
   302
    fun rename (Var ((a, i), T)) = Var ((if_none (assoc (ps, a)) a, i), T)
berghofe@11520
   303
      | rename (Free (a, T)) = Free (if_none (assoc (ps, a)) a, T)
berghofe@11520
   304
      | rename (Abs (s, T, t)) = Abs (s, T, rename t)
berghofe@11520
   305
      | rename (t $ u) = rename t $ rename u
berghofe@11520
   306
      | rename t = t;
berghofe@11520
   307
  in
berghofe@13731
   308
    map rename ts
berghofe@13731
   309
  end;
berghofe@13731
   310
berghofe@13731
   311
val rename_term = hd o rename_terms o single;
berghofe@13731
   312
berghofe@13731
   313
berghofe@13731
   314
(**** retrieve definition of constant ****)
berghofe@13731
   315
berghofe@13731
   316
fun is_instance thy T1 T2 =
berghofe@13731
   317
  Type.typ_instance (Sign.tsig_of (sign_of thy), T1, Type.varifyT T2);
berghofe@13731
   318
berghofe@13731
   319
fun get_assoc_code thy s T = apsome snd (find_first (fn ((s', T'), _) =>
berghofe@13731
   320
  s = s' andalso is_instance thy T T') (#consts (CodegenData.get thy)));
berghofe@13731
   321
berghofe@13731
   322
fun get_defn thy s T =
berghofe@13731
   323
  let
berghofe@13731
   324
    val axms = flat (map (Symtab.dest o #axioms o Theory.rep_theory)
berghofe@13731
   325
      (thy :: Theory.ancestors_of thy));
berghofe@13731
   326
    val defs = mapfilter (fn (_, t) =>
berghofe@13731
   327
      (let
berghofe@13731
   328
         val (lhs, rhs) = Logic.dest_equals t;
berghofe@13731
   329
         val (c, args) = strip_comb lhs;
berghofe@13731
   330
         val (s', T') = dest_Const c
berghofe@13731
   331
       in if s=s' then Some (T', split_last (rename_terms (args @ [rhs])))
berghofe@13731
   332
         else None end) handle TERM _ => None) axms;
berghofe@13731
   333
    val i = find_index (is_instance thy T o fst) defs
berghofe@13731
   334
  in
berghofe@13731
   335
    if i>=0 then Some (snd (nth_elem (i, defs)),
berghofe@13731
   336
      if length defs = 1 then None else Some i)
berghofe@13731
   337
    else None
berghofe@11520
   338
  end;
berghofe@11520
   339
berghofe@11520
   340
berghofe@12452
   341
(**** invoke suitable code generator for term / type ****)
berghofe@11520
   342
berghofe@12452
   343
fun invoke_codegen thy dep brack (gr, t) = (case get_first
berghofe@11520
   344
   (fn (_, f) => f thy gr dep brack t) (#codegens (CodegenData.get thy)) of
berghofe@11520
   345
      None => error ("Unable to generate code for term:\n" ^
berghofe@11520
   346
        Sign.string_of_term (sign_of thy) t ^ "\nrequired by:\n" ^
berghofe@11520
   347
        commas (Graph.all_succs gr [dep]))
berghofe@12452
   348
    | Some x => x);
berghofe@12452
   349
berghofe@12452
   350
fun invoke_tycodegen thy dep brack (gr, T) = (case get_first
berghofe@12452
   351
   (fn (_, f) => f thy gr dep brack T) (#tycodegens (CodegenData.get thy)) of
berghofe@12452
   352
      None => error ("Unable to generate code for type:\n" ^
berghofe@12452
   353
        Sign.string_of_typ (sign_of thy) T ^ "\nrequired by:\n" ^
berghofe@12452
   354
        commas (Graph.all_succs gr [dep]))
berghofe@12452
   355
    | Some x => x);
berghofe@11520
   356
berghofe@11520
   357
berghofe@11520
   358
(**** code generator for mixfix expressions ****)
berghofe@11520
   359
berghofe@11520
   360
fun parens p = Pretty.block [Pretty.str "(", p, Pretty.str ")"];
berghofe@11520
   361
berghofe@11520
   362
fun pretty_fn [] p = [p]
berghofe@11520
   363
  | pretty_fn (x::xs) p = Pretty.str ("fn " ^ x ^ " =>") ::
berghofe@11520
   364
      Pretty.brk 1 :: pretty_fn xs p;
berghofe@11520
   365
berghofe@11520
   366
fun pretty_mixfix [] [] _ = []
berghofe@11520
   367
  | pretty_mixfix (Arg :: ms) (p :: ps) qs = p :: pretty_mixfix ms ps qs
berghofe@12452
   368
  | pretty_mixfix (Ignore :: ms) ps qs = pretty_mixfix ms ps qs
berghofe@11520
   369
  | pretty_mixfix (Pretty p :: ms) ps qs = p :: pretty_mixfix ms ps qs
berghofe@12452
   370
  | pretty_mixfix (Quote _ :: ms) ps (q :: qs) = q :: pretty_mixfix ms ps qs;
berghofe@11520
   371
berghofe@11520
   372
berghofe@12452
   373
(**** default code generators ****)
berghofe@11520
   374
berghofe@11520
   375
fun eta_expand t ts i =
berghofe@11520
   376
  let
berghofe@11520
   377
    val (Ts, _) = strip_type (fastype_of t);
berghofe@11520
   378
    val j = i - length ts
berghofe@11520
   379
  in
berghofe@11520
   380
    foldr (fn (T, t) => Abs ("x", T, t))
berghofe@11520
   381
      (take (j, Ts), list_comb (t, ts @ map Bound (j-1 downto 0)))
berghofe@11520
   382
  end;
berghofe@11520
   383
berghofe@11520
   384
fun mk_app _ p [] = p
berghofe@11520
   385
  | mk_app brack p ps = if brack then
berghofe@11520
   386
       Pretty.block (Pretty.str "(" ::
berghofe@11520
   387
         separate (Pretty.brk 1) (p :: ps) @ [Pretty.str ")"])
berghofe@11520
   388
     else Pretty.block (separate (Pretty.brk 1) (p :: ps));
berghofe@11520
   389
berghofe@11520
   390
fun new_names t xs = variantlist (xs,
berghofe@11520
   391
  map (fst o fst o dest_Var) (term_vars t) union
berghofe@11520
   392
  add_term_names (t, ThmDatabase.ml_reserved));
berghofe@11520
   393
berghofe@11520
   394
fun new_name t x = hd (new_names t [x]);
berghofe@11520
   395
berghofe@11520
   396
fun default_codegen thy gr dep brack t =
berghofe@11520
   397
  let
berghofe@11520
   398
    val (u, ts) = strip_comb t;
berghofe@12452
   399
    fun codegens brack = foldl_map (invoke_codegen thy dep brack)
berghofe@11520
   400
  in (case u of
berghofe@14105
   401
      Var ((s, i), T) =>
berghofe@14105
   402
        let
berghofe@14105
   403
          val (gr', ps) = codegens true (gr, ts);
berghofe@14105
   404
          val (gr'', _) = invoke_tycodegen thy dep false (gr', T)
berghofe@14105
   405
        in Some (gr'', mk_app brack (Pretty.str (s ^
berghofe@11520
   406
           (if i=0 then "" else string_of_int i))) ps)
berghofe@11520
   407
        end
berghofe@11520
   408
berghofe@14105
   409
    | Free (s, T) =>
berghofe@14105
   410
        let
berghofe@14105
   411
          val (gr', ps) = codegens true (gr, ts);
berghofe@14105
   412
          val (gr'', _) = invoke_tycodegen thy dep false (gr', T)
berghofe@14105
   413
        in Some (gr'', mk_app brack (Pretty.str s) ps) end
berghofe@11520
   414
berghofe@11520
   415
    | Const (s, T) =>
berghofe@11520
   416
      (case get_assoc_code thy s T of
berghofe@11520
   417
         Some ms =>
berghofe@11520
   418
           let val i = num_args ms
berghofe@11520
   419
           in if length ts < i then
berghofe@11520
   420
               default_codegen thy gr dep brack (eta_expand u ts i)
berghofe@11520
   421
             else
berghofe@11520
   422
               let
berghofe@12452
   423
                 val (ts1, ts2) = args_of ms ts;
berghofe@12452
   424
                 val (gr1, ps1) = codegens false (gr, ts1);
berghofe@12452
   425
                 val (gr2, ps2) = codegens true (gr1, ts2);
berghofe@12452
   426
                 val (gr3, ps3) = codegens false (gr2, quotes_of ms);
berghofe@11520
   427
               in
berghofe@11520
   428
                 Some (gr3, mk_app brack (Pretty.block (pretty_mixfix ms ps1 ps3)) ps2)
berghofe@11520
   429
               end
berghofe@11520
   430
           end
berghofe@11520
   431
       | None => (case get_defn thy s T of
berghofe@11520
   432
           None => None
berghofe@11520
   433
         | Some ((args, rhs), k) =>
berghofe@11520
   434
             let
berghofe@11520
   435
               val id = mk_const_id (sign_of thy) s ^ (case k of
berghofe@11520
   436
                 None => "" | Some i => "_def" ^ string_of_int i);
berghofe@12452
   437
               val (gr', ps) = codegens true (gr, ts);
berghofe@11520
   438
             in
berghofe@11520
   439
               Some (Graph.add_edge (id, dep) gr' handle Graph.UNDEF _ =>
berghofe@11520
   440
                 let
berghofe@11520
   441
                   val _ = message ("expanding definition of " ^ s);
berghofe@11520
   442
                   val (Ts, _) = strip_type T;
berghofe@11520
   443
                   val (args', rhs') =
berghofe@11520
   444
                     if not (null args) orelse null Ts then (args, rhs) else
berghofe@11520
   445
                       let val v = Free (new_name rhs "x", hd Ts)
berghofe@11520
   446
                       in ([v], betapply (rhs, v)) end;
berghofe@12452
   447
                   val (gr1, p) = invoke_codegen thy id false
berghofe@12452
   448
                     (Graph.add_edge (id, dep)
berghofe@12452
   449
                        (Graph.new_node (id, (None, "")) gr'), rhs');
berghofe@12452
   450
                   val (gr2, xs) = codegens false (gr1, args');
berghofe@12580
   451
                   val (gr3, ty) = invoke_tycodegen thy id false (gr2, T);
berghofe@11520
   452
                 in Graph.map_node id (K (None, Pretty.string_of (Pretty.block
berghofe@12580
   453
                   (separate (Pretty.brk 1) (if null args' then
berghofe@12580
   454
                       [Pretty.str ("val " ^ id ^ " :"), ty]
berghofe@12580
   455
                     else Pretty.str ("fun " ^ id) :: xs) @
berghofe@12580
   456
                    [Pretty.str " =", Pretty.brk 1, p, Pretty.str ";"])) ^ "\n\n")) gr3
berghofe@11520
   457
                 end, mk_app brack (Pretty.str id) ps)
berghofe@11520
   458
             end))
berghofe@11520
   459
berghofe@11520
   460
    | Abs _ =>
berghofe@11520
   461
      let
berghofe@11520
   462
        val (bs, Ts) = ListPair.unzip (strip_abs_vars u);
berghofe@11520
   463
        val t = strip_abs_body u
berghofe@11520
   464
        val bs' = new_names t bs;
berghofe@12452
   465
        val (gr1, ps) = codegens true (gr, ts);
berghofe@12452
   466
        val (gr2, p) = invoke_codegen thy dep false
berghofe@12452
   467
          (gr1, subst_bounds (map Free (rev (bs' ~~ Ts)), t));
berghofe@11520
   468
      in
berghofe@11520
   469
        Some (gr2, mk_app brack (Pretty.block (Pretty.str "(" :: pretty_fn bs' p @
berghofe@11520
   470
          [Pretty.str ")"])) ps)
berghofe@11520
   471
      end
berghofe@11520
   472
berghofe@11520
   473
    | _ => None)
berghofe@11520
   474
  end;
berghofe@11520
   475
berghofe@12452
   476
fun default_tycodegen thy gr dep brack (TVar ((s, i), _)) =
berghofe@12452
   477
      Some (gr, Pretty.str (s ^ (if i = 0 then "" else string_of_int i)))
berghofe@12452
   478
  | default_tycodegen thy gr dep brack (TFree (s, _)) = Some (gr, Pretty.str s)
berghofe@12452
   479
  | default_tycodegen thy gr dep brack (Type (s, Ts)) =
berghofe@12452
   480
      (case assoc (#types (CodegenData.get thy), s) of
berghofe@12452
   481
         None => None
berghofe@12452
   482
       | Some ms =>
berghofe@12452
   483
           let
berghofe@12452
   484
             val (gr', ps) = foldl_map
berghofe@12452
   485
               (invoke_tycodegen thy dep false) (gr, fst (args_of ms Ts));
berghofe@12452
   486
             val (gr'', qs) = foldl_map
berghofe@12452
   487
               (invoke_tycodegen thy dep false) (gr', quotes_of ms)
berghofe@12452
   488
           in Some (gr'', Pretty.block (pretty_mixfix ms ps qs)) end);
berghofe@12452
   489
berghofe@11520
   490
berghofe@11520
   491
fun output_code gr xs = implode (map (snd o Graph.get_node gr)
berghofe@11520
   492
  (rev (Graph.all_preds gr xs)));
berghofe@11520
   493
berghofe@14598
   494
fun gen_generate_code prep_term thy =
berghofe@14598
   495
  setmp print_mode [] (Pretty.setmp_margin (!margin) (fn xs =>
berghofe@11520
   496
  let
berghofe@11520
   497
    val sg = sign_of thy;
berghofe@11520
   498
    val gr = Graph.new_node ("<Top>", (None, "")) Graph.empty;
berghofe@11520
   499
    val (gr', ps) = foldl_map (fn (gr, (s, t)) => apsnd (pair s)
berghofe@12452
   500
      (invoke_codegen thy "<Top>" false (gr, t)))
berghofe@12452
   501
        (gr, map (apsnd (prep_term sg)) xs)
berghofe@11520
   502
  in
berghofe@11520
   503
    "structure Generated =\nstruct\n\n" ^
berghofe@11520
   504
    output_code gr' ["<Top>"] ^
berghofe@11520
   505
    space_implode "\n\n" (map (fn (s', p) => Pretty.string_of (Pretty.block
berghofe@11520
   506
      [Pretty.str ("val " ^ s' ^ " ="), Pretty.brk 1, p, Pretty.str ";"])) ps) ^
berghofe@11520
   507
    "\n\nend;\n\nopen Generated;\n"
berghofe@14598
   508
  end));
berghofe@11520
   509
berghofe@11520
   510
val generate_code_i = gen_generate_code (K I);
berghofe@11520
   511
val generate_code = gen_generate_code
berghofe@11520
   512
  (fn sg => term_of o read_cterm sg o rpair TypeInfer.logicT);
berghofe@11520
   513
berghofe@12452
   514
berghofe@13753
   515
(**** Reflection ****)
berghofe@13753
   516
berghofe@13753
   517
val strip_tname = implode o tl o explode;
berghofe@13753
   518
berghofe@13753
   519
fun pretty_list xs = Pretty.block (Pretty.str "[" ::
berghofe@13753
   520
  flat (separate [Pretty.str ",", Pretty.brk 1] (map single xs)) @
berghofe@13753
   521
  [Pretty.str "]"]);
berghofe@13753
   522
berghofe@13753
   523
fun mk_type p (TVar ((s, i), _)) = Pretty.str
berghofe@13753
   524
      (strip_tname s ^ (if i = 0 then "" else string_of_int i) ^ "T")
berghofe@13753
   525
  | mk_type p (TFree (s, _)) = Pretty.str (strip_tname s ^ "T")
berghofe@13753
   526
  | mk_type p (Type (s, Ts)) = (if p then parens else I) (Pretty.block
berghofe@13753
   527
      [Pretty.str "Type", Pretty.brk 1, Pretty.str ("(\"" ^ s ^ "\","),
berghofe@13753
   528
       Pretty.brk 1, pretty_list (map (mk_type false) Ts), Pretty.str ")"]);
berghofe@13753
   529
berghofe@13753
   530
fun mk_term_of sg p (TVar ((s, i), _)) = Pretty.str
berghofe@13753
   531
      (strip_tname s ^ (if i = 0 then "" else string_of_int i) ^ "F")
berghofe@13753
   532
  | mk_term_of sg p (TFree (s, _)) = Pretty.str (strip_tname s ^ "F")
berghofe@13753
   533
  | mk_term_of sg p (Type (s, Ts)) = (if p then parens else I) (Pretty.block
berghofe@13753
   534
      (separate (Pretty.brk 1) (Pretty.str ("term_of_" ^ mk_type_id sg s) ::
berghofe@13753
   535
        flat (map (fn T => [mk_term_of sg true T, mk_type true T]) Ts))));
berghofe@13753
   536
berghofe@13753
   537
berghofe@14105
   538
(**** Test data generators ****)
berghofe@14105
   539
berghofe@14105
   540
fun mk_gen sg p xs a (TVar ((s, i), _)) = Pretty.str
berghofe@14105
   541
      (strip_tname s ^ (if i = 0 then "" else string_of_int i) ^ "G")
berghofe@14105
   542
  | mk_gen sg p xs a (TFree (s, _)) = Pretty.str (strip_tname s ^ "G")
berghofe@14105
   543
  | mk_gen sg p xs a (Type (s, Ts)) = (if p then parens else I) (Pretty.block
berghofe@14105
   544
      (separate (Pretty.brk 1) (Pretty.str ("gen_" ^ mk_type_id sg s ^
berghofe@14105
   545
        (if s mem xs then "'" else "")) :: map (mk_gen sg true xs a) Ts @
berghofe@14105
   546
        (if s mem xs then [Pretty.str a] else []))));
berghofe@14105
   547
berghofe@14105
   548
val test_fn : (int -> (string * term) list option) ref = ref (fn _ => None);
berghofe@14105
   549
berghofe@14105
   550
fun test_term thy sz i t =
berghofe@14105
   551
  let
berghofe@14105
   552
    val _ = assert (null (term_tvars t) andalso null (term_tfrees t))
berghofe@14105
   553
      "Term to be tested contains type variables";
berghofe@14105
   554
    val _ = assert (null (term_vars t))
berghofe@14105
   555
      "Term to be tested contains schematic variables";
berghofe@14105
   556
    val sg = sign_of thy;
berghofe@14105
   557
    val frees = map dest_Free (term_frees t);
berghofe@14140
   558
    val szname = variant (map fst frees) "i";
berghofe@14105
   559
    val s = "structure TestTerm =\nstruct\n\n" ^
berghofe@14105
   560
      setmp mode ["term_of", "test"] (generate_code_i thy)
berghofe@14105
   561
        [("testf", list_abs_free (frees, t))] ^
berghofe@14105
   562
      "\n" ^ Pretty.string_of
berghofe@14105
   563
        (Pretty.block [Pretty.str "val () = Codegen.test_fn :=",
berghofe@14140
   564
          Pretty.brk 1, Pretty.str ("(fn " ^ szname ^ " =>"), Pretty.brk 1,
berghofe@14105
   565
          Pretty.blk (0, [Pretty.str "let", Pretty.brk 1,
berghofe@14105
   566
            Pretty.blk (0, separate Pretty.fbrk (map (fn (s, T) =>
berghofe@14105
   567
              Pretty.block [Pretty.str ("val " ^ s ^ " ="), Pretty.brk 1,
berghofe@14105
   568
              mk_gen sg false [] "" T, Pretty.brk 1,
berghofe@14140
   569
              Pretty.str (szname ^ ";")]) frees)),
berghofe@14105
   570
            Pretty.brk 1, Pretty.str "in", Pretty.brk 1,
berghofe@14105
   571
            Pretty.block [Pretty.str "if ",
berghofe@14105
   572
              mk_app false (Pretty.str "testf") (map (Pretty.str o fst) frees),
kleing@14110
   573
              Pretty.brk 1, Pretty.str "then Library.None",
berghofe@14105
   574
              Pretty.brk 1, Pretty.str "else ",
kleing@14110
   575
              Pretty.block [Pretty.str "Library.Some ", Pretty.block (Pretty.str "[" ::
berghofe@14105
   576
                flat (separate [Pretty.str ",", Pretty.brk 1]
berghofe@14105
   577
                  (map (fn (s, T) => [Pretty.block
berghofe@14598
   578
                    [Pretty.str ("(" ^ Library.quote s ^ ","), Pretty.brk 1,
berghofe@14105
   579
                     mk_app false (mk_term_of sg false T)
berghofe@14105
   580
                       [Pretty.str s], Pretty.str ")"]]) frees)) @
berghofe@14105
   581
                  [Pretty.str "]"])]],
berghofe@14105
   582
            Pretty.brk 1, Pretty.str "end"]), Pretty.str ");"]) ^
berghofe@14105
   583
      "\n\nend;\n";
berghofe@14105
   584
    val _ = use_text Context.ml_output false s;
berghofe@14105
   585
    fun iter f k = if k > i then None
berghofe@14135
   586
      else (case (f () handle Match =>
berghofe@14135
   587
          (warning "Exception Match raised in generated code"; None)) of
berghofe@14135
   588
        None => iter f (k+1) | Some x => Some x);
berghofe@14105
   589
    fun test k = if k > sz then None
berghofe@14105
   590
      else (priority ("Test data size: " ^ string_of_int k);
berghofe@14105
   591
        case iter (fn () => !test_fn k) 1 of
berghofe@14105
   592
          None => test (k+1) | Some x => Some x);
berghofe@14105
   593
  in test 0 end;
berghofe@14105
   594
berghofe@14105
   595
fun test_goal ({size, iterations, default_type}, tvinsts) i st =
berghofe@14105
   596
  let
berghofe@14105
   597
    val sg = Toplevel.sign_of st;
berghofe@14105
   598
    fun strip (Const ("all", _) $ Abs (_, _, t)) = strip t
berghofe@14105
   599
      | strip t = t;
berghofe@14105
   600
    val (gi, frees) = Logic.goal_params
berghofe@14105
   601
      (prop_of (snd (snd (Proof.get_goal (Toplevel.proof_of st))))) i;
berghofe@14105
   602
    val gi' = ObjectLogic.atomize_term sg (map_term_types
berghofe@14105
   603
      (map_type_tfree (fn p as (s, _) => if_none (assoc (tvinsts, s))
berghofe@14105
   604
        (if_none default_type (TFree p)))) (subst_bounds (frees, strip gi)));
berghofe@14105
   605
  in case test_term (Toplevel.theory_of st) size iterations gi' of
berghofe@14105
   606
      None => writeln "No counterexamples found."
berghofe@14105
   607
    | Some cex => writeln ("Counterexample found:\n" ^
berghofe@14105
   608
        Pretty.string_of (Pretty.chunks (map (fn (s, t) =>
berghofe@14105
   609
          Pretty.block [Pretty.str (s ^ " ="), Pretty.brk 1,
berghofe@14105
   610
            Sign.pretty_term sg t]) cex)))
berghofe@14105
   611
  end;
berghofe@14105
   612
berghofe@14105
   613
berghofe@12452
   614
(**** Interface ****)
berghofe@12452
   615
berghofe@13731
   616
val str = setmp print_mode [] Pretty.str;
berghofe@13731
   617
berghofe@11520
   618
fun parse_mixfix rd s =
berghofe@11520
   619
  (case Scan.finite Symbol.stopper (Scan.repeat
berghofe@11520
   620
     (   $$ "_" >> K Arg
berghofe@11520
   621
      || $$ "?" >> K Ignore
berghofe@11520
   622
      || $$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length)
berghofe@11520
   623
      || $$ "{" |-- $$ "*" |-- Scan.repeat1
berghofe@11520
   624
           (   $$ "'" |-- Scan.one Symbol.not_eof
berghofe@11520
   625
            || Scan.unless ($$ "*" -- $$ "}") (Scan.one Symbol.not_eof)) --|
berghofe@12452
   626
         $$ "*" --| $$ "}" >> (Quote o rd o implode)
berghofe@11520
   627
      || Scan.repeat1
berghofe@11520
   628
           (   $$ "'" |-- Scan.one Symbol.not_eof
berghofe@11520
   629
            || Scan.unless ($$ "_" || $$ "?" || $$ "/" || $$ "{" |-- $$ "*")
berghofe@13731
   630
                 (Scan.one Symbol.not_eof)) >> (Pretty o str o implode)))
berghofe@11520
   631
       (Symbol.explode s) of
berghofe@11520
   632
     (p, []) => p
berghofe@11520
   633
   | _ => error ("Malformed annotation: " ^ quote s));
berghofe@11520
   634
wenzelm@11546
   635
structure P = OuterParse and K = OuterSyntax.Keyword;
berghofe@11520
   636
berghofe@11520
   637
val assoc_typeP =
berghofe@11520
   638
  OuterSyntax.command "types_code"
wenzelm@11546
   639
  "associate types with target language types" K.thy_decl
berghofe@11520
   640
    (Scan.repeat1 (P.xname --| P.$$$ "(" -- P.string --| P.$$$ ")") >>
berghofe@12452
   641
     (fn xs => Toplevel.theory (fn thy => assoc_types
berghofe@12452
   642
       (map (fn (name, mfx) => (name, parse_mixfix
berghofe@12452
   643
         (typ_of o read_ctyp (sign_of thy)) mfx)) xs) thy)));
berghofe@11520
   644
berghofe@11520
   645
val assoc_constP =
berghofe@11520
   646
  OuterSyntax.command "consts_code"
wenzelm@11546
   647
  "associate constants with target language code" K.thy_decl
berghofe@11520
   648
    (Scan.repeat1
wenzelm@13003
   649
       (P.xname -- (Scan.option (P.$$$ "::" |-- P.typ)) --|
berghofe@11520
   650
        P.$$$ "(" -- P.string --| P.$$$ ")") >>
berghofe@11520
   651
     (fn xs => Toplevel.theory (fn thy => assoc_consts
berghofe@11520
   652
       (map (fn ((name, optype), mfx) => (name, optype, parse_mixfix
berghofe@11520
   653
         (term_of o read_cterm (sign_of thy) o rpair TypeInfer.logicT) mfx))
berghofe@11520
   654
           xs) thy)));
berghofe@11520
   655
berghofe@11520
   656
val generate_codeP =
wenzelm@11546
   657
  OuterSyntax.command "generate_code" "generates code for terms" K.thy_decl
wenzelm@13003
   658
    (Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") --
berghofe@13753
   659
     Scan.optional (P.$$$ "[" |-- P.enum "," P.xname --| P.$$$ "]") (!mode) --
wenzelm@13003
   660
     Scan.repeat1 (P.name --| P.$$$ "=" -- P.term) >>
berghofe@13753
   661
     (fn ((opt_fname, mode'), xs) => Toplevel.theory (fn thy =>
berghofe@11520
   662
        ((case opt_fname of
berghofe@11520
   663
            None => use_text Context.ml_output false
berghofe@11520
   664
          | Some fname => File.write (Path.unpack fname))
berghofe@13753
   665
              (setmp mode mode' (generate_code thy) xs); thy))));
berghofe@11520
   666
berghofe@14105
   667
val params =
berghofe@14105
   668
  [("size", P.nat >> (K o set_size)),
berghofe@14105
   669
   ("iterations", P.nat >> (K o set_iterations)),
berghofe@14105
   670
   ("default_type", P.typ >> set_default_type)];
berghofe@14105
   671
berghofe@14105
   672
val parse_test_params = P.short_ident :-- (fn s =>
berghofe@14105
   673
  P.$$$ "=" |-- if_none (assoc (params, s)) Scan.fail) >> snd;
berghofe@14105
   674
berghofe@14105
   675
fun parse_tyinst xs =
berghofe@14105
   676
  (P.type_ident --| P.$$$ "=" -- P.typ >> (fn (v, s) => fn sg =>
berghofe@14105
   677
    fn (x, ys) => (x, (v, typ_of (read_ctyp sg s)) :: ys))) xs;
berghofe@14105
   678
berghofe@14105
   679
fun app [] x = x
berghofe@14105
   680
  | app (f :: fs) x = app fs (f x);
berghofe@14105
   681
berghofe@14105
   682
val test_paramsP =
berghofe@14105
   683
  OuterSyntax.command "quickcheck_params" "set parameters for random testing" K.thy_decl
berghofe@14105
   684
    (P.$$$ "[" |-- P.list1 parse_test_params --| P.$$$ "]" >>
berghofe@14105
   685
      (fn fs => Toplevel.theory (fn thy =>
berghofe@14105
   686
         map_test_params (app (map (fn f => f (sign_of thy)) fs)) thy)));
berghofe@14105
   687
berghofe@14105
   688
val testP =
berghofe@14105
   689
  OuterSyntax.command "quickcheck" "try to find counterexample for subgoal" K.diag
berghofe@14105
   690
  (Scan.option (P.$$$ "[" |-- P.list1
berghofe@14105
   691
    (   parse_test_params >> (fn f => fn sg => apfst (f sg))
berghofe@14105
   692
     || parse_tyinst) --| P.$$$ "]") -- Scan.optional P.nat 1 >>
berghofe@14105
   693
    (fn (ps, g) => Toplevel.keep (fn st =>
berghofe@14105
   694
      test_goal (app (if_none (apsome
berghofe@14105
   695
          (map (fn f => f (Toplevel.sign_of st))) ps) [])
berghofe@14105
   696
        (get_test_params (Toplevel.theory_of st), [])) g st)));
berghofe@14105
   697
berghofe@14105
   698
val parsers = [assoc_typeP, assoc_constP, generate_codeP, test_paramsP, testP];
berghofe@11520
   699
berghofe@12452
   700
val setup =
berghofe@12452
   701
  [CodegenData.init,
berghofe@12452
   702
   add_codegen "default" default_codegen,
berghofe@12452
   703
   add_tycodegen "default" default_tycodegen,
berghofe@12555
   704
   assoc_types [("fun", parse_mixfix (K dummyT) "(_ ->/ _)")],
berghofe@12555
   705
   Attrib.add_attributes [("code",
berghofe@12555
   706
     (code_attr, K Attrib.undef_local_attribute),
berghofe@12555
   707
     "declare theorems for code generation")]];
berghofe@11520
   708
berghofe@11520
   709
end;
berghofe@11520
   710
berghofe@11520
   711
OuterSyntax.add_parsers Codegen.parsers;