src/Tools/code/code_package.ML
author haftmann
Wed, 15 Aug 2007 08:57:42 +0200
changeset 24283 8ca96f4e49cd
parent 24250 c59c09b09794
child 24348 c708ea5b109a
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     1
(*  Title:      Tools/code/code_package.ML
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     2
    ID:         $Id$
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     4
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     5
Code generator translation kernel.  Code generator Isar setup.
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     6
*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     7
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     8
signature CODE_PACKAGE =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     9
sig
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    10
  (* interfaces *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    11
  val eval_conv: theory
24283
haftmann
parents: 24250
diff changeset
    12
    -> (CodeThingol.code -> CodeThingol.iterm * CodeThingol.itype -> string list -> cterm -> thm)
haftmann
parents: 24250
diff changeset
    13
    -> cterm -> thm;
haftmann
parents: 24250
diff changeset
    14
  val eval_term: theory
haftmann
parents: 24250
diff changeset
    15
    -> (CodeThingol.code -> CodeThingol.iterm * CodeThingol.itype -> string list -> cterm -> 'a)
haftmann
parents: 24250
diff changeset
    16
    -> cterm -> 'a;
haftmann
parents: 24250
diff changeset
    17
  val satisfies_ref: bool option ref;
haftmann
parents: 24250
diff changeset
    18
  val satisfies: theory -> cterm -> string list -> bool;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    19
  val codegen_command: theory -> string -> unit;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    20
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    21
  (* axiomatic interfaces *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    22
  type appgen;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    23
  val add_appconst: string * appgen -> theory -> theory;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    24
  val appgen_let: appgen;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    25
  val appgen_if: appgen;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    26
  val appgen_case: (theory -> term
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    27
    -> ((string * typ) list * ((term * typ) * (term * term) list)) option)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    28
    -> appgen;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    29
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    30
  val timing: bool ref;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    31
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    32
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    33
structure CodePackage : CODE_PACKAGE =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    34
struct
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    35
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    36
open BasicCodeThingol;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    37
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    38
(** code translation **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    39
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    40
(* theory data *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    41
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    42
type appgen = theory -> ((sort -> sort) * Sorts.algebra) * Consts.T
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    43
  -> CodeFuncgr.T
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    44
  -> (string * typ) * term list -> CodeThingol.transact -> iterm * CodeThingol.transact;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    45
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    46
type appgens = (int * (appgen * stamp)) Symtab.table;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    47
val merge_appgens : appgens * appgens -> appgens =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    48
  Symtab.merge (fn ((bounds1, (_, stamp1)), (bounds2, (_, stamp2))) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    49
    bounds1 = bounds2 andalso stamp1 = stamp2);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    50
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    51
structure Consttab = CodeUnit.Consttab;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    52
type abstypes = typ Symtab.table * CodeUnit.const Consttab.table;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    53
fun merge_abstypes ((typs1, consts1) : abstypes, (typs2, consts2) : abstypes) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    54
  (Symtab.merge (Type.eq_type Vartab.empty) (typs1, typs2),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    55
    Consttab.merge CodeUnit.eq_const (consts1, consts2));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    56
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    57
structure Translation = TheoryDataFun
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    58
(
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    59
  type T = appgens * abstypes;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    60
  val empty = (Symtab.empty, (Symtab.empty, Consttab.empty));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    61
  val copy = I;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    62
  val extend = I;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    63
  fun merge _ ((appgens1, abstypes1), (appgens2, abstypes2)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    64
    (merge_appgens (appgens1, appgens2), merge_abstypes (abstypes1, abstypes2));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    65
);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    66
24283
haftmann
parents: 24250
diff changeset
    67
fun code_depgr thy [] = CodeFuncgr.make thy []
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    68
  | code_depgr thy consts =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    69
      let
24283
haftmann
parents: 24250
diff changeset
    70
        val gr = CodeFuncgr.make thy consts;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    71
        val select = CodeFuncgr.Constgraph.all_succs gr consts;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    72
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    73
        CodeFuncgr.Constgraph.subgraph
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    74
          (member CodeUnit.eq_const select) gr
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    75
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    76
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    77
fun code_thms thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    78
  Pretty.writeln o CodeFuncgr.pretty thy o code_depgr thy;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    79
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    80
fun code_deps thy consts =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    81
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    82
    val gr = code_depgr thy consts;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    83
    fun mk_entry (const, (_, (_, parents))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    84
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    85
        val name = CodeUnit.string_of_const thy const;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    86
        val nameparents = map (CodeUnit.string_of_const thy) parents;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    87
      in { name = name, ID = name, dir = "", unfold = true,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    88
        path = "", parents = nameparents }
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    89
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    90
    val prgr = CodeFuncgr.Constgraph.fold ((fn x => fn xs => xs @ [x]) o mk_entry) gr [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    91
  in Present.display_graph prgr end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    92
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    93
structure Program = CodeDataFun
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    94
(
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    95
  type T = CodeThingol.code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    96
  val empty = CodeThingol.empty_code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    97
  fun merge _ = CodeThingol.merge_code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    98
  fun purge _ NONE _ = CodeThingol.empty_code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    99
    | purge NONE _ _ = CodeThingol.empty_code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   100
    | purge (SOME thy) (SOME cs) code =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   101
        let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   102
          val cs_exisiting =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   103
            map_filter (CodeName.const_rev thy) (Graph.keys code);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   104
          val dels = (Graph.all_preds code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   105
              o map (CodeName.const thy)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   106
              o filter (member CodeUnit.eq_const cs_exisiting)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   107
            ) cs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   108
        in Graph.del_nodes dels code end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   109
);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   110
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   111
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   112
(* translation kernel *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   113
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   114
fun get_abstype thy (tyco, tys) = case Symtab.lookup ((fst o snd o Translation.get) thy) tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   115
 of SOME ty => SOME ((map_atyps (fn TFree (n, _) => nth tys (the (Int.fromString n)))) ty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   116
  | NONE => NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   117
24283
haftmann
parents: 24250
diff changeset
   118
val value_name = "Isabelle_Eval.EVAL.EVAL";
haftmann
parents: 24250
diff changeset
   119
haftmann
parents: 24250
diff changeset
   120
fun ensure_def thy = CodeThingol.ensure_def
haftmann
parents: 24250
diff changeset
   121
  (fn s => if s = value_name then "<term>" else CodeName.labelled_name thy s);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   122
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   123
fun ensure_def_class thy (algbr as ((_, algebra), _)) funcgr class =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   124
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   125
    val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   126
    val (v, cs) = AxClass.params_of_class thy class;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   127
    val class' = CodeName.class thy class;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   128
    val classrels' = map (curry (CodeName.classrel thy) class) superclasses;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   129
    val classops' = map (CodeName.const thy o CodeUnit.const_of_cexpr thy) cs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   130
    val defgen_class =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   131
      fold_map (ensure_def_class thy algbr funcgr) superclasses
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   132
      ##>> (fold_map (exprgen_typ thy algbr funcgr) o map snd) cs
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   133
      #>> (fn (superclasses, classoptyps) =>
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   134
        CodeThingol.Class (superclasses ~~ classrels', (unprefix "'" v, classops' ~~ classoptyps)))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   135
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   136
    ensure_def thy defgen_class ("generating class " ^ quote class) class'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   137
    #> pair class'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   138
  end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   139
and ensure_def_classrel thy algbr funcgr (subclass, superclass) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   140
  ensure_def_class thy algbr funcgr subclass
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   141
  #>> (fn _ => CodeName.classrel thy (subclass, superclass))
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   142
and ensure_def_tyco thy algbr funcgr "fun" =
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   143
      pair "fun"
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   144
  | ensure_def_tyco thy algbr funcgr tyco =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   145
      let
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   146
        val defgen_datatype =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   147
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   148
            val (vs, cos) = Code.get_datatype thy tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   149
          in
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   150
            fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   151
            ##>> fold_map (fn (c, tys) =>
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   152
              fold_map (exprgen_typ thy algbr funcgr) tys
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   153
              #>> (fn tys' =>
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   154
                ((CodeName.const thy o CodeUnit.const_of_cexpr thy)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   155
                  (c, tys ---> Type (tyco, map TFree vs)), tys'))) cos
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   156
            #>> (fn (vs, cos) => CodeThingol.Datatype (vs, cos))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   157
          end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   158
        val tyco' = CodeName.tyco thy tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   159
      in
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   160
        ensure_def thy defgen_datatype ("generating type constructor " ^ quote tyco) tyco'
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   161
        #> pair tyco'
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   162
      end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   163
and exprgen_tyvar_sort thy (algbr as ((proj_sort, _), _)) funcgr (v, sort) trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   164
  trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   165
  |> fold_map (ensure_def_class thy algbr funcgr) (proj_sort sort)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   166
  |>> (fn sort => (unprefix "'" v, sort))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   167
and exprgen_typ thy algbr funcgr (TFree vs) trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   168
      trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   169
      |> exprgen_tyvar_sort thy algbr funcgr vs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   170
      |>> (fn (v, sort) => ITyVar v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   171
  | exprgen_typ thy algbr funcgr (Type (tyco, tys)) trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   172
      case get_abstype thy (tyco, tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   173
       of SOME ty =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   174
            trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   175
            |> exprgen_typ thy algbr funcgr ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   176
        | NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   177
            trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   178
            |> ensure_def_tyco thy algbr funcgr tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   179
            ||>> fold_map (exprgen_typ thy algbr funcgr) tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   180
            |>> (fn (tyco, tys) => tyco `%% tys);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   181
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   182
exception CONSTRAIN of (string * typ) * typ;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   183
val timing = ref false;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   184
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   185
fun exprgen_dicts thy (algbr as ((proj_sort, algebra), consts)) funcgr (ty_ctxt, sort_decl) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   186
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   187
    val pp = Sign.pp thy;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   188
    datatype typarg =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   189
        Global of (class * string) * typarg list list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   190
      | Local of (class * class) list * (string * (int * sort));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   191
    fun class_relation (Global ((_, tyco), yss), _) class =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   192
          Global ((class, tyco), yss)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   193
      | class_relation (Local (classrels, v), subclass) superclass =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   194
          Local ((subclass, superclass) :: classrels, v);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   195
    fun type_constructor tyco yss class =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   196
      Global ((class, tyco), (map o map) fst yss);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   197
    fun type_variable (TFree (v, sort)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   198
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   199
        val sort' = proj_sort sort;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   200
      in map_index (fn (n, class) => (Local ([], (v, (n, sort'))), class)) sort' end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   201
    val typargs = Sorts.of_sort_derivation pp algebra
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   202
      {class_relation = class_relation, type_constructor = type_constructor,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   203
       type_variable = type_variable}
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   204
      (ty_ctxt, proj_sort sort_decl);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   205
    fun mk_dict (Global (inst, yss)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   206
          ensure_def_inst thy algbr funcgr inst
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   207
          ##>> (fold_map o fold_map) mk_dict yss
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   208
          #>> (fn (inst, dss) => DictConst (inst, dss))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   209
      | mk_dict (Local (classrels, (v, (k, sort)))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   210
          fold_map (ensure_def_classrel thy algbr funcgr) classrels
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   211
          #>> (fn classrels => DictVar (classrels, (unprefix "'" v, (k, length sort))))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   212
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   213
    fold_map mk_dict typargs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   214
  end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   215
and exprgen_dict_parms thy (algbr as (_, consts)) funcgr (c, ty_ctxt) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   216
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   217
    val c' = CodeUnit.const_of_cexpr thy (c, ty_ctxt)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   218
    val idf = CodeName.const thy c';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   219
    val ty_decl = Consts.the_declaration consts idf;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   220
    val (tys, tys_decl) = pairself (curry (Consts.typargs consts) idf) (ty_ctxt, ty_decl);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   221
    val sorts = map (snd o dest_TVar) tys_decl;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   222
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   223
    fold_map (exprgen_dicts thy algbr funcgr) (tys ~~ sorts)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   224
  end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   225
and ensure_def_inst thy (algbr as ((_, algebra), _)) funcgr (class, tyco) trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   226
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   227
    val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   228
    val (var, classops) = try (AxClass.params_of_class thy) class |> the_default ("'a", [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   229
    val vs = Name.names (Name.declare var Name.context) "'a" (Sorts.mg_domain algebra tyco [class]);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   230
    val arity_typ = Type (tyco, map TFree vs);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   231
    fun gen_superarity superclass trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   232
      trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   233
      |> ensure_def_class thy algbr funcgr superclass
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   234
      ||>> ensure_def_classrel thy algbr funcgr (class, superclass)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   235
      ||>> exprgen_dicts thy algbr funcgr (arity_typ, [superclass])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   236
      |>> (fn ((superclass, classrel), [DictConst (inst, dss)]) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   237
            (superclass, (classrel, (inst, dss))));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   238
    fun gen_classop_def (classop as (c, ty)) trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   239
      trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   240
      |> ensure_def_const thy algbr funcgr (CodeUnit.const_of_cexpr thy classop)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   241
      ||>> exprgen_term thy algbr funcgr (Const (c, map_type_tfree (K arity_typ) ty));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   242
    fun defgen_inst trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   243
      trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   244
      |> ensure_def_class thy algbr funcgr class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   245
      ||>> ensure_def_tyco thy algbr funcgr tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   246
      ||>> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   247
      ||>> fold_map gen_superarity superclasses
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   248
      ||>> fold_map gen_classop_def classops
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   249
      |>> (fn ((((class, tyco), arity), superarities), classops) =>
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   250
             CodeThingol.Classinst ((class, (tyco, arity)), (superarities, classops)));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   251
    val inst = CodeName.instance thy (class, tyco);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   252
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   253
    trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   254
    |> ensure_def thy defgen_inst ("generating instance " ^ quote class ^ " / " ^ quote tyco) inst
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   255
    |> pair inst
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   256
  end
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   257
and ensure_def_const thy (algbr as (_, consts)) funcgr (const as (c, opt_tyco)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   258
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   259
    val c' = CodeName.const thy const;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   260
    fun defgen_datatypecons trns =
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   261
      trns 
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   262
      |> ensure_def_tyco thy algbr funcgr
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   263
          ((the o Code.get_datatype_of_constr thy) const)
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   264
      |>> (fn _ => CodeThingol.Bot);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   265
    fun defgen_classop trns =
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   266
      trns 
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   267
      |> ensure_def_class thy algbr funcgr
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   268
        ((the o AxClass.class_of_param thy) c)
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   269
      |>> (fn _ => CodeThingol.Bot);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   270
    fun defgen_fun trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   271
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   272
        val const' = perhaps (Consttab.lookup ((snd o snd o Translation.get) thy)) const;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   273
        val raw_thms = CodeFuncgr.funcs funcgr const';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   274
        val ty = (Logic.unvarifyT o CodeFuncgr.typ funcgr) const';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   275
        val thms = if (null o Term.typ_tfrees) ty orelse (null o fst o strip_type) ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   276
          then raw_thms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   277
          else map (CodeUnit.expand_eta 1) raw_thms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   278
        val timeap = if !timing then Output.timeap_msg ("time for " ^ c')
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   279
          else I;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   280
        val vs = (map dest_TFree o Consts.typargs consts) (c', ty);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   281
        val dest_eqthm =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   282
          apfst (snd o strip_comb) o Logic.dest_equals o Logic.unvarify o prop_of;
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   283
        fun exprgen_eq (args, rhs) =
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   284
          fold_map (exprgen_term thy algbr funcgr) args
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   285
          ##>> exprgen_term thy algbr funcgr rhs;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   286
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   287
        trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   288
        |> timeap (fold_map (exprgen_eq o dest_eqthm) thms)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   289
        ||>> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   290
        ||>> exprgen_typ thy algbr funcgr ty
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   291
        |>> (fn ((eqs, vs), ty) => CodeThingol.Fun (eqs, (vs, ty)))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   292
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   293
    val defgen = if (is_some o Code.get_datatype_of_constr thy) const
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   294
      then defgen_datatypecons
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   295
      else if is_some opt_tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   296
        orelse (not o is_some o AxClass.class_of_param thy) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   297
      then defgen_fun
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   298
      else defgen_classop
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   299
  in
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   300
    ensure_def thy defgen ("generating constant " ^ CodeUnit.string_of_const thy const) c'
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   301
    #> pair c'
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   302
  end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   303
and exprgen_term thy algbr funcgr (Const (c, ty)) trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   304
      trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   305
      |> select_appgen thy algbr funcgr ((c, ty), [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   306
  | exprgen_term thy algbr funcgr (Free (v, ty)) trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   307
      trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   308
      |> exprgen_typ thy algbr funcgr ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   309
      |>> (fn _ => IVar v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   310
  | exprgen_term thy algbr funcgr (Abs (raw_v, ty, raw_t)) trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   311
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   312
        val (v, t) = Syntax.variant_abs (raw_v, ty, raw_t);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   313
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   314
        trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   315
        |> exprgen_typ thy algbr funcgr ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   316
        ||>> exprgen_term thy algbr funcgr t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   317
        |>> (fn (ty, t) => (v, ty) `|-> t)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   318
      end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   319
  | exprgen_term thy algbr funcgr (t as _ $ _) trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   320
      case strip_comb t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   321
       of (Const (c, ty), ts) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   322
            trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   323
            |> select_appgen thy algbr funcgr ((c, ty), ts)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   324
        | (t', ts) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   325
            trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   326
            |> exprgen_term thy algbr funcgr t'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   327
            ||>> fold_map (exprgen_term thy algbr funcgr) ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   328
            |>> (fn (t, ts) => t `$$ ts)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   329
and appgen_default thy algbr funcgr ((c, ty), ts) trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   330
  trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   331
  |> ensure_def_const thy algbr funcgr (CodeUnit.const_of_cexpr thy (c, ty))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   332
  ||>> fold_map (exprgen_typ thy algbr funcgr) ((fst o Term.strip_type) ty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   333
  ||>> exprgen_typ thy algbr funcgr ((snd o Term.strip_type) ty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   334
  ||>> exprgen_dict_parms thy algbr funcgr (c, ty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   335
  ||>> fold_map (exprgen_term thy algbr funcgr) ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   336
  |>> (fn ((((c, tys), ty), iss), ts) => IConst (c, (iss, tys)) `$$ ts)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   337
and select_appgen thy algbr funcgr ((c, ty), ts) trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   338
  case Symtab.lookup (fst (Translation.get thy)) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   339
   of SOME (i, (appgen, _)) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   340
        if length ts < i then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   341
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   342
            val k = length ts;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   343
            val tys = (curry Library.take (i - k) o curry Library.drop k o fst o strip_type) ty;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   344
            val ctxt = (fold o fold_aterms)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   345
              (fn Free (v, _) => Name.declare v | _ => I) ts Name.context;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   346
            val vs = Name.names ctxt "a" tys;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   347
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   348
            trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   349
            |> fold_map (exprgen_typ thy algbr funcgr) tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   350
            ||>> appgen thy algbr funcgr ((c, ty), ts @ map Free vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   351
            |>> (fn (tys, t) => map2 (fn (v, _) => pair v) vs tys `|--> t)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   352
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   353
        else if length ts > i then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   354
          trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   355
          |> appgen thy algbr funcgr ((c, ty), Library.take (i, ts))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   356
          ||>> fold_map (exprgen_term thy algbr funcgr) (Library.drop (i, ts))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   357
          |>> (fn (t, ts) => t `$$ ts)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   358
        else
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   359
          trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   360
          |> appgen thy algbr funcgr ((c, ty), ts)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   361
    | NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   362
        trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   363
        |> appgen_default thy algbr funcgr ((c, ty), ts);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   364
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   365
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   366
(* entrance points into translation kernel *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   367
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   368
fun ensure_def_const' thy algbr funcgr c trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   369
  ensure_def_const thy algbr funcgr c trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   370
  handle CONSTRAIN ((c, ty), ty_decl) => error (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   371
    "Constant " ^ c ^ " with most general type\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   372
    ^ CodeUnit.string_of_typ thy ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   373
    ^ "\noccurs with type\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   374
    ^ CodeUnit.string_of_typ thy ty_decl);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   375
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   376
fun perhaps_def_const thy algbr funcgr c trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   377
  case try (ensure_def_const thy algbr funcgr c) trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   378
   of SOME (c, trns) => (SOME c, trns)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   379
    | NONE => (NONE, trns);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   380
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   381
fun exprgen_term' thy algbr funcgr t trns =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   382
  exprgen_term thy algbr funcgr t trns
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   383
  handle CONSTRAIN ((c, ty), ty_decl) => error ("In term " ^ (quote o Sign.string_of_term thy) t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   384
    ^ ",\nconstant " ^ c ^ " with most general type\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   385
    ^ CodeUnit.string_of_typ thy ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   386
    ^ "\noccurs with type\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   387
    ^ CodeUnit.string_of_typ thy ty_decl);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   388
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   389
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   390
(* parametrized application generators, for instantiation in object logic *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   391
(* (axiomatic extensions of translation kernel) *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   392
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   393
fun appgen_case dest_case_expr thy algbr funcgr (app as (c_ty, ts)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   394
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   395
    val SOME ([], ((st, sty), ds)) = dest_case_expr thy (list_comb (Const c_ty, ts));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   396
    fun clause_gen (dt, bt) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   397
      exprgen_term thy algbr funcgr dt
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   398
      ##>> exprgen_term thy algbr funcgr bt;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   399
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   400
    exprgen_term thy algbr funcgr st
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   401
    ##>> exprgen_typ thy algbr funcgr sty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   402
    ##>> fold_map clause_gen ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   403
    ##>> appgen_default thy algbr funcgr app
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   404
    #>> (fn (((se, sty), ds), t0) => ICase (((se, sty), ds), t0))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   405
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   406
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   407
fun appgen_let thy algbr funcgr (app as (_, [st, ct])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   408
  exprgen_term thy algbr funcgr ct
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   409
  ##>> exprgen_term thy algbr funcgr st
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   410
  ##>> appgen_default thy algbr funcgr app
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   411
  #>> (fn (((v, ty) `|-> be, se), t0) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   412
            ICase (CodeThingol.collapse_let (((v, ty), se), be), t0)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   413
        | (_, t0) => t0);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   414
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   415
fun appgen_if thy algbr funcgr (app as (_, [tb, tt, tf])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   416
  exprgen_term thy algbr funcgr tb
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   417
  ##>> exprgen_typ thy algbr funcgr (Type ("bool", []))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   418
  ##>> exprgen_term thy algbr funcgr (Const ("True", Type ("bool", [])))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   419
  ##>> exprgen_term thy algbr funcgr tt
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   420
  ##>> exprgen_term thy algbr funcgr (Const ("False", Type ("bool", [])))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   421
  ##>> exprgen_term thy algbr funcgr tf
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   422
  ##>> appgen_default thy algbr funcgr app
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   423
  #>> (fn ((((((tb, B), T), tt), F), tf), t0) => ICase (((tb, B), [(T, tt), (F, tf)]), t0));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   424
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   425
fun add_appconst (c, appgen) thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   426
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   427
    val i = (length o fst o strip_type o Sign.the_const_type thy) c;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   428
    val _ = Program.change thy (K CodeThingol.empty_code);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   429
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   430
    (Translation.map o apfst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   431
      (Symtab.update (c, (i, (appgen, stamp ())))) thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   432
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   433
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   434
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   435
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   436
(** abstype and constsubst interface **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   437
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   438
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   439
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   440
fun add_consts thy f (c1, c2 as (c, opt_tyco)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   441
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   442
    val _ = if
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   443
        is_some (AxClass.class_of_param thy c) andalso is_none opt_tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   444
        orelse is_some (Code.get_datatype_of_constr thy c2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   445
      then error ("Not a function: " ^ CodeUnit.string_of_const thy c2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   446
      else ();
24283
haftmann
parents: 24250
diff changeset
   447
    val funcgr = CodeFuncgr.make thy [c1, c2];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   448
    val ty1 = (f o CodeFuncgr.typ funcgr) c1;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   449
    val ty2 = CodeFuncgr.typ funcgr c2;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   450
    val _ = if Sign.typ_equiv thy (ty1, ty2) then () else
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   451
      error ("Incompatiable type signatures of " ^ CodeUnit.string_of_const thy c1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   452
        ^ " and " ^ CodeUnit.string_of_const thy c2 ^ ":\n"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   453
        ^ CodeUnit.string_of_typ thy ty1 ^ "\n" ^ CodeUnit.string_of_typ thy ty2);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   454
  in Consttab.update (c1, c2) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   455
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   456
fun gen_abstyp prep_const prep_typ (raw_abstyp, raw_substtyp) raw_absconsts thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   457
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   458
    val abstyp = Type.no_tvars (prep_typ thy raw_abstyp);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   459
    val substtyp = Type.no_tvars (prep_typ thy raw_substtyp);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   460
    val absconsts = (map o pairself) (prep_const thy) raw_absconsts;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   461
    val Type (abstyco, tys) = abstyp handle BIND => error ("Bad type: " ^ Sign.string_of_typ thy abstyp);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   462
    val typarms = map (fst o dest_TFree) tys handle MATCH => error ("Bad type: " ^ Sign.string_of_typ thy abstyp);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   463
    fun mk_index v = 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   464
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   465
        val k = find_index (fn w => v = w) typarms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   466
      in if k = ~1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   467
        then error ("Free type variable: " ^ quote v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   468
        else TFree (string_of_int k, [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   469
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   470
    val typpat = map_atyps (fn TFree (v, _) => mk_index v) substtyp;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   471
    fun apply_typpat (Type (tyco, tys)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   472
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   473
            val tys' = map apply_typpat tys;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   474
          in if tyco = abstyco then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   475
            (map_atyps (fn TFree (n, _) => nth tys' (the (Int.fromString n)))) typpat
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   476
          else
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   477
            Type (tyco, tys')
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   478
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   479
      | apply_typpat ty = ty;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   480
    val _ = Program.change thy (K CodeThingol.empty_code);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   481
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   482
    thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   483
    |> (Translation.map o apsnd) (fn (abstypes, abscs) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   484
          (abstypes
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   485
          |> Symtab.update (abstyco, typpat),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   486
          abscs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   487
          |> fold (add_consts thy apply_typpat) absconsts)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   488
       )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   489
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   490
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   491
fun gen_constsubst prep_const raw_constsubsts thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   492
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   493
    val constsubsts = (map o pairself) (prep_const thy) raw_constsubsts;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   494
    val _ = Program.change thy (K CodeThingol.empty_code);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   495
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   496
    thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   497
    |> (Translation.map o apsnd o apsnd) (fold (add_consts thy I) constsubsts)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   498
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   499
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   500
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   501
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   502
val abstyp = gen_abstyp (K I) Sign.certify_typ;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   503
val abstyp_e = gen_abstyp CodeUnit.read_const Sign.read_typ;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   504
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   505
val constsubst = gen_constsubst (K I);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   506
val constsubst_e = gen_constsubst CodeUnit.read_const;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   507
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   508
end; (*local*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   509
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   510
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   511
(** code generation interfaces **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   512
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   513
(* generic generation combinators *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   514
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   515
fun generate thy funcgr gen it =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   516
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   517
    (*FIXME*)
24283
haftmann
parents: 24250
diff changeset
   518
    val _ = CodeFuncgr.intervene thy funcgr;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   519
    val cs = map_filter (Consttab.lookup ((snd o snd o Translation.get) thy))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   520
      (CodeFuncgr.all funcgr);
24283
haftmann
parents: 24250
diff changeset
   521
    val CodeFuncgr' = CodeFuncgr.make thy cs;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   522
    val naming = NameSpace.qualified_names NameSpace.default_naming;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   523
    val consttab = Consts.empty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   524
      |> fold (fn c => Consts.declare naming
24283
haftmann
parents: 24250
diff changeset
   525
           ((CodeName.const thy c, CodeFuncgr.typ CodeFuncgr' c), true))
haftmann
parents: 24250
diff changeset
   526
           (CodeFuncgr.all CodeFuncgr');
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   527
    val algbr = (Code.operational_algebra thy, consttab);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   528
  in   
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   529
    Program.change_yield thy
24283
haftmann
parents: 24250
diff changeset
   530
      (CodeThingol.start_transact (gen thy algbr CodeFuncgr' it))
haftmann
parents: 24250
diff changeset
   531
    |> fst
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   532
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   533
24283
haftmann
parents: 24250
diff changeset
   534
fun raw_eval f thy g =
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   535
  let
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   536
    val value_name = "Isabelle_Eval.EVAL.EVAL";
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   537
    fun ensure_eval thy algbr funcgr t = 
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   538
      let
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   539
        val defgen_eval =
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   540
          exprgen_term' thy algbr funcgr t
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   541
          ##>> exprgen_typ thy algbr funcgr (fastype_of t)
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   542
          #>> (fn (t, ty) => CodeThingol.Fun ([([], t)], ([], ty)));
24283
haftmann
parents: 24250
diff changeset
   543
        fun result (dep, code) =
haftmann
parents: 24250
diff changeset
   544
          let
haftmann
parents: 24250
diff changeset
   545
            val CodeThingol.Fun ([([], t)], ([], ty)) = Graph.get_node code value_name;
haftmann
parents: 24250
diff changeset
   546
            val deps = Graph.imm_succs code value_name;
haftmann
parents: 24250
diff changeset
   547
            val code' = Graph.del_nodes [value_name] code;
haftmann
parents: 24250
diff changeset
   548
            val code'' = CodeThingol.project_code false [] (SOME deps) code';
haftmann
parents: 24250
diff changeset
   549
          in ((code'', (t, ty), deps), (dep, code')) end;
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   550
      in
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   551
        ensure_def thy defgen_eval "evaluation" value_name
24283
haftmann
parents: 24250
diff changeset
   552
        #> result
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   553
      end;
24283
haftmann
parents: 24250
diff changeset
   554
    fun h funcgr ct =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   555
      let
24283
haftmann
parents: 24250
diff changeset
   556
        val (code, (t, ty), deps) = generate thy funcgr ensure_eval (Thm.term_of ct);
haftmann
parents: 24250
diff changeset
   557
      in g code (t, ty) deps ct end;
haftmann
parents: 24250
diff changeset
   558
  in f thy h end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   559
24283
haftmann
parents: 24250
diff changeset
   560
fun eval_conv thy = raw_eval CodeFuncgr.eval_conv thy;
haftmann
parents: 24250
diff changeset
   561
fun eval_term thy = raw_eval CodeFuncgr.eval_term thy;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   562
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   563
val satisfies_ref : bool option ref = ref NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   564
24283
haftmann
parents: 24250
diff changeset
   565
fun satisfies thy ct witnesses =
haftmann
parents: 24250
diff changeset
   566
  let
haftmann
parents: 24250
diff changeset
   567
    fun evl code (t, ty) deps ct =
haftmann
parents: 24250
diff changeset
   568
      let
haftmann
parents: 24250
diff changeset
   569
        val t0 = Thm.term_of ct
haftmann
parents: 24250
diff changeset
   570
        val _ = (Term.map_types o Term.map_atyps) (fn _ =>
haftmann
parents: 24250
diff changeset
   571
          error ("Term " ^ Sign.string_of_term thy t0 ^ " contains polymorphic type"))
haftmann
parents: 24250
diff changeset
   572
          t0;
haftmann
parents: 24250
diff changeset
   573
      in
haftmann
parents: 24250
diff changeset
   574
        CodeTarget.eval_term thy ("CodePackage.satisfies_ref", satisfies_ref)
haftmann
parents: 24250
diff changeset
   575
          code (t, ty) witnesses
haftmann
parents: 24250
diff changeset
   576
      end;
haftmann
parents: 24250
diff changeset
   577
  in eval_term thy evl ct end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   578
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   579
fun filter_generatable thy consts =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   580
  let
24283
haftmann
parents: 24250
diff changeset
   581
    val (consts', funcgr) = CodeFuncgr.make_consts thy consts;
haftmann
parents: 24250
diff changeset
   582
    val consts'' = generate thy funcgr (fold_map ooo perhaps_def_const) consts';
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   583
    val consts''' = map_filter (fn (const, SOME _) => SOME const | (_, NONE) => NONE)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   584
      (consts' ~~ consts'');
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   585
  in consts''' end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   586
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   587
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   588
(** toplevel interface and setup **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   589
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   590
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   591
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   592
structure P = OuterParse
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   593
and K = OuterKeyword
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   594
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   595
fun code raw_cs seris thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   596
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   597
    val (perm1, cs) = CodeUnit.read_const_exprs thy
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   598
      (filter_generatable thy) raw_cs;
24283
haftmann
parents: 24250
diff changeset
   599
    val (perm2, cs') = case generate thy (CodeFuncgr.make thy cs) (fold_map ooo ensure_def_const') cs
haftmann
parents: 24250
diff changeset
   600
     of [] => (true, NONE)
haftmann
parents: 24250
diff changeset
   601
      | cs => (false, SOME cs);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   602
    val code = Program.get thy;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   603
    val seris' = map (fn (((target, module), file), args) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   604
      CodeTarget.get_serializer thy target (perm1 orelse perm2) module file args
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   605
        CodeName.labelled_name cs') seris;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   606
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   607
    (map (fn f => f code) seris' : unit list; ())
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   608
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   609
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   610
fun code_thms_cmd thy =
24283
haftmann
parents: 24250
diff changeset
   611
  code_thms thy o snd o CodeUnit.read_const_exprs thy (fst o CodeFuncgr.make_consts thy);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   612
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   613
fun code_deps_cmd thy =
24283
haftmann
parents: 24250
diff changeset
   614
  code_deps thy o snd o CodeUnit.read_const_exprs thy (fst o CodeFuncgr.make_consts thy);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   615
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   616
val (inK, module_nameK, fileK) = ("in", "module_name", "file");
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   617
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   618
val code_exprP =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   619
  (Scan.repeat P.term
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   620
  -- Scan.repeat (P.$$$ inK |-- P.name
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   621
     -- Scan.option (P.$$$ module_nameK |-- P.name)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   622
     -- Scan.option (P.$$$ fileK |-- P.name)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   623
     -- Scan.optional (P.$$$ "(" |-- P.arguments --| P.$$$ ")") []
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   624
  ) >> (fn (raw_cs, seris) => code raw_cs seris));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   625
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   626
val _ = OuterSyntax.add_keywords [inK, module_nameK, fileK];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   627
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   628
val (codeK, code_abstypeK, code_axiomsK, code_thmsK, code_depsK) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   629
  ("code_gen", "code_abstype", "code_axioms", "code_thms", "code_deps");
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   630
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   631
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   632
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   633
val codeP =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   634
  OuterSyntax.improper_command codeK "generate executable code for constants"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   635
    K.diag (P.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   636
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   637
fun codegen_command thy cmd =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   638
  case Scan.read OuterLex.stopper (P.!!! code_exprP) ((filter OuterLex.is_proper o OuterSyntax.scan) cmd)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   639
   of SOME f => (writeln "Now generating code..."; f thy)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   640
    | NONE => error ("Bad directive " ^ quote cmd);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   641
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   642
val code_abstypeP =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   643
  OuterSyntax.command code_abstypeK "axiomatic abstypes for code generation" K.thy_decl (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   644
    (P.typ -- P.typ -- Scan.optional (P.$$$ "where" |-- Scan.repeat1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   645
        (P.term --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.term)) [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   646
    >> (Toplevel.theory o uncurry abstyp_e)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   647
  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   648
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   649
val code_axiomsP =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   650
  OuterSyntax.command code_axiomsK "axiomatic constant equalities for code generation" K.thy_decl (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   651
    Scan.repeat1 (P.term --| (P.$$$ "\\<equiv>" || P.$$$ "==") -- P.term)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   652
    >> (Toplevel.theory o constsubst_e)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   653
  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   654
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   655
val code_thmsP =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   656
  OuterSyntax.improper_command code_thmsK "print system of defining equations for code" OuterKeyword.diag
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   657
    (Scan.repeat P.term
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   658
      >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   659
        o Toplevel.keep ((fn thy => code_thms_cmd thy cs) o Toplevel.theory_of)));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   660
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   661
val code_depsP =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   662
  OuterSyntax.improper_command code_depsK "visualize dependencies of defining equations for code" OuterKeyword.diag
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   663
    (Scan.repeat P.term
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   664
      >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   665
        o Toplevel.keep ((fn thy => code_deps_cmd thy cs) o Toplevel.theory_of)));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   666
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   667
val _ = OuterSyntax.add_parsers [codeP, code_abstypeP, code_axiomsP, code_thmsP, code_depsP];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   668
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   669
end; (* local *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   670
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   671
end; (* struct *)