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