src/Pure/Tools/codegen_package.ML
author haftmann
Wed, 30 Aug 2006 08:29:30 +0200
changeset 20434 110a223ba63c
parent 20428 67fa1c6ba89e
child 20439 1bf42b262a38
permissions -rw-r--r--
fixed bug in wfrec appgen
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     1
(*  Title:      Pure/Tools/codegen_package.ML
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     2
    ID:         $Id$
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     4
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
     5
Code generator from Isabelle theories to
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     6
intermediate language ("Thin-gol").
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     7
*)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     8
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     9
signature CODEGEN_PACKAGE =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    10
sig
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    11
  val codegen_term: term -> theory -> CodegenThingol.iterm * theory;
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
    12
  val eval_term: (string (*reference name!*) * 'a ref) * term
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
    13
    -> theory -> 'a * theory;
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    14
  val is_dtcon: string -> bool;
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    15
  val consts_of_idfs: theory -> string list -> (string * typ) list;
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    16
  val idfs_of_consts: theory -> (string * typ) list -> string list;
19967
33da452f0abe slight refinements
haftmann
parents: 19956
diff changeset
    17
  val get_root_module: theory -> CodegenThingol.module * theory;
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    18
  val get_ml_fun_datatype: theory -> (string -> string)
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    19
    -> ((string * CodegenThingol.funn) list -> Pretty.T)
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    20
        * ((string * CodegenThingol.datatyp) list -> Pretty.T);
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    21
20401
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
    22
  val add_pretty_list: string -> string -> string -> (Pretty.T list -> Pretty.T)
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
    23
   -> ((string -> string) * (string -> string)) option -> int * string
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
    24
   -> theory -> theory;
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
    25
  val add_pretty_ml_string: string -> string -> string -> string
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
    26
   -> (string -> string) -> (string -> string) -> string -> theory -> theory;
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20105
diff changeset
    27
  val purge_code: theory -> theory;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    28
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    29
  type appgen;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    30
  val add_appconst: xstring * appgen -> theory -> theory;
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    31
  val add_appconst_i: string * appgen -> theory -> theory;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    32
  val appgen_default: appgen;
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
    33
  val appgen_rep_bin: (theory -> term -> IntInf.int) -> appgen;
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
    34
  val appgen_char: (term -> int option) -> appgen;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    35
  val appgen_case: (theory -> term
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    36
    -> ((string * typ) list * ((term * typ) * (term * term) list)) option)
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    37
    -> appgen;
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    38
  val appgen_let: appgen;
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    39
  val appgen_wfrec: appgen;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    40
19008
14c1b2f5dda4 improved code generator devarification
haftmann
parents: 18963
diff changeset
    41
  val print_code: theory -> unit;
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
    42
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18217
diff changeset
    43
  structure CodegenData: THEORY_DATA;
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    44
  type auxtab;
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
    45
  val mk_tabs: theory -> string list option -> (string * typ) list -> auxtab;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    46
end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    47
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    48
structure CodegenPackage : CODEGEN_PACKAGE =
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    49
struct
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    50
18850
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
    51
open CodegenThingol;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    52
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    53
(* shallow name spaces *)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    54
20216
f30b73385060 added eval_term
haftmann
parents: 20213
diff changeset
    55
val nsp_module = ""; (*a dummy by convention*)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    56
val nsp_class = "class";
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
    57
val nsp_tyco = "tyco";
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    58
val nsp_const = "const";
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
    59
val nsp_dtcon = "dtcon";
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    60
val nsp_mem = "mem";
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    61
val nsp_inst = "inst";
20216
f30b73385060 added eval_term
haftmann
parents: 20213
diff changeset
    62
val nsp_eval = "EVAL"; (*only for evaluation*)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    63
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    64
fun add_nsp shallow name =
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    65
  name
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    66
  |> NameSpace.unpack
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    67
  |> split_last
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    68
  |> apsnd (single #> cons shallow)
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    69
  |> (op @)
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    70
  |> NameSpace.pack;
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    71
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    72
fun dest_nsp nsp idf =
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    73
  let
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    74
    val idf' = NameSpace.unpack idf;
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    75
    val (idf'', idf_base) = split_last idf';
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    76
    val (modl, shallow) = split_last idf'';
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    77
  in
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    78
    if nsp = shallow
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    79
   then (SOME o NameSpace.pack) (modl @ [idf_base])
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    80
    else NONE
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    81
  end;
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
    82
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
    83
fun if_nsp nsp f idf =
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
    84
  Option.map f (dest_nsp nsp idf);
19956
f992e507020e slight improvements in code generation
haftmann
parents: 19953
diff changeset
    85
f992e507020e slight improvements in code generation
haftmann
parents: 19953
diff changeset
    86
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    87
(* code generator basics *)
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
    88
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
    89
type auxtab = (bool * string list option) * CodegenTheorems.thmtab;
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
    90
type appgen = theory -> auxtab
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    91
  -> (string * typ) * term list -> transact -> iterm * transact;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    92
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    93
val serializers = ref (
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    94
  Symtab.empty
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    95
  |> Symtab.update (
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    96
       #ml CodegenSerializer.serializers
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    97
       |> apsnd (fn seri => seri
20183
fd546b0c8a7c major simplifications for integers
haftmann
parents: 20175
diff changeset
    98
            nsp_dtcon
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
    99
            [[nsp_module], [nsp_class, nsp_tyco],
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   100
              [nsp_const, nsp_dtcon, nsp_class, nsp_mem, nsp_inst]]
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   101
          )
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   102
     )
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   103
  |> Symtab.update (
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   104
       #haskell CodegenSerializer.serializers
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   105
       |> apsnd (fn seri => seri
19953
2f54a51f1801 class package refinements, slight code generation refinements
haftmann
parents: 19884
diff changeset
   106
            (nsp_dtcon, [nsp_module, nsp_class, nsp_tyco, nsp_dtcon])
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   107
            [[nsp_module], [nsp_class], [nsp_tyco], [nsp_const,  nsp_mem],
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   108
              [nsp_dtcon], [nsp_inst]]
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   109
          )
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   110
     )
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   111
);
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   112
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   113
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   114
(* theory data for code generator *)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   115
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   116
type appgens = (int * (appgen * stamp)) Symtab.table
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   117
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   118
fun merge_appgens (x : appgens * appgens) =
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   119
  Symtab.merge (fn ((bounds1, (_, stamp1)), (bounds2, (_, stamp2))) =>
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   120
    bounds1 = bounds2 andalso stamp1 = stamp2) x
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   121
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   122
type target_data = {
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   123
  syntax_class: ((string * (string -> string option)) * stamp) Symtab.table,
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   124
  syntax_inst: unit Symtab.table,
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   125
  syntax_tyco: (itype CodegenSerializer.pretty_syntax * stamp) Symtab.table,
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   126
  syntax_const: (iterm CodegenSerializer.pretty_syntax * stamp) Symtab.table
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   127
};
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   128
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   129
fun map_target_data f { syntax_class, syntax_inst, syntax_tyco, syntax_const } =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   130
  let
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   131
    val (syntax_class, syntax_inst, syntax_tyco, syntax_const) =
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   132
      f (syntax_class, syntax_inst, syntax_tyco, syntax_const)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   133
  in {
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   134
    syntax_class = syntax_class,
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   135
    syntax_inst = syntax_inst,
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   136
    syntax_tyco = syntax_tyco,
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   137
    syntax_const = syntax_const } : target_data
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   138
  end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   139
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   140
fun merge_target_data
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   141
  ({ syntax_class = syntax_class1, syntax_inst = syntax_inst1,
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   142
       syntax_tyco = syntax_tyco1, syntax_const = syntax_const1 },
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   143
   { syntax_class = syntax_class2, syntax_inst = syntax_inst2,
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   144
       syntax_tyco = syntax_tyco2, syntax_const = syntax_const2 }) =
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   145
  { syntax_class = Symtab.merge (eq_snd (op =)) (syntax_class1, syntax_class2),
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   146
    syntax_inst = Symtab.merge (op =) (syntax_inst1, syntax_inst2),
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   147
    syntax_tyco = Symtab.merge (eq_snd (op =)) (syntax_tyco1, syntax_tyco2),
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   148
    syntax_const = Symtab.merge (eq_snd (op =)) (syntax_const1, syntax_const2) } : target_data;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   149
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   150
structure CodegenData = TheoryDataFun
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   151
(struct
19953
2f54a51f1801 class package refinements, slight code generation refinements
haftmann
parents: 19884
diff changeset
   152
  val name = "Pure/codegen_package";
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   153
  type T = {
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   154
    modl: module,
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   155
    appgens: appgens,
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   156
    target_data: target_data Symtab.table
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   157
  };
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   158
  val empty = {
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   159
    modl = empty_module,
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   160
    appgens = Symtab.empty,
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   161
    target_data =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   162
      Symtab.empty
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   163
      |> Symtab.fold (fn (target, _) =>
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   164
           Symtab.update (target,
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   165
             { syntax_class = Symtab.empty, syntax_inst = Symtab.empty,
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   166
               syntax_tyco = Symtab.empty, syntax_const = Symtab.empty })
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   167
         ) (! serializers)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   168
  } : T;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   169
  val copy = I;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   170
  val extend = I;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   171
  fun merge _ (
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   172
    { modl = modl1, appgens = appgens1,
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   173
      target_data = target_data1 },
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   174
    { modl = modl2, appgens = appgens2,
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   175
      target_data = target_data2 }
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   176
  ) = {
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   177
    modl = merge_module (modl1, modl2),
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   178
    appgens = merge_appgens (appgens1, appgens2),
19025
596fb1eb7856 simplified TableFun.join;
wenzelm
parents: 19008
diff changeset
   179
    target_data = Symtab.join (K merge_target_data) (target_data1, target_data2)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   180
  };
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   181
  fun print thy (data : T) =
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   182
    let
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   183
      val module = #modl data
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   184
    in
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   185
      (Pretty.writeln o Pretty.chunks) [pretty_module module, pretty_deps module]
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   186
    end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   187
end);
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   188
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18704
diff changeset
   189
val _ = Context.add_setup CodegenData.init;
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18704
diff changeset
   190
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   191
fun map_codegen_data f thy =
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   192
  case CodegenData.get thy
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   193
   of { modl, appgens, target_data } =>
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   194
      let val (modl, appgens, target_data) =
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   195
        f (modl, appgens, target_data)
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   196
      in CodegenData.put { modl = modl, appgens = appgens,
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   197
           target_data = target_data } thy end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   198
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   199
val print_code = CodegenData.print;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   200
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   201
val purge_code = map_codegen_data (fn (_, appgens, target_data) =>
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   202
  (empty_module, appgens, target_data));
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   203
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   204
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   205
(* name handling *)
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   206
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   207
fun idf_of_class thy class =
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   208
  CodegenNames.class thy class
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   209
  |> add_nsp nsp_class;
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   210
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   211
fun class_of_idf thy = if_nsp nsp_class (CodegenNames.class_rev thy);
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   212
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   213
fun idf_of_tyco thy tyco =
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   214
  CodegenNames.tyco thy tyco
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   215
  |> add_nsp nsp_tyco;
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   216
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   217
fun tyco_of_idf thy = if_nsp nsp_tyco (CodegenNames.tyco_rev thy);
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   218
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   219
fun idf_of_inst thy inst =
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   220
  CodegenNames.instance thy inst
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   221
  |> add_nsp nsp_inst;
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   222
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   223
fun inst_of_idf thy = if_nsp nsp_inst (CodegenNames.instance_rev thy);
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   224
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   225
fun idf_of_const thy thmtab (c_ty as (c, ty)) =
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   226
  if is_some (CodegenTheorems.get_dtyp_of_cons thmtab c_ty) then
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   227
    CodegenNames.const thy c_ty
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   228
    |> add_nsp nsp_dtcon
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   229
  else if (is_some o CodegenConsts.class_of_classop thy o CodegenConsts.typinst_of_typ thy) c_ty then
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   230
    CodegenNames.const thy c_ty
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   231
    |> add_nsp nsp_mem
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   232
  else
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   233
    CodegenNames.const thy c_ty
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   234
    |> add_nsp nsp_const;
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   235
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   236
fun idf_of_classop thy c_ty =
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   237
  CodegenNames.const thy c_ty
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   238
  |> add_nsp nsp_mem;
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   239
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   240
fun const_of_idf thy idf =
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   241
  case dest_nsp nsp_const idf
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   242
   of SOME c => CodegenNames.const_rev thy c |> SOME
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   243
    | _ => (case dest_nsp nsp_dtcon idf
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   244
   of SOME c => CodegenNames.const_rev thy c |> SOME
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   245
    | _ => (case dest_nsp nsp_mem idf
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   246
   of SOME c => CodegenNames.const_rev thy c |> SOME
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   247
    | _ => NONE));
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   248
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   249
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   250
(* application generators *)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   251
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   252
fun gen_add_appconst prep_const (raw_c, appgen) thy =
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   253
  let
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   254
    val c = prep_const thy raw_c;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   255
    val i = (length o fst o strip_type o Sign.the_const_type thy) c
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   256
  in map_codegen_data
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   257
    (fn (modl, appgens, target_data) =>
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   258
       (modl,
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   259
        appgens |> Symtab.update (c, (i, (appgen, stamp ()))),
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   260
        target_data)) thy
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   261
  end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   262
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   263
val add_appconst = gen_add_appconst Sign.intern_const;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   264
val add_appconst_i = gen_add_appconst (K I);
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   265
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   266
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   267
(* extraction kernel *)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   268
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   269
fun check_strict thy f x ((false, _), _) =
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   270
      false
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   271
  | check_strict thy f x ((_, SOME targets), _) =
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   272
      exists (
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   273
        is_none o (fn tab => Symtab.lookup tab x) o f o the
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   274
          o (Symtab.lookup ((#target_data o CodegenData.get) thy))
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   275
      ) targets
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   276
  | check_strict thy f x ((true, _), _) =
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   277
      true;
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   278
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   279
fun no_strict ((_, targets), thmtab) = ((false, targets), thmtab);
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   280
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   281
fun sortlookups_const thy thmtab (c, typ_ctxt) =
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   282
  let
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   283
    val typ_decl = case CodegenTheorems.get_fun_thms thmtab (c, typ_ctxt)
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   284
     of thms as thm :: _ => CodegenTheorems.extr_typ thy thm
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   285
      | [] => (case AxClass.class_of_param thy c
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   286
         of SOME class => (case ClassPackage.the_consts_sign thy class of (v, cs) =>
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   287
              (Logic.varifyT o map_type_tfree (fn u as (w, _) =>
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   288
                if w = v then TFree (v, [class]) else TFree u))
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   289
              ((the o AList.lookup (op =) cs) c))
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   290
          | NONE => Sign.the_const_type thy c);
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   291
  in
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   292
    Vartab.empty
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   293
    |> Sign.typ_match thy (typ_decl, typ_ctxt) 
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   294
    |> Vartab.dest
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   295
    |> map (fn (_, (sort, ty)) => ClassPackage.sortlookup thy (ty, sort))
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   296
    |> filter_out null
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   297
  end;
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   298
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   299
fun ensure_def_class thy tabs cls trns =
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   300
  let
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   301
    fun defgen_class thy (tabs as (_, thmtab)) cls trns =
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   302
      case class_of_idf thy cls
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   303
       of SOME cls =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   304
            let
19283
88172041c084 fixed clsvar bug
haftmann
parents: 19280
diff changeset
   305
              val (v, cs) = (ClassPackage.the_consts_sign thy) cls;
19953
2f54a51f1801 class package refinements, slight code generation refinements
haftmann
parents: 19884
diff changeset
   306
              val sortctxts = map (ClassPackage.sortcontext_of_typ thy o snd) cs;
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   307
              val idfs = map (idf_of_const thy thmtab) cs;
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   308
            in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   309
              trns
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   310
              |> debug_msg (fn _ => "trying defgen class declaration for " ^ quote cls)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   311
              |> fold_map (ensure_def_class thy tabs) (ClassPackage.the_superclasses thy cls)
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   312
              ||>> (fold_map (exprgen_type thy tabs) o map snd) cs
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   313
              ||>> (fold_map o fold_map) (exprgen_tyvar_sort thy tabs) sortctxts
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   314
              |-> (fn ((supcls, memtypes), sortctxts) => succeed
19283
88172041c084 fixed clsvar bug
haftmann
parents: 19280
diff changeset
   315
                (Class (supcls, (unprefix "'" v, idfs ~~ (sortctxts ~~ memtypes)))))
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   316
            end
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   317
        | _ =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   318
            trns
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   319
            |> fail ("No class definition found for " ^ quote cls);
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   320
    val cls' = idf_of_class thy cls;
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   321
  in
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   322
    trns
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   323
    |> debug_msg (fn _ => "generating class " ^ quote cls)
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   324
    |> ensure_def (defgen_class thy tabs) true ("generating class " ^ quote cls) cls'
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   325
    |> pair cls'
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   326
  end
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   327
and ensure_def_tyco thy (tabs as (_, thmtab)) tyco trns =
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   328
  let
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   329
    val tyco' = idf_of_tyco thy tyco;
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   330
    val strict = check_strict thy #syntax_tyco tyco' tabs;
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   331
    fun defgen_datatype thy (tabs as (_, thmtab)) dtco trns =
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   332
      case tyco_of_idf thy dtco
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
   333
       of SOME dtco =>
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   334
         (case CodegenTheorems.get_dtyp_spec thmtab dtco
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   335
             of SOME (vars, cos) =>
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   336
                  trns
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   337
                  |> debug_msg (fn _ => "trying defgen datatype for " ^ quote dtco)
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   338
                  |> fold_map (exprgen_tyvar_sort thy tabs) vars
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   339
                  ||>> fold_map (fn (c, tys) =>
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   340
                    fold_map (exprgen_type thy tabs) tys
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   341
                    #-> (fn tys' => pair (idf_of_const thy thmtab (c, tys ---> Type (dtco, map TFree vars)), tys'))) cos
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   342
                  |-> (fn (vars, cos) => succeed (Datatype
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   343
                       (vars, cos)))
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
   344
              | NONE =>
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
   345
                  trns
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   346
                  |> fail ("No datatype found for " ^ quote dtco))
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
   347
        | NONE =>
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
   348
            trns
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   349
            |> fail ("Not a type constructor: " ^ quote dtco)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   350
  in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   351
    trns
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   352
    |> debug_msg (fn _ => "generating type constructor " ^ quote tyco)
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   353
    |> ensure_def (defgen_datatype thy tabs) strict
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   354
        ("generating type constructor " ^ quote tyco) tyco'
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   355
    |> pair tyco'
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   356
  end
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   357
and exprgen_tyvar_sort thy tabs (v, sort) trns =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   358
  trns
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   359
  |> fold_map (ensure_def_class thy tabs) (ClassPackage.operational_sort_of thy sort)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   360
  |-> (fn sort => pair (unprefix "'" v, sort))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   361
and exprgen_type thy tabs (TVar _) trns =
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   362
      error "TVar encountered in typ during code generation"
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   363
  | exprgen_type thy tabs (TFree v_s) trns =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   364
      trns
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   365
      |> exprgen_tyvar_sort thy tabs v_s
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   366
      |-> (fn (v, sort) => pair (ITyVar v))
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   367
  | exprgen_type thy tabs (Type ("fun", [t1, t2])) trns =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   368
      trns
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   369
      |> exprgen_type thy tabs t1
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   370
      ||>> exprgen_type thy tabs t2
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   371
      |-> (fn (t1', t2') => pair (t1' `-> t2'))
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   372
  | exprgen_type thy tabs (Type (tyco, tys)) trns =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   373
      trns
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   374
      |> ensure_def_tyco thy tabs tyco
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   375
      ||>> fold_map (exprgen_type thy tabs) tys
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   376
      |-> (fn (tyco, tys) => pair (tyco `%% tys));
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   377
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   378
fun exprgen_classlookup thy tabs (ClassPackage.Instance (inst, ls)) trns =
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   379
      trns
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   380
      |> ensure_def_inst thy tabs inst
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   381
      ||>> (fold_map o fold_map) (exprgen_classlookup thy tabs) ls
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   382
      |-> (fn (inst, ls) => pair (Instance (inst, ls)))
19253
f3ce97b5661a refined representation of instance dictionaries
haftmann
parents: 19213
diff changeset
   383
  | exprgen_classlookup thy tabs (ClassPackage.Lookup (clss, (v, (i, j)))) trns =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   384
      trns
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   385
      |> fold_map (ensure_def_class thy tabs) clss
19253
f3ce97b5661a refined representation of instance dictionaries
haftmann
parents: 19213
diff changeset
   386
      |-> (fn clss => pair (Lookup (clss, (v |> unprefix "'", if j = 1 then ~1 else i))))
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   387
and mk_fun thy (tabs as (_, thmtab)) (c, ty) trns =
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   388
  case CodegenTheorems.get_fun_thms thmtab (c, ty)
19953
2f54a51f1801 class package refinements, slight code generation refinements
haftmann
parents: 19884
diff changeset
   389
   of eq_thms as eq_thm :: _ =>
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   390
        let
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   391
          val msg = cat_lines ("generating code for theorems " :: map string_of_thm eq_thms);
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   392
          val ty = (Logic.unvarifyT o CodegenTheorems.extr_typ thy) eq_thm
19953
2f54a51f1801 class package refinements, slight code generation refinements
haftmann
parents: 19884
diff changeset
   393
          val sortcontext = ClassPackage.sortcontext_of_typ thy ty;
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   394
          fun dest_eqthm eq_thm =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   395
            let
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   396
              val ((t, args), rhs) =
19953
2f54a51f1801 class package refinements, slight code generation refinements
haftmann
parents: 19884
diff changeset
   397
                (apfst strip_comb o Logic.dest_equals o Logic.legacy_unvarify o prop_of) eq_thm;
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   398
            in case t
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   399
             of Const (c', _) => if c' = c then (args, rhs)
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   400
                 else error ("Illegal function equation for " ^ quote c
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   401
                   ^ ", actually defining " ^ quote c')
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   402
              | _ => error ("Illegal function equation for " ^ quote c)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   403
            end;
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   404
          fun exprgen_eq (args, rhs) trns =
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   405
            trns
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   406
            |> fold_map (exprgen_term thy tabs) args
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   407
            ||>> exprgen_term thy tabs rhs;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   408
          fun checkvars (args, rhs) =
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   409
            if CodegenThingol.vars_distinct args then (args, rhs)
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   410
            else error ("Repeated variables on left hand side of function")
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   411
        in
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   412
          trns
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   413
          |> message msg (fn trns => trns
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   414
          |> fold_map (exprgen_eq o dest_eqthm) eq_thms
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   415
          |-> (fn eqs => pair (map checkvars eqs))
19953
2f54a51f1801 class package refinements, slight code generation refinements
haftmann
parents: 19884
diff changeset
   416
          ||>> fold_map (exprgen_tyvar_sort thy tabs) sortcontext
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   417
          ||>> exprgen_type thy tabs ty
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   418
          |-> (fn ((eqs, sortctxt), ty) => (pair o SOME) ((eqs, (sortctxt, ty)),
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   419
            map snd sortcontext)))
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   420
        end
19953
2f54a51f1801 class package refinements, slight code generation refinements
haftmann
parents: 19884
diff changeset
   421
    | [] => (NONE, trns)
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   422
and ensure_def_inst thy tabs (cls, tyco) trns =
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   423
  let
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   424
    fun defgen_inst thy (tabs as (_, thmtab)) inst trns =
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   425
      case inst_of_idf thy inst
19956
f992e507020e slight improvements in code generation
haftmann
parents: 19953
diff changeset
   426
       of SOME (class, tyco) =>
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   427
            let
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   428
              val (arity, memdefs) = ClassPackage.the_inst_sign thy (class, tyco);
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   429
              val (_, members) = ClassPackage.the_consts_sign thy class;
19953
2f54a51f1801 class package refinements, slight code generation refinements
haftmann
parents: 19884
diff changeset
   430
              val arity_typ = Type (tyco, (map TFree arity));
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   431
              val operational_arity = map_filter (fn (v, sort) =>
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   432
                case ClassPackage.operational_sort_of thy sort
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   433
                 of [] => NONE
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   434
                  | sort => SOME (v, sort)) arity;
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   435
              fun gen_suparity supclass trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   436
                trns
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   437
                |> ensure_def_class thy tabs supclass
19953
2f54a51f1801 class package refinements, slight code generation refinements
haftmann
parents: 19884
diff changeset
   438
                ||>> fold_map (exprgen_classlookup thy tabs)
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   439
                      (ClassPackage.sortlookup thy (arity_typ, [supclass]));
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   440
              fun gen_membr ((m0, ty0), (m, ty)) trns =
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   441
                trns
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   442
                |> ensure_def_const thy tabs (m0, ty0)
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   443
                ||>> exprgen_term thy tabs (Const (m, ty));
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   444
            in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   445
              trns
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   446
              |> debug_msg (fn _ => "trying defgen class instance for (" ^ quote cls
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   447
                   ^ ", " ^ quote tyco ^ ")")
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   448
              |> ensure_def_class thy tabs class
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   449
              ||>> ensure_def_tyco thy tabs tyco
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   450
              ||>> fold_map (exprgen_tyvar_sort thy tabs) arity
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   451
              ||>> fold_map gen_suparity (ClassPackage.the_superclasses thy class)
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   452
              ||>> fold_map gen_membr (members ~~ memdefs)
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   453
              |-> (fn ((((class, tyco), arity), suparities), memdefs) =>
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   454
                     succeed (Classinst ((class, (tyco, arity)), (suparities, memdefs))))
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   455
            end
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   456
        | _ =>
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   457
            trns |> fail ("No class instance found for " ^ quote inst);
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   458
    val inst = idf_of_inst thy (cls, tyco);
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   459
  in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   460
    trns
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   461
    |> debug_msg (fn _ => "generating instance " ^ quote cls ^ " / " ^ quote tyco)
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   462
    |> ensure_def (defgen_inst thy tabs) true
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   463
         ("generating instance " ^ quote cls ^ " / " ^ quote tyco) inst
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   464
    |> pair inst
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   465
  end
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   466
and ensure_def_const thy (tabs as (_, thmtab)) (c, ty) trns =
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   467
  let
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   468
    fun defgen_datatypecons thy (tabs as (_, thmtab)) co trns =
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   469
      case CodegenTheorems.get_dtyp_of_cons thmtab ((the o const_of_idf thy) co)
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   470
       of SOME tyco =>
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   471
            trns
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   472
            |> debug_msg (fn _ => "trying defgen datatype constructor for " ^ quote co)
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   473
            |> ensure_def_tyco thy tabs tyco
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   474
            |-> (fn _ => succeed Bot)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   475
        | _ =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   476
            trns
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   477
            |> fail ("Not a datatype constructor: "
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   478
                ^ (quote o CodegenConsts.string_of_const_typ thy) (c, ty));
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   479
    fun defgen_clsmem thy (tabs as (_, thmtab)) m trns =
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   480
      case CodegenConsts.class_of_classop thy
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   481
        ((CodegenConsts.typinst_of_typ thy o the o const_of_idf thy) m)
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   482
       of SOME class =>
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   483
            trns
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   484
            |> debug_msg (fn _ => "trying defgen class member for " ^ quote m)
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   485
            |> ensure_def_class thy tabs class
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   486
            |-> (fn _ => succeed Bot)
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   487
        | _ =>
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   488
            trns |> fail ("No class found for " ^ (quote o CodegenConsts.string_of_const_typ thy) (c, ty))
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   489
    fun defgen_funs thy (tabs as (_, thmtab)) c' trns =
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   490
        trns
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   491
        |> mk_fun thy tabs ((the o const_of_idf thy) c')
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   492
        |-> (fn SOME (funn, _) => succeed (Fun funn)
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   493
              | NONE => fail ("No defining equations found for "
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   494
                   ^ (quote o CodegenConsts.string_of_const_typ thy) (c, ty)))
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   495
    fun get_defgen tabs idf strict =
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   496
      if (is_some oo dest_nsp) nsp_const idf
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   497
      then defgen_funs thy tabs strict
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   498
      else if (is_some oo dest_nsp) nsp_mem idf
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   499
      then defgen_clsmem thy tabs strict
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   500
      else if (is_some oo dest_nsp) nsp_dtcon idf
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   501
      then defgen_datatypecons thy tabs strict
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   502
      else error ("Illegal shallow name space for constant: " ^ quote idf);
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   503
    val idf = idf_of_const thy thmtab (c, ty);
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   504
    val strict = check_strict thy #syntax_const idf tabs;
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   505
  in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   506
    trns
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   507
    |> debug_msg (fn _ => "generating constant "
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   508
        ^ (quote o CodegenConsts.string_of_const_typ thy) (c, ty))
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   509
    |> ensure_def (get_defgen tabs idf) strict ("generating constant "
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   510
         ^ CodegenConsts.string_of_const_typ thy (c, ty)) idf
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
   511
    |> pair idf
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   512
  end
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   513
and exprgen_term thy tabs (Const (f, ty)) trns =
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   514
      trns
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   515
      |> appgen thy tabs ((f, ty), [])
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   516
      |-> (fn e => pair e)
18912
dd168daf172d improvement in devarifications
haftmann
parents: 18885
diff changeset
   517
  | exprgen_term thy tabs (Var _) trns =
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   518
      error "Var encountered in term during code generation"
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   519
  | exprgen_term thy tabs (Free (v, ty)) trns =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   520
      trns
18912
dd168daf172d improvement in devarifications
haftmann
parents: 18885
diff changeset
   521
      |> exprgen_type thy tabs ty
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   522
      |-> (fn ty => pair (IVar v))
19967
33da452f0abe slight refinements
haftmann
parents: 19956
diff changeset
   523
  | exprgen_term thy tabs (Abs (raw_v, ty, raw_t)) trns =
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   524
      let
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   525
        val (v, t) = Syntax.variant_abs (CodegenNames.purify_var raw_v, ty, raw_t);
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   526
      in
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   527
        trns
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   528
        |> exprgen_type thy tabs ty
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   529
        ||>> exprgen_term thy tabs t
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   530
        |-> (fn (ty, e) => pair ((v, ty) `|-> e))
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   531
      end
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   532
  | exprgen_term thy tabs (t as t1 $ t2) trns =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   533
      let
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   534
        val (t', ts) = strip_comb t
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   535
      in case t'
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   536
       of Const (f, ty) =>
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   537
            trns
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   538
            |> appgen thy tabs ((f, ty), ts)
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   539
            |-> (fn e => pair e)
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   540
        | _ =>
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   541
            trns
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   542
            |> exprgen_term thy tabs t'
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   543
            ||>> fold_map (exprgen_term thy tabs) ts
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   544
            |-> (fn (e, es) => pair (e `$$ es))
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   545
      end
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   546
and appgen_default thy (tabs as (_, thmtab)) ((c, ty), ts) trns =
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   547
  trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   548
  |> ensure_def_const thy tabs (c, ty)
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   549
  ||>> exprgen_type thy tabs ty
18885
ee8b5c36ba2b substantial cleanup and simplifications
haftmann
parents: 18865
diff changeset
   550
  ||>> (fold_map o fold_map) (exprgen_classlookup thy tabs)
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   551
         (sortlookups_const thy thmtab (c, ty))
18912
dd168daf172d improvement in devarifications
haftmann
parents: 18885
diff changeset
   552
  ||>> fold_map (exprgen_term thy tabs) ts
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   553
  |-> (fn (((c, ty), ls), es) =>
19202
0b9eb4b0ad98 substantial improvement in codegen iml
haftmann
parents: 19177
diff changeset
   554
         pair (IConst (c, (ls, ty)) `$$ es))
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   555
and appgen thy tabs ((f, ty), ts) trns =
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   556
  case Symtab.lookup ((#appgens o CodegenData.get) thy) f
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   557
   of SOME (i, (ag, _)) =>
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   558
        if length ts < i then
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   559
          let
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   560
            val tys = Library.take (i - length ts, ((fst o strip_type) ty));
20192
956cd30ef3be renamed Name.give_names to Name.names and moved Name.alphanum to Symbol.alphanum
haftmann
parents: 20191
diff changeset
   561
            val vs = Name.names (Name.declare f Name.context) "a" tys;
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   562
          in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   563
            trns
18912
dd168daf172d improvement in devarifications
haftmann
parents: 18885
diff changeset
   564
            |> fold_map (exprgen_type thy tabs) tys
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   565
            ||>> ag thy tabs ((f, ty), ts @ map Free vs)
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   566
            |-> (fn (tys, e) => pair (map2 (fn (v, _) => pair v) vs tys `|--> e))
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   567
          end
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   568
        else if length ts > i then
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   569
          trns
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   570
          |> ag thy tabs ((f, ty), Library.take (i, ts))
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   571
          ||>> fold_map (exprgen_term thy tabs) (Library.drop (i, ts))
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   572
          |-> (fn (e, es) => pair (e `$$ es))
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   573
        else
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   574
          trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   575
          |> ag thy tabs ((f, ty), ts)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   576
    | NONE =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   577
        trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   578
        |> appgen_default thy tabs ((f, ty), ts);
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   579
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   580
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   581
(* parametrized generators, for instantiation in HOL *)
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   582
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   583
fun appgen_rep_bin int_of_numeral thy tabs (app as (c as (_, ty), [bin])) trns =
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   584
  case try (int_of_numeral thy) bin
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   585
   of SOME i => if i < 0 then (*preprocessor eliminates negative numerals*)
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   586
        trns
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   587
        |> appgen_default thy (no_strict tabs) app
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   588
      else
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   589
        trns
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   590
        |> exprgen_term thy (no_strict tabs) (Const c)
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   591
        ||>> exprgen_term thy (no_strict tabs) bin
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   592
        |-> (fn (e1, e2) => pair (CodegenThingol.INum (i, e1 `$ e2)))
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   593
    | NONE =>
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   594
        trns
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   595
        |> appgen_default thy tabs app;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   596
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   597
fun appgen_char char_to_index thy tabs (app as ((_, ty), _)) trns =
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   598
  case (char_to_index o list_comb o apfst Const) app
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   599
   of SOME i =>
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   600
        trns
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   601
        |> exprgen_type thy tabs ty
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   602
        ||>> appgen_default thy tabs app
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   603
        |-> (fn (_, e0) => pair (IChar (chr i, e0)))
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   604
    | NONE =>
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   605
        trns
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   606
        |> appgen_default thy tabs app;
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   607
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   608
fun appgen_case dest_case_expr thy tabs (app as (c_ty, ts)) trns =
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   609
  let
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   610
    val SOME ([], ((st, sty), ds)) = dest_case_expr thy (list_comb (Const c_ty, ts));
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   611
    fun clausegen (dt, bt) trns =
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   612
      trns
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   613
      |> exprgen_term thy tabs dt
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   614
      ||>> exprgen_term thy tabs bt;
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   615
  in
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   616
    trns
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   617
    |> exprgen_term thy tabs st
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   618
    ||>> exprgen_type thy tabs sty
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   619
    ||>> fold_map clausegen ds
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   620
    ||>> appgen_default thy tabs app
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   621
    |-> (fn (((se, sty), ds), e0) => pair (ICase (((se, sty), ds), e0)))
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   622
  end;
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   623
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   624
fun appgen_let thy tabs (app as (_, [st, ct])) trns =
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   625
  trns
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   626
  |> exprgen_term thy tabs ct
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   627
  ||>> exprgen_term thy tabs st
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   628
  ||>> appgen_default thy tabs app
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   629
  |-> (fn (((v, ty) `|-> be, se), e0) =>
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   630
            pair (ICase (((se, ty), case be
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   631
              of ICase (((IVar w, _), ds), _) => if v = w then ds else [(IVar v, be)]
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   632
               | _ => [(IVar v, be)]
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   633
            ), e0))
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   634
        | (_, e0) => pair e0);
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   635
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   636
fun appgen_wfrec thy (tabs as (_, thmtab)) ((c, ty), [_, tf, tx]) trns =
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   637
  let
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   638
    val ty_def = (op ---> o apfst tl o strip_type o Logic.unvarifyT o Sign.the_const_type thy) c;
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   639
    val ty' = (op ---> o apfst tl o strip_type) ty;
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   640
    val idf = idf_of_const thy thmtab (c, ty);
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   641
  in
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   642
    trns
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   643
    |> ensure_def ((K o fail) "no extraction for wfrec") false ("generating wfrec") idf
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   644
    |> exprgen_type thy tabs ty'
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   645
    ||>> exprgen_type thy tabs ty_def
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   646
    ||>> exprgen_term thy tabs tf
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   647
    ||>> exprgen_term thy tabs tx
20434
110a223ba63c fixed bug in wfrec appgen
haftmann
parents: 20428
diff changeset
   648
    |-> (fn (((_, ty), tf), tx) => pair (IConst (idf, ([], ty)) `$ tf `$ tx))
19038
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   649
  end;
62c5f7591a43 improved handling of iml abstractions
haftmann
parents: 19025
diff changeset
   650
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   651
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   652
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   653
(** theory interface **)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   654
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   655
fun mk_tabs thy targets cs =
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   656
  ((true, targets), CodegenTheorems.mk_thmtab thy cs);
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   657
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   658
fun get_serializer target =
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   659
  case Symtab.lookup (!serializers) target
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   660
   of SOME seri => seri
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   661
    | NONE => Scan.fail_with (fn _ => "Unknown code target language: " ^ quote target) ();
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   662
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   663
fun map_module f =
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   664
  map_codegen_data (fn (modl, gens, target_data) =>
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   665
    (f modl, gens, target_data));
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   666
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   667
fun purge_defs NONE thy =
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   668
      map_module (K CodegenThingol.empty_module) thy
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20183
diff changeset
   669
  | purge_defs (SOME []) thy =
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20183
diff changeset
   670
      thy
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   671
  | purge_defs (SOME cs) thy =
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20183
diff changeset
   672
      map_module (K CodegenThingol.empty_module) thy;
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20183
diff changeset
   673
      (*let
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   674
        val tabs = mk_tabs thy NONE;
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   675
        val idfs = map (idf_of_const' thy tabs) cs;
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   676
        fun purge idfs modl =
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   677
          CodegenThingol.purge_module (filter (can (get_def modl)) idfs) modl
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   678
      in
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   679
        map_module (purge idfs) thy
20191
b43fd26e1aaa improvements for lazy code generation
haftmann
parents: 20183
diff changeset
   680
      end;*)
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   681
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   682
fun expand_module targets cs init gen arg thy =
19597
8ced57ffc090 major refinement of codegen_theorems.ML
haftmann
parents: 19571
diff changeset
   683
  thy
8ced57ffc090 major refinement of codegen_theorems.ML
haftmann
parents: 19571
diff changeset
   684
  |> CodegenTheorems.notify_dirty
8ced57ffc090 major refinement of codegen_theorems.ML
haftmann
parents: 19571
diff changeset
   685
  |> `(#modl o CodegenData.get)
8ced57ffc090 major refinement of codegen_theorems.ML
haftmann
parents: 19571
diff changeset
   686
  |> (fn (modl, thy) =>
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   687
        (start_transact init (gen thy (mk_tabs thy targets cs) arg) modl, thy))
19597
8ced57ffc090 major refinement of codegen_theorems.ML
haftmann
parents: 19571
diff changeset
   688
  |-> (fn (x, modl) => map_module (K modl) #> pair x);
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   689
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   690
fun consts_of t =
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   691
  fold_aterms (fn Const c => cons c | _ => I) t [];
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   692
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   693
fun codegen_term t thy =
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   694
  let
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   695
    val _ = Thm.cterm_of thy t;
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   696
  in
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   697
    thy
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   698
    |> expand_module (SOME []) (consts_of t) NONE exprgen_term t
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   699
  end;
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   700
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   701
val is_dtcon = has_nsp nsp_dtcon;
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   702
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   703
fun consts_of_idfs thy =
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   704
  map (the o const_of_idf thy);
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   705
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   706
fun idfs_of_consts thy cs =
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   707
  map (idf_of_const thy (snd (mk_tabs thy NONE cs))) cs;
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   708
19967
33da452f0abe slight refinements
haftmann
parents: 19956
diff changeset
   709
fun get_root_module thy =
33da452f0abe slight refinements
haftmann
parents: 19956
diff changeset
   710
  thy
33da452f0abe slight refinements
haftmann
parents: 19956
diff changeset
   711
  |> CodegenTheorems.notify_dirty
33da452f0abe slight refinements
haftmann
parents: 19956
diff changeset
   712
  |> `(#modl o CodegenData.get);
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   713
20213
2b319e945905 added eval_term
haftmann
parents: 20194
diff changeset
   714
fun eval_term (ref_spec, t) thy =
2b319e945905 added eval_term
haftmann
parents: 20194
diff changeset
   715
  let
20401
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   716
    val _ = Term.fold_atyps (fn _ =>
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   717
      error ("Term" ^ Sign.string_of_term thy t ^ "is polymorhpic"))
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   718
      (Term.fastype_of t);
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   719
    fun preprocess_term t =
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   720
      let
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   721
        val x = Free (Name.variant (add_term_names (t, [])) "x", fastype_of t);
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   722
        (* fake definition *)
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   723
        val eq = setmp quick_and_dirty true (SkipProof.make_thm thy)
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   724
          (Logic.mk_equals (x, t));
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   725
        fun err () = error "preprocess_term: bad preprocessor"
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   726
      in case map prop_of (CodegenTheorems.preprocess thy [eq])
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   727
       of [Const ("==", _) $ x' $ t'] => if x = x' then t' else err ()
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   728
        | _ => err ()
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   729
      end;
20213
2b319e945905 added eval_term
haftmann
parents: 20194
diff changeset
   730
    val target_data =
2b319e945905 added eval_term
haftmann
parents: 20194
diff changeset
   731
      ((fn data => (the o Symtab.lookup data) "ml") o #target_data o CodegenData.get) thy;
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   732
    val eval = CodegenSerializer.eval_term nsp_eval nsp_dtcon [[nsp_module], [nsp_class, nsp_tyco], [nsp_const, nsp_dtcon, nsp_class, nsp_mem, nsp_inst], [nsp_eval]]
20213
2b319e945905 added eval_term
haftmann
parents: 20194
diff changeset
   733
      ((Option.map fst oo Symtab.lookup) (#syntax_tyco target_data),
2b319e945905 added eval_term
haftmann
parents: 20194
diff changeset
   734
       (Option.map fst oo Symtab.lookup) (#syntax_const target_data))
2b319e945905 added eval_term
haftmann
parents: 20194
diff changeset
   735
      (Symtab.keys (#syntax_tyco target_data) @ Symtab.keys (#syntax_const target_data))
2b319e945905 added eval_term
haftmann
parents: 20194
diff changeset
   736
  in
2b319e945905 added eval_term
haftmann
parents: 20194
diff changeset
   737
    thy
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   738
    |> codegen_term (preprocess_term t)
20213
2b319e945905 added eval_term
haftmann
parents: 20194
diff changeset
   739
    ||>> `(#modl o CodegenData.get)
2b319e945905 added eval_term
haftmann
parents: 20194
diff changeset
   740
    |-> (fn (t', modl) => `(fn _ => eval (ref_spec, t') modl))
2b319e945905 added eval_term
haftmann
parents: 20194
diff changeset
   741
  end;
2b319e945905 added eval_term
haftmann
parents: 20194
diff changeset
   742
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   743
fun get_ml_fun_datatype thy resolv =
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   744
  let
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   745
    val target_data =
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   746
      ((fn data => (the o Symtab.lookup data) "ml") o #target_data o CodegenData.get) thy;
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   747
  in
20183
fd546b0c8a7c major simplifications for integers
haftmann
parents: 20175
diff changeset
   748
    CodegenSerializer.ml_fun_datatype nsp_dtcon
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   749
      ((Option.map fst oo Symtab.lookup o #syntax_tyco) target_data,
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   750
      (Option.map fst oo Symtab.lookup o #syntax_const) target_data)
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   751
      resolv
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   752
  end;
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   753
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   754
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   755
(** target languages **)
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   756
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   757
(* syntax *)
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   758
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   759
fun read_typ thy =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   760
  Sign.read_typ (thy, K NONE);
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   761
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   762
fun read_quote get reader consts_of gen raw thy =
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   763
  let
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   764
    val it = reader thy raw;
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   765
    val cs = consts_of it;
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   766
  in
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   767
    thy
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   768
    |> expand_module (SOME (Symtab.keys (#target_data (CodegenData.get thy)))) cs ((SOME o get) thy)
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   769
         (fn thy => fn tabs => gen thy tabs) [it]
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   770
    |-> (fn [x] => pair x)
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   771
  end;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   772
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   773
fun gen_add_syntax_class prep_class prep_const raw_class target (pretty, raw_ops) thy =
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   774
  let
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   775
    val class = (idf_of_class thy o prep_class thy) raw_class;
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   776
    val ops = (map o apfst) (idf_of_classop thy o prep_const thy) raw_ops;
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   777
    val syntax_ops = AList.lookup (op =) ops;
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   778
  in
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   779
    thy
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   780
    |> map_codegen_data
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   781
      (fn (modl, gens, target_data) =>
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   782
         (modl, gens,
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   783
          target_data |> Symtab.map_entry target
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   784
            (map_target_data
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   785
              (fn (syntax_class, syntax_inst, syntax_tyco, syntax_const) =>
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   786
               (syntax_class
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   787
                |> Symtab.update (class, ((pretty, syntax_ops), stamp ())), syntax_inst,
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   788
                     syntax_tyco, syntax_const)))))
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   789
  end;
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   790
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   791
val add_syntax_class = gen_add_syntax_class Sign.intern_class CodegenConsts.read_const_typ;
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   792
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   793
fun gen_add_syntax_inst prep_class prep_tyco (raw_class, raw_tyco) target thy =
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   794
  let
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   795
    val inst = idf_of_inst thy (prep_class thy raw_class, prep_tyco thy raw_tyco);
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   796
  in
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   797
    thy
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   798
    |> map_codegen_data
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   799
      (fn (modl, gens, target_data) =>
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   800
         (modl, gens,
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   801
          target_data |> Symtab.map_entry target
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   802
            (map_target_data
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   803
              (fn (syntax_class, syntax_inst, syntax_tyco, syntax_const) =>
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   804
               (syntax_class, syntax_inst |> Symtab.update (inst, ()),
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   805
                  syntax_tyco, syntax_const)))))
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   806
  end;
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   807
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   808
val add_syntax_inst = gen_add_syntax_inst Sign.intern_class Sign.intern_type;
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   809
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
   810
fun parse_syntax_tyco raw_tyco =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   811
  let
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   812
    fun prep_tyco thy raw_tyco =
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   813
      raw_tyco
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
   814
      |> Sign.intern_type thy
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   815
      |> idf_of_tyco thy;
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   816
    fun no_args_tyco thy raw_tyco =
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   817
      AList.lookup (op =) ((NameSpace.dest_table o #types o Type.rep_tsig o Sign.tsig_of) thy)
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   818
        (Sign.intern_type thy raw_tyco)
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   819
      |> (fn SOME ((Type.LogicalType i), _) => i);
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
   820
    fun mk reader target thy =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   821
      let
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   822
        val _ = get_serializer target;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   823
        val tyco = prep_tyco thy raw_tyco;
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   824
      in
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   825
        thy
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   826
        |> reader
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   827
        |-> (fn pretty => map_codegen_data
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   828
          (fn (modl, gens, target_data) =>
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   829
             (modl, gens,
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   830
              target_data |> Symtab.map_entry target
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   831
                (map_target_data
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   832
                  (fn (syntax_class, syntax_inst, syntax_tyco, syntax_const) =>
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   833
                   (syntax_class, syntax_inst, syntax_tyco |> Symtab.update
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   834
                      (tyco, (pretty, stamp ())),
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   835
                    syntax_const))))))
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   836
      end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   837
  in
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   838
    CodegenSerializer.parse_syntax (fn thy => no_args_tyco thy raw_tyco)
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   839
    (read_quote (fn thy => prep_tyco thy raw_tyco) read_typ (K [])
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   840
      (fn thy => fn tabs => fold_map (exprgen_type thy tabs)))
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   841
    #-> (fn reader => pair (mk reader))
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   842
  end;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   843
18704
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
   844
fun add_pretty_syntax_const c target pretty =
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
   845
  map_codegen_data
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   846
    (fn (modl, gens, target_data) =>
18704
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
   847
       (modl, gens,
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
   848
        target_data |> Symtab.map_entry target
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
   849
          (map_target_data
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   850
            (fn (syntax_class, syntax_inst, syntax_tyco, syntax_const) =>
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   851
             (syntax_class, syntax_inst, syntax_tyco,
18704
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
   852
              syntax_const
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
   853
              |> Symtab.update
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   854
                 (c, (pretty, stamp ())))))));
18704
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
   855
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
   856
fun parse_syntax_const raw_const =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   857
  let
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
   858
    fun prep_const thy raw_const =
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   859
      let
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   860
        val c_ty = CodegenConsts.read_const_typ thy raw_const
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   861
      in idf_of_const thy (snd (mk_tabs thy NONE [c_ty])) c_ty end;
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   862
    fun no_args_const thy raw_const =
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   863
      (length o fst o strip_type o snd o CodegenConsts.read_const_typ thy) raw_const;
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
   864
    fun mk reader target thy =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   865
      let
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   866
        val _ = get_serializer target;
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
   867
        val c = prep_const thy raw_const;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   868
      in
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   869
        thy
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   870
        |> reader
18704
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
   871
        |-> (fn pretty => add_pretty_syntax_const c target pretty)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   872
      end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   873
  in
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   874
    CodegenSerializer.parse_syntax (fn thy => no_args_const thy raw_const)
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   875
      (read_quote (fn thy => prep_const thy raw_const) Sign.read_term consts_of
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   876
      (fn thy => fn tabs => fold_map (exprgen_term thy tabs)))
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   877
    #-> (fn reader => pair (mk reader))
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   878
  end;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   879
20401
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   880
fun add_pretty_list target raw_nil raw_cons mk_list mk_char_string target_cons thy =
18704
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
   881
  let
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   882
    val _ = get_serializer target;
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   883
    fun prep_const raw =
18704
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
   884
      let
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   885
        val c = Sign.intern_const thy raw
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   886
      in (c, Sign.the_const_type thy c) end;
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   887
    val nil' = prep_const raw_nil;
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   888
    val cons' = prep_const raw_cons;
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   889
    val tabs = mk_tabs thy NONE [nil', cons'];
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   890
    fun mk_const c_ty =
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   891
      idf_of_const thy (snd tabs) c_ty;
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   892
    val nil'' = mk_const nil';
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   893
    val cons'' = mk_const cons';
20401
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   894
    val pr = CodegenSerializer.pretty_list nil'' cons'' mk_list mk_char_string target_cons;
18704
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
   895
  in
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
   896
    thy
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   897
    |> add_pretty_syntax_const cons'' target pr
18704
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
   898
  end;
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
   899
20401
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   900
fun add_pretty_ml_string target raw_nil raw_cons raw_str mk_char mk_string target_implode thy =
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   901
  let
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   902
    val _ = get_serializer target;
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   903
    fun prep_const raw =
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   904
      let
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   905
        val c = Sign.intern_const thy raw
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   906
      in (c, Sign.the_const_type thy c) end;
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   907
    val cs' = map prep_const [raw_nil, raw_cons, raw_str];
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   908
    val tabs = mk_tabs thy NONE cs';
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   909
    fun mk_const c_ty =
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   910
      idf_of_const thy (snd tabs) c_ty;
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   911
    val [nil', cons', str'] = map mk_const cs';
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   912
    val pr = CodegenSerializer.pretty_ml_string nil' cons' mk_char mk_string target_implode;
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   913
  in
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   914
    thy
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   915
    |> add_pretty_syntax_const str' target pr
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   916
  end;
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   917
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   918
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   919
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   920
(** code basis change notifications **)
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   921
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   922
val _ = Context.add_setup (CodegenTheorems.add_notify purge_defs);
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   923
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   924
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   925
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   926
(** toplevel interface **)
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   927
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   928
local
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   929
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   930
fun generate_code targets (SOME raw_consts) thy =
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   931
      let
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   932
        val consts = map (CodegenConsts.read_const_typ thy) raw_consts;
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   933
        val _ = case targets of SOME targets => (map get_serializer targets; ()) | _ => ();
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   934
      in
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   935
        thy
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   936
        |> expand_module targets consts NONE (fold_map oo ensure_def_const) consts
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   937
        |-> (fn cs => pair (SOME cs))
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   938
      end
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   939
  | generate_code _ NONE thy =
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   940
      (NONE, thy);
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   941
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   942
fun serialize_code target seri raw_consts thy =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   943
  let
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   944
    fun serialize cs thy =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   945
      let
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   946
        val module = (#modl o CodegenData.get) thy;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   947
        val target_data =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   948
          thy
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   949
          |> CodegenData.get
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   950
          |> #target_data
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   951
          |> (fn data => (the oo Symtab.lookup) data target);
20216
f30b73385060 added eval_term
haftmann
parents: 20213
diff changeset
   952
        val s_class = #syntax_class target_data
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   953
        val s_inst = #syntax_inst target_data
20216
f30b73385060 added eval_term
haftmann
parents: 20213
diff changeset
   954
        val s_tyco = #syntax_tyco target_data
f30b73385060 added eval_term
haftmann
parents: 20213
diff changeset
   955
        val s_const = #syntax_const target_data
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   956
      in
20216
f30b73385060 added eval_term
haftmann
parents: 20213
diff changeset
   957
        (seri (
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   958
          (Option.map fst oo Symtab.lookup) s_class,
20216
f30b73385060 added eval_term
haftmann
parents: 20213
diff changeset
   959
          (Option.map fst oo Symtab.lookup) s_tyco,
f30b73385060 added eval_term
haftmann
parents: 20213
diff changeset
   960
          (Option.map fst oo Symtab.lookup) s_const
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   961
        ) (Symtab.keys s_class @ Symtab.keys s_inst
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   962
             @ Symtab.keys s_tyco @ Symtab.keys s_const, cs) module : unit; thy)
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   963
      end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   964
  in
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   965
    thy
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   966
    |> generate_code (SOME [target]) raw_consts
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   967
    |-> (fn cs => serialize cs)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   968
  end;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   969
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   970
fun purge_consts raw_ts thy =
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   971
  let
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   972
    val cs = map (CodegenConsts.read_const_typ thy) raw_ts;
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   973
  in fold CodegenTheorems.purge_defs cs thy end;
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
   974
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   975
structure P = OuterParse
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   976
and K = OuterKeyword
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   977
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   978
in
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   979
18850
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
   980
val (generateK, serializeK,
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   981
     syntax_classK, syntax_instK, syntax_tycoK, syntax_constK,
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   982
     purgeK) =
18850
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
   983
  ("code_generate", "code_serialize",
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   984
   "code_class", "code_instance", "code_typapp", "code_constapp",
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   985
   "code_purge");
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   986
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   987
val generateP =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   988
  OuterSyntax.command generateK "generate executable code for constants" K.thy_decl (
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   989
    (Scan.option (P.$$$ "(" |-- P.list1 P.name --| P.$$$ ")")
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   990
    >> (fn SOME ["-"] => SOME [] | ts => ts))
19816
a8c8ed1c85e0 removed 'primitive definitions' added (non)strict generation, minor fixes
haftmann
parents: 19806
diff changeset
   991
    -- Scan.repeat1 P.term
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   992
    >> (fn (targets, raw_consts) =>
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   993
          Toplevel.theory (generate_code targets (SOME raw_consts) #> snd))
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   994
  );
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   995
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   996
val serializeP =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
   997
  OuterSyntax.command serializeK "serialize executable code for constants" K.thy_decl (
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   998
    P.name
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   999
    -- Scan.option (Scan.repeat1 P.term)
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1000
    #-> (fn (target, raw_consts) =>
18850
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
  1001
          P.$$$ "("
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
  1002
          |-- get_serializer target
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
  1003
          --| P.$$$ ")"
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1004
          >> (fn seri =>
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1005
            Toplevel.theory (serialize_code target seri raw_consts)
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1006
          ))
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1007
  );
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1008
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1009
val syntax_classP =
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1010
  OuterSyntax.command syntax_classK "define code syntax for class" K.thy_decl (
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1011
    Scan.repeat1 (
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1012
      P.xname
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1013
      -- Scan.repeat1 (
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
  1014
           P.name -- (P.string -- Scan.optional
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
  1015
             (P.$$$ "(" |-- Scan.repeat1 (P.term -- P.string) --| P.$$$ ")") [])
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1016
         )
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1017
    )
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1018
    >> (Toplevel.theory oo fold) (fn (raw_class, syns) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1019
          fold (fn (target, p) => add_syntax_class raw_class target p) syns)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1020
  );
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1021
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
  1022
val syntax_instP =
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
  1023
  OuterSyntax.command syntax_instK "define code syntax for instance" K.thy_decl (
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
  1024
    Scan.repeat1 (
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
  1025
      P.$$$ "(" |-- P.xname --| P.$$$ "::" -- P.xname --| P.$$$ ")"
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
  1026
      -- Scan.repeat1 P.name
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
  1027
    )
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
  1028
    >> (Toplevel.theory oo fold) (fn (raw_inst, targets) =>
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
  1029
          fold (fn target => add_syntax_inst raw_inst target) targets)
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
  1030
  );
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
  1031
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1032
val syntax_tycoP =
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1033
  OuterSyntax.command syntax_tycoK "define code syntax for type constructor" K.thy_decl (
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1034
    Scan.repeat1 (
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1035
      P.xname
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
  1036
      #-> (fn raw_tyco => Scan.repeat1 (
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
  1037
             P.name -- parse_syntax_tyco raw_tyco
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
  1038
          ))
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1039
    )
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
  1040
    >> (Toplevel.theory oo fold o fold)
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
  1041
          (fn (target, modifier) => modifier target)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1042
  );
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1043
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1044
val syntax_constP =
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1045
  OuterSyntax.command syntax_constK "define code syntax for constant" K.thy_decl (
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1046
    Scan.repeat1 (
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
  1047
      P.term
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
  1048
      #-> (fn raw_const => Scan.repeat1 (
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
  1049
             P.name -- parse_syntax_const raw_const
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
  1050
          ))
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1051
    )
19008
14c1b2f5dda4 improved code generator devarification
haftmann
parents: 18963
diff changeset
  1052
    >> (Toplevel.theory oo fold o fold)
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18960
diff changeset
  1053
          (fn (target, modifier) => modifier target)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1054
  );
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1055
19341
3414c04fbc39 added definitional code generator module: codegen_theorems.ML
haftmann
parents: 19283
diff changeset
  1056
val purgeP =
20175
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20105
diff changeset
  1057
  OuterSyntax.command purgeK "purge all incrementally generated code" K.thy_decl
0a8ca32f6e64 class package and codegen refinements
haftmann
parents: 20105
diff changeset
  1058
    (Scan.succeed (Toplevel.theory purge_code));
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1059
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
  1060
val _ = OuterSyntax.add_parsers [generateP, serializeP,
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
  1061
  syntax_classP, syntax_instP, syntax_tycoP, syntax_constP,
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
  1062
  purgeP];
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1063
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1064
end; (* local *)
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1065
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1066
end; (* struct *)