src/Pure/Tools/codegen_package.ML
author wenzelm
Wed, 11 Oct 2006 22:59:36 +0200
changeset 20988 0887d0dd3210
parent 20976 e324808e9f1f
child 21012 f08574148b7a
permissions -rw-r--r--
* isabelle-process: option -S (secure mode) disables some critical operations;
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;
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
    11
  val codegen_term: theory -> term -> thm * iterm;
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
    12
  val eval_term: theory -> (string (*reference name!*) * 'a option ref) * term -> 'a;
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
    13
  val const_of_idf: theory -> string -> string * typ;
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    14
  val get_root_module: theory -> CodegenThingol.code;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    15
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    16
  type appgen;
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
    17
  val add_appconst: string * appgen -> theory -> theory;
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
    18
  val appgen_numeral: (theory -> term -> IntInf.int) -> appgen;
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
    19
  val appgen_char: (term -> int option) -> appgen;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    20
  val appgen_case: (theory -> term
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    21
    -> ((string * typ) list * ((term * typ) * (term * term) list)) option)
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    22
    -> appgen;
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    23
  val appgen_let: appgen;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    24
end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    25
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    26
structure CodegenPackage : CODEGEN_PACKAGE =
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    27
struct
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    28
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    29
open BasicCodegenThingol;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    30
val tracing = CodegenThingol.tracing;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    31
val succeed = CodegenThingol.succeed;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    32
val fail = CodegenThingol.fail;
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
    33
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
    34
(** code extraction **)
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
    35
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
    36
(* theory data *)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    37
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    38
structure Code = CodeDataFun
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    39
(struct
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    40
  val name = "Pure/code";
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    41
  type T = CodegenThingol.code;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    42
  val empty = CodegenThingol.empty_code;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    43
  fun merge _ = CodegenThingol.merge_code;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    44
  fun purge _ NONE _ = CodegenThingol.empty_code
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    45
    | purge NONE _ _ = CodegenThingol.empty_code
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    46
    | purge (SOME thy) (SOME cs) code =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    47
        let
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    48
          val cs_exisiting =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    49
            map_filter (CodegenNames.const_rev thy) (Graph.keys code);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    50
        in
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    51
          Graph.del_nodes
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    52
            ((Graph.all_succs code
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    53
              o map (CodegenNames.const thy)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    54
              o filter (member CodegenConsts.eq_const cs_exisiting)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    55
              ) cs)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    56
            code
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    57
        end;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    58
end);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    59
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
    60
type appgen = theory -> ((sort -> sort) * Sorts.algebra) * Consts.T
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
    61
  -> CodegenFuncgr.T
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    62
  -> bool * string list option
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
    63
  -> (string * typ) * term list -> CodegenThingol.transact -> iterm * CodegenThingol.transact;
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
    64
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
    65
type appgens = (int * (appgen * stamp)) Symtab.table;
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    66
val merge_appgens : appgens * appgens -> appgens =
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
    67
  Symtab.merge (fn ((bounds1, (_, stamp1)), (bounds2, (_, stamp2))) =>
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    68
    bounds1 = bounds2 andalso stamp1 = stamp2);
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    69
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    70
structure Consttab = CodegenConsts.Consttab;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    71
type abstypes = typ Symtab.table * CodegenConsts.const Consttab.table;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    72
fun merge_abstypes ((typs1, consts1) : abstypes, (typs2, consts2) : abstypes) =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    73
  (Symtab.merge (Type.eq_type Vartab.empty) (typs1, typs2),
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    74
    Consttab.merge CodegenConsts.eq_const (consts1, consts2));
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    75
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
    76
structure CodegenPackageData = TheoryDataFun
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    77
(struct
19953
2f54a51f1801 class package refinements, slight code generation refinements
haftmann
parents: 19884
diff changeset
    78
  val name = "Pure/codegen_package";
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    79
  type T = appgens * abstypes;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    80
  val empty = (Symtab.empty, (Symtab.empty, Consttab.empty));
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    81
  val copy = I;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    82
  val extend = I;
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    83
  fun merge _ ((appgens1, abstypes1), (appgens2, abstypes2)) =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    84
    (merge_appgens (appgens1, appgens2), merge_abstypes (abstypes1, abstypes2));
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
    85
  fun print _ _ = ();
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    86
end);
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    87
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
    88
val _ = Context.add_setup (Code.init #> CodegenPackageData.init);
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18704
diff changeset
    89
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
    90
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
    91
(* extraction kernel *)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
    92
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    93
fun check_strict (false, _) has_seri x =
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    94
      false
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    95
  | check_strict (_, SOME targets) has_seri x =
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
    96
      not (has_seri targets x)
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
    97
  | check_strict (true, _) has_seri x =
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    98
      true;
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
    99
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   100
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
   101
 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
   102
  | NONE => NONE;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   103
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   104
fun ensure_def_class thy (algbr as ((proj_sort, _), _)) funcgr strct class trns =
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   105
  let
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   106
    val (v, cs) = (ClassPackage.the_consts_sign thy) class;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   107
    val superclasses = (proj_sort o Sign.super_classes thy) class;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   108
    val classops' = map (CodegenNames.const thy o CodegenConsts.norm_of_typ thy) cs;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   109
    val class' = CodegenNames.class thy class;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   110
    fun defgen_class trns =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   111
      trns
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   112
      |> fold_map (ensure_def_class thy algbr funcgr strct) superclasses
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   113
      ||>> (fold_map (exprgen_type thy algbr funcgr strct) o map snd) cs
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   114
      |-> (fn (superclasses, classoptyps) => succeed
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   115
        (CodegenThingol.Class (superclasses,
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   116
          (unprefix "'" v, classops' ~~ classoptyps))))
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   117
  in
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   118
    trns
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   119
    |> tracing (fn _ => "generating class " ^ quote class)
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   120
    |> CodegenThingol.ensure_def defgen_class true
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   121
        ("generating class " ^ quote class) class'
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   122
    |> pair class'
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   123
  end
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   124
and ensure_def_tyco thy algbr funcgr strct tyco trns =
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   125
  let
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   126
    fun defgen_datatype trns =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   127
      case CodegenData.get_datatype thy tyco
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   128
       of SOME (vs, cos) =>
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   129
            trns
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   130
            |> fold_map (exprgen_tyvar_sort thy algbr funcgr strct) vs
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   131
            ||>> fold_map (fn (c, tys) =>
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   132
              fold_map (exprgen_type thy algbr funcgr strct) tys
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   133
              #-> (fn tys' =>
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   134
                pair ((CodegenNames.const thy o CodegenConsts.norm_of_typ thy)
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   135
                  (c, tys ---> Type (tyco, map TFree vs)), tys'))) cos
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   136
            |-> (fn (vs, cos) => succeed (CodegenThingol.Datatype (vs, cos)))
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   137
        | NONE =>
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   138
            trns
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   139
            |> fail ("No such datatype: " ^ quote tyco)
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   140
    val tyco' = CodegenNames.tyco thy tyco;
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   141
    val strict = check_strict strct (CodegenSerializer.tyco_has_serialization thy) tyco';
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   142
  in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   143
    trns
20835
haftmann
parents: 20699
diff changeset
   144
    |> tracing (fn _ => "generating type constructor " ^ quote tyco)
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   145
    |> CodegenThingol.ensure_def defgen_datatype strict
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   146
        ("generating type constructor " ^ quote tyco) tyco'
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   147
    |> pair tyco'
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   148
  end
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   149
and exprgen_tyvar_sort thy (algbr as ((proj_sort, _), _)) funcgr strct (v, sort) trns =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   150
  trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   151
  |> fold_map (ensure_def_class thy algbr funcgr strct) (proj_sort sort)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   152
  |-> (fn sort => pair (unprefix "'" v, sort))
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   153
and exprgen_type thy algbr funcgr strct (TVar _) trns =
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   154
      error "TVar encountered in typ during code generation"
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   155
  | exprgen_type thy algbr funcgr strct (TFree vs) trns =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   156
      trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   157
      |> exprgen_tyvar_sort thy algbr funcgr strct vs
19167
f237c0cb3882 refined representation of codegen intermediate language
haftmann
parents: 19150
diff changeset
   158
      |-> (fn (v, sort) => pair (ITyVar v))
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   159
  | exprgen_type thy algbr funcgr strct (Type ("fun", [t1, t2])) trns =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   160
      trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   161
      |> exprgen_type thy algbr funcgr strct t1
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   162
      ||>> exprgen_type thy algbr funcgr strct t2
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   163
      |-> (fn (t1', t2') => pair (t1' `-> t2'))
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   164
  | exprgen_type thy algbr funcgr strct (Type (tyco, tys)) trns =
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   165
      case get_abstype thy (tyco, tys)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   166
       of SOME ty =>
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   167
            trns
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   168
            |> exprgen_type thy algbr funcgr strct ty
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   169
        | NONE =>
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   170
            trns
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   171
            |> ensure_def_tyco thy algbr funcgr strct tyco
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   172
            ||>> fold_map (exprgen_type thy algbr funcgr strct) tys
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   173
            |-> (fn (tyco, tys) => pair (tyco `%% tys));
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   174
20835
haftmann
parents: 20699
diff changeset
   175
exception CONSTRAIN of (string * typ) * typ;
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   176
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   177
fun exprgen_typinst thy (algbr as ((proj_sort, algebra), consts)) funcgr strct (ty_ctxt, sort_decl) trns =
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   178
  let
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   179
    val pp = Sign.pp thy;
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   180
    datatype inst =
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   181
        Inst of (class * string) * inst list list
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   182
      | Contxt of (string * sort) * (class list * int);
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   183
    fun classrel (l as Contxt (v_sort, (classes, n)), _) class  =
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   184
          Contxt (v_sort, (class :: classes, n))
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   185
      | classrel (Inst ((_, tyco), lss), _) class =
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   186
          Inst ((class, tyco), lss);
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   187
    fun constructor tyco iss class =
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   188
      Inst ((class, tyco), (map o map) fst iss);
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   189
    fun variable (TFree (v, sort)) =
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   190
      let
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   191
        val sort' = proj_sort sort;
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   192
      in map_index (fn (n, class) => (Contxt ((v, sort'), ([], n)), class)) sort' end;
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   193
    val insts = Sorts.of_sort_derivation pp algebra
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   194
      {classrel = classrel, constructor = constructor, variable = variable}
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   195
      (ty_ctxt, proj_sort sort_decl);
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   196
    fun mk_dict (Inst (inst, instss)) trns =
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   197
          trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   198
          |> ensure_def_inst thy algbr funcgr strct inst
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   199
          ||>> (fold_map o fold_map) mk_dict instss
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   200
          |-> (fn (inst, instss) => pair (Instance (inst, instss)))
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   201
      | mk_dict (Contxt ((v, sort), (classes, k))) trns =
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   202
          trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   203
          |> fold_map (ensure_def_class thy algbr funcgr strct) classes
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   204
          |-> (fn classes => pair (Context (classes, (unprefix "'" v,
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   205
                if length sort = 1 then ~1 else k))))
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   206
  in
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   207
    trns
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   208
    |> fold_map mk_dict insts
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   209
  end
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   210
and exprgen_typinst_const thy (algbr as (_, consts)) funcgr strct (c, ty_ctxt) trns =
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   211
  let
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   212
    val c' = CodegenConsts.norm_of_typ thy (c, ty_ctxt)
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   213
    val idf = CodegenNames.const thy c';
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   214
    val ty_decl = Consts.declaration consts idf;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   215
    val insts = (op ~~ o apsnd (map (snd o dest_TVar)) oo pairself)
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   216
      (curry (Consts.typargs consts) idf) (ty_ctxt, ty_decl);
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   217
    val _ = if exists not (map (Sign.of_sort thy) insts)
20835
haftmann
parents: 20699
diff changeset
   218
      then raise CONSTRAIN ((c, ty_decl), ty_ctxt) else ();
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   219
  in
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   220
    trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   221
    |> fold_map (exprgen_typinst thy algbr funcgr strct) insts
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   222
  end
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   223
and ensure_def_inst thy (algbr as ((proj_sort, _), _)) funcgr strct (class, tyco) trns =
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   224
  let
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   225
    val (vs, classop_defs) = ((apsnd o map) Const o ClassPackage.the_inst_sign thy)
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   226
      (class, tyco);
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   227
    val classops = (map (CodegenConsts.norm_of_typ thy) o snd
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   228
      o ClassPackage.the_consts_sign thy) class;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   229
    val arity_typ = Type (tyco, (map TFree vs));
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   230
    val superclasses = (proj_sort o Sign.super_classes thy) class
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   231
    fun gen_superarity superclass trns =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   232
      trns
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   233
      |> ensure_def_class thy algbr funcgr strct superclass
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   234
      ||>> exprgen_typinst thy algbr funcgr strct (arity_typ, [superclass])
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   235
      |-> (fn (superclass, [Instance (inst, iss)]) => pair (superclass, (inst, iss)));
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   236
    fun gen_classop_def (classop, t) trns =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   237
      trns
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   238
      |> ensure_def_const thy algbr funcgr strct classop
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   239
      ||>> exprgen_term thy algbr funcgr strct t;
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   240
    fun defgen_inst trns =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   241
      trns
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   242
      |> ensure_def_class thy algbr funcgr strct class
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   243
      ||>> ensure_def_tyco thy algbr funcgr strct tyco
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   244
      ||>> fold_map (exprgen_tyvar_sort thy algbr funcgr strct) vs
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   245
      ||>> fold_map gen_superarity superclasses
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   246
      ||>> fold_map gen_classop_def (classops ~~ classop_defs)
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   247
      |-> (fn ((((class, tyco), arity), superarities), classops) =>
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   248
             succeed (CodegenThingol.Classinst ((class, (tyco, arity)), (superarities, classops))));
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   249
    val inst = CodegenNames.instance thy (class, tyco);
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   250
  in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   251
    trns
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   252
    |> tracing (fn _ => "generating instance " ^ quote class ^ " / " ^ quote tyco)
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   253
    |> CodegenThingol.ensure_def defgen_inst true
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   254
         ("generating instance " ^ quote class ^ " / " ^ quote tyco) inst
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   255
    |> pair inst
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   256
  end
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   257
and ensure_def_const thy (algbr as (_, consts)) funcgr strct (c, tys) trns =
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   258
  let
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   259
    val c' = CodegenNames.const thy (c, tys);
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   260
    fun defgen_datatypecons trns =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   261
      trns
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   262
      |> ensure_def_tyco thy algbr funcgr strct
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   263
          ((the o CodegenData.get_datatype_of_constr thy) (c, tys))
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   264
      |-> (fn _ => succeed CodegenThingol.Bot);
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   265
    fun defgen_classop trns =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   266
      trns
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   267
      |> ensure_def_class thy algbr funcgr strct ((the o AxClass.class_of_param thy) c)
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   268
      |-> (fn _ => succeed CodegenThingol.Bot);
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   269
    fun defgen_fun trns =
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   270
      case CodegenFuncgr.get_funcs funcgr
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   271
        (perhaps (Consttab.lookup ((snd o snd o CodegenPackageData.get) thy)) (c, tys))
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   272
       of eq_thms as eq_thm :: _ =>
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   273
            let
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   274
              val msg = cat_lines ("generating code for theorems " :: map string_of_thm eq_thms);
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   275
              val ty = (Logic.unvarifyT o CodegenData.typ_func thy) eq_thm;
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   276
              val vs = (map dest_TFree o Consts.typargs consts) (c', ty);
20835
haftmann
parents: 20699
diff changeset
   277
              val dest_eqthm =
haftmann
parents: 20699
diff changeset
   278
                apfst (snd o strip_comb) o Logic.dest_equals o Logic.unvarify o prop_of;
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   279
              fun exprgen_eq (args, rhs) trns =
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   280
                trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   281
                |> fold_map (exprgen_term thy algbr funcgr strct) args
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   282
                ||>> exprgen_term thy algbr funcgr strct rhs;
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   283
            in
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   284
              trns
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   285
              |> CodegenThingol.message msg (fn trns => trns
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   286
              |> fold_map (exprgen_eq o dest_eqthm) eq_thms
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   287
              ||>> fold_map (exprgen_tyvar_sort thy algbr funcgr strct) vs
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   288
              ||>> exprgen_type thy algbr funcgr strct ty
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   289
              |-> (fn ((eqs, vs), ty) => succeed (CodegenThingol.Fun (eqs, (vs, ty)))))
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   290
            end
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   291
        | [] =>
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   292
              trns
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   293
              |> fail ("No defining equations found for "
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   294
                   ^ (quote o CodegenConsts.string_of_const thy) (c, tys));
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   295
    val defgen =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   296
      if CodegenNames.has_nsp CodegenNames.nsp_fun c'
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   297
      then defgen_fun
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   298
      else if CodegenNames.has_nsp CodegenNames.nsp_classop c'
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   299
      then defgen_classop
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   300
      else if CodegenNames.has_nsp CodegenNames.nsp_dtco c'
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   301
      then defgen_datatypecons
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   302
      else error ("Illegal shallow name space for constant: " ^ quote c');
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   303
    val strict = check_strict strct (CodegenSerializer.const_has_serialization thy) c';
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   304
  in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   305
    trns
20835
haftmann
parents: 20699
diff changeset
   306
    |> tracing (fn _ => "generating constant "
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   307
        ^ (quote o CodegenConsts.string_of_const thy) (c, tys))
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   308
    |> CodegenThingol.ensure_def defgen strict ("generating constant "
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   309
         ^ CodegenConsts.string_of_const thy (c, tys)) c'
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   310
    |> pair c'
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   311
  end
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   312
and exprgen_term thy algbr funcgr strct (Const (c, ty)) trns =
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   313
      trns
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   314
      |> select_appgen thy algbr funcgr strct ((c, ty), [])
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   315
  | exprgen_term thy algbr funcgr strct (Var _) trns =
20389
8b6ecb22ef35 cleanup
haftmann
parents: 20386
diff changeset
   316
      error "Var encountered in term during code generation"
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   317
  | exprgen_term thy algbr funcgr strct (Free (v, ty)) trns =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   318
      trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   319
      |> exprgen_type thy algbr funcgr strct ty
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   320
      |-> (fn _ => pair (IVar v))
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   321
  | exprgen_term thy algbr funcgr strct (Abs (raw_v, ty, raw_t)) trns =
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   322
      let
20386
d1cbe5aa6bf2 module restructuring
haftmann
parents: 20353
diff changeset
   323
        val (v, t) = Syntax.variant_abs (CodegenNames.purify_var raw_v, ty, raw_t);
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   324
      in
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   325
        trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   326
        |> exprgen_type thy algbr funcgr strct ty
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   327
        ||>> exprgen_term thy algbr funcgr strct t
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   328
        |-> (fn (ty, t) => pair ((v, ty) `|-> t))
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   329
      end
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   330
  | exprgen_term thy algbr funcgr strct (t as _ $ _) trns =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   331
      case strip_comb t
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   332
       of (Const (c, ty), ts) =>
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   333
            trns
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   334
            |> select_appgen thy algbr funcgr strct ((c, ty), ts)
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   335
            |-> (fn t => pair t)
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   336
        | (t', ts) =>
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   337
            trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   338
            |> exprgen_term thy algbr funcgr strct t'
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   339
            ||>> fold_map (exprgen_term thy algbr funcgr strct) ts
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   340
            |-> (fn (t, ts) => pair (t `$$ ts))
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   341
and appgen_default thy algbr funcgr strct ((c, ty), ts) trns =
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   342
  trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   343
  |> ensure_def_const thy algbr funcgr strct (CodegenConsts.norm_of_typ thy (c, ty))
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   344
  ||>> exprgen_type thy algbr funcgr strct ty
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   345
  ||>> exprgen_typinst_const thy algbr funcgr strct (c, ty)
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   346
  ||>> fold_map (exprgen_term thy algbr funcgr strct) ts
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   347
  |-> (fn (((c, ty), iss), ts) =>
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   348
         pair (IConst (c, (iss, ty)) `$$ ts))
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   349
and select_appgen thy algbr funcgr strct ((c, ty), ts) trns =
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   350
  case Symtab.lookup (fst (CodegenPackageData.get thy)) c
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   351
   of SOME (i, (appgen, _)) =>
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   352
        if length ts < i then
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   353
          let
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   354
            val tys = Library.take (i - length ts, ((fst o strip_type) ty));
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   355
            val vs = Name.names (Name.declare c Name.context) "a" tys;
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   356
          in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   357
            trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   358
            |> fold_map (exprgen_type thy algbr funcgr strct) tys
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   359
            ||>> appgen thy algbr funcgr strct ((c, ty), ts @ map Free vs)
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   360
            |-> (fn (tys, t) => pair (map2 (fn (v, _) => pair v) vs tys `|--> t))
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   361
          end
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   362
        else if length ts > i then
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   363
          trns
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   364
          |> appgen thy algbr funcgr strct ((c, ty), Library.take (i, ts))
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   365
          ||>> fold_map (exprgen_term thy algbr funcgr strct) (Library.drop (i, ts))
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   366
          |-> (fn (t, ts) => pair (t `$$ ts))
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   367
        else
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   368
          trns
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   369
          |> appgen thy algbr funcgr strct ((c, ty), ts)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   370
    | NONE =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   371
        trns
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   372
        |> appgen_default thy algbr funcgr strct ((c, ty), ts);
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   373
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   374
20835
haftmann
parents: 20699
diff changeset
   375
(* entrance points into extraction kernel *)
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   376
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   377
fun ensure_def_const' thy algbr funcgr strct c trns =
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   378
  ensure_def_const thy algbr funcgr strct c trns
20835
haftmann
parents: 20699
diff changeset
   379
  handle CONSTRAIN ((c, ty), ty_decl) => error (
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   380
    "Constant " ^ c ^ " with most general type\n"
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   381
    ^ Sign.string_of_typ thy ty
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   382
    ^ "\noccurs with type\n"
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   383
    ^ Sign.string_of_typ thy ty_decl);
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   384
fun exprgen_term' thy algbr funcgr strct t trns =
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   385
  exprgen_term thy algbr funcgr strct t trns
20835
haftmann
parents: 20699
diff changeset
   386
  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
   387
    ^ ",\nconstant " ^ c ^ " with most general type\n"
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   388
    ^ Sign.string_of_typ thy ty
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   389
    ^ "\noccurs with type\n"
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   390
    ^ Sign.string_of_typ thy ty_decl);
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   391
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   392
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   393
(* parametrized application generators, for instantiation in object logic *)
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   394
(* (axiomatic extensions of extraction kernel *)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   395
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   396
fun appgen_numeral int_of_numeral thy algbr funcgr strct (app as (c, ts)) trns =
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20466
diff changeset
   397
  case try (int_of_numeral thy) (list_comb (Const c, ts))
20835
haftmann
parents: 20699
diff changeset
   398
   of SOME i =>
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   399
        trns
20835
haftmann
parents: 20699
diff changeset
   400
        |> appgen_default thy algbr funcgr strct app
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   401
        |-> (fn t => pair (CodegenThingol.INum (i, t)))
19884
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   402
    | NONE =>
a7be206d8655 improvements in code generator
haftmann
parents: 19816
diff changeset
   403
        trns
20835
haftmann
parents: 20699
diff changeset
   404
        |> appgen_default thy algbr funcgr strct app;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   405
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   406
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
   407
  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
   408
   of SOME i =>
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   409
        trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   410
        |> exprgen_type thy algbr funcgr strct ty
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   411
        ||>> appgen_default thy algbr funcgr strct app
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   412
        |-> (fn (_, t0) => pair (IChar (chr i, t0)))
19607
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   413
    | NONE =>
07eeb832f28d introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents: 19597
diff changeset
   414
        trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   415
        |> 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
   416
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   417
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
   418
  let
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   419
    val SOME ([], ((st, sty), ds)) = dest_case_expr thy (list_comb (Const c_ty, ts));
20976
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   420
    fun fold_map_aterms f (t $ u) s =
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   421
          s
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   422
          |> fold_map_aterms f t
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   423
          ||>> fold_map_aterms f u
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   424
          |-> (fn (t1, t2) => pair (t1 $ t2))
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   425
      | fold_map_aterms f (Abs (v, ty, t)) s =
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   426
          s
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   427
          |> fold_map_aterms f t
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   428
          |-> (fn t' => pair (Abs (v, ty, t')))
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   429
      | fold_map_aterms f a s = f a s;
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   430
    fun purify_term_frees (Free (v, ty)) ctxt = 
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   431
          let
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   432
            val ([v'], ctxt') = Name.variants [CodegenNames.purify_var v] ctxt;
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   433
          in (Free (v, ty), ctxt') end
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   434
      | purify_term_frees t ctxt = (t, ctxt);
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   435
    fun clausegen (raw_dt, raw_bt) trns =
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   436
      let
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   437
        val ((dt, bt), _) = Name.context
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   438
          |> fold_map_aterms purify_term_frees raw_dt
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   439
          ||>> fold_map_aterms purify_term_frees raw_bt;
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   440
      in
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   441
        trns
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   442
        |> exprgen_term thy algbr funcgr strct dt
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   443
        ||>> exprgen_term thy algbr funcgr strct bt
e324808e9f1f minor refinements in serialization
haftmann
parents: 20931
diff changeset
   444
      end;
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   445
  in
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   446
    trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   447
    |> exprgen_term thy algbr funcgr strct st
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   448
    ||>> exprgen_type thy algbr funcgr strct sty
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   449
    ||>> fold_map clausegen ds
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   450
    ||>> appgen_default thy algbr funcgr strct app
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   451
    |-> (fn (((se, sty), ds), e0) => pair (ICase (((se, sty), ds), e0)))
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   452
  end;
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   453
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   454
fun appgen_let thy algbr funcgr strct (app as (_, [st, ct])) trns =
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   455
  trns
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   456
  |> exprgen_term thy algbr funcgr strct ct
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   457
  ||>> exprgen_term thy algbr funcgr strct st
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   458
  ||>> appgen_default thy algbr funcgr strct app
20105
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   459
  |-> (fn (((v, ty) `|-> be, se), e0) =>
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   460
            pair (ICase (((se, ty), case be
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   461
              of ICase (((IVar w, _), ds), _) => if v = w then ds else [(IVar v, be)]
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   462
               | _ => [(IVar v, be)]
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   463
            ), e0))
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   464
        | (_, e0) => pair e0);
454f4be984b7 adaptions in codegen
haftmann
parents: 20076
diff changeset
   465
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   466
fun add_appconst (c, appgen) thy =
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   467
  let
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   468
    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
   469
    val _ = Code.change thy (K CodegenThingol.empty_code);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   470
  in
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   471
    (CodegenPackageData.map o apfst)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   472
      (Symtab.update (c, (i, (appgen, stamp ())))) thy
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   473
  end;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   474
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   475
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   476
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   477
(** abstype and constsubst interface **)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   478
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   479
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
   480
  let
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   481
    val abstyp = Type.no_tvars (prep_typ thy raw_abstyp);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   482
    val substtyp = Type.no_tvars (prep_typ thy raw_substtyp);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   483
    val absconsts = (map o pairself) (prep_const thy) raw_absconsts;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   484
    val Type (abstyco, tys) = abstyp handle BIND => error ("bad type: " ^ Sign.string_of_typ thy abstyp);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   485
    val typarms = map (fst o dest_TFree) tys handle MATCH => error ("bad type: " ^ Sign.string_of_typ thy abstyp);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   486
    fun mk_index v = 
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   487
      let
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   488
        val k = find_index (fn w => v = w) typarms;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   489
      in if k = ~1
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   490
        then error ("free type variable: " ^ quote v)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   491
        else TFree (string_of_int k, [])
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   492
      end;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   493
    val typpat = map_atyps (fn TFree (v, _) => mk_index v) substtyp;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   494
    fun apply_typpat (Type (tyco, tys)) =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   495
          let
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   496
            val tys' = map apply_typpat tys;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   497
          in if tyco = abstyco then
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   498
            (map_atyps (fn TFree (n, _) => nth tys' (the (Int.fromString n)))) typpat
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   499
          else
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   500
            Type (tyco, tys')
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   501
          end
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   502
      | apply_typpat ty = ty;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   503
    val string_of_typ = setmp show_sorts true (Sign.string_of_typ thy);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   504
    fun add_consts (c1, c2) =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   505
      let
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   506
        val _ = if CodegenNames.has_nsp CodegenNames.nsp_fun (CodegenNames.const thy c2)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   507
          then ()
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   508
          else error ("not a function: " ^ CodegenConsts.string_of_const thy c2);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   509
        val funcgr = CodegenFuncgr.mk_funcgr thy [c1, c2] [];
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   510
        val ty_map = CodegenFuncgr.get_func_typs funcgr;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   511
        val ty1 = (apply_typpat o the o AList.lookup CodegenConsts.eq_const ty_map) c1;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   512
        val ty2 = (the o AList.lookup CodegenConsts.eq_const ty_map) c2;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   513
        val _ = if Sign.typ_equiv thy (ty1, ty2) then () else
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   514
          error ("Incompatiable type signatures of " ^ CodegenConsts.string_of_const thy c1
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   515
            ^ " and " ^ CodegenConsts.string_of_const thy c2 ^ ":\n"
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   516
            ^ string_of_typ ty1 ^ "\n" ^ string_of_typ ty2);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   517
      in Consttab.update (c1, c2) end;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   518
    val _ = Code.change thy (K CodegenThingol.empty_code);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   519
  in
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   520
    thy
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   521
    |> (CodegenPackageData.map o apsnd) (fn (abstypes, abscs) =>
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   522
          (abstypes
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   523
          |> Symtab.update (abstyco, typpat),
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   524
          abscs
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   525
          |> fold add_consts absconsts)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   526
       )
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   527
  end;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   528
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   529
fun gen_constsubst prep_const raw_constsubsts thy =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   530
  let
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   531
    val constsubsts = (map o pairself) (prep_const thy) raw_constsubsts;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   532
    val string_of_typ = setmp show_sorts true (Sign.string_of_typ thy);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   533
    fun add_consts (c1, c2) =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   534
      let
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   535
        val _ = if CodegenNames.has_nsp CodegenNames.nsp_fun (CodegenNames.const thy c2)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   536
          then ()
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   537
          else error ("not a function: " ^ CodegenConsts.string_of_const thy c2);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   538
        val funcgr = CodegenFuncgr.mk_funcgr thy [c1, c2] [];
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   539
        val ty_map = CodegenFuncgr.get_func_typs funcgr;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   540
        val ty1 = (the o AList.lookup CodegenConsts.eq_const ty_map) c1;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   541
        val ty2 = (the o AList.lookup CodegenConsts.eq_const ty_map) c2;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   542
        val _ = if Sign.typ_equiv thy (ty1, ty2) then () else
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   543
          error ("Incompatiable type signatures of " ^ CodegenConsts.string_of_const thy c1
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   544
            ^ " and " ^ CodegenConsts.string_of_const thy c2 ^ ":\n"
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   545
            ^ string_of_typ ty1 ^ "\n" ^ string_of_typ ty2);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   546
      in Consttab.update (c1, c2) end;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   547
    val _ = Code.change thy (K CodegenThingol.empty_code);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   548
  in
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   549
    thy
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   550
    |> (CodegenPackageData.map o apsnd o apsnd) (fold add_consts constsubsts)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   551
  end;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   552
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   553
val abstyp = gen_abstyp CodegenConsts.norm Sign.certify_typ;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   554
val abstyp_e = gen_abstyp CodegenConsts.read_const (fn thy => Sign.read_typ (thy, K NONE));
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   555
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   556
val constsubst = gen_constsubst CodegenConsts.norm;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   557
val constsubst_e = gen_constsubst CodegenConsts.read_const;
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   558
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   559
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   560
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   561
(** code generation interfaces **)
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   562
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   563
fun generate thy (cs, rhss) targets init gen it =
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   564
  let
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   565
    val raw_funcgr = CodegenFuncgr.mk_funcgr thy cs rhss;
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   566
    val cs' = map_filter (Consttab.lookup ((snd o snd o CodegenPackageData.get) thy))
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   567
      (CodegenFuncgr.Constgraph.keys raw_funcgr);
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   568
    val funcgr = CodegenFuncgr.mk_funcgr thy cs' [];
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   569
    val qnaming = NameSpace.qualified_names NameSpace.default_naming;
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   570
    val algebr = ClassPackage.operational_algebra thy;
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   571
    val consttab = Consts.empty
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   572
      |> fold (fn (c, ty) => Consts.declare qnaming
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   573
           ((CodegenNames.const thy c, ty), true))
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   574
           (CodegenFuncgr.get_func_typs funcgr);
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   575
    val algbr = (algebr, consttab);
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   576
  in   
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   577
    Code.change_yield thy (CodegenThingol.start_transact init (gen thy algbr funcgr
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   578
        (true, targets) it))
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   579
    |> (fn (x, modl) => x)
20466
7c20ddbd911b explicit table with constant types
haftmann
parents: 20456
diff changeset
   580
  end;
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   581
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   582
fun codegen_term thy t =
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   583
  let
20835
haftmann
parents: 20699
diff changeset
   584
    fun const_typ (c, ty) =
haftmann
parents: 20699
diff changeset
   585
      let
haftmann
parents: 20699
diff changeset
   586
        val const = CodegenConsts.norm_of_typ thy (c, ty);
haftmann
parents: 20699
diff changeset
   587
        val funcgr = CodegenFuncgr.mk_funcgr thy [const] [];
haftmann
parents: 20699
diff changeset
   588
      in case CodegenFuncgr.get_funcs funcgr const
haftmann
parents: 20699
diff changeset
   589
       of (thm :: _) => CodegenData.typ_func thy thm
haftmann
parents: 20699
diff changeset
   590
        | [] => Sign.the_const_type thy c
haftmann
parents: 20699
diff changeset
   591
      end;
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   592
    val ct = Thm.cterm_of thy t;
20835
haftmann
parents: 20699
diff changeset
   593
    val (thm, ct') = CodegenData.preprocess_cterm thy
haftmann
parents: 20699
diff changeset
   594
      (const_typ) ct;
haftmann
parents: 20699
diff changeset
   595
    val t' = Thm.term_of ct';
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   596
    val cs_rhss = CodegenConsts.consts_of thy t';
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   597
  in
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   598
    (thm, generate thy cs_rhss (SOME []) NONE exprgen_term' t')
20353
d73e49780ef2 code generator refinements
haftmann
parents: 20216
diff changeset
   599
  end;
19136
00ade10f611d some refinements
haftmann
parents: 19111
diff changeset
   600
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   601
fun const_of_idf thy =
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   602
  CodegenConsts.typ_of_inst thy o the o CodegenNames.const_rev thy;
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   603
19967
33da452f0abe slight refinements
haftmann
parents: 19956
diff changeset
   604
fun get_root_module thy =
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   605
  Code.get thy;
19042
630b8dd0b31a exported some interfaces useful for other code generator approaches
haftmann
parents: 19038
diff changeset
   606
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   607
fun eval_term thy (ref_spec, t) =
20213
2b319e945905 added eval_term
haftmann
parents: 20194
diff changeset
   608
  let
20401
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   609
    val _ = Term.fold_atyps (fn _ =>
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   610
      error ("Term" ^ Sign.string_of_term thy t ^ "is polymorhpic"))
f01ae74f29f2 more concise string serialization
haftmann
parents: 20389
diff changeset
   611
      (Term.fastype_of t);
20835
haftmann
parents: 20699
diff changeset
   612
    val (_, t') = codegen_term thy t;
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   613
  in
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   614
    CodegenSerializer.eval_term thy (ref_spec, t') (Code.get thy)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   615
  end;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   616
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   617
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   618
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   619
(** toplevel interface and setup **)
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   620
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   621
local
19150
1457d810b408 class package and codegen refinements
haftmann
parents: 19136
diff changeset
   622
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   623
structure P = OuterParse
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   624
and K = OuterKeyword
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   625
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   626
fun code raw_cs seris thy =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   627
  let
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   628
    val cs = map (CodegenConsts.read_const thy) raw_cs;
20456
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   629
    val targets = case map fst seris
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   630
     of [] => NONE
42be3a46dcd8 pervasive refinements
haftmann
parents: 20439
diff changeset
   631
      | xs => SOME xs;
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   632
    val seris' = map_filter (fn (target, args as _ :: _) =>
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   633
      SOME (CodegenSerializer.get_serializer thy (target, args)) | _ => NONE) seris;
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   634
    fun generate' thy = case cs
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   635
     of [] => []
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   636
      | _ =>
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   637
          generate thy (cs, []) targets NONE (fold_map oooo ensure_def_const') cs;
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   638
    fun serialize' [] code seri =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   639
          seri NONE code 
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   640
      | serialize' cs code seri =
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   641
          seri (SOME cs) code;
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   642
    val cs = generate' thy;
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   643
    val code = Code.get thy;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   644
  in
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   645
    (map (serialize' cs code) seris'; ())
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   646
  end;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   647
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   648
val (codeK, code_abstypeK, code_constsubstK) =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   649
  ("code_gen", "code_abstype", "code_constsubst");
20699
0cc77abb185a refinements in codegen serializer
haftmann
parents: 20600
diff changeset
   650
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   651
in
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   652
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   653
val codeP =
20835
haftmann
parents: 20699
diff changeset
   654
  OuterSyntax.improper_command codeK "generate and serialize executable code for constants" K.diag (
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   655
    Scan.repeat P.term
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   656
    -- Scan.repeat (P.$$$ "(" |--
20896
1484c7af6d68 cleaned up interfaces
haftmann
parents: 20855
diff changeset
   657
        P.name -- P.arguments
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   658
        --| P.$$$ ")")
20600
6d75e02ed285 added codegen_data
haftmann
parents: 20485
diff changeset
   659
    >> (fn (raw_cs, seris) => Toplevel.keep (code raw_cs seris o Toplevel.theory_of))
20439
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   660
  );
1bf42b262a38 code refinements
haftmann
parents: 20434
diff changeset
   661
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   662
val code_abstypeP =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   663
  OuterSyntax.command code_abstypeK "axiomatic abstypes for code generation" K.thy_decl (
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   664
    (P.typ -- P.typ -- Scan.optional (P.$$$ "where" |-- Scan.repeat1
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   665
        (P.term --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.term)) [])
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   666
    >> (Toplevel.theory o uncurry abstyp_e)
20428
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   667
  );
67fa1c6ba89e refinements
haftmann
parents: 20401
diff changeset
   668
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   669
val code_constsubstP =
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   670
  OuterSyntax.command code_constsubstK "axiomatic constant substitutions for code generation" K.thy_decl (
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   671
    Scan.repeat1 (P.term --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.term)
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   672
    >> (Toplevel.theory o constsubst_e)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   673
  );
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   674
20931
19d9b78218fd added code_abstype and code_constsubst
haftmann
parents: 20896
diff changeset
   675
val _ = OuterSyntax.add_parsers [codeP, code_abstypeP, code_constsubstP];
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   676
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   677
end; (* local *)
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   678
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   679
end; (* struct *)