src/Pure/Tools/codegen_package.ML
author wenzelm
Sat, 14 Apr 2007 17:36:19 +0200
changeset 22687 53943f4dab21
parent 22570 f166a5416b3f
child 22737 d87ccbcc2702
permissions -rw-r--r--
cleaned/simplified Sign.read_typ, Thm.read_cterm etc.; removed dead code;
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
20855
9f60d493c8fe clarified header comments
haftmann
parents: 20835
diff changeset
     5
Code generator extraction kernel. Code generator Isar setup.
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     6
*)
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
signature CODEGEN_PACKAGE =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     9
sig
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    10
  include BASIC_CODEGEN_THINGOL;
21121
fae2187d6e2f refined
haftmann
parents: 21081
diff changeset
    11
  val codegen_term: theory -> term -> iterm;
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
    12
  val eval_term: theory -> (string (*reference name!*) * 'a option ref) * term -> 'a;
22464
164e7be27736 added "satisfies" interface
haftmann
parents: 22423
diff changeset
    13
  val satisfies_ref: bool option ref;
164e7be27736 added "satisfies" interface
haftmann
parents: 22423
diff changeset
    14
  val satisfies: theory -> term -> string list -> bool;
21881
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
    15
  val codegen_command: theory -> string -> unit;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    16
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    17
  type appgen;
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
    18
  val add_appconst: string * appgen -> theory -> theory;
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21722
diff changeset
    19
  val appgen_numeral: (term -> IntInf.int option) -> appgen;
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
    20
  val appgen_char: (term -> int option) -> appgen;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    21
  val appgen_case: (theory -> term
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    22
    -> ((string * typ) list * ((term * typ) * (term * term) list)) option)
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    23
    -> appgen;
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    24
  val appgen_let: appgen;
21012
haftmann
parents: 20976
diff changeset
    25
haftmann
parents: 20976
diff changeset
    26
  val timing: bool ref;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    27
end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    28
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    29
structure CodegenPackage : CODEGEN_PACKAGE =
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    30
struct
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    31
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    32
open BasicCodegenThingol;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    33
val tracing = CodegenThingol.tracing;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    34
val succeed = CodegenThingol.succeed;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    35
val fail = CodegenThingol.fail;
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
    36
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
    37
(** code extraction **)
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
    38
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
    39
(* theory data *)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    40
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
    41
type appgen = theory -> ((sort -> sort) * Sorts.algebra) * Consts.T
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
    42
  -> CodegenFuncgr.T
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    43
  -> bool * string list option
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    44
  -> (string * typ) * term list -> CodegenThingol.transact -> iterm * CodegenThingol.transact;
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
    45
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
    46
type appgens = (int * (appgen * stamp)) Symtab.table;
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    47
val merge_appgens : appgens * appgens -> appgens =
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    48
  Symtab.merge (fn ((bounds1, (_, stamp1)), (bounds2, (_, stamp2))) =>
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    49
    bounds1 = bounds2 andalso stamp1 = stamp2);
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    50
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    51
structure Consttab = CodegenConsts.Consttab;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    52
type abstypes = typ Symtab.table * CodegenConsts.const Consttab.table;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    53
fun merge_abstypes ((typs1, consts1) : abstypes, (typs2, consts2) : abstypes) =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    54
  (Symtab.merge (Type.eq_type Vartab.empty) (typs1, typs2),
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    55
    Consttab.merge CodegenConsts.eq_const (consts1, consts2));
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    56
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
    57
structure CodegenPackageData = TheoryDataFun
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    58
(struct
22213
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    59
  val name = "Pure/codegen_package_setup";
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    60
  type T = appgens * abstypes;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    61
  val empty = (Symtab.empty, (Symtab.empty, Consttab.empty));
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    62
  val copy = I;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    63
  val extend = I;
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    64
  fun merge _ ((appgens1, abstypes1), (appgens2, abstypes2)) =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    65
    (merge_appgens (appgens1, appgens2), merge_abstypes (abstypes1, abstypes2));
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    66
  fun print _ _ = ();
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    67
end);
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    68
22213
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    69
structure Funcgr = CodegenFuncgrRetrieval (
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    70
  val name = "Pure/codegen_package_thms";
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    71
  fun rewrites thy = [];
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    72
);
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    73
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    74
fun print_codethms thy =
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    75
  Pretty.writeln o CodegenFuncgr.pretty thy o Funcgr.make thy;
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    76
22507
haftmann
parents: 22464
diff changeset
    77
fun code_deps thy consts =
haftmann
parents: 22464
diff changeset
    78
  let
haftmann
parents: 22464
diff changeset
    79
    val gr = Funcgr.make thy consts;
haftmann
parents: 22464
diff changeset
    80
    fun mk_entry (const, (_, (_, parents))) =
haftmann
parents: 22464
diff changeset
    81
      let
haftmann
parents: 22464
diff changeset
    82
        val name = CodegenConsts.string_of_const thy const;
haftmann
parents: 22464
diff changeset
    83
        val nameparents = map (CodegenConsts.string_of_const thy) parents;
haftmann
parents: 22464
diff changeset
    84
      in { name = name, ID = name, dir = "", unfold = true,
haftmann
parents: 22464
diff changeset
    85
        path = "", parents = nameparents }
haftmann
parents: 22464
diff changeset
    86
      end;
haftmann
parents: 22464
diff changeset
    87
    val prgr = CodegenFuncgr.Constgraph.fold ((fn x => fn xs => xs @ [x]) o mk_entry) gr [];
haftmann
parents: 22464
diff changeset
    88
  in Present.display_graph prgr end;
haftmann
parents: 22464
diff changeset
    89
22213
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    90
structure Code = CodeDataFun
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    91
(struct
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    92
  val name = "Pure/codegen_package_code";
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    93
  type T = CodegenThingol.code;
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    94
  val empty = CodegenThingol.empty_code;
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    95
  fun merge _ = CodegenThingol.merge_code;
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    96
  fun purge _ NONE _ = CodegenThingol.empty_code
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    97
    | purge NONE _ _ = CodegenThingol.empty_code
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    98
    | purge (SOME thy) (SOME cs) code =
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
    99
        let
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   100
          val cs_exisiting =
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   101
            map_filter (CodegenNames.const_rev thy) (Graph.keys code);
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   102
          val dels = (Graph.all_preds code
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   103
              o map (CodegenNames.const thy)
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   104
              o filter (member CodegenConsts.eq_const cs_exisiting)
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   105
            ) cs;
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   106
        in Graph.del_nodes dels code end;
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   107
end);
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   108
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   109
val _ = Context.add_setup (CodegenPackageData.init #> Funcgr.init #> Code.init);
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18704
diff changeset
   110
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   111
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   112
(* preparing defining equations *)
21990
d382f901304c different handling of eta expansion
haftmann
parents: 21938
diff changeset
   113
d382f901304c different handling of eta expansion
haftmann
parents: 21938
diff changeset
   114
fun prep_eqs thy (thms as thm :: _) =
d382f901304c different handling of eta expansion
haftmann
parents: 21938
diff changeset
   115
  let
22035
acc52f0d8d8e improved variable name handling
haftmann
parents: 22021
diff changeset
   116
    val ty = (Logic.unvarifyT o CodegenFunc.typ_func) thm;
21990
d382f901304c different handling of eta expansion
haftmann
parents: 21938
diff changeset
   117
    val thms' = if (null o Term.typ_tfrees) ty orelse (null o fst o strip_type) ty
d382f901304c different handling of eta expansion
haftmann
parents: 21938
diff changeset
   118
      then thms
22035
acc52f0d8d8e improved variable name handling
haftmann
parents: 22021
diff changeset
   119
      else map (CodegenFunc.expand_eta 1) thms;
21990
d382f901304c different handling of eta expansion
haftmann
parents: 21938
diff changeset
   120
  in (ty, thms') end;
d382f901304c different handling of eta expansion
haftmann
parents: 21938
diff changeset
   121
d382f901304c different handling of eta expansion
haftmann
parents: 21938
diff changeset
   122
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   123
(* extraction kernel *)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   124
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   125
fun check_strict (false, _) has_seri x =
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   126
      false
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   127
  | check_strict (_, SOME targets) has_seri x =
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   128
      not (has_seri targets x)
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   129
  | check_strict (true, _) has_seri x =
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   130
      true;
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   131
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   132
fun get_abstype thy (tyco, tys) = case Symtab.lookup ((fst o snd o CodegenPackageData.get) thy) tyco
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   133
 of SOME ty => SOME ((map_atyps (fn TFree (n, _) => nth tys (the (Int.fromString n)))) ty)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   134
  | NONE => NONE;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   135
22035
acc52f0d8d8e improved variable name handling
haftmann
parents: 22021
diff changeset
   136
fun ensure_def thy = CodegenThingol.ensure_def (CodegenNames.labelled_name thy);
acc52f0d8d8e improved variable name handling
haftmann
parents: 22021
diff changeset
   137
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   138
fun ensure_def_class thy (algbr as ((_, algebra), _)) funcgr strct class =
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   139
  let
22185
haftmann
parents: 22076
diff changeset
   140
    val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
21895
6cbc0f69a21c clarified code
haftmann
parents: 21881
diff changeset
   141
    val (v, cs) = AxClass.params_of_class thy class;
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22035
diff changeset
   142
    val class' = CodegenNames.class thy class;
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22035
diff changeset
   143
    val classrels' = map (curry (CodegenNames.classrel thy) class) superclasses;
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   144
    val classops' = map (CodegenNames.const thy o CodegenConsts.const_of_cexpr thy) cs;
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   145
    val defgen_class =
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   146
      fold_map (ensure_def_class thy algbr funcgr strct) superclasses
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   147
      ##>> (fold_map (exprgen_type thy algbr funcgr strct) o map snd) cs
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   148
      #-> (fn (superclasses, classoptyps) => succeed
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22035
diff changeset
   149
        (CodegenThingol.Class (superclasses ~~ classrels', (unprefix "'" v, classops' ~~ classoptyps))))
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   150
  in
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   151
    tracing (fn _ => "generating class " ^ quote class)
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   152
    #> ensure_def thy defgen_class true
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   153
        ("generating class " ^ quote class) class'
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   154
    #> pair class'
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   155
  end
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   156
and ensure_def_classrel thy algbr funcgr strct (subclass, superclass) =
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   157
  ensure_def_class thy algbr funcgr strct subclass
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   158
  #>> (fn _ => CodegenNames.classrel thy (subclass, superclass))
22354
ec6a033b27be proper treatment of -> as type constructor
haftmann
parents: 22305
diff changeset
   159
and ensure_def_tyco thy algbr funcgr strct "fun" trns =
ec6a033b27be proper treatment of -> as type constructor
haftmann
parents: 22305
diff changeset
   160
      trns
ec6a033b27be proper treatment of -> as type constructor
haftmann
parents: 22305
diff changeset
   161
      |> pair "fun"
ec6a033b27be proper treatment of -> as type constructor
haftmann
parents: 22305
diff changeset
   162
  | ensure_def_tyco thy algbr funcgr strct tyco trns =
ec6a033b27be proper treatment of -> as type constructor
haftmann
parents: 22305
diff changeset
   163
      let
ec6a033b27be proper treatment of -> as type constructor
haftmann
parents: 22305
diff changeset
   164
        fun defgen_datatype trns =
22396
6c7f9207fa9e permitting empty datatypes
haftmann
parents: 22354
diff changeset
   165
          let
22423
c1836b14c63a dropped code datatype certificates
haftmann
parents: 22396
diff changeset
   166
            val (vs, cos) = CodegenData.get_datatype thy tyco;
22396
6c7f9207fa9e permitting empty datatypes
haftmann
parents: 22354
diff changeset
   167
          in
6c7f9207fa9e permitting empty datatypes
haftmann
parents: 22354
diff changeset
   168
            trns
6c7f9207fa9e permitting empty datatypes
haftmann
parents: 22354
diff changeset
   169
            |> fold_map (exprgen_tyvar_sort thy algbr funcgr strct) vs
6c7f9207fa9e permitting empty datatypes
haftmann
parents: 22354
diff changeset
   170
            ||>> fold_map (fn (c, tys) =>
6c7f9207fa9e permitting empty datatypes
haftmann
parents: 22354
diff changeset
   171
              fold_map (exprgen_type thy algbr funcgr strct) tys
6c7f9207fa9e permitting empty datatypes
haftmann
parents: 22354
diff changeset
   172
              #-> (fn tys' =>
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   173
                pair ((CodegenNames.const thy o CodegenConsts.const_of_cexpr thy)
22396
6c7f9207fa9e permitting empty datatypes
haftmann
parents: 22354
diff changeset
   174
                  (c, tys ---> Type (tyco, map TFree vs)), tys'))) cos
6c7f9207fa9e permitting empty datatypes
haftmann
parents: 22354
diff changeset
   175
            |-> (fn (vs, cos) => succeed (CodegenThingol.Datatype (vs, cos)))
6c7f9207fa9e permitting empty datatypes
haftmann
parents: 22354
diff changeset
   176
          end;
22354
ec6a033b27be proper treatment of -> as type constructor
haftmann
parents: 22305
diff changeset
   177
        val tyco' = CodegenNames.tyco thy tyco;
ec6a033b27be proper treatment of -> as type constructor
haftmann
parents: 22305
diff changeset
   178
      in
ec6a033b27be proper treatment of -> as type constructor
haftmann
parents: 22305
diff changeset
   179
        trns
ec6a033b27be proper treatment of -> as type constructor
haftmann
parents: 22305
diff changeset
   180
        |> tracing (fn _ => "generating type constructor " ^ quote tyco)
22396
6c7f9207fa9e permitting empty datatypes
haftmann
parents: 22354
diff changeset
   181
        |> ensure_def thy defgen_datatype true
22354
ec6a033b27be proper treatment of -> as type constructor
haftmann
parents: 22305
diff changeset
   182
            ("generating type constructor " ^ quote tyco) tyco'
ec6a033b27be proper treatment of -> as type constructor
haftmann
parents: 22305
diff changeset
   183
        |> pair tyco'
ec6a033b27be proper treatment of -> as type constructor
haftmann
parents: 22305
diff changeset
   184
      end
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   185
and exprgen_tyvar_sort thy (algbr as ((proj_sort, _), _)) funcgr strct (v, sort) trns =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   186
  trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   187
  |> fold_map (ensure_def_class thy algbr funcgr strct) (proj_sort sort)
22185
haftmann
parents: 22076
diff changeset
   188
  |>> (fn sort => (unprefix "'" v, sort))
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   189
and exprgen_type thy algbr funcgr strct (TFree vs) trns =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   190
      trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   191
      |> exprgen_tyvar_sort thy algbr funcgr strct vs
22185
haftmann
parents: 22076
diff changeset
   192
      |>> (fn (v, sort) => ITyVar v)
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   193
  | exprgen_type thy algbr funcgr strct (Type (tyco, tys)) trns =
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   194
      case get_abstype thy (tyco, tys)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   195
       of SOME ty =>
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   196
            trns
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   197
            |> exprgen_type thy algbr funcgr strct ty
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   198
        | NONE =>
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   199
            trns
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   200
            |> ensure_def_tyco thy algbr funcgr strct tyco
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   201
            ||>> fold_map (exprgen_type thy algbr funcgr strct) tys
22185
haftmann
parents: 22076
diff changeset
   202
            |>> (fn (tyco, tys) => tyco `%% tys);
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   203
20835
haftmann
parents: 20699
diff changeset
   204
exception CONSTRAIN of (string * typ) * typ;
21012
haftmann
parents: 20976
diff changeset
   205
val timing = ref false;
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   206
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   207
fun exprgen_dicts thy (algbr as ((proj_sort, algebra), consts)) funcgr strct (ty_ctxt, sort_decl) =
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   208
  let
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   209
    val pp = Sign.pp thy;
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22035
diff changeset
   210
    datatype typarg =
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22035
diff changeset
   211
        Global of (class * string) * typarg list list
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22035
diff changeset
   212
      | Local of (class * class) list * (string * (int * sort));
22570
f166a5416b3f renamed of_sort_derivation record fields (avoid clash with Alice keywords);
wenzelm
parents: 22554
diff changeset
   213
    fun class_relation (Global ((_, tyco), yss), _) class =
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22035
diff changeset
   214
          Global ((class, tyco), yss)
22570
f166a5416b3f renamed of_sort_derivation record fields (avoid clash with Alice keywords);
wenzelm
parents: 22554
diff changeset
   215
      | class_relation (Local (classrels, v), subclass) superclass =
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22035
diff changeset
   216
          Local ((subclass, superclass) :: classrels, v);
22570
f166a5416b3f renamed of_sort_derivation record fields (avoid clash with Alice keywords);
wenzelm
parents: 22554
diff changeset
   217
    fun type_constructor tyco yss class =
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22035
diff changeset
   218
      Global ((class, tyco), (map o map) fst yss);
22570
f166a5416b3f renamed of_sort_derivation record fields (avoid clash with Alice keywords);
wenzelm
parents: 22554
diff changeset
   219
    fun type_variable (TFree (v, sort)) =
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   220
      let
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   221
        val sort' = proj_sort sort;
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22035
diff changeset
   222
      in map_index (fn (n, class) => (Local ([], (v, (n, sort'))), class)) sort' end;
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22035
diff changeset
   223
    val typargs = Sorts.of_sort_derivation pp algebra
22570
f166a5416b3f renamed of_sort_derivation record fields (avoid clash with Alice keywords);
wenzelm
parents: 22554
diff changeset
   224
      {class_relation = class_relation, type_constructor = type_constructor,
f166a5416b3f renamed of_sort_derivation record fields (avoid clash with Alice keywords);
wenzelm
parents: 22554
diff changeset
   225
       type_variable = type_variable}
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   226
      (ty_ctxt, proj_sort sort_decl);
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   227
    fun mk_dict (Global (inst, yss)) =
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   228
          ensure_def_inst thy algbr funcgr strct inst
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   229
          ##>> (fold_map o fold_map) mk_dict yss
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   230
          #>> (fn (inst, dss) => DictConst (inst, dss))
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   231
      | mk_dict (Local (classrels, (v, (k, sort)))) =
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   232
          fold_map (ensure_def_classrel thy algbr funcgr strct) classrels
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   233
          #>> (fn classrels => DictVar (classrels, (unprefix "'" v, (k, length sort))))
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   234
  in
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   235
    fold_map mk_dict typargs
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   236
  end
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   237
and exprgen_dict_parms thy (algbr as (_, consts)) funcgr strct (c, ty_ctxt) =
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   238
  let
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   239
    val c' = CodegenConsts.const_of_cexpr thy (c, ty_ctxt)
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   240
    val idf = CodegenNames.const thy c';
21722
25239591e732 tuned Consts signature;
wenzelm
parents: 21463
diff changeset
   241
    val ty_decl = Consts.the_declaration consts idf;
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   242
    val (tys, tys_decl) = pairself (curry (Consts.typargs consts) idf) (ty_ctxt, ty_decl);
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   243
    val sorts = map (snd o dest_TVar) tys_decl;
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   244
  in
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   245
    fold_map (exprgen_dicts thy algbr funcgr strct) (tys ~~ sorts)
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   246
  end
22185
haftmann
parents: 22076
diff changeset
   247
and ensure_def_inst thy (algbr as ((_, algebra), _)) funcgr strct (class, tyco) trns =
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   248
  let
22185
haftmann
parents: 22076
diff changeset
   249
    val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
haftmann
parents: 22076
diff changeset
   250
    val (var, classops) = try (AxClass.params_of_class thy) class |> the_default ("'a", [])
haftmann
parents: 22076
diff changeset
   251
    val vs = Name.names (Name.declare var Name.context) "'a" (Sorts.mg_domain algebra tyco [class]);
haftmann
parents: 22076
diff changeset
   252
    val arity_typ = Type (tyco, map TFree vs);
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   253
    fun gen_superarity superclass trns =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   254
      trns
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   255
      |> ensure_def_class thy algbr funcgr strct superclass
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22035
diff changeset
   256
      ||>> ensure_def_classrel thy algbr funcgr strct (class, superclass)
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22035
diff changeset
   257
      ||>> exprgen_dicts thy algbr funcgr strct (arity_typ, [superclass])
22185
haftmann
parents: 22076
diff changeset
   258
      |>> (fn ((superclass, classrel), [DictConst (inst, dss)]) =>
haftmann
parents: 22076
diff changeset
   259
            (superclass, (classrel, (inst, dss))));
haftmann
parents: 22076
diff changeset
   260
    fun gen_classop_def (classop as (c, ty)) trns =
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   261
      trns
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   262
      |> ensure_def_const thy algbr funcgr strct (CodegenConsts.const_of_cexpr thy classop)
22185
haftmann
parents: 22076
diff changeset
   263
      ||>> exprgen_term thy algbr funcgr strct (Const (c, map_type_tfree (K arity_typ) ty));
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   264
    fun defgen_inst trns =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   265
      trns
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   266
      |> ensure_def_class thy algbr funcgr strct class
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   267
      ||>> ensure_def_tyco thy algbr funcgr strct tyco
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   268
      ||>> fold_map (exprgen_tyvar_sort thy algbr funcgr strct) vs
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   269
      ||>> fold_map gen_superarity superclasses
22185
haftmann
parents: 22076
diff changeset
   270
      ||>> fold_map gen_classop_def classops
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   271
      |-> (fn ((((class, tyco), arity), superarities), classops) =>
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   272
             succeed (CodegenThingol.Classinst ((class, (tyco, arity)), (superarities, classops))));
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   273
    val inst = CodegenNames.instance thy (class, tyco);
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   274
  in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   275
    trns
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   276
    |> tracing (fn _ => "generating instance " ^ quote class ^ " / " ^ quote tyco)
22185
haftmann
parents: 22076
diff changeset
   277
    |> ensure_def thy defgen_inst true ("generating instance " ^ quote class ^ " / " ^ quote tyco) inst
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   278
    |> pair inst
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   279
  end
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   280
and ensure_def_const thy (algbr as (_, consts)) funcgr strct (const as (c, opt_tyco)) trns =
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   281
  let
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   282
    val c' = CodegenNames.const thy const;
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   283
    fun defgen_datatypecons trns =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   284
      trns
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   285
      |> ensure_def_tyco thy algbr funcgr strct
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   286
          ((the o CodegenData.get_datatype_of_constr thy) const)
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   287
      |-> (fn _ => succeed CodegenThingol.Bot);
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   288
    fun defgen_classop trns =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   289
      trns
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   290
      |> ensure_def_class thy algbr funcgr strct ((the o AxClass.class_of_param thy) c)
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   291
      |-> (fn _ => succeed CodegenThingol.Bot);
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   292
    fun defgen_fun trns =
21121
fae2187d6e2f refined
haftmann
parents: 21081
diff changeset
   293
      case CodegenFuncgr.funcs funcgr
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   294
        (perhaps (Consttab.lookup ((snd o snd o CodegenPackageData.get) thy)) const)
21990
d382f901304c different handling of eta expansion
haftmann
parents: 21938
diff changeset
   295
       of thms as _ :: _ =>
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   296
            let
21990
d382f901304c different handling of eta expansion
haftmann
parents: 21938
diff changeset
   297
              val (ty, eq_thms) = prep_eqs thy thms;
21012
haftmann
parents: 20976
diff changeset
   298
              val timeap = if !timing then Output.timeap_msg ("time for " ^ c')
haftmann
parents: 20976
diff changeset
   299
                else I;
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   300
              val msg = cat_lines ("generating code for theorems " :: map string_of_thm eq_thms);
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   301
              val vs = (map dest_TFree o Consts.typargs consts) (c', ty);
20835
haftmann
parents: 20699
diff changeset
   302
              val dest_eqthm =
haftmann
parents: 20699
diff changeset
   303
                apfst (snd o strip_comb) o Logic.dest_equals o Logic.unvarify o prop_of;
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   304
              fun exprgen_eq (args, rhs) trns =
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   305
                trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   306
                |> fold_map (exprgen_term thy algbr funcgr strct) args
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   307
                ||>> exprgen_term thy algbr funcgr strct rhs;
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   308
            in
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   309
              trns
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   310
              |> CodegenThingol.message msg (fn trns => trns
21012
haftmann
parents: 20976
diff changeset
   311
              |> timeap (fold_map (exprgen_eq o dest_eqthm) eq_thms)
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   312
              ||>> fold_map (exprgen_tyvar_sort thy algbr funcgr strct) vs
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   313
              ||>> exprgen_type thy algbr funcgr strct ty
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   314
              |-> (fn ((eqs, vs), ty) => succeed (CodegenThingol.Fun (eqs, (vs, ty)))))
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   315
            end
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   316
        | [] =>
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   317
              trns
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   318
              |> fail ("No defining equations found for "
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   319
                   ^ (quote o CodegenConsts.string_of_const thy) const);
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   320
    val defgen = if (is_some o CodegenData.get_datatype_of_constr thy) const
21081
837b535137a9 dropped classop shallow namespace
haftmann
parents: 21062
diff changeset
   321
      then defgen_datatypecons
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   322
      else if is_some opt_tyco
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   323
        orelse (not o is_some o AxClass.class_of_param thy) c
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   324
      then defgen_fun
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   325
      else defgen_classop
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   326
    val strict = check_strict strct (CodegenSerializer.const_has_serialization thy) c';
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   327
  in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   328
    trns
20835
haftmann
parents: 20699
diff changeset
   329
    |> tracing (fn _ => "generating constant "
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   330
        ^ (quote o CodegenConsts.string_of_const thy) const)
22035
acc52f0d8d8e improved variable name handling
haftmann
parents: 22021
diff changeset
   331
    |> ensure_def thy defgen strict ("generating constant "
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   332
         ^ CodegenConsts.string_of_const thy const) c'
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   333
    |> pair c'
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   334
  end
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   335
and exprgen_term thy algbr funcgr strct (Const (c, ty)) trns =
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   336
      trns
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   337
      |> select_appgen thy algbr funcgr strct ((c, ty), [])
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   338
  | exprgen_term thy algbr funcgr strct (Free (v, ty)) trns =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   339
      trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   340
      |> exprgen_type thy algbr funcgr strct ty
22185
haftmann
parents: 22076
diff changeset
   341
      |>> (fn _ => IVar v)
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   342
  | exprgen_term thy algbr funcgr strct (Abs (raw_v, ty, raw_t)) trns =
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   343
      let
22035
acc52f0d8d8e improved variable name handling
haftmann
parents: 22021
diff changeset
   344
        val (v, t) = Syntax.variant_abs (raw_v, ty, raw_t);
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   345
      in
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   346
        trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   347
        |> exprgen_type thy algbr funcgr strct ty
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   348
        ||>> exprgen_term thy algbr funcgr strct t
22185
haftmann
parents: 22076
diff changeset
   349
        |>> (fn (ty, t) => (v, ty) `|-> t)
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   350
      end
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   351
  | exprgen_term thy algbr funcgr strct (t as _ $ _) trns =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   352
      case strip_comb t
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   353
       of (Const (c, ty), ts) =>
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   354
            trns
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   355
            |> select_appgen thy algbr funcgr strct ((c, ty), ts)
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   356
        | (t', ts) =>
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   357
            trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   358
            |> exprgen_term thy algbr funcgr strct t'
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   359
            ||>> fold_map (exprgen_term thy algbr funcgr strct) ts
22185
haftmann
parents: 22076
diff changeset
   360
            |>> (fn (t, ts) => t `$$ ts)
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   361
and appgen_default thy algbr funcgr strct ((c, ty), ts) trns =
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   362
  trns
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   363
  |> ensure_def_const thy algbr funcgr strct (CodegenConsts.const_of_cexpr thy (c, ty))
22305
0e56750a092b changed representation of constants
haftmann
parents: 22213
diff changeset
   364
  ||>> fold_map (exprgen_type thy algbr funcgr strct) ((fst o Term.strip_type) ty)
0e56750a092b changed representation of constants
haftmann
parents: 22213
diff changeset
   365
  ||>> exprgen_type thy algbr funcgr strct ((snd o Term.strip_type) ty)
22076
42ae57200d96 changed dictionary representation to explicit classrel witnesses
haftmann
parents: 22035
diff changeset
   366
  ||>> exprgen_dict_parms thy algbr funcgr strct (c, ty)
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   367
  ||>> fold_map (exprgen_term thy algbr funcgr strct) ts
22305
0e56750a092b changed representation of constants
haftmann
parents: 22213
diff changeset
   368
  |>> (fn ((((c, tys), ty), iss), ts) => IConst (c, (iss, tys)) `$$ ts)
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   369
and select_appgen thy algbr funcgr strct ((c, ty), ts) trns =
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   370
  case Symtab.lookup (fst (CodegenPackageData.get thy)) c
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   371
   of SOME (i, (appgen, _)) =>
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   372
        if length ts < i then
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   373
          let
21161
ba4a40552f06 fixed problem with eta-expansion
haftmann
parents: 21129
diff changeset
   374
            val k = length ts;
ba4a40552f06 fixed problem with eta-expansion
haftmann
parents: 21129
diff changeset
   375
            val tys = (curry Library.take (i - k) o curry Library.drop k o fst o strip_type) ty;
ba4a40552f06 fixed problem with eta-expansion
haftmann
parents: 21129
diff changeset
   376
            val ctxt = (fold o fold_aterms)
ba4a40552f06 fixed problem with eta-expansion
haftmann
parents: 21129
diff changeset
   377
              (fn Free (v, _) => Name.declare v | _ => I) ts Name.context;
ba4a40552f06 fixed problem with eta-expansion
haftmann
parents: 21129
diff changeset
   378
            val vs = Name.names ctxt "a" tys;
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   379
          in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   380
            trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   381
            |> fold_map (exprgen_type thy algbr funcgr strct) tys
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   382
            ||>> appgen thy algbr funcgr strct ((c, ty), ts @ map Free vs)
22185
haftmann
parents: 22076
diff changeset
   383
            |>> (fn (tys, t) => map2 (fn (v, _) => pair v) vs tys `|--> t)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   384
          end
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   385
        else if length ts > i then
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   386
          trns
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   387
          |> appgen thy algbr funcgr strct ((c, ty), Library.take (i, ts))
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   388
          ||>> fold_map (exprgen_term thy algbr funcgr strct) (Library.drop (i, ts))
22185
haftmann
parents: 22076
diff changeset
   389
          |>> (fn (t, ts) => t `$$ ts)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   390
        else
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   391
          trns
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   392
          |> appgen thy algbr funcgr strct ((c, ty), ts)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   393
    | NONE =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   394
        trns
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   395
        |> appgen_default thy algbr funcgr strct ((c, ty), ts);
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   396
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   397
20835
haftmann
parents: 20699
diff changeset
   398
(* entrance points into extraction kernel *)
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   399
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   400
fun ensure_def_const' thy algbr funcgr strct c trns =
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   401
  ensure_def_const thy algbr funcgr strct c trns
20835
haftmann
parents: 20699
diff changeset
   402
  handle CONSTRAIN ((c, ty), ty_decl) => error (
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   403
    "Constant " ^ c ^ " with most general type\n"
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   404
    ^ CodegenConsts.string_of_typ thy ty
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   405
    ^ "\noccurs with type\n"
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   406
    ^ CodegenConsts.string_of_typ thy ty_decl);
22185
haftmann
parents: 22076
diff changeset
   407
haftmann
parents: 22076
diff changeset
   408
fun perhaps_def_const thy algbr funcgr strct c trns =
haftmann
parents: 22076
diff changeset
   409
  case try (ensure_def_const thy algbr funcgr strct c) trns
haftmann
parents: 22076
diff changeset
   410
   of SOME (c, trns) => (SOME c, trns)
haftmann
parents: 22076
diff changeset
   411
    | NONE => (NONE, trns);
haftmann
parents: 22076
diff changeset
   412
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   413
fun exprgen_term' thy algbr funcgr strct t trns =
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   414
  exprgen_term thy algbr funcgr strct t trns
20835
haftmann
parents: 20699
diff changeset
   415
  handle CONSTRAIN ((c, ty), ty_decl) => error ("In term " ^ (quote o Sign.string_of_term thy) t
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   416
    ^ ",\nconstant " ^ c ^ " with most general type\n"
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   417
    ^ CodegenConsts.string_of_typ thy ty
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   418
    ^ "\noccurs with type\n"
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   419
    ^ CodegenConsts.string_of_typ thy ty_decl);
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   420
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   421
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   422
(* parametrized application generators, for instantiation in object logic *)
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   423
(* (axiomatic extensions of extraction kernel *)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   424
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   425
fun appgen_numeral int_of_numeral thy algbr funcgr strct (app as (c, ts)) trns =
21820
2f2b6a965ccc introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents: 21722
diff changeset
   426
  case int_of_numeral (list_comb (Const c, ts))
20835
haftmann
parents: 20699
diff changeset
   427
   of SOME i =>
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   428
        trns
21012
haftmann
parents: 20976
diff changeset
   429
        |> pair (CodegenThingol.INum i)
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   430
    | NONE =>
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   431
        trns
20835
haftmann
parents: 20699
diff changeset
   432
        |> appgen_default thy algbr funcgr strct app;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   433
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   434
fun appgen_char char_to_index thy algbr funcgr 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
   435
  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
   436
   of SOME i =>
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   437
        trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   438
        |> exprgen_type thy algbr funcgr strct ty
22185
haftmann
parents: 22076
diff changeset
   439
        |>> (fn _ => IChar (chr i))
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   440
    | NONE =>
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   441
        trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   442
        |> appgen_default thy algbr funcgr strct app;
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   443
21012
haftmann
parents: 20976
diff changeset
   444
val debug_term = ref (Bound 0);
haftmann
parents: 20976
diff changeset
   445
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   446
fun appgen_case dest_case_expr thy algbr funcgr strct (app as (c_ty, ts)) trns =
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   447
  let
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   448
    val SOME ([], ((st, sty), ds)) = dest_case_expr thy (list_comb (Const c_ty, ts));
22035
acc52f0d8d8e improved variable name handling
haftmann
parents: 22021
diff changeset
   449
    fun clausegen (dt, bt) trns =
acc52f0d8d8e improved variable name handling
haftmann
parents: 22021
diff changeset
   450
      trns
acc52f0d8d8e improved variable name handling
haftmann
parents: 22021
diff changeset
   451
      |> exprgen_term thy algbr funcgr strct dt
acc52f0d8d8e improved variable name handling
haftmann
parents: 22021
diff changeset
   452
      ||>> exprgen_term thy algbr funcgr strct bt;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   453
  in
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   454
    trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   455
    |> exprgen_term thy algbr funcgr strct st
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   456
    ||>> exprgen_type thy algbr funcgr strct sty
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   457
    ||>> fold_map clausegen ds
22185
haftmann
parents: 22076
diff changeset
   458
    |>> (fn ((se, sty), ds) => ICase ((se, sty), ds))
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   459
  end;
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   460
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   461
fun appgen_let thy algbr funcgr strct (app as (_, [st, ct])) trns =
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   462
  trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   463
  |> exprgen_term thy algbr funcgr strct ct
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   464
  ||>> exprgen_term thy algbr funcgr strct st
21012
haftmann
parents: 20976
diff changeset
   465
  |-> (fn ((v, ty) `|-> be, se) =>
haftmann
parents: 20976
diff changeset
   466
            pair (ICase ((se, ty), case be
haftmann
parents: 20976
diff changeset
   467
              of ICase ((IVar w, _), ds) => if v = w then ds else [(IVar v, be)]
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   468
               | _ => [(IVar v, be)]
21012
haftmann
parents: 20976
diff changeset
   469
            ))
haftmann
parents: 20976
diff changeset
   470
        | _ => appgen_default thy algbr funcgr strct app);
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   471
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   472
fun add_appconst (c, appgen) thy =
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   473
  let
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   474
    val i = (length o fst o strip_type o Sign.the_const_type thy) c;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   475
    val _ = Code.change thy (K CodegenThingol.empty_code);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   476
  in
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   477
    (CodegenPackageData.map o apfst)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   478
      (Symtab.update (c, (i, (appgen, stamp ())))) thy
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   479
  end;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   480
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   481
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   482
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   483
(** abstype and constsubst interface **)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   484
21916
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   485
local
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   486
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   487
fun add_consts thy f (c1, c2 as (c, opt_tyco)) =
21916
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   488
  let
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   489
    val _ = if
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   490
        is_some (AxClass.class_of_param thy c) andalso is_none opt_tyco
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   491
        orelse is_some (CodegenData.get_datatype_of_constr thy c2)
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   492
      then error ("Not a function: " ^ CodegenConsts.string_of_const thy c2)
21916
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   493
      else ();
22213
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   494
    val funcgr = Funcgr.make thy [c1, c2];
21916
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   495
    val ty1 = (f o CodegenFuncgr.typ funcgr) c1;
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   496
    val ty2 = CodegenFuncgr.typ funcgr c2;
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   497
    val _ = if Sign.typ_equiv thy (ty1, ty2) then () else
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   498
      error ("Incompatiable type signatures of " ^ CodegenConsts.string_of_const thy c1
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   499
        ^ " and " ^ CodegenConsts.string_of_const thy c2 ^ ":\n"
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   500
        ^ CodegenConsts.string_of_typ thy ty1 ^ "\n" ^ CodegenConsts.string_of_typ thy ty2);
21916
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   501
  in Consttab.update (c1, c2) end;
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   502
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   503
fun gen_abstyp prep_const prep_typ (raw_abstyp, raw_substtyp) raw_absconsts thy =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   504
  let
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   505
    val abstyp = Type.no_tvars (prep_typ thy raw_abstyp);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   506
    val substtyp = Type.no_tvars (prep_typ thy raw_substtyp);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   507
    val absconsts = (map o pairself) (prep_const thy) raw_absconsts;
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   508
    val Type (abstyco, tys) = abstyp handle BIND => error ("Bad type: " ^ Sign.string_of_typ thy abstyp);
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   509
    val typarms = map (fst o dest_TFree) tys handle MATCH => error ("Bad type: " ^ Sign.string_of_typ thy abstyp);
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   510
    fun mk_index v = 
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   511
      let
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   512
        val k = find_index (fn w => v = w) typarms;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   513
      in if k = ~1
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   514
        then error ("Free type variable: " ^ quote v)
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   515
        else TFree (string_of_int k, [])
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   516
      end;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   517
    val typpat = map_atyps (fn TFree (v, _) => mk_index v) substtyp;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   518
    fun apply_typpat (Type (tyco, tys)) =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   519
          let
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   520
            val tys' = map apply_typpat tys;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   521
          in if tyco = abstyco then
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   522
            (map_atyps (fn TFree (n, _) => nth tys' (the (Int.fromString n)))) typpat
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   523
          else
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   524
            Type (tyco, tys')
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   525
          end
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   526
      | apply_typpat ty = ty;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   527
    val _ = Code.change thy (K CodegenThingol.empty_code);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   528
  in
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   529
    thy
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   530
    |> (CodegenPackageData.map o apsnd) (fn (abstypes, abscs) =>
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   531
          (abstypes
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   532
          |> Symtab.update (abstyco, typpat),
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   533
          abscs
21916
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   534
          |> fold (add_consts thy apply_typpat) absconsts)
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   535
       )
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   536
  end;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   537
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   538
fun gen_constsubst prep_const raw_constsubsts thy =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   539
  let
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   540
    val constsubsts = (map o pairself) (prep_const thy) raw_constsubsts;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   541
    val _ = Code.change thy (K CodegenThingol.empty_code);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   542
  in
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   543
    thy
21916
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   544
    |> (CodegenPackageData.map o apsnd o apsnd) (fold (add_consts thy I) constsubsts)
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   545
  end;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   546
21916
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   547
in
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   548
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   549
val abstyp = gen_abstyp (K I) Sign.certify_typ;
22687
53943f4dab21 cleaned/simplified Sign.read_typ, Thm.read_cterm etc.;
wenzelm
parents: 22570
diff changeset
   550
val abstyp_e = gen_abstyp CodegenConsts.read_const Sign.read_typ;
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   551
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   552
val constsubst = gen_constsubst (K I);
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   553
val constsubst_e = gen_constsubst CodegenConsts.read_const;
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   554
21916
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   555
end; (*local*)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   556
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   557
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   558
(** code generation interfaces **)
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   559
21881
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   560
(* generic generation combinators *)
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   561
22185
haftmann
parents: 22076
diff changeset
   562
fun generate thy funcgr targets gen it =
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   563
  let
21121
fae2187d6e2f refined
haftmann
parents: 21081
diff changeset
   564
    val cs = map_filter (Consttab.lookup ((snd o snd o CodegenPackageData.get) thy))
fae2187d6e2f refined
haftmann
parents: 21081
diff changeset
   565
      (CodegenFuncgr.all funcgr);
22213
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   566
    val funcgr' = Funcgr.make thy cs;
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   567
    val qnaming = NameSpace.qualified_names NameSpace.default_naming;
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   568
    val consttab = Consts.empty
21121
fae2187d6e2f refined
haftmann
parents: 21081
diff changeset
   569
      |> fold (fn c => Consts.declare qnaming
fae2187d6e2f refined
haftmann
parents: 21081
diff changeset
   570
           ((CodegenNames.const thy c, CodegenFuncgr.typ funcgr' c), true))
fae2187d6e2f refined
haftmann
parents: 21081
diff changeset
   571
           (CodegenFuncgr.all funcgr');
22185
haftmann
parents: 22076
diff changeset
   572
    val algbr = (CodegenData.operational_algebra thy, consttab);
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   573
  in   
22185
haftmann
parents: 22076
diff changeset
   574
    Code.change_yield thy (CodegenThingol.start_transact (gen thy algbr funcgr'
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   575
        (true, targets) it))
21121
fae2187d6e2f refined
haftmann
parents: 21081
diff changeset
   576
    |> fst
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   577
  end;
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   578
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   579
fun codegen_term thy t =
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   580
  let
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   581
    val ct = Thm.cterm_of thy t;
22213
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   582
    val (ct', funcgr) = Funcgr.make_term thy (K (K K)) ct;
20835
haftmann
parents: 20699
diff changeset
   583
    val t' = Thm.term_of ct';
22185
haftmann
parents: 22076
diff changeset
   584
  in generate thy funcgr (SOME []) exprgen_term' t' end;
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   585
22464
164e7be27736 added "satisfies" interface
haftmann
parents: 22423
diff changeset
   586
fun raw_eval_term thy (ref_spec, t) args =
20213
2b319e945905 added eval_term
haftmann
parents: 20194
diff changeset
   587
  let
22507
haftmann
parents: 22464
diff changeset
   588
    val _ = (Term.map_types o Term.map_atyps) (fn _ =>
haftmann
parents: 22464
diff changeset
   589
      error ("Term " ^ Sign.string_of_term thy t ^ " contains polymorphic type"))
21388
9d8344cf029f corrected polymorphism check
haftmann
parents: 21161
diff changeset
   590
      t;
21121
fae2187d6e2f refined
haftmann
parents: 21081
diff changeset
   591
    val t' = codegen_term thy t;
22464
164e7be27736 added "satisfies" interface
haftmann
parents: 22423
diff changeset
   592
  in
164e7be27736 added "satisfies" interface
haftmann
parents: 22423
diff changeset
   593
    CodegenSerializer.eval_term thy CodegenNames.labelled_name
164e7be27736 added "satisfies" interface
haftmann
parents: 22423
diff changeset
   594
      (Code.get thy) (ref_spec, t') args
164e7be27736 added "satisfies" interface
haftmann
parents: 22423
diff changeset
   595
  end;
164e7be27736 added "satisfies" interface
haftmann
parents: 22423
diff changeset
   596
164e7be27736 added "satisfies" interface
haftmann
parents: 22423
diff changeset
   597
val satisfies_ref : bool option ref = ref NONE;
164e7be27736 added "satisfies" interface
haftmann
parents: 22423
diff changeset
   598
164e7be27736 added "satisfies" interface
haftmann
parents: 22423
diff changeset
   599
fun eval_term thy t = raw_eval_term thy t [];
164e7be27736 added "satisfies" interface
haftmann
parents: 22423
diff changeset
   600
fun satisfies thy t witnesses = raw_eval_term thy
164e7be27736 added "satisfies" interface
haftmann
parents: 22423
diff changeset
   601
  (("CodegenPackage.satisfies_ref", satisfies_ref), t) witnesses;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   602
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   603
21881
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   604
(* constant specifications with wildcards *)
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   605
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   606
fun consts_of thy thyname =
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   607
  let
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   608
    val this_thy = Option.map theory thyname |> the_default thy;
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   609
    val cs = Symtab.fold (fn (c, (_, NONE)) => cons c | _ => I)
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   610
      ((snd o #constants o Consts.dest o #consts o Sign.rep_sg) this_thy) [];
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   611
    fun classop c = case AxClass.class_of_param thy c
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   612
     of NONE => [(c, NONE)]
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   613
      | SOME class => Symtab.fold
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   614
          (fn (tyco, classes) => if AList.defined (op =) classes class
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   615
            then cons (c, SOME tyco) else I)
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   616
          ((#arities o Sorts.rep_algebra o Sign.classes_of) this_thy)
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   617
          [(c, NONE)];
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   618
    val consts = maps classop cs;
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   619
    fun test_instance thy (class, tyco) =
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   620
      can (Sorts.mg_domain (Sign.classes_of thy) tyco) [class]
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   621
    fun belongs_here thyname (c, NONE) =
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   622
          not (exists (fn thy => Sign.declared_const thy c) (Theory.parents_of this_thy))
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   623
      | belongs_here thyname (c, SOME tyco) =
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   624
          not (exists (fn thy => test_instance thy ((the o AxClass.class_of_param thy) c, tyco))
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   625
            (Theory.parents_of this_thy))
21881
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   626
  in case thyname
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   627
   of NONE => consts
22554
d1499fff65d8 simplified constant representation in code generator
haftmann
parents: 22507
diff changeset
   628
    | SOME thyname => filter (belongs_here thyname) consts
21881
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   629
  end;
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   630
22185
haftmann
parents: 22076
diff changeset
   631
fun filter_generatable thy targets consts =
21881
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   632
  let
22213
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   633
    val (consts', funcgr) = Funcgr.make_consts thy consts;
22185
haftmann
parents: 22076
diff changeset
   634
    val consts'' = generate thy funcgr targets (fold_map oooo perhaps_def_const) consts';
haftmann
parents: 22076
diff changeset
   635
    val consts''' = map_filter (fn (const, SOME _) => SOME const | (_, NONE) => NONE)
haftmann
parents: 22076
diff changeset
   636
      (consts' ~~ consts'');
haftmann
parents: 22076
diff changeset
   637
  in consts''' end;
21881
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   638
22185
haftmann
parents: 22076
diff changeset
   639
fun read_constspec thy targets "*" = filter_generatable thy targets (consts_of thy NONE)
haftmann
parents: 22076
diff changeset
   640
  | read_constspec thy targets s = if String.isSuffix ".*" s
haftmann
parents: 22076
diff changeset
   641
      then filter_generatable thy targets (consts_of thy (SOME (unsuffix ".*" s)))
21881
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   642
      else [CodegenConsts.read_const thy s];
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   643
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   644
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   645
(** toplevel interface and setup **)
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   646
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   647
local
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   648
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   649
structure P = OuterParse
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   650
and K = OuterKeyword
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   651
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   652
fun code raw_cs seris thy =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   653
  let
21081
837b535137a9 dropped classop shallow namespace
haftmann
parents: 21062
diff changeset
   654
    val seris' = map (fn (target, args as _ :: _) =>
22305
0e56750a092b changed representation of constants
haftmann
parents: 22213
diff changeset
   655
          (target, SOME (CodegenSerializer.get_serializer thy target args CodegenNames.labelled_name))
21081
837b535137a9 dropped classop shallow namespace
haftmann
parents: 21062
diff changeset
   656
      | (target, []) => (CodegenSerializer.assert_serializer thy target, NONE)) seris;
22185
haftmann
parents: 22076
diff changeset
   657
    val targets = case map fst seris' of [] => NONE | xs => SOME xs;
haftmann
parents: 22076
diff changeset
   658
    val cs = maps (read_constspec thy targets) raw_cs;
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   659
    fun generate' thy = case cs
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   660
     of [] => []
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   661
      | _ =>
22213
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   662
          generate thy (Funcgr.make thy cs) targets
22185
haftmann
parents: 22076
diff changeset
   663
            (fold_map oooo ensure_def_const') cs;
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   664
    fun serialize' [] code seri =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   665
          seri NONE code 
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   666
      | serialize' cs code seri =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   667
          seri (SOME cs) code;
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   668
    val cs = generate' thy;
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   669
    val code = Code.get thy;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   670
  in
21081
837b535137a9 dropped classop shallow namespace
haftmann
parents: 21062
diff changeset
   671
    (map (serialize' cs code) (map_filter snd seris'); ())
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   672
  end;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   673
22507
haftmann
parents: 22464
diff changeset
   674
fun print_codethms_cmd thy =
22213
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   675
  print_codethms thy o map (CodegenConsts.read_const thy);
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   676
22507
haftmann
parents: 22464
diff changeset
   677
fun code_deps_cmd thy =
haftmann
parents: 22464
diff changeset
   678
  code_deps thy o map (CodegenConsts.read_const thy);
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   679
22213
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   680
val code_exprP = (
21916
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   681
    (Scan.repeat P.term
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   682
    -- Scan.repeat (P.$$$ "(" |--
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   683
        P.name -- P.arguments
21881
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   684
        --| P.$$$ ")"))
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   685
    >> (fn (raw_cs, seris) => code raw_cs seris)
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   686
  );
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   687
22507
haftmann
parents: 22464
diff changeset
   688
val (codeK, code_abstypeK, code_axiomsK, code_thmsK, code_depsK) =
haftmann
parents: 22464
diff changeset
   689
  ("code_gen", "code_abstype", "code_axioms", "code_thms", "code_deps");
22213
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   690
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   691
in
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   692
21881
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   693
val codeP =
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   694
  OuterSyntax.improper_command codeK "generate and serialize executable code for constants"
22213
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   695
    K.diag (P.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
21881
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   696
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   697
fun codegen_command thy cmd =
22213
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   698
  case Scan.read OuterLex.stopper (P.!!! code_exprP) ((filter OuterLex.is_proper o OuterSyntax.scan) cmd)
21916
68c848f636bb some clarifications
haftmann
parents: 21895
diff changeset
   699
   of SOME f => (writeln "Now generating code..."; f thy)
22197
461130ccfef4 clarified code
haftmann
parents: 22185
diff changeset
   700
    | NONE => error ("Bad directive " ^ quote cmd);
21881
c1ef5c2e3c68 added Thyname.* and * constant expressions
haftmann
parents: 21820
diff changeset
   701
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   702
val code_abstypeP =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   703
  OuterSyntax.command code_abstypeK "axiomatic abstypes for code generation" K.thy_decl (
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   704
    (P.typ -- P.typ -- Scan.optional (P.$$$ "where" |-- Scan.repeat1
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   705
        (P.term --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.term)) [])
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   706
    >> (Toplevel.theory o uncurry abstyp_e)
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   707
  );
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   708
21062
876dd2695423 slight adaption
haftmann
parents: 21012
diff changeset
   709
val code_axiomsP =
876dd2695423 slight adaption
haftmann
parents: 21012
diff changeset
   710
  OuterSyntax.command code_axiomsK "axiomatic constant equalities for code generation" K.thy_decl (
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   711
    Scan.repeat1 (P.term --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.term)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   712
    >> (Toplevel.theory o constsubst_e)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   713
  );
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   714
22305
0e56750a092b changed representation of constants
haftmann
parents: 22213
diff changeset
   715
val code_thmsP =
0e56750a092b changed representation of constants
haftmann
parents: 22213
diff changeset
   716
  OuterSyntax.improper_command code_thmsK "print cached defining equations" OuterKeyword.diag
0e56750a092b changed representation of constants
haftmann
parents: 22213
diff changeset
   717
    (Scan.repeat P.term
0e56750a092b changed representation of constants
haftmann
parents: 22213
diff changeset
   718
      >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
22507
haftmann
parents: 22464
diff changeset
   719
        o Toplevel.keep ((fn thy => print_codethms_cmd thy cs) o Toplevel.theory_of)));
22213
2dd23002c465 adjusted to new codegen_funcgr interface
haftmann
parents: 22197
diff changeset
   720
22507
haftmann
parents: 22464
diff changeset
   721
val code_depsP =
haftmann
parents: 22464
diff changeset
   722
  OuterSyntax.improper_command code_depsK "visualize dependencies of defining equations" OuterKeyword.diag
haftmann
parents: 22464
diff changeset
   723
    (Scan.repeat P.term
haftmann
parents: 22464
diff changeset
   724
      >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
haftmann
parents: 22464
diff changeset
   725
        o Toplevel.keep ((fn thy => code_deps_cmd thy cs) o Toplevel.theory_of)));
haftmann
parents: 22464
diff changeset
   726
haftmann
parents: 22464
diff changeset
   727
val _ = OuterSyntax.add_parsers [codeP, code_abstypeP, code_axiomsP, code_thmsP, code_depsP];
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   728
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   729
end; (* local *)
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   730
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   731
end; (* struct *)