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