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