src/Pure/Tools/codegen_package.ML
author haftmann
Tue, 31 Jan 2006 16:14:37 +0100
changeset 18865 31aed965135c
parent 18850 92ef83e5eaea
child 18885 ee8b5c36ba2b
permissions -rw-r--r--
minor cleanups
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     1
(*  Title:      Pure/Tools/codegen_package.ML
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     2
    ID:         $Id$
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     4
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
     5
Code generator from Isabelle theories to
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     6
intermediate language ("Thin-gol").
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     7
*)
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     8
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
     9
signature CODEGEN_PACKAGE =
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    10
sig
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
    11
  type auxtab;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    12
  type eqextr = theory -> auxtab
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    13
    -> (string * typ) -> (thm list * typ) option;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    14
  type defgen;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    15
  type appgen = theory -> auxtab
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
    16
    -> (string * typ) * term list -> CodegenThingol.transact
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
    17
    -> CodegenThingol.iexpr * CodegenThingol.transact;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    18
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
    19
  val add_appconst: string * ((int * int) * appgen) -> theory -> theory;
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
    20
  val add_appconst_i: xstring * ((int * int) * appgen) -> theory -> theory;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    21
  val add_eqextr: string * eqextr -> theory -> theory;
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
    22
  val add_prim_class: xstring -> string list -> (string * string)
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
    23
    -> theory -> theory;
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
    24
  val add_prim_tyco: xstring -> string list -> (string * string)
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
    25
    -> theory -> theory;
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
    26
  val add_prim_const: xstring * string option -> string list -> (string * string)
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
    27
    -> theory -> theory;
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
    28
  val add_prim_i: string -> string list -> (string * Pretty.T)
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
    29
    -> theory -> theory;
18704
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
    30
  val add_pretty_list: string -> string -> string * (int * string)
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
    31
    -> theory -> theory;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    32
  val add_alias: string * string -> theory -> theory;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    33
  val set_is_datatype: (theory -> string -> bool) -> theory -> theory;
18455
b293c1087f1d slight improvements
haftmann
parents: 18454
diff changeset
    34
  val set_get_all_datatype_cons : (theory -> (string * string) list)
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
    35
    -> theory -> theory;
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
    36
  val set_defgen_datatype: defgen -> theory -> theory;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    37
  val set_int_tyco: string -> theory -> theory;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    38
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
    39
  val exprgen_type: theory -> auxtab
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    40
    -> typ -> CodegenThingol.transact -> CodegenThingol.itype * CodegenThingol.transact;
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
    41
  val exprgen_term: theory -> auxtab
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    42
    -> term -> CodegenThingol.transact -> CodegenThingol.iexpr * CodegenThingol.transact;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    43
  val appgen_default: appgen;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    44
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
    45
  val appgen_let: (int -> term -> term list * term)
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
    46
    -> appgen;
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
    47
  val appgen_split: (int -> term -> term list * term)
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
    48
    -> appgen;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    49
  val appgen_number_of: (term -> term) -> (term -> IntInf.int) -> string -> string
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
    50
    -> appgen;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    51
  val add_case_const: (theory -> string -> (string * int) list option) -> xstring
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
    52
    -> theory -> theory;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    53
  val add_case_const_i: (theory -> string -> (string * int) list option) -> string
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
    54
    -> theory -> theory;
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
    55
  val defgen_datatype_proto: (theory -> string -> ((string * sort) list * string list) option)
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
    56
    -> (theory -> string * string -> typ list option)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    57
    -> defgen;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    58
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
    59
  val print_codegen_generated: theory -> unit;
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
    60
  val rename_inconsistent: theory -> theory;
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
    61
  val ensure_datatype_case_consts: (theory -> string list)
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
    62
    -> (theory -> string -> (string * int) list option)
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
    63
    -> theory -> theory;
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
    64
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
    65
  (*debugging purpose only*)
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
    66
  structure InstNameMangler: NAME_MANGLER;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
    67
  structure ConstNameMangler: NAME_MANGLER;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
    68
  structure DatatypeconsNameMangler: NAME_MANGLER;
18231
2eea98bbf650 improved failure tracking
haftmann
parents: 18217
diff changeset
    69
  structure CodegenData: THEORY_DATA;
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
    70
  val mk_tabs: theory -> auxtab;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
    71
  val alias_get: theory -> string -> string;
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
    72
  val idf_of_name: theory -> string -> string -> string;
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
    73
  val idf_of_const: theory -> auxtab -> string * typ -> string;
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    74
end;
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    75
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    76
structure CodegenPackage : CODEGEN_PACKAGE =
18169
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    77
struct
45def66f86cb added modules for code generator generation two, not operational yet
haftmann
parents:
diff changeset
    78
18850
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
    79
open CodegenThingol;
18385
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
    80
infix 8 `%%;
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
    81
infixr 6 `->;
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
    82
infixr 6 `-->;
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
    83
infix 4 `$;
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
    84
infix 4 `$$;
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
    85
infixr 5 `|->;
d0071d93978e improvements in class and eq handling
haftmann
parents: 18380
diff changeset
    86
infixr 5 `|-->;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    87
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    88
(* auxiliary *)
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    89
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
    90
fun devarify_type ty = (fst o Type.freeze_thaw_type o Term.zero_var_indexesT) ty;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
    91
fun devarify_term t = (fst o Type.freeze_thaw o Term.zero_var_indexes) t;
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
    92
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
    93
val is_number = is_some o Int.fromString;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    94
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    95
fun merge_opt _ (x1, NONE) = x1
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    96
  | merge_opt _ (NONE, x2) = x2
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    97
  | merge_opt eq (SOME x1, SOME x2) =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
    98
      if eq (x1, x2) then SOME x1 else error ("incompatible options during merge");
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
    99
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   100
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   101
(* shallow name spaces *)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   102
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   103
val nsp_class = "class";
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   104
val nsp_tyco = "tyco";
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   105
val nsp_const = "const";
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   106
val nsp_overl = "overl";
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   107
val nsp_dtcon = "dtcon";
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   108
val nsp_mem = "mem";
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   109
val nsp_inst = "inst";
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   110
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   111
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   112
(* code generator basics *)
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   113
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   114
structure InstNameMangler = NameManglerFun (
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   115
  type ctxt = theory;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   116
  type src = string * (class * string);
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   117
  val ord = prod_ord string_ord (prod_ord string_ord string_ord);
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   118
  fun mk thy ((thyname, (cls, tyco)), i) =
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   119
    NameSpace.base cls ^ "_" ^ NameSpace.base tyco ^ implode (replicate i "'")
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   120
    |> NameSpace.append thyname;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   121
  fun is_valid _ _ = true;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   122
  fun maybe_unique _ _ = NONE;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   123
  fun re_mangle _ dst = error ("no such instance: " ^ quote dst);
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   124
);
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   125
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   126
structure ConstNameMangler = NameManglerFun (
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   127
  type ctxt = theory;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   128
  type src = string * (typ * typ);
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   129
  val ord = prod_ord string_ord (prod_ord Term.typ_ord Term.typ_ord);
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   130
  fun mk thy ((c, (ty_decl, ty)), i) =
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   131
    let
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   132
      fun mangle (Type (tyco, tys)) =
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   133
            NameSpace.base tyco :: Library.flat (List.mapPartial mangle tys) |> SOME
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   134
        | mangle _ =
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   135
            NONE
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   136
    in
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   137
      Vartab.empty
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   138
      |> Sign.typ_match thy (ty_decl, ty)
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   139
      |> map (snd o snd) o Vartab.dest
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   140
      |> List.mapPartial mangle
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   141
      |> Library.flat
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   142
      |> null ? K ["x"]
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   143
      |> cons c
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   144
      |> space_implode "_"
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   145
      |> curry (op ^ o swap) ((implode oo replicate) i "'")
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   146
    end;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   147
  fun is_valid _ _ = true;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   148
  fun maybe_unique _ _ = NONE;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   149
  fun re_mangle _ dst = error ("no such constant: " ^ quote dst);
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   150
);
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   151
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   152
structure DatatypeconsNameMangler = NameManglerFun (
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   153
  type ctxt = theory;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   154
  type src = string * string;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   155
  val ord = prod_ord string_ord string_ord;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   156
  fun mk thy ((co, dtco), i) =
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   157
        let
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   158
          fun basename 0 = NameSpace.base co
18455
b293c1087f1d slight improvements
haftmann
parents: 18454
diff changeset
   159
            | basename 1 = NameSpace.base dtco ^ "_" ^ NameSpace.base co
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   160
            | basename i = NameSpace.base dtco ^ "_" ^ NameSpace.base co ^ "_" ^ (implode oo replicate) (i-1) "'";
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   161
          fun strip_dtco name =
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   162
            case (rev o NameSpace.unpack) name
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   163
             of x1::x2::xs =>
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   164
                  if x2 = NameSpace.base dtco
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   165
                  then NameSpace.pack (x1::xs)
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   166
                  else name
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   167
              | _ => name;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   168
        in
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   169
          NameSpace.append (NameSpace.drop_base dtco) (basename i)
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   170
          |> strip_dtco
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   171
        end;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   172
  fun is_valid _ _ = true;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   173
  fun maybe_unique _ _ = NONE;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   174
  fun re_mangle _ dst = error ("no such datatype constructor: " ^ quote dst);
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   175
);
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   176
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   177
type auxtab = ((typ * thm) list Symtab.table * string Symtab.table)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   178
  * (InstNameMangler.T * ((typ * typ list) Symtab.table * ConstNameMangler.T)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   179
  * DatatypeconsNameMangler.T);
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   180
type eqextr = theory -> auxtab
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   181
  -> (string * typ) -> (thm list * typ) option;
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   182
type defgen = theory -> auxtab -> gen_defgen;
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   183
type appgen = theory -> auxtab
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   184
  -> (string * typ) * term list -> transact -> iexpr * transact;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   185
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   186
val serializers = ref (
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   187
  Symtab.empty
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   188
  |> Symtab.update (
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   189
       #ml CodegenSerializer.serializers
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   190
       |> apsnd (fn seri => seri
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   191
            (nsp_dtcon, nsp_class, K false)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   192
            [[nsp_class, nsp_tyco], [nsp_const, nsp_overl, nsp_dtcon, nsp_class, nsp_mem, nsp_inst]]
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   193
          )
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   194
     )
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   195
  |> Symtab.update (
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   196
       #haskell CodegenSerializer.serializers
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   197
       |> apsnd (fn seri => seri
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   198
            nsp_dtcon
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   199
            [[nsp_class], [nsp_tyco], [nsp_const, nsp_overl, nsp_mem], [nsp_dtcon], [nsp_inst]]
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   200
          )
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   201
     )
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   202
);
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   203
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   204
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   205
(* theory data for code generator *)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   206
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   207
type gens = {
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   208
  appconst: ((int * int) * (appgen * stamp)) Symtab.table,
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   209
  eqextrs: (string * (eqextr * stamp)) list
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   210
};
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   211
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   212
fun map_gens f { appconst, eqextrs } =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   213
  let
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   214
    val (appconst, eqextrs) =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   215
      f (appconst, eqextrs)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   216
  in { appconst = appconst, eqextrs = eqextrs } : gens end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   218
fun merge_gens
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   219
  ({ appconst = appconst1 , eqextrs = eqextrs1 },
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   220
   { appconst = appconst2 , eqextrs = eqextrs2 }) =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   221
  { appconst = Symtab.merge
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   222
      (fn ((bounds1, (_, stamp1)), (bounds2, (_, stamp2))) => bounds1 = bounds2
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   223
         andalso stamp1 = stamp2)
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   224
      (appconst1, appconst2),
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   225
    eqextrs = AList.merge (op =) (eq_snd (op =)) (eqextrs1, eqextrs2)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   226
  } : gens;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   227
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   228
type logic_data = {
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   229
  is_datatype: ((theory -> string -> bool) * stamp) option,
18455
b293c1087f1d slight improvements
haftmann
parents: 18454
diff changeset
   230
  get_all_datatype_cons: ((theory -> (string * string) list) * stamp) option,
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   231
  defgen_datatype: (defgen * stamp) option,
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   232
  alias: string Symtab.table * string Symtab.table
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   233
};
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   234
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   235
fun map_logic_data f { is_datatype, get_all_datatype_cons, defgen_datatype, alias } =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   236
  let
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   237
    val ((is_datatype, get_all_datatype_cons, defgen_datatype), alias) =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   238
      f ((is_datatype, get_all_datatype_cons, defgen_datatype), alias)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   239
  in { is_datatype = is_datatype, get_all_datatype_cons = get_all_datatype_cons,
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   240
    defgen_datatype = defgen_datatype, alias = alias } : logic_data end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   241
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   242
fun merge_logic_data
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   243
  ({ is_datatype = is_datatype1, get_all_datatype_cons = get_all_datatype_cons1,
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   244
       defgen_datatype = defgen_datatype1, alias = alias1 },
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   245
   { is_datatype = is_datatype2, get_all_datatype_cons = get_all_datatype_cons2,
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   246
       defgen_datatype = defgen_datatype2, alias = alias2 }) =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   247
  let
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   248
  in
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   249
    { is_datatype = merge_opt (eq_snd (op =)) (is_datatype1, is_datatype2),
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   250
      get_all_datatype_cons = merge_opt (eq_snd (op =))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   251
        (get_all_datatype_cons1, get_all_datatype_cons2),
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   252
      defgen_datatype = merge_opt (eq_snd (op =)) (defgen_datatype1, defgen_datatype2),
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   253
      alias = (Symtab.merge (op =) (fst alias1, fst alias2),
18304
684832c9fa62 minor improvements
haftmann
parents: 18282
diff changeset
   254
               Symtab.merge (op =) (snd alias1, snd alias2)) } : logic_data
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   255
  end;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   256
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   257
type target_data = {
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   258
  syntax_class: string Symtab.table,
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   259
  syntax_tyco: (itype CodegenSerializer.pretty_syntax * stamp) Symtab.table,
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   260
  syntax_const: (iexpr CodegenSerializer.pretty_syntax * stamp) Symtab.table
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   261
};
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   262
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   263
fun map_target_data f { syntax_class, syntax_tyco, syntax_const } =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   264
  let
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   265
    val (syntax_class, syntax_tyco, syntax_const) =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   266
      f (syntax_class, syntax_tyco, syntax_const)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   267
  in {
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   268
    syntax_class = syntax_class,
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   269
    syntax_tyco = syntax_tyco,
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   270
    syntax_const = syntax_const } : target_data
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   271
  end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   272
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   273
fun merge_target_data
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   274
  ({ syntax_class = syntax_class1, syntax_tyco = syntax_tyco1, syntax_const = syntax_const1 },
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   275
   { syntax_class = syntax_class2, syntax_tyco = syntax_tyco2, syntax_const = syntax_const2 }) =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   276
  { syntax_class = Symtab.merge (op =) (syntax_class1, syntax_class2),
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   277
    syntax_tyco = Symtab.merge (eq_snd (op =)) (syntax_tyco1, syntax_tyco2),
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   278
    syntax_const = Symtab.merge (eq_snd (op =)) (syntax_const1, syntax_const2) } : target_data;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   279
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   280
structure CodegenData = TheoryDataFun
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   281
(struct
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   282
  val name = "Pure/codegen_package";
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   283
  type T = {
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   284
    modl: module,
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   285
    gens: gens,
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   286
    logic_data: logic_data,
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   287
    target_data: target_data Symtab.table
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   288
  };
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   289
  val empty = {
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   290
    modl = empty_module,
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   291
    gens = { appconst = Symtab.empty, eqextrs = [] } : gens,
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   292
    logic_data = { is_datatype = NONE, get_all_datatype_cons = NONE,
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   293
      defgen_datatype = NONE,
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   294
      alias = (Symtab.empty, Symtab.empty) } : logic_data,
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   295
    target_data =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   296
      Symtab.empty
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   297
      |> Symtab.fold (fn (target, _) =>
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   298
           Symtab.update (target,
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   299
             { syntax_class = Symtab.empty, syntax_tyco = Symtab.empty, syntax_const = Symtab.empty })
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   300
         ) (! serializers)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   301
  } : T;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   302
  val copy = I;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   303
  val extend = I;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   304
  fun merge _ (
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   305
    { modl = modl1, gens = gens1,
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   306
      target_data = target_data1, logic_data = logic_data1 },
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   307
    { modl = modl2, gens = gens2,
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   308
      target_data = target_data2, logic_data = logic_data2 }
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   309
  ) = {
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   310
    modl = merge_module (modl1, modl2),
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   311
    gens = merge_gens (gens1, gens2),
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   312
    logic_data = merge_logic_data (logic_data1, logic_data2),
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   313
    target_data = Symtab.join (K (merge_target_data #> SOME))
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   314
      (target_data1, target_data2)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   315
  };
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18704
diff changeset
   316
  fun print _ _ = ();
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   317
end);
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   318
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18704
diff changeset
   319
val _ = Context.add_setup CodegenData.init;
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18704
diff changeset
   320
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   321
fun map_codegen_data f thy =
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   322
  case CodegenData.get thy
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   323
   of { modl, gens, target_data, logic_data } =>
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   324
      let val (modl, gens, target_data, logic_data) =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   325
        f (modl, gens, target_data, logic_data)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   326
      in CodegenData.put { modl = modl, gens = gens,
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   327
           target_data = target_data, logic_data = logic_data } thy end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   328
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   329
fun print_codegen_generated thy =
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   330
  let
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   331
    val module = (#modl o CodegenData.get) thy;
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   332
  in
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   333
    (writeln o Pretty.output o Pretty.chunks) [pretty_module module, pretty_deps module]
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   334
  end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   335
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   336
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   337
(* name handling *)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   338
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   339
fun add_alias (src, dst) =
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   340
  map_codegen_data
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   341
    (fn (modl, gens, target_data, logic_data) =>
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   342
       (modl, gens, target_data,
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   343
        logic_data |> map_logic_data
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   344
          (apsnd (fn (tab, tab_rev) =>
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   345
            (tab |> Symtab.update (src, dst),
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   346
             tab_rev |> Symtab.update (dst, src))))));
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   347
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   348
val alias_get = perhaps o Symtab.lookup o fst o #alias o #logic_data o CodegenData.get;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   349
val alias_rev = perhaps o Symtab.lookup o snd o #alias o #logic_data o CodegenData.get;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   350
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   351
fun add_nsp shallow name =
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   352
  name
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   353
  |> NameSpace.unpack
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   354
  |> split_last
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   355
  |> apsnd (single #> cons shallow)
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   356
  |> (op @)
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   357
  |> NameSpace.pack;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   358
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   359
fun dest_nsp nsp idf =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   360
  let
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   361
    val idf' = NameSpace.unpack idf;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   362
    val (idf'', idf_base) = split_last idf';
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   363
    val (modl, shallow) = split_last idf'';
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   364
  in
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   365
    if nsp = shallow
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   366
    then (SOME o NameSpace.pack) (modl @ [idf_base])
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   367
    else NONE
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   368
  end;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   369
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   370
fun idf_of_name thy shallow name =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   371
  name
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   372
  |> alias_get thy
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   373
  |> add_nsp shallow;
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   374
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   375
fun name_of_idf thy shallow idf =
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   376
  idf
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   377
  |> dest_nsp shallow
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   378
  |> Option.map (alias_rev thy);
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   379
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   380
fun idf_of_const thy (tabs as ((_, clsmemtab), (_, (overltab1, overltab2), dtcontab)))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   381
      (c, ty) =
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   382
  let
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   383
    fun get_overloaded (c, ty) =
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   384
      case Symtab.lookup overltab1 c
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   385
       of SOME (ty_decl, tys) =>
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   386
            (case find_first (curry (Sign.typ_instance thy) ty) tys
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   387
             of SOME ty' => ConstNameMangler.get thy overltab2
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   388
                  (idf_of_name thy nsp_overl c, (ty_decl, ty')) |> SOME
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   389
              | _ => NONE)
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   390
        | _ => NONE
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   391
    fun get_datatypecons (c, ty) =
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   392
      case (snd o strip_type) ty
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   393
       of Type (tyco, _) =>
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   394
            try (DatatypeconsNameMangler.get thy dtcontab) (c, tyco)
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   395
        | _ => NONE;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   396
  in case get_datatypecons (c, ty)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   397
   of SOME c' => idf_of_name thy nsp_dtcon c'
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   398
    | NONE => case get_overloaded (c, ty)
18515
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   399
   of SOME idf => idf
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   400
    | NONE => case Symtab.lookup clsmemtab c
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   401
   of SOME _ => idf_of_name thy nsp_mem c
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   402
    | NONE => idf_of_name thy nsp_const c
1cad5c2b2a0b substantial improvements in code generating
haftmann
parents: 18455
diff changeset
   403
  end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   404
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   405
fun recconst_of_idf thy (_, (_, (_, overltab2), _)) idf =
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   406
  case name_of_idf thy nsp_const idf
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   407
   of SOME c => SOME (c, Sign.the_const_type thy c)
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   408
    | NONE => (
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   409
        case dest_nsp nsp_overl idf
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   410
         of SOME _ =>
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   411
              idf
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   412
              |> ConstNameMangler.rev thy overltab2
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   413
              |> apfst (the o name_of_idf thy nsp_overl)
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   414
              |> apsnd snd
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   415
              |> SOME
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   416
          | NONE => NONE
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   417
      );
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   418
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   419
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   420
(* further theory data accessors *)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   421
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   422
fun gen_add_appconst prep_const (raw_c, (bounds, ag)) thy =
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   423
  let
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   424
    val c = prep_const thy raw_c;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   425
  in map_codegen_data
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   426
    (fn (modl, gens, target_data, logic_data) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   427
       (modl,
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   428
        gens |> map_gens
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   429
          (fn (appconst, eqextrs) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   430
            (appconst
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   431
             |> Symtab.update (c, (bounds, (ag, stamp ()))),
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   432
             eqextrs)), target_data, logic_data)) thy
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   433
  end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   434
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   435
val add_appconst = gen_add_appconst Sign.intern_const;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   436
val add_appconst_i = gen_add_appconst (K I);
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   437
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   438
fun add_eqextr (name, eqx) =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   439
  map_codegen_data
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   440
    (fn (modl, gens, target_data, logic_data) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   441
       (modl,
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   442
        gens |> map_gens
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   443
          (fn (appconst, eqextrs) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   444
            (appconst, eqextrs
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   445
             |> Output.update_warn (op =) ("overwriting existing equation extractor " ^ name)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   446
                 (name, (eqx, stamp ())))),
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   447
             target_data, logic_data));
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   448
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   449
fun get_eqextrs thy tabs =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   450
  (map (fn (_, (eqx, _)) => eqx thy tabs) o #eqextrs o #gens o CodegenData.get) thy;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   451
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   452
fun set_is_datatype f =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   453
  map_codegen_data
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   454
    (fn (modl, gens, target_data, logic_data) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   455
       (modl, gens, target_data,
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   456
        logic_data
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   457
        |> map_logic_data (apfst (fn (is_datatype, get_all_datatype_cons, defgen_datatype)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   458
             => (SOME (f, stamp ()), get_all_datatype_cons, defgen_datatype)))));
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   459
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   460
fun is_datatype thy =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   461
  case (#is_datatype o #logic_data o CodegenData.get) thy
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   462
   of NONE => K false
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   463
    | SOME (f, _) => f thy;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   464
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   465
fun set_get_all_datatype_cons f =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   466
  map_codegen_data
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   467
    (fn (modl, gens, target_data, logic_data) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   468
       (modl, gens, target_data,
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   469
        logic_data
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   470
        |> map_logic_data ((apfst (fn (is_datatype, get_all_datatype_cons, defgen_datatype)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   471
             => (is_datatype, SOME (f, stamp ()), defgen_datatype))))));
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   472
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   473
fun get_all_datatype_cons thy =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   474
  case (#get_all_datatype_cons o #logic_data o CodegenData.get) thy
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   475
   of NONE => []
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   476
    | SOME (f, _) => f thy;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   477
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   478
fun set_defgen_datatype f =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   479
  map_codegen_data
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   480
    (fn (modl, gens, target_data, logic_data) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   481
       (modl, gens, target_data,
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   482
        logic_data
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   483
        |> map_logic_data ((apfst (fn (is_datatype, get_all_datatype_cons, defgen_datatype)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   484
             => (is_datatype, get_all_datatype_cons, SOME (f, stamp ())))))));
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   485
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   486
fun defgen_datatype thy tabs dtco trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   487
  case (#defgen_datatype o #logic_data o CodegenData.get) thy
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   488
   of NONE =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   489
        trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   490
        |> fail ("no datatype definition generator present")
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   491
    | SOME (f, _) => 
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   492
        trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   493
        |> f thy tabs dtco;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   494
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   495
fun set_int_tyco tyco thy =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   496
  (serializers := (
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   497
    ! serializers
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   498
    |> Symtab.update (
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   499
         #ml CodegenSerializer.serializers
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   500
         |> apsnd (fn seri => seri
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   501
              (nsp_dtcon, nsp_class, fn tyco' => tyco' = idf_of_name thy nsp_tyco tyco )
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   502
              [[nsp_class, nsp_tyco], [nsp_const, nsp_overl, nsp_dtcon, nsp_mem, nsp_inst]]
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   503
            )
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   504
       )
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   505
    ); thy);
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   506
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   507
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   508
(* definition and expression generators *)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   509
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   510
fun ensure_def_class thy tabs cls trns =
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   511
  let
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   512
    fun defgen_class thy (tabs as (_, (insttab, _, _))) cls trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   513
      case name_of_idf thy nsp_class cls
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   514
       of SOME cls =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   515
            let
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   516
              val cs = (snd o ClassPackage.the_consts_sign thy) cls;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   517
              val sortctxts = map (ClassPackage.extract_sortctxt thy o snd) cs;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   518
              val idfs = map (idf_of_name thy nsp_mem o fst) cs;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   519
            in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   520
              trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   521
              |> debug 5 (fn _ => "trying defgen class declaration for " ^ quote cls)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   522
              |> fold_map (ensure_def_class thy tabs) (ClassPackage.the_superclasses thy cls)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   523
              ||>> fold_map (exprgen_type thy tabs o devarify_type o snd) cs
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   524
              ||>> (fold_map o fold_map) (exprgen_tyvar_sort thy tabs) sortctxts
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   525
              |-> (fn ((supcls, memtypes), sortctxts) => succeed
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   526
                (Class ((supcls, ("a", idfs ~~ (sortctxts ~~ memtypes))), [])))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   527
            end
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   528
        | _ =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   529
            trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   530
            |> fail ("no class definition found for " ^ quote cls);
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   531
    val cls' = idf_of_name thy nsp_class cls;
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   532
  in
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   533
    trns
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   534
    |> debug 4 (fn _ => "generating class " ^ quote cls)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   535
    |> gen_ensure_def [("class", defgen_class thy tabs)] ("generating class " ^ quote cls) cls'
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   536
    |> pair cls'
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   537
  end
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   538
and ensure_def_tyco thy tabs tyco trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   539
  let
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   540
    val tyco' = idf_of_name thy nsp_tyco tyco;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   541
  in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   542
    trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   543
    |> debug 4 (fn _ => "generating type constructor " ^ quote tyco)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   544
    |> gen_ensure_def [("datatype", defgen_datatype thy tabs)] ("generating type constructor " ^ quote tyco) tyco'
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   545
    |> pair tyco'
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   546
  end
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   547
and exprgen_tyvar_sort thy tabs (v, sort) trns =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   548
  trns
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   549
  |> fold_map (ensure_def_class thy tabs) (ClassPackage.syntactic_sort_of thy sort)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   550
  |-> (fn sort => pair (unprefix "'" v, sort))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   551
and exprgen_type thy tabs (TVar _) trns =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   552
      error "TVar encountered during code generation"
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   553
  | exprgen_type thy tabs (TFree v_s) trns =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   554
      trns
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   555
      |> exprgen_tyvar_sort thy tabs v_s
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   556
      |-> (fn v_s => pair (IVarT v_s))
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   557
  | exprgen_type thy tabs (Type ("fun", [t1, t2])) trns =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   558
      trns
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   559
      |> exprgen_type thy tabs t1
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   560
      ||>> exprgen_type thy tabs t2
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   561
      |-> (fn (t1', t2') => pair (t1' `-> t2'))
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   562
  | exprgen_type thy tabs (Type (tyco, tys)) trns =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   563
      trns
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   564
      |> ensure_def_tyco thy tabs tyco
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   565
      ||>> fold_map (exprgen_type thy tabs) tys
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   566
      |-> (fn (tyco, tys) => pair (tyco `%% tys));
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   567
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   568
fun mk_lookup thy tabs (ClassPackage.Instance (inst as (cls, tyco), ls)) trns =
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   569
      trns
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   570
      |> ensure_def_class thy tabs cls
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   571
      ||>> ensure_def_inst thy tabs inst
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   572
      ||>> (fold_map o fold_map) (mk_lookup thy tabs) ls
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   573
      |-> (fn ((cls, i), ls) => pair (ClassPackage.Instance ((cls, i), ls)))
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   574
  | mk_lookup thy tabs (ClassPackage.Lookup (clss, (v, i))) trns =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   575
      trns
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   576
      |> fold_map (ensure_def_class thy tabs) clss
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   577
      |-> (fn clss => pair (ClassPackage.Lookup (clss, (v |> unprefix "'", i))))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   578
and mk_fun thy tabs (c, ty) trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   579
  case get_first (fn eqx => eqx (c, ty)) (get_eqextrs thy tabs)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   580
   of SOME (eq_thms, ty) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   581
        let
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   582
          val sortctxt = ClassPackage.extract_sortctxt thy ty;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   583
          fun dest_eqthm eq_thm =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   584
            let
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   585
              val ((t, args), rhs) =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   586
                (apfst strip_comb o Logic.dest_equals o prop_of o Drule.zero_var_indexes) eq_thm;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   587
            in case t
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   588
             of Const (c', _) => if c' = c then (args, rhs)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   589
                 else error ("illegal function equation for " ^ quote c
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   590
                   ^ ", actually defining " ^ quote c')
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   591
              | _ => error ("illegal function equation for " ^ quote c)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   592
            end;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   593
          fun mk_eq (args, rhs) trns =
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   594
            trns
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   595
            |> fold_map (exprgen_term thy tabs o devarify_term) args
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   596
            ||>> (exprgen_term thy tabs o devarify_term) rhs
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   597
            |-> (fn (args, rhs) => pair (args, rhs))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   598
        in
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   599
          trns
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   600
          |> debug 6 (fn _ => "(1) retrieved function equations for " ^
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   601
               quote (c ^ "::" ^ Sign.string_of_typ thy ty))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   602
          |> fold_map (mk_eq o dest_eqthm) eq_thms
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   603
          |> debug 6 (fn _ => "(2) building equations")
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   604
          ||>> (exprgen_type thy tabs o devarify_type) ty
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   605
          |> debug 6 (fn _ => "(3) building type")
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   606
          ||>> fold_map (exprgen_tyvar_sort thy tabs) sortctxt
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   607
          |> debug 6 (fn _ => "(4) building sort context")
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   608
          |-> (fn ((eqs, ty), sortctxt) => (pair o SOME) (eqs, (sortctxt, ty)))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   609
        end
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   610
    | NONE => (NONE, trns)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   611
and ensure_def_inst thy (tabs as (_, (insttab, _, _))) (cls, tyco) trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   612
  let
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   613
    fun defgen_inst thy (tabs as (_, (insttab, _, _))) inst trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   614
      case Option.map (InstNameMangler.rev thy insttab) (name_of_idf thy nsp_inst inst)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   615
       of SOME (_, (cls, tyco)) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   616
            let
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   617
              val (arity, memdefs) = ClassPackage.the_inst_sign thy (cls, tyco);
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   618
              fun gen_suparity supclass trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   619
                trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   620
                |> ensure_def_inst thy tabs (supclass, tyco)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   621
                ||>> (fold_map o fold_map) (mk_lookup thy tabs)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   622
                     (ClassPackage.extract_sortlookup_inst thy (cls, tyco) supclass)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   623
                |-> (fn (inst, ls) => pair (supclass, (inst, ls)));
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   624
              fun gen_membr (m, ty) trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   625
                trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   626
                |> mk_fun thy tabs (m, ty)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   627
                |-> (fn SOME funn => pair (idf_of_name thy nsp_mem m, funn)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   628
                      | NONE => error ("could not derive definition for member " ^ quote m));
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   629
            in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   630
              trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   631
              |> debug 5 (fn _ => "trying defgen class instance for (" ^ quote cls
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   632
                   ^ ", " ^ quote tyco ^ ")")
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   633
              |> ensure_def_class thy tabs cls
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   634
              |> debug 5 (fn _ => "(1) got class")
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   635
              ||>> ensure_def_tyco thy tabs tyco
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   636
              |> debug 5 (fn _ => "(2) got type")
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   637
              ||>> fold_map (exprgen_tyvar_sort thy tabs) arity
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   638
              |> debug 5 (fn _ => "(3) got arity")
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   639
              ||>> fold_map gen_suparity (ClassPackage.the_superclasses thy cls)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   640
              |> debug 5 (fn _ => "(4) got superarities")
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   641
              ||>> fold_map gen_membr memdefs
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   642
              |> debug 5 (fn _ => "(5) got members")
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   643
              |-> (fn ((((cls, tyco), arity), suparities), memdefs) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   644
                     succeed (Classinst (((cls, (tyco, arity)), suparities), memdefs)))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   645
            end
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   646
        | _ =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   647
            trns |> fail ("no class instance found for " ^ quote inst);
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   648
    val thyname = (the o AList.lookup (op =) (ClassPackage.the_instances thy cls)) tyco;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   649
    val inst = idf_of_name thy nsp_inst (InstNameMangler.get thy insttab (thyname, (cls, tyco)));
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   650
  in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   651
    trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   652
    |> debug 4 (fn _ => "generating instance " ^ quote cls ^ " / " ^ quote tyco)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   653
    |> gen_ensure_def [("instance", defgen_inst thy tabs)]
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   654
         ("generating instance " ^ quote cls ^ " / " ^ quote tyco) inst
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   655
    |> pair inst
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   656
  end
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   657
and ensure_def_const thy (tabs as ((_, clsmemtab), (_, overltab, dtcontab))) (c, ty) trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   658
  let
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   659
    fun defgen_funs thy tabs c trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   660
      case recconst_of_idf thy tabs c
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   661
       of SOME (c, ty) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   662
            trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   663
            |> mk_fun thy tabs (c, ty)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   664
            |-> (fn (SOME funn) => succeed (Fun funn)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   665
                  | NONE => fail ("no defining equations found for " ^ quote c))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   666
        | NONE =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   667
            trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   668
            |> fail ("not a constant: " ^ quote c);
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   669
    fun defgen_datatypecons thy (tabs as (_, (_, _, dtcontab))) co trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   670
      case Option.map (DatatypeconsNameMangler.rev thy dtcontab) (name_of_idf thy nsp_dtcon co)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   671
       of SOME (co, dtco) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   672
            trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   673
            |> debug 5 (fn _ => "trying defgen datatype constructor for " ^ quote co)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   674
            |> ensure_def_tyco thy tabs dtco
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   675
            |-> (fn dtco => succeed Undef)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   676
        | _ =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   677
            trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   678
            |> fail ("not a datatype constructor: " ^ quote co);
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   679
    fun defgen_clsmem thy tabs m trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   680
      case name_of_idf thy nsp_mem m
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   681
       of SOME m =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   682
            trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   683
            |> debug 5 (fn _ => "trying defgen class member for " ^ quote m)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   684
            |> ensure_def_class thy tabs ((the o ClassPackage.lookup_const_class thy) m)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   685
            |-> (fn cls => succeed Undef)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   686
        | _ =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   687
            trns |> fail ("no class member found for " ^ quote m)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   688
    val c' = idf_of_const thy tabs (c, ty);
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   689
  in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   690
    trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   691
    |> debug 4 (fn _ => "generating constant " ^ quote c)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   692
    |> gen_ensure_def
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   693
         [("funs", defgen_funs thy tabs),
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   694
          ("clsmem", defgen_clsmem thy tabs),
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   695
          ("datatypecons", defgen_datatypecons thy tabs)]
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   696
         ("generating constant " ^ quote c) c'
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   697
    |> pair c'
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   698
  end
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   699
and exprgen_term thy tabs (Const (f, ty)) trns =
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   700
      trns
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   701
      |> appgen thy tabs ((f, ty), [])
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   702
      |-> (fn e => pair e)
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   703
  | exprgen_term thy tabs (Var ((v, 0), ty)) trns =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   704
      trns
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   705
      |> (exprgen_type thy tabs o devarify_type) ty
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   706
      |-> (fn ty => pair (IVarE (v, ty)))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   707
  | exprgen_term thy tabs (Var ((_, _), _)) trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   708
      error "Var with index greater 0 encountered during code generation"
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   709
  | exprgen_term thy tabs (Free (v, ty)) trns =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   710
      trns
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   711
      |> (exprgen_type thy tabs o devarify_type) ty
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   712
      |-> (fn ty => pair (IVarE (v, ty)))
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   713
  | exprgen_term thy tabs (Abs (v, ty, t)) trns =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   714
      trns
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   715
      |> (exprgen_type thy tabs o devarify_type) ty
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   716
      ||>> exprgen_term thy tabs (subst_bound (Free (v, ty), t))
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   717
      |-> (fn (ty, e) => pair ((v, ty) `|-> e))
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   718
  | exprgen_term thy tabs (t as t1 $ t2) trns =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   719
      let
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   720
        val (t', ts) = strip_comb t
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   721
      in case t'
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   722
       of Const (f, ty) =>
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   723
            trns
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   724
            |> appgen thy tabs ((f, ty), ts)
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   725
            |-> (fn e => pair e)
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   726
        | _ =>
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   727
            trns
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   728
            |> exprgen_term thy tabs t'
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   729
            ||>> fold_map (exprgen_term thy tabs) ts
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   730
            |-> (fn (e, es) => pair (e `$$ es))
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   731
      end
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   732
and appgen_default thy tabs ((c, ty), ts) trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   733
  trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   734
  |> ensure_def_const thy tabs (c, ty)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   735
  ||>> (fold_map o fold_map) (mk_lookup thy tabs)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   736
         (ClassPackage.extract_sortlookup thy (c, ty))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   737
  ||>> (exprgen_type thy tabs o devarify_type) ty
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   738
  ||>> fold_map (exprgen_term thy tabs o devarify_term) ts
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   739
  |-> (fn (((c, ls), ty), es) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   740
         pair (IConst ((c, ty), ls) `$$ es))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   741
and appgen thy tabs ((f, ty), ts) trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   742
  case Symtab.lookup ((#appconst o #gens o CodegenData.get) thy) f
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   743
   of SOME ((imin, imax), (ag, _)) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   744
        if length ts < imin then
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   745
          let
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   746
            val d = imin - length ts;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   747
            val vs = Term.invent_names (add_term_names (Const (f, ty), [])) "x" d;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   748
            val tys = Library.take (d, ((fst o strip_type) ty));
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   749
          in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   750
            trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   751
            |> debug 10 (fn _ => "eta-expanding")
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   752
            |> fold_map (exprgen_type thy tabs o devarify_type) tys
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   753
            ||>> ag thy tabs ((f, ty), ts @ map2 (curry Free) vs tys)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   754
            |-> (fn (tys, e) => pair ((vs ~~ tys) `|--> e))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   755
          end
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   756
        else if length ts > imax then
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   757
          trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   758
          |> debug 10 (fn _ => "splitting arguments (" ^ string_of_int imax ^ ", "
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   759
               ^ string_of_int (length ts) ^ ")")
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   760
          |> ag thy tabs ((f, ty), Library.take (imax, ts))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   761
          ||>> fold_map (exprgen_term thy tabs) (Library.drop (imax, ts))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   762
          |-> (fn es => pair (mk_apps es))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   763
        else
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   764
          trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   765
          |> debug 10 (fn _ => "keeping arguments")
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   766
          |> ag thy tabs ((f, ty), ts)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   767
    | NONE =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   768
        trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   769
        |> appgen_default thy tabs ((f, ty), ts);
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   770
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   771
(* fun ensure_def_eq thy tabs (dtco, (eqpred, arity)) trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   772
  let
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   773
    val name_dtco = (the ooo name_of_idf) thy nsp_tyco dtco;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   774
    val idf_eqinst = idf_of_name thy nsp_eq_inst name_dtco;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   775
    val idf_eqpred = idf_of_name thy nsp_eq_pred name_dtco;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   776
    val inst_sortlookup = map (fn (v, _) => [ClassPackage.Lookup ([], (v, 0))]) arity;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   777
    fun mk_eq_pred _ trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   778
      trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   779
      |> succeed (eqpred)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   780
    fun mk_eq_inst _ trns =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   781
      trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   782
      |> gen_ensure_def [("eqpred", mk_eq_pred)] ("generating equality predicate for " ^ quote dtco) idf_eqpred
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   783
      |> succeed (Classinst ((class_eq, (dtco, arity)), ([], [(fun_eq, (idf_eqpred, inst_sortlookup))])));
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   784
  in
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   785
    trns
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   786
    |> gen_ensure_def [("eqinst", mk_eq_inst)] ("generating equality instance for " ^ quote dtco) idf_eqinst
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   787
  end; *)
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   788
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   789
(* expression generators *)
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   790
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   791
(* fun appgen_eq thy tabs (("op =", Type ("fun", [ty, _])), [t1, t2]) trns =
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   792
  trns
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   793
  |> invoke_eq (exprgen_type thy tabs) (ensure_def_eq thy tabs) ty
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   794
  |-> (fn false => error ("could not derive equality for " ^ Sign.string_of_typ thy ty)
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   795
        | true => fn trns => trns
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   796
  |> exprgen_term thy tabs t1
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   797
  ||>> exprgen_term thy tabs t2
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   798
  |-> (fn (e1, e2) => pair (Fun_eq `$ e1 `$ e2))); *)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   799
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   800
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   801
(* function extractors *)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   802
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   803
fun eqextr_defs thy ((deftab, _), _) (c, ty) =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   804
  let
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   805
    fun eq_typ (ty1, ty2) = 
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   806
      Sign.typ_instance thy (ty1, ty2)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   807
      andalso Sign.typ_instance thy (ty2, ty1)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   808
  in
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   809
    Option.mapPartial (get_first (fn (ty', thm) => if eq_typ (ty, ty')
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   810
      then SOME ([thm], ty')
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   811
      else NONE
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   812
    )) (Symtab.lookup deftab c)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   813
  end;
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   814
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   815
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   816
(* parametrized generators, for instantiation in HOL *)
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   817
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   818
fun appgen_let strip_abs thy tabs ((c, ty), [t2, t3]) trns =
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   819
  let
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   820
    fun dest_let (l as Const (c', _) $ t $ u) =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   821
          if c = c' then
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   822
            case strip_abs 1 u
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   823
             of ([p], u') => apfst (cons (p, t)) (dest_let u')
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   824
              | _ => ([], l)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   825
          else ([], t)
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   826
      | dest_let t = ([], t);
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   827
    fun mk_let (l, r) trns =
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   828
      trns
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   829
      |> exprgen_term thy tabs l
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   830
      ||>> exprgen_term thy tabs r
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   831
      |-> (fn (l, r) => pair (r, l));
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   832
    val (lets, body) = dest_let (Const (c, ty) $ t2 $ t3)
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   833
  in
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   834
    trns
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   835
    |> fold_map mk_let lets
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   836
    ||>> exprgen_term thy tabs body
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   837
    |-> (fn (lets, body) =>
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   838
         pair (Library.foldr (fn ((e, p), body) => ICase (e, [(p, body)])) (lets, body)))
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   839
  end
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   840
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   841
fun appgen_split strip_abs thy tabs (c, [t2]) trns =
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   842
  let
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   843
    val ([p], body) = strip_abs 1 (Const c $ t2)
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   844
  in
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   845
    trns
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   846
    |> exprgen_term thy tabs p
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   847
    ||>> exprgen_term thy tabs body
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   848
    |-> (fn (IVarE v, body) => pair (IAbs (v, body)))
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   849
  end;
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   850
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   851
fun appgen_number_of mk_int_to_nat bin_to_int tyco_int tyco_nat thy tabs ((_,
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   852
  Type (_, [_, ty as Type (tyco, [])])), [bin]) trns =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   853
    if tyco = tyco_int then
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   854
      trns
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   855
      |> (exprgen_type thy tabs o devarify_type) ty
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   856
      |-> (fn ty => pair (CodegenThingol.IConst (((IntInf.toString o bin_to_int) bin, ty), [])))
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   857
    else if tyco = tyco_nat then
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   858
      trns
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   859
      |> exprgen_term thy tabs (mk_int_to_nat bin)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   860
    else error ("invalid type constructor for numeral: " ^ quote tyco);
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   861
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   862
fun appgen_datatype_case cos thy tabs ((_, ty), ts) trns =
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   863
  let
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   864
    val (ts', t) = split_last ts;
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   865
    val (tys, dty) = (split_last o fst o strip_type) ty;
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   866
    fun gen_names i =
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   867
      variantlist (replicate i "x", foldr add_term_names
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   868
       (map (fst o fst o dest_Var) (foldr add_term_vars [] ts)) ts);
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   869
    fun cg_case_d (((cname, i), ty), t) trns =
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   870
      let
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   871
        val vs = gen_names i;
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   872
        val tys = Library.take (i, (fst o strip_type) ty);
18330
444f16d232a2 introduced new map2, fold
haftmann
parents: 18304
diff changeset
   873
        val frees = map2 (curry Free) vs tys;
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   874
        val t' = Envir.beta_norm (list_comb (t, frees));
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   875
      in
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   876
        trns
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   877
        |> exprgen_term thy tabs (list_comb (Const (cname, tys ---> dty), frees))
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   878
        ||>> exprgen_term thy tabs t'
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   879
        |-> (fn (ep, e) => pair (ep, e))
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   880
      end;
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   881
  in
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   882
    trns
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   883
    |> exprgen_term thy tabs t
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   884
    ||>> fold_map cg_case_d ((cos ~~ tys) ~~ ts')
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   885
    |-> (fn (t, ds) => pair (ICase (t, ds)))
18247
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   886
  end;
b17724cae935 code generator: case expressions, improved name resolving
haftmann
parents: 18231
diff changeset
   887
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   888
fun gen_add_case_const prep_c get_case_const_data raw_c thy =
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   889
  let
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   890
    val c = prep_c thy raw_c;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   891
    val (tys, dty) = (split_last o fst o strip_type o Sign.the_const_type thy) c;
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   892
    val cos = (the o get_case_const_data thy) c;
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   893
    val n_eta = length cos + 1;
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   894
  in
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   895
    thy
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   896
    |> add_appconst_i (c, ((n_eta, n_eta), appgen_datatype_case cos))
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   897
  end;
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   898
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   899
val add_case_const = gen_add_case_const Sign.intern_const;
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   900
val add_case_const_i = gen_add_case_const (K I);
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
   901
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   902
fun defgen_datatype_proto get_datatype get_datacons thy (tabs as (_, (_, _, dtcontab))) dtco trns =
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   903
  case name_of_idf thy nsp_tyco dtco
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   904
   of SOME dtco =>
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   905
        (case get_datatype thy dtco
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   906
         of SOME (vars, cos) =>
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   907
              let
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   908
                val cotys = map (the o get_datacons thy o rpair dtco) cos;
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   909
                val coidfs = map (fn co => (DatatypeconsNameMangler.get thy dtcontab (co, dtco)) |>
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   910
                  idf_of_name thy nsp_dtcon) cos;
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   911
              in
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   912
                trns
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   913
                |> debug 5 (fn _ => "trying defgen datatype for " ^ quote dtco)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   914
                |> fold_map (exprgen_tyvar_sort thy tabs) vars
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   915
                ||>> (fold_map o fold_map) (exprgen_type thy tabs o devarify_type) cotys
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   916
                |-> (fn (sorts, tys) => succeed (Datatype
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   917
                     ((sorts, coidfs ~~ tys), [])))
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   918
              end
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   919
          | NONE =>
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   920
              trns
18380
9668764224a7 substantial improvements for class code generation
haftmann
parents: 18361
diff changeset
   921
              |> fail ("no datatype found for " ^ quote dtco))
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   922
    | NONE =>
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   923
        trns
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   924
        |> fail ("not a type constructor: " ^ quote dtco)
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
   925
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   926
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   927
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
   928
(** theory interface **)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   929
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   930
fun mk_tabs thy =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   931
  let
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   932
    fun extract_defs thy =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   933
      let
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   934
        fun dest tm =
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   935
          let
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   936
            val (lhs, rhs) = Logic.dest_equals (prop_of tm);
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   937
            val (t, args) = strip_comb lhs;
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   938
            val (c, ty) = dest_Const t
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   939
          in if forall is_Var args then SOME ((c, ty), tm) else NONE
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   940
          end handle TERM _ => NONE;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   941
        fun prep_def def = (case Codegen.preprocess thy [def] of
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   942
          [def'] => def' | _ => error "mk_auxtab: bad preprocessor");
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   943
        fun add_def (name, _) =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   944
          case (dest o prep_def o Thm.get_axiom thy) name
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   945
           of SOME ((c, ty), tm) =>
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   946
                Symtab.default (c, []) #> Symtab.map_entry c (cons (ty, tm))
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   947
            | NONE => I
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   948
      in
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   949
        Symtab.empty
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   950
        |> fold (Symtab.fold add_def o snd o #axioms o Theory.rep_theory)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   951
             (thy :: Theory.ancestors_of thy)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   952
      end;
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   953
    fun mk_insttab thy =
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   954
      InstNameMangler.empty
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   955
      |> Symtab.fold_map
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   956
          (fn (cls, (clsmems, clsinsts)) => fold_map
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   957
            (fn (tyco, thyname) => InstNameMangler.declare thy (thyname, (cls, tyco))) clsinsts)
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   958
                 (ClassPackage.get_classtab thy)
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   959
      |-> (fn _ => I);
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   960
    fun mk_overltabs thy deftab =
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   961
      (Symtab.empty, ConstNameMangler.empty)
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   962
      |> Symtab.fold
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   963
          (fn (c, [_]) => I
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   964
            | (c, tytab) =>
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   965
                if (is_none o ClassPackage.lookup_const_class thy) c
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   966
                then (fn (overltab1, overltab2) => (
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   967
                    overltab1
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   968
                    |> Symtab.update_new (c, (Sign.the_const_constraint thy c, map fst tytab)),
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   969
                    overltab2
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   970
                    |> fold (fn (ty, _) => ConstNameMangler.declare thy
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   971
                         (idf_of_name thy nsp_overl c, (Sign.the_const_constraint thy c, ty)) #> snd) tytab))
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   972
                else I
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
   973
          ) deftab;
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   974
    fun mk_dtcontab thy =
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   975
      DatatypeconsNameMangler.empty
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   976
      |> fold_map
18455
b293c1087f1d slight improvements
haftmann
parents: 18454
diff changeset
   977
          (fn (_, co_dtco) => DatatypeconsNameMangler.declare_multi thy co_dtco)
b293c1087f1d slight improvements
haftmann
parents: 18454
diff changeset
   978
            (fold (fn (co, dtco) =>
b293c1087f1d slight improvements
haftmann
parents: 18454
diff changeset
   979
              let
b293c1087f1d slight improvements
haftmann
parents: 18454
diff changeset
   980
                val key = ((NameSpace.drop_base o NameSpace.drop_base) co, NameSpace.base co)
b293c1087f1d slight improvements
haftmann
parents: 18454
diff changeset
   981
              in AList.default (op =) (key, []) #> AList.map_entry (op =) key (cons (co, dtco)) end
b293c1087f1d slight improvements
haftmann
parents: 18454
diff changeset
   982
            ) (get_all_datatype_cons thy) [])
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   983
      |-> (fn _ => I);
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   984
    fun mk_clsmemtab thy =
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   985
      Symtab.empty
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   986
      |> Symtab.fold
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   987
          (fn (class, (clsmems, _)) => fold
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   988
            (fn clsmem => Symtab.update (clsmem, class)) clsmems)
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   989
              (ClassPackage.get_classtab thy);
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   990
    val deftab = extract_defs thy;
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   991
    val insttab = mk_insttab thy;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
   992
    val overltabs = mk_overltabs thy deftab;
18454
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   993
    val dtcontab = mk_dtcontab thy;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   994
    val clsmemtab = mk_clsmemtab thy;
6720b5010a57 slight improvements in name handling
haftmann
parents: 18385
diff changeset
   995
  in ((deftab, clsmemtab), (insttab, overltabs, dtcontab)) end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
   996
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   997
fun get_serializer target =
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   998
  case Symtab.lookup (!serializers) target
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
   999
   of SOME seri => seri
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1000
    | NONE => error ("unknown code target language: " ^ quote target);
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1001
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1002
fun map_module f =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1003
  map_codegen_data (fn (modl, gens, target_data, logic_data) =>
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1004
    (f modl, gens, target_data, logic_data));
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1005
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1006
fun expand_module gen thy =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1007
  (#modl o CodegenData.get) thy
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1008
  |> start_transact (gen thy (mk_tabs thy))
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1009
  |-> (fn x:'a => fn modl => (x, map_module (K modl) thy));
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1010
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1011
fun rename_inconsistent thy =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1012
  let
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1013
    fun get_inconsistent thyname =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1014
      let
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1015
        val thy = theory thyname;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1016
        fun own_tables get =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1017
          (get thy)
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1018
          |> fold (Symtab.fold (Symtab.remove (K true)) o get) (Theory.parents_of thy)
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1019
          |> Symtab.keys;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1020
        val names = own_tables (#2 o #types o Type.rep_tsig o Sign.tsig_of)
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1021
          @ own_tables (#2 o #declarations o Consts.dest o #consts o Sign.rep_sg);
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1022
        fun diff names =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1023
          fold (fn name =>
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1024
            if is_prefix (op =) (NameSpace.unpack thyname) (NameSpace.unpack name)
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1025
            then I
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1026
            else cons (name, NameSpace.append thyname (NameSpace.base name))) names [];
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1027
      in diff names end;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1028
    val inconsistent = map get_inconsistent (ThyInfo.names ()) |> Library.flat;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1029
    fun add (src, dst) thy =
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1030
      if (is_some oo Symtab.lookup o fst o #alias o #logic_data o CodegenData.get) thy src
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1031
      then (warning ("code generator alias already defined for " ^ quote src ^ ", will not overwrite"); thy)
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1032
      else add_alias (src, dst) thy
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1033
  in fold add inconsistent thy end;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1034
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1035
fun ensure_datatype_case_consts get_datatype_case_consts get_case_const_data thy =
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1036
  let
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1037
    fun ensure case_c thy =
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1038
      if
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1039
        Symtab.defined ((#appconst o #gens o CodegenData.get) thy) case_c
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1040
      then
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1041
        (warning ("case constant " ^ quote case_c ^ " already present in application table, will not overwrite"); thy)
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1042
      else
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1043
        add_case_const_i get_case_const_data case_c thy;
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1044
  in
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1045
    fold ensure (get_datatype_case_consts thy) thy
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1046
  end;
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1047
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1048
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1049
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1050
(** target languages **)
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1051
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1052
(* primitive definitions *)
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1053
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1054
fun read_typ thy =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1055
  Sign.read_typ (thy, K NONE);
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1056
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1057
fun read_const thy (raw_c, raw_ty) =
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1058
  let
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1059
    val c = Sign.intern_const thy raw_c;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1060
    val _ = if Sign.declared_const thy c
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1061
      then ()
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1062
      else error ("no such constant: " ^ quote c);
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1063
    val ty = case raw_ty
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1064
     of NONE => Sign.the_const_constraint thy c
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1065
      | SOME raw_ty => read_typ thy raw_ty;
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1066
  in (c, ty) end;
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1067
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1068
fun read_quote reader gen raw thy =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1069
  expand_module
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1070
    (fn thy => fn tabs => gen thy tabs (reader thy raw))
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1071
    thy;
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1072
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1073
fun gen_add_prim prep_name prep_primdef raw_name deps (target, raw_primdef) thy =
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1074
  let
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1075
    val _ = if Symtab.defined ((#target_data o CodegenData.get) thy) target
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1076
      then () else error ("unknown target language: " ^ quote target);
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1077
    val tabs = mk_tabs thy;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1078
    val name = prep_name thy tabs raw_name;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1079
    val primdef = prep_primdef raw_primdef;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1080
  in
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1081
    thy
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1082
    |> map_module (CodegenThingol.add_prim name deps (target, primdef))
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1083
  end;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1084
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1085
val add_prim_i = gen_add_prim ((K o K) I) I;
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1086
val add_prim_class = gen_add_prim
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1087
  (fn thy => K (idf_of_name thy nsp_class o Sign.intern_class thy))
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1088
  (Pretty.str o CodegenSerializer.parse_targetdef I);
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1089
val add_prim_tyco = gen_add_prim
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1090
  (fn thy => K (idf_of_name thy nsp_tyco o Sign.intern_type thy))
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1091
  (Pretty.str o CodegenSerializer.parse_targetdef I);
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1092
val add_prim_const = gen_add_prim
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1093
  (fn thy => fn tabs => idf_of_const thy tabs o read_const thy)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1094
  (Pretty.str o CodegenSerializer.parse_targetdef I);
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1095
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1096
val ensure_prim = (map_module oo CodegenThingol.ensure_prim);
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1097
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1098
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1099
(* syntax *)
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1100
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1101
fun gen_add_syntax_class prep_class class target pretty thy =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1102
  thy
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1103
  |> map_codegen_data
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1104
    (fn (modl, gens, target_data, logic_data) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1105
       (modl, gens,
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1106
        target_data |> Symtab.map_entry target
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1107
          (map_target_data
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1108
            (fn (syntax_class, syntax_tyco, syntax_const) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1109
             (syntax_class
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1110
              |> Symtab.update (prep_class thy class, pretty), syntax_tyco, syntax_const))),
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1111
        logic_data));
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1112
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1113
val add_syntax_class = gen_add_syntax_class Sign.intern_class;
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1114
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1115
val parse_syntax_tyco =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1116
  let
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1117
    fun mk reader raw_tyco target thy =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1118
      let
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1119
        val _ = get_serializer target;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1120
        fun check_tyco tyco =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1121
          if Sign.declared_tyname thy tyco
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1122
          then tyco
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1123
          else error ("no such type constructor: " ^ quote tyco);
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1124
        fun prep_tyco thy tyco =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1125
          tyco
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1126
          |> Sign.intern_type thy
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1127
          |> check_tyco
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1128
          |> idf_of_name thy nsp_tyco;
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1129
        val tyco = prep_tyco thy raw_tyco;
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1130
      in
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1131
        thy
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1132
        |> ensure_prim tyco target
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1133
        |> reader
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1134
        |-> (fn pretty => map_codegen_data
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1135
          (fn (modl, gens, target_data, logic_data) =>
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1136
             (modl, gens,
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1137
              target_data |> Symtab.map_entry target
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1138
                (map_target_data
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1139
                  (fn (syntax_class, syntax_tyco, syntax_const) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1140
                   (syntax_class, syntax_tyco |> Symtab.update
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1141
                      (tyco, (pretty, stamp ())),
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1142
                    syntax_const))),
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1143
              logic_data)))
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1144
      end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1145
  in
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1146
    CodegenSerializer.parse_syntax
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1147
      (read_quote read_typ (fn thy => fn tabs => exprgen_type thy tabs o devarify_type))
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1148
    #-> (fn reader => pair (mk reader))
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1149
  end;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1150
18704
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1151
fun add_pretty_syntax_const c target pretty =
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1152
  map_codegen_data
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1153
    (fn (modl, gens, target_data, logic_data) =>
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1154
       (modl, gens,
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1155
        target_data |> Symtab.map_entry target
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1156
          (map_target_data
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1157
            (fn (syntax_class, syntax_tyco, syntax_const) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1158
             (syntax_class, syntax_tyco,
18704
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1159
              syntax_const
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1160
              |> Symtab.update
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1161
                 (c, (pretty, stamp ()))))),
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1162
        logic_data));
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1163
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1164
val parse_syntax_const =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1165
  let
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1166
    fun mk reader raw_const target thy =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1167
      let
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1168
        val _ = get_serializer target;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1169
        val tabs = mk_tabs thy;
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1170
        val c = idf_of_const thy tabs (read_const thy raw_const);
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1171
      in
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1172
        thy
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1173
        |> ensure_prim c target
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1174
        |> reader
18704
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1175
        |-> (fn pretty => add_pretty_syntax_const c target pretty)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1176
      end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1177
  in
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1178
    CodegenSerializer.parse_syntax (read_quote Sign.read_term exprgen_term)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1179
    #-> (fn reader => pair (mk reader))
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1180
  end;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1181
18704
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1182
fun add_pretty_list raw_nil raw_cons (target, seri) thy =
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1183
  let
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1184
    val _ = get_serializer target;
18704
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1185
    val tabs = mk_tabs thy;
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1186
    fun mk_const raw_name =
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1187
      let
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1188
        val name = Sign.intern_const thy raw_name;
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1189
      in idf_of_const thy tabs (name, Sign.the_const_type thy name) end;
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1190
    val nil' = mk_const raw_nil;
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1191
    val cons' = mk_const raw_cons;
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1192
    val pr' = CodegenSerializer.pretty_list nil' cons' seri;
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1193
  in
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1194
    thy
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1195
    |> ensure_prim cons' target
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1196
    |> add_pretty_syntax_const cons' target pr'
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1197
  end;
2c86ced392a8 substantial improvement in serialization handling
haftmann
parents: 18702
diff changeset
  1198
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1199
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1200
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1201
(** toplevel interface **)
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1202
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1203
local
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1204
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1205
fun generate_code (SOME raw_consts) thy =
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1206
      let
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1207
        val consts = map (read_const thy) raw_consts;
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1208
        fun generate thy tabs = fold_map (ensure_def_const thy tabs) consts
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1209
      in
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1210
        thy
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1211
        |> expand_module generate
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1212
        |-> (fn cs => pair (SOME cs))
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1213
      end
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1214
  | generate_code NONE thy =
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1215
      (NONE, thy);
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1216
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1217
fun serialize_code target seri raw_consts thy =
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1218
  let
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1219
    fun serialize cs thy =
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1220
      let
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1221
        val module = (#modl o CodegenData.get) thy;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1222
        val target_data =
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1223
          thy
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1224
          |> CodegenData.get
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1225
          |> #target_data
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1226
          |> (fn data => (the oo Symtab.lookup) data target);
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1227
        in (seri (
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1228
          (Symtab.lookup o #syntax_class) target_data,
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1229
          (Option.map fst oo Symtab.lookup o #syntax_tyco) target_data,
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1230
          (Option.map fst oo Symtab.lookup o #syntax_const) target_data
18850
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
  1231
        ) cs module : unit; thy) end;
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1232
  in
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1233
    thy
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1234
    |> generate_code raw_consts
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1235
    |-> (fn cs => serialize cs)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1236
  end;
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1237
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1238
structure P = OuterParse
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1239
and K = OuterKeyword
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1240
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1241
in
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1242
18850
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
  1243
val (generateK, serializeK,
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1244
     primclassK, primtycoK, primconstK,
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1245
     syntax_classK, syntax_tycoK, syntax_constK, aliasK) =
18850
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
  1246
  ("code_generate", "code_serialize",
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1247
   "code_primclass", "code_primtyco", "code_primconst",
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1248
   "code_syntax_class", "code_syntax_tyco", "code_syntax_const", "code_alias");
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1249
val dependingK =
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1250
  ("depending_on");
18335
99baddf6b0d0 various improvements
haftmann
parents: 18330
diff changeset
  1251
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1252
val generateP =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1253
  OuterSyntax.command generateK "generate executable code for constants" K.thy_decl (
18850
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
  1254
    Scan.repeat1 (P.name -- Scan.option (P.$$$ "::" |-- P.typ))
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1255
    >> (fn raw_consts =>
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1256
          Toplevel.theory (generate_code (SOME raw_consts) #> snd))
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1257
  );
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1258
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1259
val serializeP =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1260
  OuterSyntax.command serializeK "serialize executable code for constants" K.thy_decl (
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1261
    P.name
18850
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
  1262
    -- Scan.option (Scan.repeat1 (P.name -- Scan.option (P.$$$ "::" |-- P.typ)))
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1263
    #-> (fn (target, raw_consts) =>
18850
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
  1264
          P.$$$ "("
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
  1265
          |-- get_serializer target
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
  1266
          --| P.$$$ ")"
18756
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1267
          >> (fn seri =>
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1268
            Toplevel.theory (serialize_code target seri raw_consts)
5eb3df798405 more general serializer
haftmann
parents: 18708
diff changeset
  1269
          ))
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1270
  );
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1271
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1272
val aliasP =
18282
98431741bda3 added haskell serializer
haftmann
parents: 18247
diff changeset
  1273
  OuterSyntax.command aliasK "declare an alias for a theory identifier" K.thy_decl (
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1274
    Scan.repeat1 (P.name -- P.name)
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1275
      >> (Toplevel.theory oo fold) add_alias
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1276
  );
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1277
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1278
val primclassP =
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1279
  OuterSyntax.command primclassK "define target-lanugage specific class" K.thy_decl (
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1280
    P.xname
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1281
    -- Scan.optional (P.$$$ dependingK |--
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1282
         P.$$$ "(" |-- P.list1 P.name --| P.$$$ ")") []
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1283
    -- Scan.repeat1 (P.name -- P.text)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1284
      >> (fn ((raw_class, depends), primdefs) =>
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1285
            (Toplevel.theory oo fold) (add_prim_class raw_class depends) primdefs)
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1286
  );
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1287
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1288
val primtycoP =
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1289
  OuterSyntax.command primtycoK "define target-lanugage specific type constructor" K.thy_decl (
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1290
    P.xname
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1291
    -- Scan.optional (P.$$$ dependingK |--
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1292
         P.$$$ "(" |-- P.list1 P.name --| P.$$$ ")") []
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1293
    -- Scan.repeat1 (P.name -- P.text)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1294
      >> (fn ((raw_tyco, depends), primdefs) =>
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1295
            (Toplevel.theory oo fold) (add_prim_tyco raw_tyco depends) primdefs)
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1296
  );
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1297
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1298
val primconstP =
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1299
  OuterSyntax.command primconstK "define target-lanugage specific constant" K.thy_decl (
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1300
    (P.xname -- Scan.option (P.$$$ "::" |-- P.typ))
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1301
    -- Scan.optional (P.$$$ dependingK |--
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1302
         P.$$$ "(" |-- P.list1 P.name --| P.$$$ ")") []
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1303
    -- Scan.repeat1 (P.name -- P.text)
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1304
      >> (fn ((raw_const, depends), primdefs) =>
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1305
            (Toplevel.theory oo fold) (add_prim_const raw_const depends) primdefs)
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1306
  );
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1307
18865
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1308
val syntax_classP =
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1309
  OuterSyntax.command syntax_tycoK "define code syntax for class" K.thy_decl (
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1310
    Scan.repeat1 (
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1311
      P.xname
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1312
      -- Scan.repeat1 (
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1313
           P.name -- P.string
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1314
         )
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1315
    )
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1316
    >> (Toplevel.theory oo fold) (fn (raw_class, syns) =>
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1317
          fold (fn (target, p) => add_syntax_class raw_class target p) syns)
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1318
  );
31aed965135c minor cleanups
haftmann
parents: 18850
diff changeset
  1319
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1320
val syntax_tycoP =
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1321
  OuterSyntax.command syntax_tycoK "define code syntax for type constructor" K.thy_decl (
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1322
    Scan.repeat1 (
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1323
      P.xname
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1324
      -- Scan.repeat1 (
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1325
           P.name -- parse_syntax_tyco
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1326
         )
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1327
    )
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1328
    >> (Toplevel.theory oo fold) (fn (raw_tyco, syns) =>
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1329
          fold (fn (target, modifier) => modifier raw_tyco target) syns)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1330
  );
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1331
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1332
val syntax_constP =
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1333
  OuterSyntax.command syntax_constK "define code syntax for constant" K.thy_decl (
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1334
    Scan.repeat1 (
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1335
      (P.xname -- Scan.option (P.$$$ "::" |-- P.typ))
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1336
      -- Scan.repeat1 (
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1337
           P.name -- parse_syntax_const
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1338
         )
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1339
    )
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1340
    >> (Toplevel.theory oo fold) (fn (raw_c, syns) =>
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 18517
diff changeset
  1341
          fold (fn (target, modifier) => modifier raw_c target) syns)
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1342
  );
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1343
18850
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
  1344
val _ = OuterSyntax.add_parsers [generateP, serializeP, aliasP,
18517
788fa99aba33 slight improvements
haftmann
parents: 18516
diff changeset
  1345
  primclassP, primtycoP, primconstP, syntax_tycoP, syntax_constP];
18850
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
  1346
val _ = OuterSyntax.add_keywords [dependingK];
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1347
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1348
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1349
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18704
diff changeset
  1350
(** theory setup **)
18516
4424e2bce9af slightly improved serialization
haftmann
parents: 18515
diff changeset
  1351
18850
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
  1352
val _ = Context.add_setup (
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
  1353
  add_eqextr ("defs", eqextr_defs)
18708
4b3dadb4fe33 setup: theory -> theory;
wenzelm
parents: 18704
diff changeset
  1354
(*   add_appconst_i ("op =", ((2, 2), appgen_eq))  *)
18850
92ef83e5eaea various improvements
haftmann
parents: 18756
diff changeset
  1355
);
18217
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1356
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1357
end; (* local *)
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1358
e0b08c9534ff added codegen package
haftmann
parents: 18169
diff changeset
  1359
end; (* struct *)