src/Tools/Code/code_runtime.ML
author haftmann
Thu Jan 26 16:25:32 2017 +0100 (2017-01-26)
changeset 64958 85b87da722ab
parent 64957 3faa9b31ff78
child 64959 9ca021bd718d
permissions -rw-r--r--
tuned
haftmann@39401
     1
(*  Title:      Tools/Code/code_runtime.ML
haftmann@28054
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@28054
     3
haftmann@34028
     4
Runtime services building on code generation into implementation language SML.
haftmann@28054
     5
*)
haftmann@28054
     6
haftmann@39401
     7
signature CODE_RUNTIME =
haftmann@28054
     8
sig
haftmann@34028
     9
  val target: string
haftmann@39912
    10
  val value: Proof.context ->
haftmann@39912
    11
    (Proof.context -> unit -> 'a) * ((unit -> 'a) -> Proof.context -> Proof.context) * string ->
haftmann@39912
    12
    string * string -> 'a
haftmann@39473
    13
  type 'a cookie = (Proof.context -> unit -> 'a) * ((unit -> 'a) -> Proof.context -> Proof.context) * string
haftmann@55757
    14
  val dynamic_value: 'a cookie -> Proof.context -> string option
haftmann@39473
    15
    -> ((term -> term) -> 'a -> 'a) -> term -> string list -> 'a option
haftmann@55757
    16
  val dynamic_value_strict: 'a cookie -> Proof.context -> string option
haftmann@39473
    17
    -> ((term -> term) -> 'a -> 'a) -> term -> string list -> 'a
haftmann@55757
    18
  val dynamic_value_exn: 'a cookie -> Proof.context -> string option
haftmann@39473
    19
    -> ((term -> term) -> 'a -> 'a) -> term -> string list -> 'a Exn.result
haftmann@56973
    20
  val static_value: 'a cookie -> { ctxt: Proof.context, target: string option,
haftmann@56973
    21
    lift_postproc: (term -> term) -> 'a -> 'a, consts: string list }
haftmann@56973
    22
    -> Proof.context -> term -> 'a option
haftmann@56973
    23
  val static_value_strict: 'a cookie -> { ctxt: Proof.context, target: string option,
haftmann@56973
    24
    lift_postproc: (term -> term) -> 'a -> 'a, consts: string list }
haftmann@56973
    25
    -> Proof.context -> term -> 'a
haftmann@56973
    26
  val static_value_exn: 'a cookie -> { ctxt: Proof.context, target: string option,
haftmann@56973
    27
    lift_postproc: (term -> term) -> 'a -> 'a, consts: string list }
haftmann@56973
    28
    -> Proof.context -> term -> 'a Exn.result
haftmann@55757
    29
  val dynamic_holds_conv: Proof.context -> conv
haftmann@56973
    30
  val static_holds_conv: { ctxt: Proof.context, consts: string list } -> Proof.context -> conv
haftmann@64943
    31
  val experimental_computation: (term -> 'a) cookie
haftmann@58643
    32
    -> { ctxt: Proof.context, lift_postproc: (term -> term) -> 'a -> 'a,
haftmann@64940
    33
           terms: term list, T: typ }
haftmann@58643
    34
    -> Proof.context -> term -> 'a (*EXPERIMENTAL!*)
haftmann@40421
    35
  val code_reflect: (string * string list option) list -> string list -> string
haftmann@40421
    36
    -> string option -> theory -> theory
haftmann@39473
    37
  datatype truth = Holds
haftmann@39473
    38
  val put_truth: (unit -> truth) -> Proof.context -> Proof.context
haftmann@57435
    39
  val trace: bool Config.T
haftmann@39816
    40
  val polyml_as_definition: (binding * typ) list -> Path.T list -> theory -> theory
haftmann@28054
    41
end;
haftmann@28054
    42
haftmann@39401
    43
structure Code_Runtime : CODE_RUNTIME =
haftmann@28054
    44
struct
haftmann@28054
    45
haftmann@55150
    46
open Basic_Code_Symbol;
haftmann@39473
    47
haftmann@64957
    48
(** ML compiler as evaluation environment **)
haftmann@33992
    49
haftmann@39473
    50
(* technical prerequisites *)
haftmann@39473
    51
haftmann@39473
    52
val this = "Code_Runtime";
haftmann@39473
    53
val s_truth = Long_Name.append this "truth";
haftmann@39473
    54
val s_Holds = Long_Name.append this "Holds";
haftmann@39473
    55
haftmann@34028
    56
val target = "Eval";
haftmann@28054
    57
haftmann@39473
    58
datatype truth = Holds;
haftmann@39422
    59
wenzelm@53171
    60
val _ = Theory.setup
haftmann@59104
    61
  (Code_Target.add_derived_target (target, [(Code_ML.target_SML, I)])
haftmann@55150
    62
  #> Code_Target.set_printings (Type_Constructor (@{type_name prop},
haftmann@52435
    63
    [(target, SOME (0, (K o K o K) (Code_Printer.str s_truth)))]))
haftmann@55150
    64
  #> Code_Target.set_printings (Constant (@{const_name Code_Generator.holds},
haftmann@52435
    65
    [(target, SOME (Code_Printer.plain_const_syntax s_Holds))]))
haftmann@39473
    66
  #> Code_Target.add_reserved target this
wenzelm@53171
    67
  #> fold (Code_Target.add_reserved target) ["oo", "ooo", "oooo", "upto", "downto", "orf", "andf"]);
haftmann@39485
    68
       (*avoid further pervasive infix names*)
haftmann@39473
    69
haftmann@57435
    70
val trace = Attrib.setup_config_bool @{binding "code_runtime_trace"} (K false);
haftmann@40150
    71
haftmann@63163
    72
fun compile_ML verbose code context =
haftmann@63163
    73
 (if Config.get_generic context trace then tracing code else ();
haftmann@63164
    74
  Code_Preproc.timed "compiling ML" Context.proof_of
haftmann@63164
    75
    (ML_Context.exec (fn () => ML_Compiler0.ML ML_Env.context
haftmann@63163
    76
    {line = 0, file = "generated code", verbose = verbose,
haftmann@63164
    77
       debug = false} code)) context);
haftmann@39912
    78
haftmann@39912
    79
fun value ctxt (get, put, put_ml) (prelude, value) =
haftmann@39912
    80
  let
wenzelm@62889
    81
    val code =
wenzelm@62889
    82
      prelude ^ "\nval _ = Context.put_generic_context (SOME (Context.map_proof (" ^
wenzelm@62889
    83
      put_ml ^ " (fn () => " ^ value ^ ")) (Context.the_generic_context ())))";
haftmann@39912
    84
    val ctxt' = ctxt
haftmann@64957
    85
      |> put (fn () => error ("Bad compilation for " ^ quote put_ml))
haftmann@63163
    86
      |> Context.proof_map (compile_ML false code);
haftmann@63164
    87
    val computator = get ctxt';
haftmann@63164
    88
  in Code_Preproc.timed_exec "running ML" computator ctxt' end;
haftmann@39912
    89
haftmann@39473
    90
haftmann@64957
    91
(* evaluation into ML language values *)
haftmann@39473
    92
haftmann@39473
    93
type 'a cookie = (Proof.context -> unit -> 'a) * ((unit -> 'a) -> Proof.context -> Proof.context) * string;
haftmann@39473
    94
haftmann@55757
    95
fun reject_vars ctxt t =
haftmann@55757
    96
  ((Sign.no_frees ctxt o Sign.no_vars ctxt o map_types (K dummyT)) t; t);
haftmann@39605
    97
haftmann@64957
    98
fun build_compilation_text ctxt some_target program consts =
haftmann@64957
    99
  Code_Target.compilation_text ctxt (the_default target some_target) program consts false
haftmann@63157
   100
  #>> (fn ml_modules => space_implode "\n\n" (map snd ml_modules));
haftmann@63157
   101
haftmann@64957
   102
fun run_compilation_text cookie ctxt comp vs_t args =
haftmann@55757
   103
  let
haftmann@63157
   104
    val (program_code, value_name) = comp vs_t;
haftmann@39473
   105
    val value_code = space_implode " "
haftmann@41347
   106
      (value_name :: "()" :: map (enclose "(" ")") args);
wenzelm@40235
   107
  in Exn.interruptible_capture (value ctxt cookie) (program_code, value_code) end;
haftmann@39473
   108
haftmann@39473
   109
fun partiality_as_none e = SOME (Exn.release e)
haftmann@39473
   110
  handle General.Match => NONE
haftmann@39473
   111
    | General.Bind => NONE
haftmann@39473
   112
    | General.Fail _ => NONE;
haftmann@39473
   113
haftmann@55757
   114
fun dynamic_value_exn cookie ctxt some_target postproc t args =
haftmann@39473
   115
  let
haftmann@55757
   116
    val _ = reject_vars ctxt t;
haftmann@57435
   117
    val _ = if Config.get ctxt trace
haftmann@55757
   118
      then tracing ("Evaluation of term " ^ quote (Syntax.string_of_term ctxt t))
haftmann@41099
   119
      else ()
haftmann@63157
   120
    fun comp program _ vs_ty_t deps =
haftmann@64957
   121
      run_compilation_text cookie ctxt (build_compilation_text ctxt some_target program deps) vs_ty_t args;
haftmann@63157
   122
  in Code_Thingol.dynamic_value ctxt (Exn.map_res o postproc) comp t end;
haftmann@39473
   123
haftmann@55757
   124
fun dynamic_value_strict cookie ctxt some_target postproc t args =
haftmann@55757
   125
  Exn.release (dynamic_value_exn cookie ctxt some_target postproc t args);
haftmann@39473
   126
haftmann@55757
   127
fun dynamic_value cookie ctxt some_target postproc t args =
haftmann@55757
   128
  partiality_as_none (dynamic_value_exn cookie ctxt some_target postproc t args);
haftmann@39473
   129
haftmann@56973
   130
fun static_value_exn cookie { ctxt, target, lift_postproc, consts } =
haftmann@55757
   131
  let
haftmann@64957
   132
    fun compilation' { program, deps } =
haftmann@63157
   133
      let
haftmann@64957
   134
        val compilation'' = run_compilation_text cookie ctxt
haftmann@64957
   135
          (build_compilation_text ctxt target program (map Constant deps));
haftmann@64957
   136
      in fn _ => fn _ => fn vs_ty_t => fn _ => compilation'' vs_ty_t [] end;
haftmann@64957
   137
    val compilation = Code_Thingol.static_value { ctxt = ctxt,
wenzelm@61077
   138
      lift_postproc = Exn.map_res o lift_postproc, consts = consts }
haftmann@64957
   139
      compilation';
haftmann@64957
   140
  in fn ctxt' => compilation ctxt' o reject_vars ctxt' end;
haftmann@39473
   141
haftmann@64928
   142
fun static_value_strict cookie x = Exn.release oo static_value_exn cookie x;
haftmann@41347
   143
haftmann@64928
   144
fun static_value cookie x = partiality_as_none oo static_value_exn cookie x;
haftmann@39473
   145
haftmann@39473
   146
haftmann@39473
   147
(* evaluation for truth or nothing *)
haftmann@39473
   148
wenzelm@39820
   149
structure Truth_Result = Proof_Data
wenzelm@39820
   150
(
wenzelm@59155
   151
  type T = unit -> truth;
wenzelm@59155
   152
  val empty: T = fn () => raise Fail "Truth_Result";
wenzelm@59155
   153
  fun init _ = empty;
haftmann@39473
   154
);
haftmann@39473
   155
val put_truth = Truth_Result.put;
haftmann@39473
   156
val truth_cookie = (Truth_Result.get, put_truth, Long_Name.append this "put_truth");
haftmann@39473
   157
haftmann@58558
   158
local
haftmann@39605
   159
haftmann@58558
   160
val reject_vars = fn ctxt => tap (reject_vars ctxt o Thm.term_of);
haftmann@41349
   161
haftmann@55757
   162
fun check_holds ctxt evaluator vs_t ct =
haftmann@39473
   163
  let
haftmann@39473
   164
    val t = Thm.term_of ct;
haftmann@39473
   165
    val _ = if fastype_of t <> propT
wenzelm@59633
   166
      then error ("Not a proposition: " ^ Syntax.string_of_term ctxt t)
haftmann@39473
   167
      else ();
wenzelm@59633
   168
    val iff = Thm.cterm_of ctxt (Term.Const (@{const_name Pure.eq}, propT --> propT --> propT));
haftmann@64957
   169
    val result = case partiality_as_none (run_compilation_text truth_cookie ctxt evaluator vs_t [])
haftmann@39473
   170
     of SOME Holds => true
haftmann@39473
   171
      | _ => false;
haftmann@39473
   172
  in
haftmann@39473
   173
    Thm.mk_binop iff ct (if result then @{cprop "PROP Code_Generator.holds"} else ct)
haftmann@39473
   174
  end;
haftmann@39473
   175
haftmann@39473
   176
val (_, raw_check_holds_oracle) = Context.>>> (Context.map_theory_result
wenzelm@43619
   177
  (Thm.add_oracle (@{binding holds_by_evaluation},
haftmann@55757
   178
  fn (ctxt, evaluator, vs_t, ct) => check_holds ctxt evaluator vs_t ct)));
haftmann@39473
   179
haftmann@56920
   180
fun check_holds_oracle ctxt evaluator vs_ty_t ct =
haftmann@56920
   181
  raw_check_holds_oracle (ctxt, evaluator, vs_ty_t, ct);
haftmann@41349
   182
haftmann@41349
   183
in
haftmann@39485
   184
haftmann@55757
   185
fun dynamic_holds_conv ctxt = Code_Thingol.dynamic_conv ctxt
haftmann@56969
   186
  (fn program => fn vs_t => fn deps =>
haftmann@64957
   187
    check_holds_oracle ctxt (build_compilation_text ctxt NONE program deps) vs_t)
haftmann@55757
   188
      o reject_vars ctxt;
haftmann@39473
   189
haftmann@56973
   190
fun static_holds_conv (ctxt_consts as { ctxt, ... }) =
haftmann@63156
   191
  Code_Thingol.static_conv_thingol ctxt_consts (fn { program, deps } => fn ctxt' => fn vs_t =>
haftmann@64957
   192
    K (check_holds_oracle ctxt' (build_compilation_text ctxt NONE program (map Constant deps)) vs_t o reject_vars ctxt'));
haftmann@41349
   193
haftmann@41349
   194
end; (*local*)
haftmann@39404
   195
haftmann@39404
   196
haftmann@64957
   197
(** generator for computations -- partial implementations of the universal morphism from Isabelle to ML terms **)
haftmann@64957
   198
haftmann@64957
   199
(* auxiliary *)
haftmann@64940
   200
haftmann@64957
   201
val generated_computationN = "Generated_Computation";
haftmann@64957
   202
haftmann@64957
   203
haftmann@64957
   204
(* possible type signatures of constants *)
haftmann@64943
   205
haftmann@64940
   206
fun typ_signatures_for T =
haftmann@64940
   207
  let
haftmann@64940
   208
    val (Ts, T') = strip_type T;
haftmann@64940
   209
  in map_range (fn n => (drop n Ts ---> T', take n Ts)) (length Ts + 1) end;
haftmann@64940
   210
haftmann@64940
   211
fun typ_signatures cTs =
haftmann@64940
   212
  let
haftmann@64940
   213
    fun add (c, T) =
haftmann@64940
   214
      fold (fn (T, Ts) => Typtab.map_default (T, []) (cons (c, Ts)))
haftmann@64940
   215
        (typ_signatures_for T);
haftmann@64940
   216
  in
haftmann@64940
   217
    Typtab.empty
haftmann@64940
   218
    |> fold add cTs
haftmann@64940
   219
    |> Typtab.lookup_list
haftmann@64940
   220
  end;
haftmann@64940
   221
haftmann@64957
   222
haftmann@64957
   223
(* name mangling *)
haftmann@64957
   224
haftmann@64957
   225
local
haftmann@64957
   226
haftmann@64957
   227
fun tycos_of (Type (tyco, Ts)) = maps tycos_of Ts @ [tyco]
haftmann@64957
   228
  | tycos_of _ = [];
haftmann@64957
   229
haftmann@64957
   230
val ml_name_of = Name.desymbolize NONE o Long_Name.base_name;
haftmann@64957
   231
haftmann@64957
   232
in
haftmann@64957
   233
haftmann@64957
   234
fun of_term_for_typ Ts =
haftmann@64957
   235
  let
haftmann@64957
   236
    val names = Ts
haftmann@64957
   237
      |> map (suffix "_of_term" o space_implode "_" o map ml_name_of o tycos_of)
haftmann@64957
   238
      |> Name.variant_list [];
haftmann@64957
   239
  in the o AList.lookup (op =) (Ts ~~ names) end;
haftmann@64957
   240
haftmann@64957
   241
fun eval_for_const ctxt cTs =
haftmann@64957
   242
  let
haftmann@64957
   243
    fun symbol_list (c, T) = c :: maps tycos_of (Sign.const_typargs (Proof_Context.theory_of ctxt) (c, T))
haftmann@64957
   244
    val names = cTs
haftmann@64957
   245
      |> map (prefix "eval_" o space_implode "_" o map ml_name_of o symbol_list)
haftmann@64957
   246
      |> Name.variant_list [];
haftmann@64957
   247
  in the o AList.lookup (op =) (cTs ~~ names) end;
haftmann@64957
   248
haftmann@64957
   249
end;
haftmann@64957
   250
haftmann@64957
   251
haftmann@64957
   252
(* checks for input terms *)
haftmann@64957
   253
haftmann@64957
   254
fun monomorphic T = fold_atyps ((K o K) false) T true;
haftmann@64957
   255
haftmann@64957
   256
fun check_typ ctxt T t =
haftmann@64957
   257
  Syntax.check_term ctxt (Type.constraint T t);
haftmann@64957
   258
haftmann@64957
   259
fun check_computation_input ctxt cTs t =
haftmann@64957
   260
  let
haftmann@64957
   261
    fun check t = check_comb (strip_comb t)
haftmann@64957
   262
    and check_comb (t as Abs _, _) =
haftmann@64957
   263
          error ("Bad term, contains abstraction: " ^ Syntax.string_of_term ctxt t)
haftmann@64957
   264
      | check_comb (t as Const (cT as (c, T)), ts) =
haftmann@64957
   265
          let
haftmann@64957
   266
            val _ = if not (member (op =) cTs cT)
haftmann@64957
   267
              then error ("Bad term, computation cannot proceed on constant " ^ Syntax.string_of_term ctxt t)
haftmann@64957
   268
              else ();
haftmann@64957
   269
            val _ = if not (monomorphic T)
haftmann@64957
   270
              then error ("Bad term, contains polymorphic constant " ^ Syntax.string_of_term ctxt t)
haftmann@64957
   271
              else ();
haftmann@64957
   272
            val _ = map check ts;
haftmann@64957
   273
          in () end;
haftmann@64957
   274
    val _ = check t;
haftmann@64957
   275
  in t end;
haftmann@64957
   276
haftmann@64957
   277
haftmann@64957
   278
(* code generation for of the universal morphism *)
haftmann@64957
   279
haftmann@64940
   280
fun print_of_term_funs { typ_sign_for, eval_for_const, of_term_for_typ } Ts =
haftmann@64940
   281
  let
haftmann@64940
   282
    val var_names = map_range (fn n => "t" ^ string_of_int (n + 1));
haftmann@64940
   283
    fun print_lhs c xs = "Const (" ^ quote c ^ ", _)"
haftmann@64940
   284
      |> fold (fn x => fn s => s ^ " $ " ^ x) xs
haftmann@64943
   285
      |> enclose "(" ")";
haftmann@64940
   286
    fun print_rhs c Ts T xs = eval_for_const (c, Ts ---> T)
haftmann@64940
   287
      |> fold2 (fn T' => fn x => fn s =>
haftmann@64943
   288
         s ^ (" (" ^ of_term_for_typ T' ^ " " ^ x ^ ")")) Ts xs
haftmann@64940
   289
    fun print_eq T (c, Ts) =
haftmann@64940
   290
      let
haftmann@64940
   291
        val xs = var_names (length Ts);
haftmann@64940
   292
      in print_lhs c xs ^ " = " ^ print_rhs c Ts T xs end;
haftmann@64940
   293
    fun print_eqs T =
haftmann@64940
   294
      let
haftmann@64940
   295
        val typ_signs = typ_sign_for T;
haftmann@64940
   296
        val name = of_term_for_typ T;
haftmann@64940
   297
      in
haftmann@64943
   298
        map (print_eq T) typ_signs
haftmann@64940
   299
        |> map (prefix (name ^ " "))
haftmann@64940
   300
        |> space_implode "\n  | "
haftmann@64940
   301
      end;
haftmann@64940
   302
  in
haftmann@64940
   303
    map print_eqs Ts
haftmann@64940
   304
    |> space_implode "\nand "
haftmann@64940
   305
    |> prefix "fun "
haftmann@64940
   306
  end;
haftmann@64940
   307
haftmann@64944
   308
fun print_computation_code ctxt compiled_value requested_Ts cTs =
haftmann@64940
   309
  let
haftmann@64945
   310
    val proper_cTs = map_filter I cTs;
haftmann@64945
   311
    val typ_sign_for = typ_signatures proper_cTs;
haftmann@64940
   312
    fun add_typ T Ts =
haftmann@64940
   313
      if member (op =) Ts T
haftmann@64940
   314
      then Ts
haftmann@64940
   315
      else Ts
haftmann@64940
   316
        |> cons T
haftmann@64940
   317
        |> fold (fold add_typ o snd) (typ_sign_for T);
haftmann@64944
   318
    val required_Ts = fold add_typ requested_Ts [];
haftmann@64944
   319
    val of_term_for_typ' = of_term_for_typ required_Ts;
haftmann@64945
   320
    val eval_for_const' = eval_for_const ctxt proper_cTs;
haftmann@64945
   321
    val eval_for_const'' = the_default "_" o Option.map eval_for_const';
haftmann@64945
   322
    val eval_tuple = enclose "(" ")" (commas (map eval_for_const' proper_cTs));
haftmann@64945
   323
    fun mk_abs s = "fn " ^ s ^ " => ";
haftmann@64945
   324
    val eval_abs = space_implode ""
haftmann@64945
   325
      (map (mk_abs o eval_for_const'') cTs);
haftmann@64940
   326
    val of_term_code = print_of_term_funs {
haftmann@64940
   327
      typ_sign_for = typ_sign_for,
haftmann@64940
   328
      eval_for_const = eval_for_const',
haftmann@64944
   329
      of_term_for_typ = of_term_for_typ' } required_Ts;
haftmann@64940
   330
  in
haftmann@64940
   331
    (cat_lines [
haftmann@64940
   332
      "structure " ^ generated_computationN ^ " =",
haftmann@64940
   333
      "struct",
haftmann@64940
   334
      "",
haftmann@64940
   335
      "val " ^ eval_tuple ^ " = " ^ compiled_value ^ " ()",
haftmann@64940
   336
      "  (" ^ eval_abs,
haftmann@64940
   337
      "    " ^ eval_tuple ^ ");",
haftmann@64940
   338
      "",
haftmann@64940
   339
      of_term_code,
haftmann@64940
   340
      "",
haftmann@64940
   341
      "end"
haftmann@64944
   342
    ], map (prefix (generated_computationN ^ ".") o of_term_for_typ') requested_Ts)
haftmann@64940
   343
  end;
haftmann@64940
   344
haftmann@64957
   345
fun mount_computation ctxt cTs T raw_computation lift_postproc =
haftmann@64957
   346
  Code_Preproc.static_value { ctxt = ctxt, lift_postproc = lift_postproc, consts = [] }
haftmann@64957
   347
    (fn _ => fn ctxt' =>
haftmann@64957
   348
      check_typ ctxt' T
haftmann@64957
   349
      #> reject_vars ctxt'
haftmann@64957
   350
      #> check_computation_input ctxt' cTs
haftmann@64957
   351
      #> raw_computation);
haftmann@64943
   352
haftmann@64940
   353
fun compile_computation cookie ctxt T program evals vs_ty_evals deps =
haftmann@64940
   354
  let
haftmann@64945
   355
    fun the_const (Const cT) = cT
haftmann@64945
   356
      | the_const t = error ("No constant after preprocessing: " ^ Syntax.string_of_term ctxt t)
haftmann@64940
   357
    val raw_cTs = case evals of
haftmann@64945
   358
        Abs (_, _, t) => (map the_const o snd o strip_comb) t
haftmann@64940
   359
      | _ => error ("Bad term after preprocessing: " ^ Syntax.string_of_term ctxt evals);
haftmann@64945
   360
    val cTs = fold_rev (fn cT => fn cTs =>
haftmann@64945
   361
      (if member (op =) cTs (SOME cT) then NONE else SOME cT) :: cTs) raw_cTs [];
haftmann@64940
   362
    fun comp' vs_ty_evals =
haftmann@64940
   363
      let
haftmann@64940
   364
        val (generated_code, compiled_value) =
haftmann@64957
   365
          build_compilation_text ctxt NONE program deps vs_ty_evals;
haftmann@64944
   366
        val (of_term_code, [of_term]) = print_computation_code ctxt compiled_value [T] cTs;
haftmann@64940
   367
      in
haftmann@64940
   368
        (generated_code ^ "\n" ^ of_term_code,
haftmann@64944
   369
          enclose "(" ")" ("fn () => " ^ of_term))
haftmann@64940
   370
      end;
haftmann@64940
   371
    val compiled_computation =
haftmann@64957
   372
      Exn.release (run_compilation_text cookie ctxt comp' vs_ty_evals []);
haftmann@64957
   373
  in (map_filter I cTs, compiled_computation) end;
haftmann@64940
   374
haftmann@64940
   375
fun experimental_computation cookie { ctxt, lift_postproc, terms = ts, T } =
haftmann@64940
   376
  let
haftmann@64944
   377
    val _ = if not (monomorphic T)
haftmann@64944
   378
      then error ("Polymorphic type: " ^ Syntax.string_of_typ ctxt T)
haftmann@64944
   379
      else ();
haftmann@64943
   380
    val cTs = (fold o fold_aterms)
haftmann@64943
   381
      (fn (t as Const (cT as (_, T))) =>
haftmann@64943
   382
        if not (monomorphic T) then error ("Polymorphic constant:" ^ Syntax.string_of_term ctxt t)
haftmann@64943
   383
        else insert (op =) cT | _ => I) ts [];
haftmann@64944
   384
    val evals = Abs ("eval", map snd cTs ---> TFree (Name.aT, []), list_comb (Bound 0, map Const cTs));
haftmann@64957
   385
    val (cTs, raw_computation) = Code_Thingol.dynamic_value ctxt
haftmann@64940
   386
      (K I) (compile_computation cookie ctxt T) evals;
haftmann@64940
   387
  in
haftmann@64957
   388
    mount_computation ctxt cTs T raw_computation lift_postproc
haftmann@64940
   389
  end;
haftmann@64940
   390
haftmann@64940
   391
haftmann@64940
   392
(** code antiquotation **)
haftmann@39404
   393
haftmann@64957
   394
fun runtime_code ctxt module_name program tycos consts =
haftmann@33992
   395
  let
haftmann@55757
   396
    val thy = Proof_Context.theory_of ctxt;
haftmann@48568
   397
    val (ml_modules, target_names) =
haftmann@55757
   398
      Code_Target.produce_code_for ctxt
haftmann@55683
   399
        target NONE module_name [] program false (map Constant consts @ map Type_Constructor tycos);
haftmann@48568
   400
    val ml_code = space_implode "\n\n" (map snd ml_modules);
haftmann@55147
   401
    val (consts', tycos') = chop (length consts) target_names;
wenzelm@42359
   402
    val consts_map = map2 (fn const =>
wenzelm@42359
   403
      fn NONE =>
wenzelm@42359
   404
          error ("Constant " ^ (quote o Code.string_of_const thy) const ^
wenzelm@42359
   405
            "\nhas a user-defined serialization")
haftmann@55147
   406
       | SOME const' => (const, const')) consts consts'
wenzelm@42359
   407
    val tycos_map = map2 (fn tyco =>
wenzelm@42359
   408
      fn NONE =>
wenzelm@55304
   409
          error ("Type " ^ quote (Proof_Context.markup_type ctxt tyco) ^
wenzelm@42359
   410
            "\nhas a user-defined serialization")
haftmann@55147
   411
        | SOME tyco' => (tyco, tyco')) tycos tycos';
haftmann@34028
   412
  in (ml_code, (tycos_map, consts_map)) end;
haftmann@28054
   413
haftmann@28054
   414
local
haftmann@28054
   415
haftmann@38930
   416
structure Code_Antiq_Data = Proof_Data
haftmann@28054
   417
(
haftmann@64955
   418
  type T = { named_consts: string list,
haftmann@64955
   419
    first_occurrence: bool,
haftmann@64955
   420
    generated_code: {
haftmann@64955
   421
      code: string,
haftmann@64958
   422
      name_for_const: string -> string
haftmann@64955
   423
    } lazy
haftmann@64955
   424
  };
haftmann@64955
   425
  val empty: T = { named_consts = [],
haftmann@64955
   426
    first_occurrence = true,
haftmann@64955
   427
    generated_code = Lazy.value {
haftmann@64955
   428
      code = "",
haftmann@64958
   429
      name_for_const = I
haftmann@64955
   430
    }
haftmann@64955
   431
  };
wenzelm@59150
   432
  fun init _ = empty;
haftmann@28054
   433
);
haftmann@28054
   434
haftmann@64955
   435
val is_first_occurrence = #first_occurrence o Code_Antiq_Data.get;
haftmann@64955
   436
haftmann@64956
   437
fun lazy_code ctxt consts = Lazy.lazy (fn () =>
haftmann@64955
   438
  let
haftmann@64956
   439
    val program = Code_Thingol.consts_program ctxt consts;
haftmann@64955
   440
    val (code, (_, consts_map)) =
haftmann@64957
   441
      runtime_code ctxt Code_Target.generatedN program [] consts
haftmann@64958
   442
  in { code = code, name_for_const = the o AList.lookup (op =) consts_map } end);
haftmann@28054
   443
haftmann@64954
   444
fun register_const const ctxt =
haftmann@28054
   445
  let
haftmann@64955
   446
    val consts = insert (op =) const ((#named_consts o Code_Antiq_Data.get) ctxt);
haftmann@64955
   447
  in
haftmann@64955
   448
    ctxt
haftmann@64955
   449
    |> Code_Antiq_Data.put { 
haftmann@64955
   450
        named_consts = consts,
haftmann@64955
   451
        first_occurrence = false,
haftmann@64956
   452
        generated_code = lazy_code ctxt consts
haftmann@64955
   453
      }
haftmann@64955
   454
  end;
haftmann@30962
   455
haftmann@64955
   456
fun print_code is_first_occ const ctxt =
haftmann@30962
   457
  let
haftmann@64958
   458
    val { code, name_for_const } = (Lazy.force o #generated_code o Code_Antiq_Data.get) ctxt;
haftmann@64958
   459
    val context_code = if is_first_occ then code else "";
haftmann@64958
   460
    val body_code = ML_Context.struct_name ctxt ^ "." ^ name_for_const const;
haftmann@64958
   461
  in (context_code, body_code) end;
haftmann@28054
   462
haftmann@28054
   463
in
haftmann@28054
   464
wenzelm@56069
   465
fun ml_code_antiq raw_const ctxt =
haftmann@28054
   466
  let
wenzelm@53169
   467
    val thy = Proof_Context.theory_of ctxt;
wenzelm@53169
   468
    val const = Code.check_const thy raw_const;
haftmann@64955
   469
    val is_first = is_first_occurrence ctxt;
wenzelm@56069
   470
  in (print_code is_first const, register_const const ctxt) end;
haftmann@30962
   471
haftmann@28054
   472
end; (*local*)
haftmann@28054
   473
haftmann@28054
   474
haftmann@58558
   475
(** reflection support **)
haftmann@36470
   476
haftmann@40421
   477
fun check_datatype thy tyco some_consts =
haftmann@36470
   478
  let
haftmann@63174
   479
    val declared_constrs = (map fst o snd o fst o Code.get_type thy) tyco;
haftmann@63174
   480
    val constrs = case some_consts
haftmann@63174
   481
     of SOME [] => []
haftmann@63174
   482
      | SOME consts =>
haftmann@40421
   483
          let
haftmann@63174
   484
            val missing_constrs = subtract (op =) consts declared_constrs;
haftmann@40421
   485
            val _ = if null missing_constrs then []
wenzelm@45430
   486
              else error ("Missing constructor(s) " ^ commas_quote missing_constrs
haftmann@40421
   487
                ^ " for datatype " ^ quote tyco);
haftmann@63174
   488
            val false_constrs = subtract (op =) declared_constrs consts;
haftmann@40421
   489
            val _ = if null false_constrs then []
wenzelm@45430
   490
              else error ("Non-constructor(s) " ^ commas_quote false_constrs
haftmann@40421
   491
                ^ " for datatype " ^ quote tyco)
haftmann@63174
   492
          in consts end
haftmann@63174
   493
      | NONE => declared_constrs;
haftmann@40421
   494
  in (tyco, constrs) end;
haftmann@36470
   495
haftmann@36470
   496
fun add_eval_tyco (tyco, tyco') thy =
haftmann@36470
   497
  let
haftmann@36470
   498
    val k = Sign.arity_number thy tyco;
haftmann@40421
   499
    fun pr pr' _ [] = tyco'
haftmann@40421
   500
      | pr pr' _ [ty] =
haftmann@36470
   501
          Code_Printer.concat [pr' Code_Printer.BR ty, tyco']
haftmann@40421
   502
      | pr pr' _ tys =
haftmann@36470
   503
          Code_Printer.concat [Code_Printer.enum "," "(" ")" (map (pr' Code_Printer.BR) tys), tyco']
haftmann@36470
   504
  in
haftmann@36470
   505
    thy
haftmann@55150
   506
    |> Code_Target.set_printings (Type_Constructor (tyco, [(target, SOME (k, pr))]))
haftmann@36470
   507
  end;
haftmann@36470
   508
haftmann@36514
   509
fun add_eval_constr (const, const') thy =
haftmann@36514
   510
  let
haftmann@36514
   511
    val k = Code.args_number thy const;
haftmann@36514
   512
    fun pr pr' fxy ts = Code_Printer.brackify fxy
haftmann@38922
   513
      (const' :: the_list (Code_Printer.tuplify pr' Code_Printer.BR (map fst ts)));
haftmann@36514
   514
  in
haftmann@36514
   515
    thy
haftmann@55150
   516
    |> Code_Target.set_printings (Constant (const,
haftmann@52435
   517
      [(target, SOME (Code_Printer.simple_const_syntax (k, pr)))]))
haftmann@36514
   518
  end;
haftmann@36514
   519
haftmann@55150
   520
fun add_eval_const (const, const') = Code_Target.set_printings (Constant
haftmann@52435
   521
  (const, [(target, SOME (Code_Printer.simple_const_syntax (0, (K o K o K) const')))]));
haftmann@36470
   522
haftmann@39912
   523
fun process_reflection (code, (tyco_map, (constr_map, const_map))) module_name NONE thy =
haftmann@38935
   524
      thy
haftmann@38935
   525
      |> Code_Target.add_reserved target module_name
haftmann@63163
   526
      |> Context.theory_map (compile_ML true code)
haftmann@38935
   527
      |> fold (add_eval_tyco o apsnd Code_Printer.str) tyco_map
haftmann@38935
   528
      |> fold (add_eval_constr o apsnd Code_Printer.str) constr_map
haftmann@38935
   529
      |> fold (add_eval_const o apsnd Code_Printer.str) const_map
haftmann@39912
   530
  | process_reflection (code, _) _ (SOME file_name) thy =
haftmann@36470
   531
      let
wenzelm@37950
   532
        val preamble =
wenzelm@46737
   533
          "(* Generated from " ^
wenzelm@56208
   534
            Path.implode (Resources.thy_path (Path.basic (Context.theory_name thy))) ^
wenzelm@46737
   535
          "; DO NOT EDIT! *)";
haftmann@39912
   536
        val _ = File.write (Path.explode file_name) (preamble ^ "\n\n" ^ code);
haftmann@36470
   537
      in
haftmann@36470
   538
        thy
haftmann@36470
   539
      end;
haftmann@36470
   540
haftmann@36470
   541
fun gen_code_reflect prep_type prep_const raw_datatypes raw_functions module_name some_file thy  =
haftmann@36470
   542
  let
haftmann@55757
   543
    val ctxt = Proof_Context.init_global thy;
haftmann@36470
   544
    val datatypes = map (fn (raw_tyco, raw_cos) =>
haftmann@55757
   545
      (prep_type ctxt raw_tyco, (Option.map o map) (prep_const thy) raw_cos)) raw_datatypes;
haftmann@40421
   546
    val (tycos, constrs) = map_split (uncurry (check_datatype thy)) datatypes
haftmann@40421
   547
      |> apsnd flat;
haftmann@36470
   548
    val functions = map (prep_const thy) raw_functions;
haftmann@58557
   549
    val consts = constrs @ functions;
haftmann@63159
   550
    val program = Code_Thingol.consts_program ctxt consts;
haftmann@64957
   551
    val result = runtime_code ctxt module_name program tycos consts
haftmann@36514
   552
      |> (apsnd o apsnd) (chop (length constrs));
haftmann@36470
   553
  in
haftmann@36470
   554
    thy
haftmann@39485
   555
    |> process_reflection result module_name some_file
haftmann@36470
   556
  end;
haftmann@36470
   557
haftmann@39422
   558
val code_reflect = gen_code_reflect Code_Target.cert_tyco (K I);
haftmann@36470
   559
val code_reflect_cmd = gen_code_reflect Code_Target.read_tyco Code.read_const;
haftmann@36470
   560
haftmann@36470
   561
haftmann@28054
   562
(** Isar setup **)
haftmann@28054
   563
wenzelm@56069
   564
val _ =
wenzelm@56205
   565
  Theory.setup (ML_Antiquotation.declaration @{binding code} Args.term (fn _ => ml_code_antiq));
haftmann@28054
   566
haftmann@36470
   567
local
haftmann@36470
   568
wenzelm@36960
   569
val parse_datatype =
haftmann@63174
   570
  Parse.name -- Scan.optional (@{keyword "="} |--
haftmann@40711
   571
    (((Parse.sym_ident || Parse.string) >> (fn "_" => NONE | _ => Scan.fail ()))
haftmann@63174
   572
    || ((Parse.term ::: (Scan.repeat (@{keyword "|"} |-- Parse.term))) >> SOME))) (SOME []);
haftmann@36470
   573
haftmann@36470
   574
in
haftmann@36470
   575
haftmann@36470
   576
val _ =
wenzelm@59936
   577
  Outer_Syntax.command @{command_keyword code_reflect}
wenzelm@46961
   578
    "enrich runtime environment with generated code"
haftmann@63174
   579
    (Parse.name -- Scan.optional (@{keyword "datatypes"} |-- Parse.!!! (parse_datatype
wenzelm@46949
   580
      ::: Scan.repeat (@{keyword "and"} |-- parse_datatype))) []
haftmann@52434
   581
    -- Scan.optional (@{keyword "functions"} |-- Parse.!!!  (Scan.repeat1 Parse.name)) []
haftmann@52434
   582
    -- Scan.option (@{keyword "file"} |-- Parse.!!! Parse.name)
wenzelm@46961
   583
    >> (fn (((module_name, raw_datatypes), raw_functions), some_file) => Toplevel.theory
wenzelm@46961
   584
      (code_reflect_cmd raw_datatypes raw_functions module_name some_file)));
haftmann@36470
   585
haftmann@36470
   586
end; (*local*)
haftmann@36470
   587
haftmann@39816
   588
haftmann@39816
   589
(** using external SML files as substitute for proper definitions -- only for polyml!  **)
haftmann@39816
   590
haftmann@39816
   591
local
haftmann@39816
   592
wenzelm@39820
   593
structure Loaded_Values = Theory_Data
wenzelm@39820
   594
(
haftmann@39816
   595
  type T = string list
haftmann@39816
   596
  val empty = []
wenzelm@41472
   597
  val extend = I
wenzelm@39820
   598
  fun merge data : T = Library.merge (op =) data
haftmann@39816
   599
);
haftmann@39816
   600
haftmann@39816
   601
fun notify_val (string, value) = 
haftmann@39816
   602
  let
wenzelm@62495
   603
    val _ = #enterVal ML_Env.name_space (string, value);
wenzelm@53171
   604
    val _ = Theory.setup (Loaded_Values.map (insert (op =) string));
haftmann@39816
   605
  in () end;
haftmann@39816
   606
haftmann@39816
   607
fun abort _ = error "Only value bindings allowed.";
haftmann@39816
   608
wenzelm@62494
   609
val notifying_context : ML_Compiler0.context =
wenzelm@62354
   610
 {name_space =
wenzelm@62495
   611
   {lookupVal    = #lookupVal ML_Env.name_space,
wenzelm@62495
   612
    lookupType   = #lookupType ML_Env.name_space,
wenzelm@62495
   613
    lookupFix    = #lookupFix ML_Env.name_space,
wenzelm@62495
   614
    lookupStruct = #lookupStruct ML_Env.name_space,
wenzelm@62495
   615
    lookupSig    = #lookupSig ML_Env.name_space,
wenzelm@62495
   616
    lookupFunct  = #lookupFunct ML_Env.name_space,
haftmann@39816
   617
    enterVal     = notify_val,
haftmann@39816
   618
    enterType    = abort,
haftmann@39816
   619
    enterFix     = abort,
haftmann@39816
   620
    enterStruct  = abort,
haftmann@39816
   621
    enterSig     = abort,
haftmann@39816
   622
    enterFunct   = abort,
wenzelm@62495
   623
    allVal       = #allVal ML_Env.name_space,
wenzelm@62495
   624
    allType      = #allType ML_Env.name_space,
wenzelm@62495
   625
    allFix       = #allFix ML_Env.name_space,
wenzelm@62495
   626
    allStruct    = #allStruct ML_Env.name_space,
wenzelm@62495
   627
    allSig       = #allSig ML_Env.name_space,
wenzelm@62495
   628
    allFunct     = #allFunct ML_Env.name_space},
wenzelm@62716
   629
  print_depth = NONE,
wenzelm@62495
   630
  here = #here ML_Env.context,
wenzelm@62495
   631
  print = #print ML_Env.context,
wenzelm@62495
   632
  error = #error ML_Env.context};
haftmann@39816
   633
haftmann@39816
   634
in
haftmann@39816
   635
haftmann@39816
   636
fun use_file filepath thy =
haftmann@39816
   637
  let
haftmann@39816
   638
    val thy' = Loaded_Values.put [] thy;
wenzelm@62889
   639
    val _ = Context.put_generic_context ((SOME o Context.Theory) thy');
wenzelm@60956
   640
    val _ =
wenzelm@62902
   641
      ML_Compiler0.ML notifying_context
wenzelm@60956
   642
        {line = 0, file = Path.implode filepath, verbose = false, debug = false}
wenzelm@60956
   643
        (File.read filepath);
wenzelm@62876
   644
    val thy'' = Context.the_global_context ();
haftmann@39912
   645
    val names = Loaded_Values.get thy'';
haftmann@40320
   646
  in (names, thy'') end;
haftmann@39816
   647
haftmann@39816
   648
end;
haftmann@39816
   649
haftmann@39816
   650
fun add_definiendum (ml_name, (b, T)) thy =
haftmann@39816
   651
  thy
haftmann@39816
   652
  |> Code_Target.add_reserved target ml_name
wenzelm@63178
   653
  |> Specification.axiomatization [(b, SOME T, NoSyn)] [] [] []
haftmann@39816
   654
  |-> (fn ([Const (const, _)], _) =>
haftmann@55150
   655
    Code_Target.set_printings (Constant (const,
haftmann@52435
   656
      [(target, SOME (Code_Printer.simple_const_syntax (0, (K o K o K o Code_Printer.str) ml_name)))]))
haftmann@64940
   657
  #> tap (fn thy => Code_Target.produce_code (Proof_Context.init_global thy) false [const] target NONE Code_Target.generatedN []));
haftmann@39816
   658
haftmann@39816
   659
fun process_file filepath (definienda, thy) =
haftmann@39816
   660
  let
haftmann@39816
   661
    val (ml_names, thy') = use_file filepath thy;
haftmann@39816
   662
    val superfluous = subtract (fn ((name1, _), name2) => name1 = name2) definienda ml_names;
haftmann@39816
   663
    val _ = if null superfluous then ()
haftmann@39816
   664
      else error ("Value binding(s) " ^ commas_quote superfluous
wenzelm@41944
   665
        ^ " found in external file " ^ Path.print filepath
haftmann@39816
   666
        ^ " not present among the given contants binding(s).");
haftmann@39816
   667
    val these_definienda = AList.make (the o AList.lookup (op =) definienda) ml_names;
haftmann@39816
   668
    val thy'' = fold add_definiendum these_definienda thy';
haftmann@39816
   669
    val definienda' = fold (AList.delete (op =)) ml_names definienda;
haftmann@39816
   670
  in (definienda', thy'') end;
haftmann@39816
   671
haftmann@39816
   672
fun polyml_as_definition bTs filepaths thy =
haftmann@39816
   673
  let
haftmann@39816
   674
    val definienda = map (fn bT => ((Binding.name_of o fst) bT, bT)) bTs;
haftmann@39816
   675
    val (remaining, thy') = fold process_file filepaths (definienda, thy);
haftmann@39816
   676
    val _ = if null remaining then ()
haftmann@39816
   677
      else error ("Constant binding(s) " ^ commas_quote (map fst remaining)
haftmann@39816
   678
        ^ " not present in external file(s).");
haftmann@39816
   679
  in thy' end;
haftmann@39816
   680
haftmann@28054
   681
end; (*struct*)