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