src/Tools/code/code_package.ML
author haftmann
Wed, 28 May 2008 12:06:49 +0200
changeset 27000 e8a40d8b7897
parent 26939 1035c89b4c02
child 27001 d21bb9f73364
permissions -rw-r--r--
new serializer interface
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
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
     5
Code generator interfaces and Isar setup.
24219
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
26011
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
    10
  val evaluate_conv: theory
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
    11
    -> (term -> term * (CodeThingol.code -> CodeThingol.typscheme * CodeThingol.iterm
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
    12
       -> string list -> thm))
24283
haftmann
parents: 24250
diff changeset
    13
    -> cterm -> thm;
26011
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
    14
  val evaluate_term: theory
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
    15
    -> (term -> term * (CodeThingol.code -> CodeThingol.typscheme * CodeThingol.iterm
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
    16
       -> string list -> 'a))
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24811
diff changeset
    17
    -> term -> 'a;
26011
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
    18
  val eval_conv: string * (unit -> thm) option ref
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
    19
    -> theory -> cterm -> string list -> thm;
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
    20
  val eval_term: string * (unit -> 'a) option ref
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
    21
    -> theory -> term -> string list -> 'a;
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
    22
  val satisfies: theory -> term -> string list -> bool;
24585
haftmann
parents: 24436
diff changeset
    23
  val satisfies_ref: (unit -> bool) option ref;
25611
c0deb7307732 isatool codegen now returns exit value
haftmann
parents: 25597
diff changeset
    24
  val codegen_shell_command: string (*theory name*) -> string (*cg expr*) -> unit;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    25
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    26
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    27
structure CodePackage : CODE_PACKAGE =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    28
struct
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) 
25597
34860182b250 moved instance parameter management from class.ML to axclass.ML
haftmann
parents: 25485
diff changeset
    40
        |> Graph.map_nodes ((apsnd o map) (AxClass.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
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
    60
(** code generation interfaces **)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
    61
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
    62
(* code data *)
24844
98c006a30218 certificates for code generator case expressions
haftmann
parents: 24835
diff changeset
    63
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    64
structure Program = CodeDataFun
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    65
(
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    66
  type T = CodeThingol.code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    67
  val empty = CodeThingol.empty_code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    68
  fun merge _ = CodeThingol.merge_code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    69
  fun purge _ NONE _ = CodeThingol.empty_code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    70
    | purge NONE _ _ = CodeThingol.empty_code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    71
    | purge (SOME thy) (SOME cs) code =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    72
        let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    73
          val cs_exisiting =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    74
            map_filter (CodeName.const_rev thy) (Graph.keys code);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    75
          val dels = (Graph.all_preds code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    76
              o map (CodeName.const thy)
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
    77
              o filter (member (op =) cs_exisiting)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    78
            ) cs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    79
        in Graph.del_nodes dels code end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    80
);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    81
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
    82
(* generic generation combinators *)
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24770
diff changeset
    83
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
    84
val ensure_const = CodeThingol.ensure_const;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    85
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
    86
fun perhaps_const thy algbr funcgr c trns =
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
    87
  case try (CodeThingol.ensure_const thy algbr funcgr c) trns
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    88
   of SOME (c, trns) => (SOME c, trns)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    89
    | NONE => (NONE, trns);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    90
25969
haftmann
parents: 25935
diff changeset
    91
fun generate thy funcgr f x =
haftmann
parents: 25935
diff changeset
    92
  Program.change_yield thy (CodeThingol.transact thy funcgr
haftmann
parents: 25935
diff changeset
    93
    (fn thy => fn funcgr => fn algbr => f thy funcgr algbr x));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    94
26113
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
    95
(* export_code functionality *)
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
    96
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
    97
fun code thy permissive cs seris =
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
    98
  let
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
    99
    val code = Program.get thy;
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26939
diff changeset
   100
    fun mk_seri_dest file = case file
e8a40d8b7897 new serializer interface
haftmann
parents: 26939
diff changeset
   101
     of NONE => CodeTarget.compile
e8a40d8b7897 new serializer interface
haftmann
parents: 26939
diff changeset
   102
      | SOME "-" => writeln o CodeTarget.string
e8a40d8b7897 new serializer interface
haftmann
parents: 26939
diff changeset
   103
      | SOME f => CodeTarget.file (Path.explode f)
e8a40d8b7897 new serializer interface
haftmann
parents: 26939
diff changeset
   104
    val _ = map (fn (((target, module), file), args) =>
e8a40d8b7897 new serializer interface
haftmann
parents: 26939
diff changeset
   105
      (mk_seri_dest file (CodeTarget.serialize thy target permissive module args code cs))) seris;
e8a40d8b7897 new serializer interface
haftmann
parents: 26939
diff changeset
   106
  in () end;
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   107
26113
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   108
(* evaluation machinery *)
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   109
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
   110
fun evaluate eval_kind thy evaluator =
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   111
  let
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
   112
    fun evaluator'' evaluator''' funcgr t =
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
   113
      let
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
   114
        val ((code, (vs_ty_t, deps)), _) =
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
   115
          generate thy funcgr CodeThingol.ensure_value t;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
   116
      in evaluator''' code vs_ty_t deps end;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
   117
    fun evaluator' t =
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
   118
      let
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
   119
        val (t', evaluator''') = evaluator t;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
   120
      in (t', evaluator'' evaluator''') end;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
   121
  in eval_kind thy evaluator' end
26011
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
   122
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
   123
fun evaluate_conv thy = evaluate CodeFuncgr.eval_conv thy;
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
   124
fun evaluate_term thy = evaluate CodeFuncgr.eval_term thy;
26011
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
   125
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
   126
fun eval_ml reff args thy code ((vs, ty), t) deps =
26011
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
   127
  CodeTarget.eval thy reff code (t, ty) args;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   128
26011
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
   129
fun eval evaluate term_of reff thy ct args =
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
   130
  let
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
   131
    val _ = if null (term_frees (term_of ct)) then () else error ("Term "
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26740
diff changeset
   132
      ^ quote (Syntax.string_of_term_global thy (term_of ct))
26740
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
   133
      ^ " to be evaluated contains free variables");
6c8cd101f875 more general evaluation combinators
haftmann
parents: 26690
diff changeset
   134
  in evaluate thy (fn t => (t, eval_ml reff args thy)) ct end;
26011
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
   135
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
   136
fun eval_conv reff = eval evaluate_conv Thm.term_of reff;
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
   137
fun eval_term reff = eval evaluate_term I reff;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   138
24585
haftmann
parents: 24436
diff changeset
   139
val satisfies_ref : (unit -> bool) option ref = ref NONE;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   140
26011
d55224947082 cleaned up evaluation interfaces
haftmann
parents: 25969
diff changeset
   141
val satisfies = eval_term ("CodePackage.satisfies_ref", satisfies_ref);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   142
26113
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   143
(* code antiquotation *)
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   144
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   145
fun code_antiq (ctxt, args) = 
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   146
  let
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   147
    val thy = Context.theory_of ctxt;
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   148
    val (ts, (ctxt', args')) = Scan.repeat1 Args.term (ctxt, args);
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   149
    val cs = map (CodeUnit.check_const thy) ts;
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   150
    val (cs', code') = generate thy (CodeFuncgr.make thy cs)
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   151
      (fold_map ooo ensure_const) cs;
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26939
diff changeset
   152
    val code'' = CodeTarget.sml_of thy code' cs' ^ " ()";
26113
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   153
  in (("codevals", code''), (ctxt', args')) end;
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   154
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   155
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   156
(* const expressions *)
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   157
26615
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   158
local
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   159
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   160
fun consts_of thy some_thyname =
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   161
  let
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   162
    val this_thy = Option.map ThyInfo.get_theory some_thyname |> the_default thy;
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   163
    val raw_cs = Symtab.fold (fn (c, (_, NONE)) => cons c | _ => I)
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   164
      ((snd o #constants o Consts.dest o #consts o Sign.rep_sg) this_thy) [];
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   165
    val cs = map (CodeUnit.subst_alias thy) raw_cs;
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   166
    fun belongs_here thyname c =
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   167
      not (exists (fn thy' => Sign.declared_const thy' c) (Theory.parents_of this_thy))
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   168
  in case some_thyname
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   169
   of NONE => cs
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   170
    | SOME thyname => filter (belongs_here thyname) cs
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   171
  end;
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   172
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   173
fun read_const_expr thy "*" = ([], consts_of thy NONE)
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   174
  | read_const_expr thy s = if String.isSuffix ".*" s
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   175
      then ([], consts_of thy (SOME (unsuffix ".*" s)))
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   176
      else ([CodeUnit.read_const thy s], []);
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   177
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   178
in
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   179
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   180
fun read_const_exprs thy select exprs =
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   181
  case (pairself flat o split_list o map (read_const_expr thy)) exprs
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   182
   of (consts, []) => (false, consts)
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   183
    | (consts, consts') => (true, consts @ select consts');
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   184
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   185
end; (*local*)
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   186
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   187
fun filter_generatable thy consts =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   188
  let
24283
haftmann
parents: 24250
diff changeset
   189
    val (consts', funcgr) = CodeFuncgr.make_consts thy consts;
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
   190
    val (consts'', _) = generate thy funcgr (fold_map ooo perhaps_const) consts';
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   191
    val consts''' = map_filter (fn (const, SOME _) => SOME const | (_, NONE) => NONE)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   192
      (consts' ~~ consts'');
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   193
  in consts''' end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   194
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   195
fun generate_const_exprs thy raw_cs =
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   196
  let
26615
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   197
    val (perm1, cs) = read_const_exprs thy
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   198
      (filter_generatable thy) raw_cs;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   199
    val (perm2, cs') = case generate thy (CodeFuncgr.make thy cs)
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
   200
      (fold_map ooo ensure_const) cs
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
   201
     of ([], _) => (true, NONE)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
   202
      | (cs, _) => (false, SOME cs);
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   203
  in (perm1 orelse perm2, cs') end;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   204
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   205
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   206
(** code properties **)
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   207
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   208
fun mk_codeprops thy all_cs sel_cs =
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   209
  let
24976
821628d16552 moved Drule.unvarify to Thm.unvarify (cf. more_thm.ML);
wenzelm
parents: 24971
diff changeset
   210
    fun select (thmref, thm) = case try (Thm.unvarify o Drule.zero_var_indexes) thm
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   211
     of NONE => NONE
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   212
      | SOME thm => let
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   213
          val t = (ObjectLogic.drop_judgment thy o Thm.prop_of) thm;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   214
          val cs = fold_aterms (fn Const (c, ty) =>
25597
34860182b250 moved instance parameter management from class.ML to axclass.ML
haftmann
parents: 25485
diff changeset
   215
            cons (AxClass.unoverload_const thy (c, ty)) | _ => I) t [];
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   216
        in if exists (member (op =) sel_cs) cs
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   217
          andalso forall (member (op =) all_cs) cs
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   218
          then SOME (thmref, thm) else NONE end;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   219
    fun mk_codeprop (thmref, thm) =
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   220
      let
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   221
        val t = ObjectLogic.drop_judgment thy (Thm.prop_of thm);
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   222
        val ty_judg = fastype_of t;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   223
        val tfrees1 = fold_aterms (fn Const (c, ty) =>
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   224
          Term.add_tfreesT ty | _ => I) t [];
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   225
        val vars = Term.add_frees t [];
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   226
        val tfrees2 = fold (Term.add_tfreesT o snd) vars [];
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   227
        val tfrees' = subtract (op =) tfrees2 tfrees1 |> map TFree;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   228
        val ty = map Term.itselfT tfrees' @ map snd vars ---> ty_judg;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   229
        val tfree_vars = map Logic.mk_type tfrees';
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26285
diff changeset
   230
        val c = Facts.string_of_ref thmref
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   231
          |> NameSpace.explode
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   232
          |> (fn [x] => [x] | (x::xs) => xs)
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   233
          |> space_implode "_"
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   234
        val propdef = (((c, ty), tfree_vars @ map Free vars), t);
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   235
      in if c = "" then NONE else SOME (thmref, propdef) end;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   236
  in
26690
e30b8d500c7d Facts.dest_static;
wenzelm
parents: 26662
diff changeset
   237
    Facts.dest_static (PureThy.facts_of thy)
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26285
diff changeset
   238
    |> maps Facts.selections
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   239
    |> map_filter select
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   240
    |> map_filter mk_codeprop
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   241
  end;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   242
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   243
fun add_codeprops all_cs sel_cs thy =
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   244
  let
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   245
    val codeprops = mk_codeprops thy all_cs sel_cs;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   246
    fun lift_name_yield f x = (Name.context, x) |> f ||> snd;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   247
    fun add (thmref, (((raw_c, ty), ts), t)) (names, thy) =
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   248
      let
26336
a0e2b706ce73 renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents: 26285
diff changeset
   249
        val _ = warning ("Adding theorem " ^ quote (Facts.string_of_ref thmref)
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   250
          ^ " as code property " ^ quote raw_c);
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   251
        val ([raw_c'], names') = Name.variants [raw_c] names;
24971
4d006b03aa4a replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents: 24918
diff changeset
   252
        val (const as Const (c, _), thy') = thy |> Sign.declare_const [] (raw_c', ty, NoSyn);
4d006b03aa4a replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents: 24918
diff changeset
   253
        val eq = Logic.mk_equals (list_comb (const, ts), t);
4d006b03aa4a replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents: 24918
diff changeset
   254
        val ([def], thy'') = thy' |> PureThy.add_defs_i false [((Thm.def_name raw_c', eq), [])];
4d006b03aa4a replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents: 24918
diff changeset
   255
      in ((c, def), (names', thy'')) end;
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   256
  in
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   257
    thy
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   258
    |> Sign.sticky_prefix "codeprop"
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   259
    |> lift_name_yield (fold_map add codeprops)
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   260
    ||> Sign.restore_naming thy
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24585
diff changeset
   261
    |-> (fn c_thms => fold (Code.add_func o snd) c_thms #> pair c_thms)
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   262
  end;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   263
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   264
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
   265
(** interfaces and Isar setup **)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   266
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   267
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   268
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   269
structure P = OuterParse
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   270
and K = OuterKeyword
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   271
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   272
fun code_cmd raw_cs seris thy =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   273
  let
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   274
    val (permissive, cs) = generate_const_exprs thy raw_cs;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   275
    val _ = code thy permissive cs seris;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   276
  in () end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   277
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   278
fun code_thms_cmd thy =
26615
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   279
  code_thms thy o snd o read_const_exprs thy (fst o CodeFuncgr.make_consts thy);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   280
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   281
fun code_deps_cmd thy =
26615
8a9d3eebd534 added read_const_exprs (from Pure/Isar/code_unit.ML);
wenzelm
parents: 26604
diff changeset
   282
  code_deps thy o snd o read_const_exprs thy (fst o CodeFuncgr.make_consts thy);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   283
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   284
fun code_props_cmd raw_cs seris thy =
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   285
  let
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   286
    val (_, all_cs) = generate_const_exprs thy ["*"];
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   287
    val (permissive, cs) = generate_const_exprs thy raw_cs;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   288
    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
   289
      (map (the o CodeName.const_rev thy) (these cs)) thy;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   290
    val prop_cs = (filter_generatable thy' o map fst) c_thms;
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
   291
    val _ = if null seris then () else (generate thy' (CodeFuncgr.make thy' prop_cs)
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
   292
      (fold_map ooo ensure_const) prop_cs; ());
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   293
    val _ = if null seris then () else code thy' permissive
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   294
      (SOME (map (CodeName.const thy') prop_cs)) seris;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   295
  in thy' end;
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   296
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   297
val (inK, module_nameK, fileK) = ("in", "module_name", "file");
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   298
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   299
fun code_exprP cmd =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   300
  (Scan.repeat P.term
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   301
  -- Scan.repeat (P.$$$ inK |-- P.name
24250
c59c09b09794 *** empty log message ***
haftmann
parents: 24219
diff changeset
   302
     -- Scan.option (P.$$$ module_nameK |-- P.name)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   303
     -- Scan.option (P.$$$ fileK |-- P.name)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   304
     -- Scan.optional (P.$$$ "(" |-- P.arguments --| P.$$$ ")") []
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   305
  ) >> (fn (raw_cs, seris) => cmd raw_cs seris));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   306
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24844
diff changeset
   307
val _ = OuterSyntax.keywords [inK, module_nameK, fileK];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   308
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   309
val (codeK, code_thmsK, code_depsK, code_propsK) =
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   310
  ("export_code", "code_thms", "code_deps", "code_props");
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   311
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   312
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   313
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24844
diff changeset
   314
val _ =
25110
7253d331e9fc export_code: proper command;
wenzelm
parents: 24976
diff changeset
   315
  OuterSyntax.command codeK "generate executable code for constants"
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   316
    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
   317
26604
3f757f8acf44 replaced Isar.toplevel by Toplevel.program;
wenzelm
parents: 26336
diff changeset
   318
fun codegen_shell_command thyname cmd = Toplevel.program (fn _ =>
25611
c0deb7307732 isatool codegen now returns exit value
haftmann
parents: 25597
diff changeset
   319
  (use_thy thyname; case Scan.read OuterLex.stopper (P.!!! (code_exprP code_cmd)) ((filter OuterLex.is_proper o OuterSyntax.scan) cmd)
c0deb7307732 isatool codegen now returns exit value
haftmann
parents: 25597
diff changeset
   320
   of SOME f => (writeln "Now generating code..."; f (theory thyname))
c0deb7307732 isatool codegen now returns exit value
haftmann
parents: 25597
diff changeset
   321
    | NONE => error ("Bad directive " ^ quote cmd)))
c0deb7307732 isatool codegen now returns exit value
haftmann
parents: 25597
diff changeset
   322
  handle TOPLEVEL_ERROR => OS.Process.exit OS.Process.failure;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   323
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24844
diff changeset
   324
val _ =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   325
  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
   326
    (Scan.repeat P.term
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   327
      >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   328
        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
   329
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24844
diff changeset
   330
val _ =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   331
  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
   332
    (Scan.repeat P.term
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   333
      >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   334
        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
   335
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24844
diff changeset
   336
val _ =
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   337
  OuterSyntax.command code_propsK "generate characteristic properties for executable constants"
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   338
    K.thy_decl (P.!!! (code_exprP code_props_cmd) >> Toplevel.theory);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   339
26113
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   340
val _ = ML_Context.value_antiq "code" code_antiq;
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26011
diff changeset
   341
24436
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   342
end; (*local*)
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   343
b694324cd2be added code_props
haftmann
parents: 24423
diff changeset
   344
end; (*struct*)