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