src/Pure/codegen.ML
author wenzelm
Tue Aug 16 13:42:26 2005 +0200 (2005-08-16)
changeset 17057 0934ac31985f
parent 16769 7f188f2127f7
child 17144 6642e0f96f44
permissions -rw-r--r--
OuterKeyword;
berghofe@11520
     1
(*  Title:      Pure/codegen.ML
berghofe@11520
     2
    ID:         $Id$
wenzelm@11539
     3
    Author:     Stefan Berghofer, TU Muenchen
berghofe@11520
     4
wenzelm@11539
     5
Generic code generator.
berghofe@11520
     6
*)
berghofe@11520
     7
berghofe@11520
     8
signature CODEGEN =
berghofe@11520
     9
sig
berghofe@11520
    10
  val quiet_mode : bool ref
berghofe@11520
    11
  val message : string -> unit
berghofe@13753
    12
  val mode : string list ref
berghofe@13886
    13
  val margin : int ref
berghofe@11520
    14
berghofe@12452
    15
  datatype 'a mixfix =
berghofe@11520
    16
      Arg
berghofe@11520
    17
    | Ignore
berghofe@16769
    18
    | Module
berghofe@11520
    19
    | Pretty of Pretty.T
berghofe@12452
    20
    | Quote of 'a;
berghofe@11520
    21
berghofe@16649
    22
  type deftab
berghofe@16649
    23
  type codegr
berghofe@12452
    24
  type 'a codegen
berghofe@12452
    25
berghofe@12452
    26
  val add_codegen: string -> term codegen -> theory -> theory
berghofe@12452
    27
  val add_tycodegen: string -> typ codegen -> theory -> theory
berghofe@14197
    28
  val add_attribute: string -> (Args.T list -> theory attribute * Args.T list) -> theory -> theory
berghofe@15261
    29
  val add_preprocessor: (theory -> thm list -> thm list) -> theory -> theory
berghofe@15261
    30
  val preprocess: theory -> thm list -> thm list
berghofe@11520
    31
  val print_codegens: theory -> unit
berghofe@16649
    32
  val generate_code: theory -> (string * string) list -> (string * string) list
berghofe@16649
    33
  val generate_code_i: theory -> (string * term) list -> (string * string) list
berghofe@16769
    34
  val assoc_consts: (xstring * string option * (term mixfix list *
berghofe@16769
    35
    (string * string) list)) list -> theory -> theory
berghofe@16769
    36
  val assoc_consts_i: (xstring * typ option * (term mixfix list *
berghofe@16769
    37
    (string * string) list)) list -> theory -> theory
berghofe@16769
    38
  val assoc_types: (xstring * (typ mixfix list *
berghofe@16769
    39
    (string * string) list)) list -> theory -> theory
berghofe@16769
    40
  val get_assoc_code: theory -> string -> typ ->
berghofe@16769
    41
    (term mixfix list * (string * string) list) option
berghofe@16769
    42
  val get_assoc_type: theory -> string ->
berghofe@16769
    43
    (typ mixfix list * (string * string) list) option
berghofe@16649
    44
  val invoke_codegen: theory -> deftab -> string -> string -> bool ->
berghofe@16649
    45
    codegr * term -> codegr * Pretty.T
berghofe@16649
    46
  val invoke_tycodegen: theory -> deftab -> string -> string -> bool ->
berghofe@16649
    47
    codegr * typ -> codegr * Pretty.T
berghofe@14858
    48
  val mk_id: string -> string
berghofe@16649
    49
  val mk_const_id: theory -> string -> string -> string -> string
berghofe@16649
    50
  val mk_type_id: theory -> string -> string -> string -> string
berghofe@16649
    51
  val thyname_of_type: string -> theory -> string
berghofe@16649
    52
  val thyname_of_const: string -> theory -> string
berghofe@16649
    53
  val rename_terms: term list -> term list
berghofe@11520
    54
  val rename_term: term -> term
berghofe@15398
    55
  val new_names: term -> string list -> string list
berghofe@15398
    56
  val new_name: term -> string -> string
berghofe@16649
    57
  val get_defn: theory -> deftab -> string -> typ ->
berghofe@16649
    58
    ((typ * (string * (term list * term))) * int option) option
berghofe@11520
    59
  val is_instance: theory -> typ -> typ -> bool
berghofe@11520
    60
  val parens: Pretty.T -> Pretty.T
berghofe@11520
    61
  val mk_app: bool -> Pretty.T -> Pretty.T list -> Pretty.T
berghofe@11520
    62
  val eta_expand: term -> term list -> int -> term
berghofe@14105
    63
  val strip_tname: string -> string
berghofe@13753
    64
  val mk_type: bool -> typ -> Pretty.T
berghofe@16649
    65
  val mk_term_of: theory -> string -> bool -> typ -> Pretty.T
berghofe@16649
    66
  val mk_gen: theory -> string -> bool -> string list -> string -> typ -> Pretty.T
berghofe@14105
    67
  val test_fn: (int -> (string * term) list option) ref
berghofe@14105
    68
  val test_term: theory -> int -> int -> term -> (string * term) list option
berghofe@12452
    69
  val parse_mixfix: (string -> 'a) -> string -> 'a mixfix list
berghofe@16649
    70
  val mk_deftab: theory -> deftab
berghofe@11520
    71
end;
berghofe@11520
    72
berghofe@11520
    73
structure Codegen : CODEGEN =
berghofe@11520
    74
struct
berghofe@11520
    75
berghofe@11520
    76
val quiet_mode = ref true;
berghofe@11520
    77
fun message s = if !quiet_mode then () else writeln s;
berghofe@11520
    78
berghofe@13753
    79
val mode = ref ([] : string list);
berghofe@13753
    80
berghofe@13886
    81
val margin = ref 80;
berghofe@13886
    82
berghofe@11520
    83
(**** Mixfix syntax ****)
berghofe@11520
    84
berghofe@12452
    85
datatype 'a mixfix =
berghofe@11520
    86
    Arg
berghofe@11520
    87
  | Ignore
berghofe@16769
    88
  | Module
berghofe@11520
    89
  | Pretty of Pretty.T
berghofe@12452
    90
  | Quote of 'a;
berghofe@11520
    91
berghofe@11520
    92
fun is_arg Arg = true
berghofe@11520
    93
  | is_arg Ignore = true
berghofe@11520
    94
  | is_arg _ = false;
berghofe@11520
    95
berghofe@12452
    96
fun quotes_of [] = []
berghofe@12452
    97
  | quotes_of (Quote q :: ms) = q :: quotes_of ms
berghofe@12452
    98
  | quotes_of (_ :: ms) = quotes_of ms;
berghofe@12452
    99
berghofe@12452
   100
fun args_of [] xs = ([], xs)
berghofe@12452
   101
  | args_of (Arg :: ms) (x :: xs) = apfst (cons x) (args_of ms xs)
berghofe@12452
   102
  | args_of (Ignore :: ms) (_ :: xs) = args_of ms xs
berghofe@12452
   103
  | args_of (_ :: ms) xs = args_of ms xs;
berghofe@11520
   104
skalberg@15570
   105
fun num_args x = length (List.filter is_arg x);
berghofe@11520
   106
berghofe@11520
   107
berghofe@11520
   108
(**** theory data ****)
berghofe@11520
   109
berghofe@16649
   110
(* preprocessed definition table *)
berghofe@16649
   111
berghofe@16649
   112
type deftab =
berghofe@16649
   113
  (typ *              (* type of constant *)
berghofe@16649
   114
    (string *         (* name of theory containing definition of constant *)
berghofe@16649
   115
      (term list *    (* parameters *)
berghofe@16649
   116
       term)))        (* right-hand side *)
berghofe@16649
   117
  list Symtab.table;
berghofe@16649
   118
berghofe@16649
   119
(* code dependency graph *)
berghofe@16649
   120
berghofe@16649
   121
type codegr =
berghofe@16649
   122
  (exn option *    (* slot for arbitrary data *)
berghofe@16649
   123
   string *        (* name of structure containing piece of code *)
berghofe@16649
   124
   string)         (* piece of code *)
berghofe@16649
   125
  Graph.T;
berghofe@16649
   126
berghofe@14105
   127
(* type of code generators *)
berghofe@11520
   128
berghofe@16649
   129
type 'a codegen =
berghofe@16649
   130
  theory ->    (* theory in which generate_code was called *)
berghofe@16649
   131
  deftab ->    (* definition table (for efficiency) *)
berghofe@16649
   132
  codegr ->    (* code dependency graph *)
berghofe@16649
   133
  string ->    (* node name of caller (for recording dependencies) *)
berghofe@16649
   134
  string ->    (* theory name of caller (for modular code generation) *)
berghofe@16649
   135
  bool ->      (* whether to parenthesize generated expression *)
berghofe@16649
   136
  'a ->        (* item to generate code from *)
berghofe@16649
   137
  (codegr * Pretty.T) option;
berghofe@12452
   138
berghofe@14105
   139
(* parameters for random testing *)
berghofe@14105
   140
berghofe@14105
   141
type test_params =
berghofe@14105
   142
  {size: int, iterations: int, default_type: typ option};
berghofe@14105
   143
berghofe@14105
   144
fun merge_test_params
berghofe@14105
   145
  {size = size1, iterations = iterations1, default_type = default_type1}
berghofe@14105
   146
  {size = size2, iterations = iterations2, default_type = default_type2} =
berghofe@14105
   147
  {size = Int.max (size1, size2),
berghofe@14105
   148
   iterations = Int.max (iterations1, iterations2),
berghofe@14105
   149
   default_type = case default_type1 of
skalberg@15531
   150
       NONE => default_type2
berghofe@14105
   151
     | _ => default_type1};
berghofe@14105
   152
berghofe@14105
   153
val default_test_params : test_params =
skalberg@15531
   154
  {size = 10, iterations = 100, default_type = NONE};
berghofe@14105
   155
berghofe@14105
   156
fun set_size size ({iterations, default_type, ...} : test_params) =
berghofe@14105
   157
  {size = size, iterations = iterations, default_type = default_type};
berghofe@14105
   158
berghofe@14105
   159
fun set_iterations iterations ({size, default_type, ...} : test_params) =
berghofe@14105
   160
  {size = size, iterations = iterations, default_type = default_type};
berghofe@14105
   161
wenzelm@16458
   162
fun set_default_type s thy ({size, iterations, ...} : test_params) =
berghofe@14105
   163
  {size = size, iterations = iterations,
wenzelm@16458
   164
   default_type = SOME (typ_of (read_ctyp thy s))};
berghofe@14105
   165
berghofe@14105
   166
(* data kind 'Pure/codegen' *)
wenzelm@16458
   167
wenzelm@16458
   168
structure CodegenData = TheoryDataFun
wenzelm@16458
   169
(struct
berghofe@11520
   170
  val name = "Pure/codegen";
berghofe@11520
   171
  type T =
berghofe@12452
   172
    {codegens : (string * term codegen) list,
berghofe@12452
   173
     tycodegens : (string * typ codegen) list,
berghofe@16769
   174
     consts : ((string * typ) * (term mixfix list * (string * string) list)) list,
berghofe@16769
   175
     types : (string * (typ mixfix list * (string * string) list)) list,
berghofe@14197
   176
     attrs: (string * (Args.T list -> theory attribute * Args.T list)) list,
berghofe@15261
   177
     preprocs: (stamp * (theory -> thm list -> thm list)) list,
berghofe@14105
   178
     test_params: test_params};
berghofe@11520
   179
berghofe@12555
   180
  val empty =
berghofe@14105
   181
    {codegens = [], tycodegens = [], consts = [], types = [], attrs = [],
berghofe@15261
   182
     preprocs = [], test_params = default_test_params};
berghofe@11520
   183
  val copy = I;
wenzelm@16458
   184
  val extend = I;
berghofe@11520
   185
wenzelm@16458
   186
  fun merge _
berghofe@12555
   187
    ({codegens = codegens1, tycodegens = tycodegens1,
berghofe@14105
   188
      consts = consts1, types = types1, attrs = attrs1,
berghofe@15261
   189
      preprocs = preprocs1, test_params = test_params1},
berghofe@12555
   190
     {codegens = codegens2, tycodegens = tycodegens2,
berghofe@14105
   191
      consts = consts2, types = types2, attrs = attrs2,
berghofe@15261
   192
      preprocs = preprocs2, test_params = test_params2}) =
berghofe@15261
   193
    {codegens = merge_alists' codegens1 codegens2,
berghofe@15261
   194
     tycodegens = merge_alists' tycodegens1 tycodegens2,
berghofe@12555
   195
     consts = merge_alists consts1 consts2,
berghofe@12555
   196
     types = merge_alists types1 types2,
berghofe@14105
   197
     attrs = merge_alists attrs1 attrs2,
berghofe@15261
   198
     preprocs = merge_alists' preprocs1 preprocs2,
berghofe@14105
   199
     test_params = merge_test_params test_params1 test_params2};
berghofe@11520
   200
wenzelm@16458
   201
  fun print _ ({codegens, tycodegens, ...} : T) =
berghofe@12452
   202
    Pretty.writeln (Pretty.chunks
berghofe@12452
   203
      [Pretty.strs ("term code generators:" :: map fst codegens),
berghofe@12452
   204
       Pretty.strs ("type code generators:" :: map fst tycodegens)]);
wenzelm@16458
   205
end);
berghofe@11520
   206
wenzelm@15801
   207
val _ = Context.add_setup [CodegenData.init];
berghofe@11520
   208
val print_codegens = CodegenData.print;
berghofe@11520
   209
berghofe@11520
   210
berghofe@14105
   211
(**** access parameters for random testing ****)
berghofe@14105
   212
berghofe@14105
   213
fun get_test_params thy = #test_params (CodegenData.get thy);
berghofe@14105
   214
berghofe@14105
   215
fun map_test_params f thy =
berghofe@15261
   216
  let val {codegens, tycodegens, consts, types, attrs, preprocs, test_params} =
berghofe@14105
   217
    CodegenData.get thy;
berghofe@14105
   218
  in CodegenData.put {codegens = codegens, tycodegens = tycodegens,
berghofe@15261
   219
    consts = consts, types = types, attrs = attrs, preprocs = preprocs,
berghofe@14105
   220
    test_params = f test_params} thy
berghofe@14105
   221
  end;
berghofe@14105
   222
berghofe@14105
   223
berghofe@12452
   224
(**** add new code generators to theory ****)
berghofe@11520
   225
berghofe@11520
   226
fun add_codegen name f thy =
berghofe@15261
   227
  let val {codegens, tycodegens, consts, types, attrs, preprocs, test_params} =
berghofe@14105
   228
    CodegenData.get thy
berghofe@11520
   229
  in (case assoc (codegens, name) of
skalberg@15531
   230
      NONE => CodegenData.put {codegens = (name, f) :: codegens,
berghofe@12555
   231
        tycodegens = tycodegens, consts = consts, types = types,
berghofe@15261
   232
        attrs = attrs, preprocs = preprocs, test_params = test_params} thy
skalberg@15531
   233
    | SOME _ => error ("Code generator " ^ name ^ " already declared"))
berghofe@12452
   234
  end;
berghofe@12452
   235
berghofe@12452
   236
fun add_tycodegen name f thy =
berghofe@15261
   237
  let val {codegens, tycodegens, consts, types, attrs, preprocs, test_params} =
berghofe@14105
   238
    CodegenData.get thy
berghofe@12452
   239
  in (case assoc (tycodegens, name) of
skalberg@15531
   240
      NONE => CodegenData.put {tycodegens = (name, f) :: tycodegens,
berghofe@12555
   241
        codegens = codegens, consts = consts, types = types,
berghofe@15261
   242
        attrs = attrs, preprocs = preprocs, test_params = test_params} thy
skalberg@15531
   243
    | SOME _ => error ("Code generator " ^ name ^ " already declared"))
berghofe@11520
   244
  end;
berghofe@11520
   245
berghofe@11520
   246
berghofe@12555
   247
(**** code attribute ****)
berghofe@12555
   248
berghofe@12555
   249
fun add_attribute name att thy =
berghofe@15261
   250
  let val {codegens, tycodegens, consts, types, attrs, preprocs, test_params} =
berghofe@14105
   251
    CodegenData.get thy
berghofe@12555
   252
  in (case assoc (attrs, name) of
skalberg@15531
   253
      NONE => CodegenData.put {tycodegens = tycodegens,
berghofe@12555
   254
        codegens = codegens, consts = consts, types = types,
berghofe@15261
   255
        attrs = if name = "" then attrs @ [(name, att)] else (name, att) :: attrs,
berghofe@15261
   256
        preprocs = preprocs,
berghofe@15261
   257
        test_params = test_params} thy
skalberg@15531
   258
    | SOME _ => error ("Code attribute " ^ name ^ " already declared"))
berghofe@12555
   259
  end;
berghofe@12555
   260
berghofe@14197
   261
fun mk_parser (a, p) = (if a = "" then Scan.succeed "" else Args.$$$ a) |-- p;
berghofe@14197
   262
berghofe@12555
   263
val code_attr =
wenzelm@15664
   264
  Attrib.syntax (Scan.peek (fn thy => foldr op || Scan.fail (map mk_parser
wenzelm@15664
   265
    (#attrs (CodegenData.get thy)))));
berghofe@12555
   266
wenzelm@15801
   267
val _ = Context.add_setup
wenzelm@15801
   268
 [Attrib.add_attributes
wenzelm@15801
   269
  [("code", (code_attr, K Attrib.undef_local_attribute),
wenzelm@15801
   270
     "declare theorems for code generation")]];
wenzelm@15801
   271
berghofe@12555
   272
berghofe@15261
   273
(**** preprocessors ****)
berghofe@15261
   274
berghofe@15261
   275
fun add_preprocessor p thy =
berghofe@15261
   276
  let val {codegens, tycodegens, consts, types, attrs, preprocs, test_params} =
berghofe@15261
   277
    CodegenData.get thy
berghofe@15261
   278
  in CodegenData.put {tycodegens = tycodegens,
berghofe@15261
   279
    codegens = codegens, consts = consts, types = types,
berghofe@15261
   280
    attrs = attrs, preprocs = (stamp (), p) :: preprocs,
berghofe@15261
   281
    test_params = test_params} thy
berghofe@15261
   282
  end;
berghofe@15261
   283
berghofe@15261
   284
fun preprocess thy ths =
berghofe@15261
   285
  let val {preprocs, ...} = CodegenData.get thy
skalberg@15570
   286
  in Library.foldl (fn (ths, (_, f)) => f thy ths) (ths, preprocs) end;
berghofe@15261
   287
berghofe@15261
   288
fun unfold_attr (thy, eqn) =
berghofe@15261
   289
  let
berghofe@15261
   290
    val (name, _) = dest_Const (head_of
berghofe@15261
   291
      (fst (Logic.dest_equals (prop_of eqn))));
berghofe@15261
   292
    fun prep thy = map (fn th =>
berghofe@15261
   293
      if name mem term_consts (prop_of th) then
berghofe@15398
   294
        rewrite_rule [eqn] (Thm.transfer thy th)
berghofe@15261
   295
      else th)
berghofe@15261
   296
  in (add_preprocessor prep thy, eqn) end;
berghofe@15261
   297
wenzelm@15801
   298
val _ = Context.add_setup [add_attribute "unfold" (Scan.succeed unfold_attr)];
wenzelm@15801
   299
berghofe@15261
   300
berghofe@11520
   301
(**** associate constants with target language code ****)
berghofe@11520
   302
skalberg@15570
   303
fun gen_assoc_consts prep_type xs thy = Library.foldl (fn (thy, (s, tyopt, syn)) =>
berghofe@11520
   304
  let
berghofe@15261
   305
    val {codegens, tycodegens, consts, types, attrs, preprocs, test_params} =
berghofe@14105
   306
      CodegenData.get thy;
wenzelm@16458
   307
    val cname = Sign.intern_const thy s;
berghofe@11520
   308
  in
wenzelm@16458
   309
    (case Sign.const_type thy cname of
skalberg@15531
   310
       SOME T =>
berghofe@11520
   311
         let val T' = (case tyopt of
skalberg@15531
   312
                NONE => T
skalberg@15531
   313
              | SOME ty =>
wenzelm@16458
   314
                  let val U = prep_type thy ty
wenzelm@16458
   315
                  in if Sign.typ_instance thy (U, T) then U
berghofe@11520
   316
                    else error ("Illegal type constraint for constant " ^ cname)
berghofe@11520
   317
                  end)
berghofe@11520
   318
         in (case assoc (consts, (cname, T')) of
skalberg@15531
   319
             NONE => CodegenData.put {codegens = codegens,
berghofe@12452
   320
               tycodegens = tycodegens,
berghofe@12555
   321
               consts = ((cname, T'), syn) :: consts,
berghofe@15261
   322
               types = types, attrs = attrs, preprocs = preprocs,
berghofe@15261
   323
               test_params = test_params} thy
skalberg@15531
   324
           | SOME _ => error ("Constant " ^ cname ^ " already associated with code"))
berghofe@11520
   325
         end
berghofe@11520
   326
     | _ => error ("Not a constant: " ^ s))
berghofe@11520
   327
  end) (thy, xs);
berghofe@11520
   328
berghofe@11520
   329
val assoc_consts_i = gen_assoc_consts (K I);
wenzelm@16458
   330
val assoc_consts = gen_assoc_consts (typ_of oo read_ctyp);
berghofe@11520
   331
wenzelm@15801
   332
berghofe@11520
   333
(**** associate types with target language types ****)
berghofe@11520
   334
skalberg@15570
   335
fun assoc_types xs thy = Library.foldl (fn (thy, (s, syn)) =>
berghofe@11520
   336
  let
berghofe@15261
   337
    val {codegens, tycodegens, consts, types, attrs, preprocs, test_params} =
berghofe@14105
   338
      CodegenData.get thy;
berghofe@16649
   339
    val tc = Sign.intern_type thy s
berghofe@11520
   340
  in
berghofe@11520
   341
    (case assoc (types, tc) of
skalberg@15531
   342
       NONE => CodegenData.put {codegens = codegens,
berghofe@12452
   343
         tycodegens = tycodegens, consts = consts,
berghofe@14105
   344
         types = (tc, syn) :: types, attrs = attrs,
berghofe@15261
   345
         preprocs = preprocs, test_params = test_params} thy
skalberg@15531
   346
     | SOME _ => error ("Type " ^ tc ^ " already associated with code"))
berghofe@11520
   347
  end) (thy, xs);
berghofe@11520
   348
berghofe@12452
   349
fun get_assoc_type thy s = assoc (#types (CodegenData.get thy), s);
wenzelm@11546
   350
berghofe@11520
   351
berghofe@11520
   352
(**** make valid ML identifiers ****)
berghofe@11520
   353
berghofe@14858
   354
fun is_ascii_letdig x = Symbol.is_ascii_letter x orelse
berghofe@14858
   355
  Symbol.is_ascii_digit x orelse Symbol.is_ascii_quasi x;
berghofe@14858
   356
berghofe@14858
   357
fun dest_sym s = (case split_last (snd (take_prefix (equal "\\") (explode s))) of
berghofe@14858
   358
    ("<" :: "^" :: xs, ">") => (true, implode xs)
berghofe@14858
   359
  | ("<" :: xs, ">") => (false, implode xs)
berghofe@14858
   360
  | _ => sys_error "dest_sym");
wenzelm@16458
   361
berghofe@14858
   362
fun mk_id s = if s = "" then "" else
berghofe@11520
   363
  let
berghofe@14858
   364
    fun check_str [] = []
berghofe@14858
   365
      | check_str xs = (case take_prefix is_ascii_letdig xs of
berghofe@14858
   366
          ([], " " :: zs) => check_str zs
berghofe@14858
   367
        | ([], z :: zs) =>
berghofe@14858
   368
          if size z = 1 then string_of_int (ord z) :: check_str zs
berghofe@14858
   369
          else (case dest_sym z of
berghofe@14858
   370
              (true, "isub") => check_str zs
berghofe@14858
   371
            | (true, "isup") => "" :: check_str zs
berghofe@14858
   372
            | (ctrl, s') => (if ctrl then "ctrl_" ^ s' else s') :: check_str zs)
berghofe@14858
   373
        | (ys, zs) => implode ys :: check_str zs);
berghofe@14858
   374
    val s' = space_implode "_"
skalberg@15570
   375
      (List.concat (map (check_str o Symbol.explode) (NameSpace.unpack s)))
berghofe@11520
   376
  in
berghofe@14858
   377
    if Symbol.is_ascii_letter (hd (explode s')) then s' else "id_" ^ s'
berghofe@11520
   378
  end;
berghofe@11520
   379
berghofe@16649
   380
fun extrn thy f thyname s =
berghofe@16649
   381
  let
berghofe@16649
   382
    val xs = NameSpace.unpack s;
berghofe@16649
   383
    val s' = setmp NameSpace.long_names false (setmp NameSpace.short_names false
berghofe@16649
   384
      (setmp NameSpace.unique_names true (f thy))) s;
berghofe@16649
   385
    val xs' = NameSpace.unpack s'
berghofe@16649
   386
  in
berghofe@16649
   387
    if "modular" mem !mode andalso length xs = length xs' andalso hd xs' = thyname
berghofe@16649
   388
    then NameSpace.pack (tl xs') else s'
berghofe@16649
   389
  end;
berghofe@16649
   390
berghofe@16649
   391
(* thyname:  theory name for caller                                        *)
berghofe@16649
   392
(* thyname': theory name for callee                                        *)
berghofe@16649
   393
(* if caller and callee reside in different theories, use qualified access *)
berghofe@16649
   394
berghofe@16649
   395
fun mk_const_id thy thyname thyname' s =
berghofe@16649
   396
  let
berghofe@16649
   397
    val s' = mk_id (extrn thy Sign.extern_const thyname' s);
berghofe@16649
   398
    val s'' = if s' mem ThmDatabase.ml_reserved then s' ^ "_const" else s'
berghofe@16649
   399
  in
berghofe@16649
   400
    if "modular" mem !mode andalso thyname <> thyname' andalso thyname' <> ""
berghofe@16649
   401
    then thyname' ^ "." ^ s'' else s''
berghofe@16649
   402
  end;
berghofe@13073
   403
berghofe@16649
   404
fun mk_type_id' f thy thyname thyname' s =
berghofe@16649
   405
  let
berghofe@16649
   406
    val s' = mk_id (extrn thy Sign.extern_type thyname' s);
berghofe@16649
   407
    val s'' = f (if s' mem ThmDatabase.ml_reserved then s' ^ "_type" else s')
berghofe@16649
   408
  in
berghofe@16649
   409
    if "modular" mem !mode andalso thyname <> thyname' andalso thyname' <> ""
berghofe@16649
   410
    then thyname' ^ "." ^ s'' else s''
berghofe@16649
   411
  end;
berghofe@16649
   412
berghofe@16649
   413
val mk_type_id = mk_type_id' I;
berghofe@16649
   414
berghofe@16649
   415
fun theory_of_type s thy = 
berghofe@16649
   416
  if Sign.declared_tyname thy s
berghofe@16649
   417
  then SOME (if_none (get_first (theory_of_type s) (Theory.parents_of thy)) thy)
berghofe@16649
   418
  else NONE;
berghofe@16649
   419
berghofe@16649
   420
fun theory_of_const s thy = 
berghofe@16649
   421
  if Sign.declared_const thy s
berghofe@16649
   422
  then SOME (if_none (get_first (theory_of_const s) (Theory.parents_of thy)) thy)
berghofe@16649
   423
  else NONE;
berghofe@16649
   424
berghofe@16649
   425
fun thyname_of_type s thy = (case theory_of_type s thy of
berghofe@16649
   426
    NONE => error ("thyname_of_type: no such type: " ^ quote s)
berghofe@16649
   427
  | SOME thy' => Context.theory_name thy');
berghofe@16649
   428
berghofe@16649
   429
fun thyname_of_const s thy = (case theory_of_const s thy of
berghofe@16649
   430
    NONE => error ("thyname_of_const: no such constant: " ^ quote s)
berghofe@16649
   431
  | SOME thy' => Context.theory_name thy');
berghofe@11520
   432
berghofe@13731
   433
fun rename_terms ts =
berghofe@11520
   434
  let
skalberg@15574
   435
    val names = foldr add_term_names
skalberg@15574
   436
      (map (fst o fst) (Drule.vars_of_terms ts)) ts;
berghofe@14858
   437
    val reserved = names inter ThmDatabase.ml_reserved;
skalberg@15570
   438
    val (illegal, alt_names) = split_list (List.mapPartial (fn s =>
skalberg@15531
   439
      let val s' = mk_id s in if s = s' then NONE else SOME (s, s') end) names)
berghofe@14858
   440
    val ps = (reserved @ illegal) ~~
berghofe@14858
   441
      variantlist (map (suffix "'") reserved @ alt_names, names);
berghofe@11520
   442
skalberg@15570
   443
    fun rename_id s = getOpt (assoc (ps, s), s);
berghofe@14858
   444
berghofe@14858
   445
    fun rename (Var ((a, i), T)) = Var ((rename_id a, i), T)
berghofe@14858
   446
      | rename (Free (a, T)) = Free (rename_id a, T)
berghofe@11520
   447
      | rename (Abs (s, T, t)) = Abs (s, T, rename t)
berghofe@11520
   448
      | rename (t $ u) = rename t $ rename u
berghofe@11520
   449
      | rename t = t;
berghofe@11520
   450
  in
berghofe@13731
   451
    map rename ts
berghofe@13731
   452
  end;
berghofe@13731
   453
berghofe@13731
   454
val rename_term = hd o rename_terms o single;
berghofe@13731
   455
berghofe@13731
   456
berghofe@13731
   457
(**** retrieve definition of constant ****)
berghofe@13731
   458
berghofe@13731
   459
fun is_instance thy T1 T2 =
berghofe@16649
   460
  Sign.typ_instance thy (T1, Type.varifyT T2);
berghofe@13731
   461
skalberg@15570
   462
fun get_assoc_code thy s T = Option.map snd (find_first (fn ((s', T'), _) =>
berghofe@13731
   463
  s = s' andalso is_instance thy T T') (#consts (CodegenData.get thy)));
berghofe@13731
   464
berghofe@16769
   465
fun get_aux_code xs = List.mapPartial (fn (m, code) =>
berghofe@16769
   466
  if m = "" orelse m mem !mode then SOME code else NONE) xs;
berghofe@16769
   467
berghofe@16649
   468
fun mk_deftab thy =
berghofe@13731
   469
  let
berghofe@16649
   470
    val axmss = map (fn thy' =>
berghofe@16649
   471
      (Context.theory_name thy', snd (#axioms (Theory.rep_theory thy'))))
berghofe@16649
   472
      (thy :: Theory.ancestors_of thy);
berghofe@15261
   473
    fun prep_def def = (case preprocess thy [def] of
berghofe@16649
   474
      [def'] => prop_of def' | _ => error "mk_deftab: bad preprocessor");
berghofe@15261
   475
    fun dest t =
berghofe@15261
   476
      let
berghofe@15261
   477
        val (lhs, rhs) = Logic.dest_equals t;
berghofe@15261
   478
        val (c, args) = strip_comb lhs;
berghofe@16649
   479
        val (s, T) = dest_Const c
berghofe@16649
   480
      in if forall is_Var args then SOME (s, (T, (args, rhs))) else NONE
skalberg@15531
   481
      end handle TERM _ => NONE;
berghofe@16649
   482
    fun add_def thyname (defs, (name, t)) = (case dest t of
berghofe@16649
   483
        NONE => defs
berghofe@16649
   484
      | SOME _ => (case dest (prep_def (Thm.get_axiom thy name)) of
berghofe@16649
   485
          NONE => defs
berghofe@16649
   486
        | SOME (s, (T, (args, rhs))) => Symtab.update
berghofe@16649
   487
            ((s, (T, (thyname, split_last (rename_terms (args @ [rhs])))) ::
berghofe@16649
   488
            if_none (Symtab.lookup (defs, s)) []), defs)))
berghofe@13731
   489
  in
berghofe@16649
   490
    foldl (fn ((thyname, axms), defs) =>
berghofe@16649
   491
      Symtab.foldl (add_def thyname) (defs, axms)) Symtab.empty axmss
berghofe@11520
   492
  end;
berghofe@11520
   493
berghofe@16649
   494
fun get_defn thy defs s T = (case Symtab.lookup (defs, s) of
berghofe@16649
   495
    NONE => NONE
berghofe@16649
   496
  | SOME ds =>
berghofe@16649
   497
      let val i = find_index (is_instance thy T o fst) ds
berghofe@16649
   498
      in if i >= 0 then
berghofe@16649
   499
          SOME (List.nth (ds, i), if length ds = 1 then NONE else SOME i)
berghofe@16649
   500
        else NONE
berghofe@16649
   501
      end);
berghofe@16649
   502
berghofe@11520
   503
berghofe@12452
   504
(**** invoke suitable code generator for term / type ****)
berghofe@11520
   505
berghofe@16649
   506
fun invoke_codegen thy defs dep thyname brack (gr, t) = (case get_first
berghofe@16649
   507
   (fn (_, f) => f thy defs gr dep thyname brack t) (#codegens (CodegenData.get thy)) of
skalberg@15531
   508
      NONE => error ("Unable to generate code for term:\n" ^
berghofe@16649
   509
        Sign.string_of_term thy t ^ "\nrequired by:\n" ^
berghofe@11520
   510
        commas (Graph.all_succs gr [dep]))
skalberg@15531
   511
    | SOME x => x);
berghofe@12452
   512
berghofe@16649
   513
fun invoke_tycodegen thy defs dep thyname brack (gr, T) = (case get_first
berghofe@16649
   514
   (fn (_, f) => f thy defs gr dep thyname brack T) (#tycodegens (CodegenData.get thy)) of
skalberg@15531
   515
      NONE => error ("Unable to generate code for type:\n" ^
berghofe@16649
   516
        Sign.string_of_typ thy T ^ "\nrequired by:\n" ^
berghofe@12452
   517
        commas (Graph.all_succs gr [dep]))
skalberg@15531
   518
    | SOME x => x);
berghofe@11520
   519
berghofe@11520
   520
berghofe@11520
   521
(**** code generator for mixfix expressions ****)
berghofe@11520
   522
berghofe@11520
   523
fun parens p = Pretty.block [Pretty.str "(", p, Pretty.str ")"];
berghofe@11520
   524
berghofe@11520
   525
fun pretty_fn [] p = [p]
berghofe@11520
   526
  | pretty_fn (x::xs) p = Pretty.str ("fn " ^ x ^ " =>") ::
berghofe@11520
   527
      Pretty.brk 1 :: pretty_fn xs p;
berghofe@11520
   528
berghofe@16769
   529
fun pretty_mixfix _ _ [] [] _ = []
berghofe@16769
   530
  | pretty_mixfix module module' (Arg :: ms) (p :: ps) qs =
berghofe@16769
   531
      p :: pretty_mixfix module module' ms ps qs
berghofe@16769
   532
  | pretty_mixfix module module' (Ignore :: ms) ps qs =
berghofe@16769
   533
      pretty_mixfix module module' ms ps qs
berghofe@16769
   534
  | pretty_mixfix module module' (Module :: ms) ps qs =
berghofe@16769
   535
      (if "modular" mem !mode andalso module <> module'
berghofe@16769
   536
       then cons (Pretty.str (module' ^ ".")) else I)
berghofe@16769
   537
      (pretty_mixfix module module' ms ps qs)
berghofe@16769
   538
  | pretty_mixfix module module' (Pretty p :: ms) ps qs =
berghofe@16769
   539
      p :: pretty_mixfix module module' ms ps qs
berghofe@16769
   540
  | pretty_mixfix module module' (Quote _ :: ms) ps (q :: qs) =
berghofe@16769
   541
      q :: pretty_mixfix module module' ms ps qs;
berghofe@11520
   542
berghofe@11520
   543
berghofe@12452
   544
(**** default code generators ****)
berghofe@11520
   545
berghofe@11520
   546
fun eta_expand t ts i =
berghofe@11520
   547
  let
berghofe@11520
   548
    val (Ts, _) = strip_type (fastype_of t);
berghofe@11520
   549
    val j = i - length ts
berghofe@11520
   550
  in
skalberg@15574
   551
    foldr (fn (T, t) => Abs ("x", T, t))
skalberg@15574
   552
      (list_comb (t, ts @ map Bound (j-1 downto 0))) (Library.take (j, Ts))
berghofe@11520
   553
  end;
berghofe@11520
   554
berghofe@11520
   555
fun mk_app _ p [] = p
berghofe@11520
   556
  | mk_app brack p ps = if brack then
berghofe@11520
   557
       Pretty.block (Pretty.str "(" ::
berghofe@11520
   558
         separate (Pretty.brk 1) (p :: ps) @ [Pretty.str ")"])
berghofe@11520
   559
     else Pretty.block (separate (Pretty.brk 1) (p :: ps));
berghofe@11520
   560
berghofe@14858
   561
fun new_names t xs = variantlist (map mk_id xs,
berghofe@11520
   562
  map (fst o fst o dest_Var) (term_vars t) union
berghofe@11520
   563
  add_term_names (t, ThmDatabase.ml_reserved));
berghofe@11520
   564
berghofe@11520
   565
fun new_name t x = hd (new_names t [x]);
berghofe@11520
   566
berghofe@16649
   567
fun default_codegen thy defs gr dep thyname brack t =
berghofe@11520
   568
  let
berghofe@11520
   569
    val (u, ts) = strip_comb t;
berghofe@16649
   570
    fun codegens brack = foldl_map (invoke_codegen thy defs dep thyname brack)
berghofe@11520
   571
  in (case u of
berghofe@14105
   572
      Var ((s, i), T) =>
berghofe@14105
   573
        let
berghofe@14105
   574
          val (gr', ps) = codegens true (gr, ts);
berghofe@16649
   575
          val (gr'', _) = invoke_tycodegen thy defs dep thyname false (gr', T)
skalberg@15531
   576
        in SOME (gr'', mk_app brack (Pretty.str (s ^
berghofe@11520
   577
           (if i=0 then "" else string_of_int i))) ps)
berghofe@11520
   578
        end
berghofe@11520
   579
berghofe@14105
   580
    | Free (s, T) =>
berghofe@14105
   581
        let
berghofe@14105
   582
          val (gr', ps) = codegens true (gr, ts);
berghofe@16649
   583
          val (gr'', _) = invoke_tycodegen thy defs dep thyname false (gr', T)
skalberg@15531
   584
        in SOME (gr'', mk_app brack (Pretty.str s) ps) end
berghofe@11520
   585
berghofe@11520
   586
    | Const (s, T) =>
berghofe@11520
   587
      (case get_assoc_code thy s T of
berghofe@16769
   588
         SOME (ms, aux) =>
berghofe@11520
   589
           let val i = num_args ms
berghofe@11520
   590
           in if length ts < i then
berghofe@16649
   591
               default_codegen thy defs gr dep thyname brack (eta_expand u ts i)
berghofe@11520
   592
             else
berghofe@11520
   593
               let
berghofe@12452
   594
                 val (ts1, ts2) = args_of ms ts;
berghofe@12452
   595
                 val (gr1, ps1) = codegens false (gr, ts1);
berghofe@12452
   596
                 val (gr2, ps2) = codegens true (gr1, ts2);
berghofe@12452
   597
                 val (gr3, ps3) = codegens false (gr2, quotes_of ms);
berghofe@16769
   598
                 val (thyname', suffix) = (case get_defn thy defs s T of
berghofe@16769
   599
                     NONE => (thyname_of_const s thy, "")
berghofe@16769
   600
                   | SOME ((U, (thyname', _)), NONE) => (thyname', "")
berghofe@16769
   601
                   | SOME ((U, (thyname', _)), SOME i) =>
berghofe@16769
   602
                       (thyname', "_def" ^ string_of_int i));
berghofe@16769
   603
                 val node_id = s ^ suffix;
berghofe@16769
   604
                 val p = mk_app brack (Pretty.block
berghofe@16769
   605
                   (pretty_mixfix thyname thyname' ms ps1 ps3)) ps2
berghofe@16769
   606
               in SOME (case try (Graph.get_node gr3) node_id of
berghofe@16769
   607
                   NONE => (case get_aux_code aux of
berghofe@16769
   608
                       [] => (gr3, p)
berghofe@16769
   609
                     | xs => (Graph.add_edge (node_id, dep) (Graph.new_node
berghofe@16769
   610
                         (node_id, (NONE, thyname', space_implode "\n" xs ^ "\n")) gr3), p))
berghofe@16769
   611
                 | SOME _ => (Graph.add_edge (node_id, dep) gr3, p))
berghofe@11520
   612
               end
berghofe@11520
   613
           end
berghofe@16649
   614
       | NONE => (case get_defn thy defs s T of
skalberg@15531
   615
           NONE => NONE
berghofe@16649
   616
         | SOME ((U, (thyname', (args, rhs))), k) =>
berghofe@11520
   617
             let
berghofe@16649
   618
               val suffix = (case k of NONE => "" | SOME i => "_def" ^ string_of_int i);
berghofe@16649
   619
               val node_id = s ^ suffix;
berghofe@16649
   620
               val def_id = mk_const_id thy thyname' thyname' s ^ suffix;
berghofe@16649
   621
               val call_id = mk_const_id thy thyname thyname' s ^ suffix;
berghofe@12452
   622
               val (gr', ps) = codegens true (gr, ts);
berghofe@11520
   623
             in
berghofe@16649
   624
               SOME (Graph.add_edge (node_id, dep) gr' handle Graph.UNDEF _ =>
berghofe@11520
   625
                 let
berghofe@11520
   626
                   val _ = message ("expanding definition of " ^ s);
berghofe@11520
   627
                   val (Ts, _) = strip_type T;
berghofe@11520
   628
                   val (args', rhs') =
berghofe@11520
   629
                     if not (null args) orelse null Ts then (args, rhs) else
berghofe@11520
   630
                       let val v = Free (new_name rhs "x", hd Ts)
berghofe@11520
   631
                       in ([v], betapply (rhs, v)) end;
berghofe@16649
   632
                   val (gr1, p) = invoke_codegen thy defs node_id thyname' false
berghofe@16649
   633
                     (Graph.add_edge (node_id, dep)
berghofe@16649
   634
                        (Graph.new_node (node_id, (NONE, "", "")) gr'), rhs');
berghofe@12452
   635
                   val (gr2, xs) = codegens false (gr1, args');
berghofe@16649
   636
                   val (gr3, _) = invoke_tycodegen thy defs dep thyname false (gr2, T);
berghofe@16649
   637
                   val (gr4, ty) = invoke_tycodegen thy defs node_id thyname' false (gr3, U);
berghofe@16649
   638
                 in Graph.map_node node_id (K (NONE, thyname', Pretty.string_of
berghofe@16649
   639
                   (Pretty.block (separate (Pretty.brk 1)
berghofe@16649
   640
                     (if null args' then
berghofe@16649
   641
                        [Pretty.str ("val " ^ def_id ^ " :"), ty]
berghofe@16649
   642
                      else Pretty.str ("fun " ^ def_id) :: xs) @
berghofe@16649
   643
                    [Pretty.str " =", Pretty.brk 1, p, Pretty.str ";"])) ^ "\n\n")) gr4
berghofe@16649
   644
                 end, mk_app brack (Pretty.str call_id) ps)
berghofe@11520
   645
             end))
berghofe@11520
   646
berghofe@11520
   647
    | Abs _ =>
berghofe@11520
   648
      let
berghofe@11520
   649
        val (bs, Ts) = ListPair.unzip (strip_abs_vars u);
berghofe@11520
   650
        val t = strip_abs_body u
berghofe@11520
   651
        val bs' = new_names t bs;
berghofe@12452
   652
        val (gr1, ps) = codegens true (gr, ts);
berghofe@16649
   653
        val (gr2, p) = invoke_codegen thy defs dep thyname false
berghofe@12452
   654
          (gr1, subst_bounds (map Free (rev (bs' ~~ Ts)), t));
berghofe@11520
   655
      in
skalberg@15531
   656
        SOME (gr2, mk_app brack (Pretty.block (Pretty.str "(" :: pretty_fn bs' p @
berghofe@11520
   657
          [Pretty.str ")"])) ps)
berghofe@11520
   658
      end
berghofe@11520
   659
skalberg@15531
   660
    | _ => NONE)
berghofe@11520
   661
  end;
berghofe@11520
   662
berghofe@16649
   663
fun default_tycodegen thy defs gr dep thyname brack (TVar ((s, i), _)) =
skalberg@15531
   664
      SOME (gr, Pretty.str (s ^ (if i = 0 then "" else string_of_int i)))
berghofe@16649
   665
  | default_tycodegen thy defs gr dep thyname brack (TFree (s, _)) =
berghofe@16649
   666
      SOME (gr, Pretty.str s)
berghofe@16649
   667
  | default_tycodegen thy defs gr dep thyname brack (Type (s, Ts)) =
berghofe@12452
   668
      (case assoc (#types (CodegenData.get thy), s) of
skalberg@15531
   669
         NONE => NONE
berghofe@16769
   670
       | SOME (ms, aux) =>
berghofe@12452
   671
           let
berghofe@12452
   672
             val (gr', ps) = foldl_map
berghofe@16649
   673
               (invoke_tycodegen thy defs dep thyname false)
berghofe@16649
   674
               (gr, fst (args_of ms Ts));
berghofe@12452
   675
             val (gr'', qs) = foldl_map
berghofe@16649
   676
               (invoke_tycodegen thy defs dep thyname false)
berghofe@16769
   677
               (gr', quotes_of ms);
berghofe@16769
   678
             val thyname' = thyname_of_type s thy;
berghofe@16769
   679
             val node_id = s ^ " (type)";
berghofe@16769
   680
             val p = Pretty.block (pretty_mixfix thyname thyname' ms ps qs)
berghofe@16769
   681
           in SOME (case try (Graph.get_node gr'') node_id of
berghofe@16769
   682
               NONE => (case get_aux_code aux of
berghofe@16769
   683
                   [] => (gr'', p)
berghofe@16769
   684
                 | xs => (Graph.add_edge (node_id, dep) (Graph.new_node
berghofe@16769
   685
                     (node_id, (NONE, thyname', space_implode "\n" xs ^ "\n")) gr''), p))
berghofe@16769
   686
             | SOME _ => (Graph.add_edge (node_id, dep) gr'', p))
berghofe@16769
   687
           end);
berghofe@12452
   688
wenzelm@15801
   689
val _ = Context.add_setup
wenzelm@15801
   690
 [add_codegen "default" default_codegen,
wenzelm@15801
   691
  add_tycodegen "default" default_tycodegen];
wenzelm@15801
   692
berghofe@11520
   693
berghofe@16649
   694
fun mk_struct name s = "structure " ^ name ^ " =\nstruct\n\n" ^ s ^ "end;\n";
berghofe@16649
   695
berghofe@16649
   696
fun add_to_module name s ms =
berghofe@16649
   697
  overwrite (ms, (name, the (assoc (ms, name)) ^ s));
berghofe@16649
   698
berghofe@16649
   699
fun output_code gr xs =
berghofe@16649
   700
  let
berghofe@16649
   701
    val code =
berghofe@16649
   702
      map (fn s => (s, Graph.get_node gr s)) (rev (Graph.all_preds gr xs))
berghofe@16649
   703
    fun string_of_cycle (a :: b :: cs) =
berghofe@16649
   704
          let val SOME (x, y) = get_first (fn (x, (_, a', _)) =>
berghofe@16649
   705
            if a = a' then Option.map (pair x)
berghofe@16649
   706
              (find_first (equal b o #2 o Graph.get_node gr)
berghofe@16649
   707
                (Graph.imm_succs gr x))
berghofe@16649
   708
            else NONE) code
berghofe@16649
   709
          in x ^ " called by " ^ y ^ "\n" ^ string_of_cycle (b :: cs) end
berghofe@16649
   710
      | string_of_cycle _ = ""
berghofe@16649
   711
  in
berghofe@16649
   712
    if "modular" mem !mode then
berghofe@16649
   713
      let
berghofe@16649
   714
        val modules = distinct (map (#2 o snd) code);
berghofe@16649
   715
        val mod_gr = foldr (uncurry Graph.add_edge_acyclic)
berghofe@16649
   716
          (foldr (uncurry (Graph.new_node o rpair ())) Graph.empty modules)
berghofe@16649
   717
          (List.concat (map (fn (s, (_, thyname, _)) => map (pair thyname)
berghofe@16649
   718
            (filter_out (equal thyname) (map (#2 o Graph.get_node gr)
berghofe@16649
   719
              (Graph.imm_succs gr s)))) code));
berghofe@16649
   720
        val modules' =
berghofe@16649
   721
          rev (Graph.all_preds mod_gr (map (#2 o Graph.get_node gr) xs))
berghofe@16649
   722
      in
berghofe@16649
   723
        foldl (fn ((_, (_, thyname, s)), ms) => add_to_module thyname s ms)
berghofe@16649
   724
          (map (rpair "") modules') code
berghofe@16649
   725
      end handle Graph.CYCLES (cs :: _) =>
berghofe@16649
   726
        error ("Cyclic dependency of modules:\n" ^ commas cs ^
berghofe@16649
   727
          "\n" ^ string_of_cycle cs)
berghofe@16649
   728
    else [("Generated", implode (map (#3 o snd) code))]
berghofe@16649
   729
  end;
berghofe@11520
   730
berghofe@14598
   731
fun gen_generate_code prep_term thy =
berghofe@14598
   732
  setmp print_mode [] (Pretty.setmp_margin (!margin) (fn xs =>
berghofe@11520
   733
  let
berghofe@16649
   734
    val defs = mk_deftab thy;
berghofe@16649
   735
    val gr = Graph.new_node ("<Top>", (NONE, "Generated", "")) Graph.empty;
berghofe@16649
   736
    fun expand (t as Abs _) = t
berghofe@16649
   737
      | expand t = (case fastype_of t of
berghofe@16649
   738
          Type ("fun", [T, U]) => Abs ("x", T, t $ Bound 0) | _ => t);
berghofe@11520
   739
    val (gr', ps) = foldl_map (fn (gr, (s, t)) => apsnd (pair s)
berghofe@16649
   740
      (invoke_codegen thy defs "<Top>" "Generated" false (gr, t)))
berghofe@16649
   741
        (gr, map (apsnd (expand o prep_term thy)) xs);
wenzelm@14818
   742
    val code =
wenzelm@14818
   743
      space_implode "\n\n" (map (fn (s', p) => Pretty.string_of (Pretty.block
wenzelm@14818
   744
        [Pretty.str ("val " ^ s' ^ " ="), Pretty.brk 1, p, Pretty.str ";"])) ps) ^
berghofe@16649
   745
      "\n\n"
berghofe@16649
   746
  in
berghofe@16649
   747
    map (fn (name, s) => (name, mk_struct name s))
berghofe@16649
   748
      (add_to_module "Generated" code (output_code gr' ["<Top>"]))
berghofe@16649
   749
  end));
berghofe@11520
   750
berghofe@11520
   751
val generate_code_i = gen_generate_code (K I);
berghofe@11520
   752
val generate_code = gen_generate_code
wenzelm@16458
   753
  (fn thy => term_of o read_cterm thy o rpair TypeInfer.logicT);
berghofe@11520
   754
berghofe@12452
   755
berghofe@13753
   756
(**** Reflection ****)
berghofe@13753
   757
berghofe@13753
   758
val strip_tname = implode o tl o explode;
berghofe@13753
   759
berghofe@13753
   760
fun pretty_list xs = Pretty.block (Pretty.str "[" ::
skalberg@15570
   761
  List.concat (separate [Pretty.str ",", Pretty.brk 1] (map single xs)) @
berghofe@13753
   762
  [Pretty.str "]"]);
berghofe@13753
   763
berghofe@13753
   764
fun mk_type p (TVar ((s, i), _)) = Pretty.str
berghofe@13753
   765
      (strip_tname s ^ (if i = 0 then "" else string_of_int i) ^ "T")
berghofe@13753
   766
  | mk_type p (TFree (s, _)) = Pretty.str (strip_tname s ^ "T")
berghofe@13753
   767
  | mk_type p (Type (s, Ts)) = (if p then parens else I) (Pretty.block
berghofe@13753
   768
      [Pretty.str "Type", Pretty.brk 1, Pretty.str ("(\"" ^ s ^ "\","),
berghofe@13753
   769
       Pretty.brk 1, pretty_list (map (mk_type false) Ts), Pretty.str ")"]);
berghofe@13753
   770
berghofe@16649
   771
fun mk_term_of thy thyname p (TVar ((s, i), _)) = Pretty.str
berghofe@13753
   772
      (strip_tname s ^ (if i = 0 then "" else string_of_int i) ^ "F")
berghofe@16649
   773
  | mk_term_of thy thyname p (TFree (s, _)) = Pretty.str (strip_tname s ^ "F")
berghofe@16649
   774
  | mk_term_of thy thyname p (Type (s, Ts)) = (if p then parens else I)
berghofe@16649
   775
      (Pretty.block (separate (Pretty.brk 1)
berghofe@16649
   776
        (Pretty.str (mk_type_id' (fn s' => "term_of_" ^ s')
berghofe@16649
   777
          thy thyname (thyname_of_type s thy) s) ::
berghofe@16649
   778
        List.concat (map (fn T =>
berghofe@16649
   779
          [mk_term_of thy thyname true T, mk_type true T]) Ts))));
berghofe@13753
   780
berghofe@13753
   781
berghofe@14105
   782
(**** Test data generators ****)
berghofe@14105
   783
berghofe@16649
   784
fun mk_gen thy thyname p xs a (TVar ((s, i), _)) = Pretty.str
berghofe@14105
   785
      (strip_tname s ^ (if i = 0 then "" else string_of_int i) ^ "G")
berghofe@16649
   786
  | mk_gen thy thyname p xs a (TFree (s, _)) = Pretty.str (strip_tname s ^ "G")
berghofe@16649
   787
  | mk_gen thy thyname p xs a (Type (s, Ts)) = (if p then parens else I)
berghofe@16649
   788
      (Pretty.block (separate (Pretty.brk 1)
berghofe@16649
   789
        (Pretty.str (mk_type_id' (fn s' => "gen_" ^ s')
berghofe@16649
   790
          thy thyname (thyname_of_type s thy) s ^
berghofe@16649
   791
          (if s mem xs then "'" else "")) ::
berghofe@16649
   792
         map (mk_gen thy thyname true xs a) Ts @
berghofe@16649
   793
         (if s mem xs then [Pretty.str a] else []))));
berghofe@14105
   794
skalberg@15531
   795
val test_fn : (int -> (string * term) list option) ref = ref (fn _ => NONE);
berghofe@14105
   796
berghofe@15029
   797
fun test_term thy sz i = setmp print_mode [] (fn t =>
berghofe@14105
   798
  let
berghofe@14105
   799
    val _ = assert (null (term_tvars t) andalso null (term_tfrees t))
berghofe@14105
   800
      "Term to be tested contains type variables";
berghofe@14105
   801
    val _ = assert (null (term_vars t))
berghofe@14105
   802
      "Term to be tested contains schematic variables";
berghofe@14105
   803
    val frees = map dest_Free (term_frees t);
berghofe@14140
   804
    val szname = variant (map fst frees) "i";
berghofe@16649
   805
    val code = space_implode "\n" (map snd
berghofe@16649
   806
      (setmp mode ["term_of", "test"] (generate_code_i thy)
berghofe@16649
   807
        [("testf", list_abs_free (frees, t))]));
berghofe@16649
   808
    val s = "structure TestTerm =\nstruct\n\n" ^ code ^
berghofe@16649
   809
      "\nopen Generated;\n\n" ^ Pretty.string_of
berghofe@14105
   810
        (Pretty.block [Pretty.str "val () = Codegen.test_fn :=",
berghofe@14140
   811
          Pretty.brk 1, Pretty.str ("(fn " ^ szname ^ " =>"), Pretty.brk 1,
berghofe@14105
   812
          Pretty.blk (0, [Pretty.str "let", Pretty.brk 1,
berghofe@14105
   813
            Pretty.blk (0, separate Pretty.fbrk (map (fn (s, T) =>
berghofe@14886
   814
              Pretty.block [Pretty.str ("val " ^ mk_id s ^ " ="), Pretty.brk 1,
berghofe@16649
   815
              mk_gen thy "" false [] "" T, Pretty.brk 1,
berghofe@14140
   816
              Pretty.str (szname ^ ";")]) frees)),
berghofe@14105
   817
            Pretty.brk 1, Pretty.str "in", Pretty.brk 1,
berghofe@14105
   818
            Pretty.block [Pretty.str "if ",
berghofe@14886
   819
              mk_app false (Pretty.str "testf") (map (Pretty.str o mk_id o fst) frees),
skalberg@15531
   820
              Pretty.brk 1, Pretty.str "then NONE",
berghofe@14105
   821
              Pretty.brk 1, Pretty.str "else ",
skalberg@15531
   822
              Pretty.block [Pretty.str "SOME ", Pretty.block (Pretty.str "[" ::
skalberg@15570
   823
                List.concat (separate [Pretty.str ",", Pretty.brk 1]
berghofe@14105
   824
                  (map (fn (s, T) => [Pretty.block
berghofe@15326
   825
                    [Pretty.str ("(" ^ Library.quote (Symbol.escape s) ^ ","), Pretty.brk 1,
berghofe@16649
   826
                     mk_app false (mk_term_of thy "" false T)
berghofe@14886
   827
                       [Pretty.str (mk_id s)], Pretty.str ")"]]) frees)) @
berghofe@14105
   828
                  [Pretty.str "]"])]],
wenzelm@14980
   829
            Pretty.brk 1, Pretty.str "end"]), Pretty.str ");"]) ^
berghofe@14105
   830
      "\n\nend;\n";
berghofe@14105
   831
    val _ = use_text Context.ml_output false s;
skalberg@15531
   832
    fun iter f k = if k > i then NONE
berghofe@14135
   833
      else (case (f () handle Match =>
skalberg@15531
   834
          (warning "Exception Match raised in generated code"; NONE)) of
skalberg@15531
   835
        NONE => iter f (k+1) | SOME x => SOME x);
skalberg@15531
   836
    fun test k = if k > sz then NONE
berghofe@14105
   837
      else (priority ("Test data size: " ^ string_of_int k);
berghofe@14105
   838
        case iter (fn () => !test_fn k) 1 of
skalberg@15531
   839
          NONE => test (k+1) | SOME x => SOME x);
berghofe@15029
   840
  in test 0 end);
berghofe@14105
   841
berghofe@14105
   842
fun test_goal ({size, iterations, default_type}, tvinsts) i st =
berghofe@14105
   843
  let
wenzelm@16458
   844
    val thy = Toplevel.theory_of st;
berghofe@14105
   845
    fun strip (Const ("all", _) $ Abs (_, _, t)) = strip t
berghofe@14105
   846
      | strip t = t;
berghofe@14105
   847
    val (gi, frees) = Logic.goal_params
berghofe@14105
   848
      (prop_of (snd (snd (Proof.get_goal (Toplevel.proof_of st))))) i;
wenzelm@16458
   849
    val gi' = ObjectLogic.atomize_term thy (map_term_types
skalberg@15570
   850
      (map_type_tfree (fn p as (s, _) => getOpt (assoc (tvinsts, s),
skalberg@15570
   851
        getOpt (default_type,TFree p)))) (subst_bounds (frees, strip gi)));
berghofe@14105
   852
  in case test_term (Toplevel.theory_of st) size iterations gi' of
skalberg@15531
   853
      NONE => writeln "No counterexamples found."
skalberg@15531
   854
    | SOME cex => writeln ("Counterexample found:\n" ^
berghofe@14105
   855
        Pretty.string_of (Pretty.chunks (map (fn (s, t) =>
berghofe@14105
   856
          Pretty.block [Pretty.str (s ^ " ="), Pretty.brk 1,
wenzelm@16458
   857
            Sign.pretty_term thy t]) cex)))
berghofe@14105
   858
  end;
berghofe@14105
   859
berghofe@14105
   860
berghofe@12452
   861
(**** Interface ****)
berghofe@12452
   862
berghofe@13731
   863
val str = setmp print_mode [] Pretty.str;
berghofe@13731
   864
berghofe@11520
   865
fun parse_mixfix rd s =
berghofe@11520
   866
  (case Scan.finite Symbol.stopper (Scan.repeat
berghofe@11520
   867
     (   $$ "_" >> K Arg
berghofe@11520
   868
      || $$ "?" >> K Ignore
berghofe@16769
   869
      || $$ "\\<module>" >> K Module
berghofe@11520
   870
      || $$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length)
berghofe@11520
   871
      || $$ "{" |-- $$ "*" |-- Scan.repeat1
berghofe@11520
   872
           (   $$ "'" |-- Scan.one Symbol.not_eof
berghofe@11520
   873
            || Scan.unless ($$ "*" -- $$ "}") (Scan.one Symbol.not_eof)) --|
berghofe@12452
   874
         $$ "*" --| $$ "}" >> (Quote o rd o implode)
berghofe@11520
   875
      || Scan.repeat1
berghofe@11520
   876
           (   $$ "'" |-- Scan.one Symbol.not_eof
berghofe@16769
   877
            || Scan.unless ($$ "_" || $$ "?" || $$ "\\<module>" || $$ "/" || $$ "{" |-- $$ "*")
berghofe@13731
   878
                 (Scan.one Symbol.not_eof)) >> (Pretty o str o implode)))
berghofe@11520
   879
       (Symbol.explode s) of
berghofe@11520
   880
     (p, []) => p
berghofe@11520
   881
   | _ => error ("Malformed annotation: " ^ quote s));
berghofe@11520
   882
wenzelm@15801
   883
val _ = Context.add_setup
berghofe@16769
   884
  [assoc_types [("fun", (parse_mixfix (K dummyT) "(_ ->/ _)",
berghofe@16769
   885
     [("term_of",
berghofe@16769
   886
       "fun term_of_fun_type _ T _ U _ = Free (\"<function>\", T --> U);\n"),
berghofe@16769
   887
      ("test",
berghofe@16769
   888
       "fun gen_fun_type _ G i =\n\
berghofe@16769
   889
       \  let\n\
berghofe@16769
   890
       \    val f = ref (fn x => raise ERROR);\n\
berghofe@16769
   891
       \    val _ = (f := (fn x =>\n\
berghofe@16769
   892
       \      let\n\
berghofe@16769
   893
       \        val y = G i;\n\
berghofe@16769
   894
       \        val f' = !f\n\
berghofe@16769
   895
       \      in (f := (fn x' => if x = x' then y else f' x'); y) end))\n\
berghofe@16769
   896
       \  in (fn x => !f x) end;\n")]))]];
wenzelm@15801
   897
wenzelm@15801
   898
wenzelm@17057
   899
structure P = OuterParse and K = OuterKeyword;
berghofe@11520
   900
berghofe@16769
   901
fun strip_newlines s = implode (fst (take_suffix (equal "\n")
berghofe@16769
   902
  (snd (take_prefix (equal "\n") (explode s))))) ^ "\n";
berghofe@16769
   903
berghofe@16769
   904
val parse_attach = Scan.repeat (P.$$$ "attach" |--
berghofe@16769
   905
  Scan.optional (P.$$$ "(" |-- P.xname --| P.$$$ ")") "" --
berghofe@16769
   906
    (P.verbatim >> strip_newlines));
berghofe@16769
   907
berghofe@11520
   908
val assoc_typeP =
berghofe@11520
   909
  OuterSyntax.command "types_code"
wenzelm@11546
   910
  "associate types with target language types" K.thy_decl
berghofe@16769
   911
    (Scan.repeat1 (P.xname --| P.$$$ "(" -- P.string --| P.$$$ ")" -- parse_attach) >>
berghofe@12452
   912
     (fn xs => Toplevel.theory (fn thy => assoc_types
berghofe@16769
   913
       (map (fn ((name, mfx), aux) => (name, (parse_mixfix
berghofe@16769
   914
         (typ_of o read_ctyp thy) mfx, aux))) xs) thy)));
berghofe@11520
   915
berghofe@11520
   916
val assoc_constP =
berghofe@11520
   917
  OuterSyntax.command "consts_code"
wenzelm@11546
   918
  "associate constants with target language code" K.thy_decl
berghofe@11520
   919
    (Scan.repeat1
wenzelm@13003
   920
       (P.xname -- (Scan.option (P.$$$ "::" |-- P.typ)) --|
berghofe@16769
   921
        P.$$$ "(" -- P.string --| P.$$$ ")" -- parse_attach) >>
berghofe@11520
   922
     (fn xs => Toplevel.theory (fn thy => assoc_consts
berghofe@16769
   923
       (map (fn (((name, optype), mfx), aux) => (name, optype, (parse_mixfix
berghofe@16769
   924
         (term_of o read_cterm thy o rpair TypeInfer.logicT) mfx, aux)))
berghofe@11520
   925
           xs) thy)));
berghofe@11520
   926
berghofe@11520
   927
val generate_codeP =
wenzelm@11546
   928
  OuterSyntax.command "generate_code" "generates code for terms" K.thy_decl
wenzelm@13003
   929
    (Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") --
berghofe@13753
   930
     Scan.optional (P.$$$ "[" |-- P.enum "," P.xname --| P.$$$ "]") (!mode) --
wenzelm@13003
   931
     Scan.repeat1 (P.name --| P.$$$ "=" -- P.term) >>
berghofe@13753
   932
     (fn ((opt_fname, mode'), xs) => Toplevel.theory (fn thy =>
berghofe@16649
   933
       let val code = setmp mode mode' (generate_code thy) xs
berghofe@16649
   934
       in ((case opt_fname of
berghofe@16649
   935
           NONE => use_text Context.ml_output false
berghofe@16649
   936
             (space_implode "\n" (map snd code) ^ "\nopen Generated;\n")
berghofe@16649
   937
         | SOME fname =>
berghofe@16649
   938
             if "modular" mem mode' then
berghofe@16649
   939
               app (fn (name, s) => File.write
berghofe@16649
   940
                   (Path.append (Path.unpack fname) (Path.basic (name ^ ".ML"))) s)
berghofe@16649
   941
                 (("ROOT", implode (map (fn (name, _) =>
berghofe@16649
   942
                     "use \"" ^ name ^ ".ML\";\n") code)) :: code)
berghofe@16649
   943
             else File.write (Path.unpack fname) (snd (hd code))); thy)
berghofe@16649
   944
       end)));
berghofe@11520
   945
berghofe@14105
   946
val params =
berghofe@14105
   947
  [("size", P.nat >> (K o set_size)),
berghofe@14105
   948
   ("iterations", P.nat >> (K o set_iterations)),
berghofe@14105
   949
   ("default_type", P.typ >> set_default_type)];
berghofe@14105
   950
berghofe@14105
   951
val parse_test_params = P.short_ident :-- (fn s =>
skalberg@15570
   952
  P.$$$ "=" |-- getOpt (assoc (params, s), Scan.fail)) >> snd;
berghofe@14105
   953
berghofe@14105
   954
fun parse_tyinst xs =
wenzelm@16458
   955
  (P.type_ident --| P.$$$ "=" -- P.typ >> (fn (v, s) => fn thy =>
wenzelm@16458
   956
    fn (x, ys) => (x, (v, typ_of (read_ctyp thy s)) :: ys))) xs;
berghofe@14105
   957
berghofe@14105
   958
fun app [] x = x
berghofe@14105
   959
  | app (f :: fs) x = app fs (f x);
berghofe@14105
   960
berghofe@14105
   961
val test_paramsP =
berghofe@14105
   962
  OuterSyntax.command "quickcheck_params" "set parameters for random testing" K.thy_decl
berghofe@14105
   963
    (P.$$$ "[" |-- P.list1 parse_test_params --| P.$$$ "]" >>
berghofe@14105
   964
      (fn fs => Toplevel.theory (fn thy =>
berghofe@16649
   965
         map_test_params (app (map (fn f => f thy) fs)) thy)));
berghofe@14105
   966
berghofe@14105
   967
val testP =
berghofe@14105
   968
  OuterSyntax.command "quickcheck" "try to find counterexample for subgoal" K.diag
berghofe@14105
   969
  (Scan.option (P.$$$ "[" |-- P.list1
wenzelm@16458
   970
    (   parse_test_params >> (fn f => fn thy => apfst (f thy))
berghofe@14105
   971
     || parse_tyinst) --| P.$$$ "]") -- Scan.optional P.nat 1 >>
berghofe@14105
   972
    (fn (ps, g) => Toplevel.keep (fn st =>
skalberg@15570
   973
      test_goal (app (getOpt (Option.map
skalberg@15570
   974
          (map (fn f => f (Toplevel.sign_of st))) ps, []))
berghofe@14105
   975
        (get_test_params (Toplevel.theory_of st), [])) g st)));
berghofe@14105
   976
berghofe@16769
   977
val _ = OuterSyntax.add_keywords ["attach"];
berghofe@16769
   978
wenzelm@15801
   979
val _ = OuterSyntax.add_parsers
wenzelm@15801
   980
  [assoc_typeP, assoc_constP, generate_codeP, test_paramsP, testP];
berghofe@11520
   981
berghofe@11520
   982
end;