src/HOL/Library/code_test.ML
author wenzelm
Sat Dec 17 12:24:13 2016 +0100 (2016-12-17)
changeset 64577 0288a566c966
parent 63806 c54a53ef1873
child 64578 7b20f9f94f4e
permissions -rw-r--r--
tuned whitespace;
tuned comments;
wenzelm@59720
     1
(*  Title:      HOL/Library/code_test.ML
wenzelm@64577
     2
    Author:     Andreas Lochbihler, ETH Z├╝rich
Andreas@58039
     3
wenzelm@64577
     4
Test infrastructure for the code generator.
Andreas@58039
     5
*)
Andreas@58039
     6
wenzelm@64577
     7
signature CODE_TEST =
wenzelm@64577
     8
sig
wenzelm@64577
     9
  val add_driver:
wenzelm@64577
    10
    string * ((Proof.context -> (string * string) list * string -> Path.T -> string) * string) ->
wenzelm@64577
    11
    theory -> theory
wenzelm@64577
    12
  val get_driver: theory -> string ->
wenzelm@64577
    13
    ((Proof.context -> (string * string) list * string -> Path.T -> string) * string) option
wenzelm@64577
    14
  val overlord: bool Config.T
wenzelm@64577
    15
  val successN: string
wenzelm@64577
    16
  val failureN: string
wenzelm@64577
    17
  val start_markerN: string
wenzelm@64577
    18
  val end_markerN: string
wenzelm@64577
    19
  val test_terms: Proof.context -> term list -> string -> unit
wenzelm@64577
    20
  val test_targets: Proof.context -> term list -> string list -> unit list
wenzelm@64577
    21
  val test_code_cmd: string list -> string list -> Toplevel.state -> unit
Andreas@58039
    22
wenzelm@64577
    23
  val eval_term: string -> Proof.context -> term -> term
Andreas@58039
    24
wenzelm@64577
    25
  val gen_driver:
wenzelm@64577
    26
   (theory -> Path.T -> string list -> string ->
wenzelm@64577
    27
    {files: (Path.T * string) list,
wenzelm@64577
    28
     compile_cmd: string option,
wenzelm@64577
    29
     run_cmd: string,
wenzelm@64577
    30
     mk_code_file: string -> Path.T}) ->
wenzelm@64577
    31
   string -> string -> string -> theory -> (string * string) list * string -> Path.T -> string
Andreas@58039
    32
wenzelm@64577
    33
  val ISABELLE_POLYML: string
wenzelm@64577
    34
  val polymlN: string
wenzelm@64577
    35
  val evaluate_in_polyml: Proof.context -> (string * string) list * string -> Path.T -> string
Andreas@58039
    36
wenzelm@64577
    37
  val mltonN: string
wenzelm@64577
    38
  val ISABELLE_MLTON: string
wenzelm@64577
    39
  val evaluate_in_mlton: Proof.context -> (string * string) list * string -> Path.T -> string
Andreas@58039
    40
wenzelm@64577
    41
  val smlnjN: string
wenzelm@64577
    42
  val ISABELLE_SMLNJ: string
wenzelm@64577
    43
  val evaluate_in_smlnj: Proof.context -> (string * string) list * string -> Path.T -> string
wenzelm@64577
    44
wenzelm@64577
    45
  val ocamlN: string
wenzelm@64577
    46
  val ISABELLE_OCAMLC: string
wenzelm@64577
    47
  val evaluate_in_ocaml: Proof.context -> (string * string) list * string -> Path.T -> string
Andreas@58039
    48
wenzelm@64577
    49
  val ghcN: string
wenzelm@64577
    50
  val ISABELLE_GHC: string
wenzelm@64577
    51
  val ghc_options: string Config.T
wenzelm@64577
    52
  val evaluate_in_ghc: Proof.context -> (string * string) list * string -> Path.T -> string
Andreas@58039
    53
wenzelm@64577
    54
  val scalaN: string
wenzelm@64577
    55
  val ISABELLE_SCALA: string
wenzelm@64577
    56
  val evaluate_in_scala: Proof.context -> (string * string) list * string -> Path.T -> string
Andreas@58039
    57
end
Andreas@58039
    58
wenzelm@64577
    59
structure Code_Test: CODE_TEST =
wenzelm@64577
    60
struct
Andreas@58039
    61
Andreas@58039
    62
(* convert a list of terms into nested tuples and back *)
wenzelm@64577
    63
Andreas@58039
    64
fun mk_tuples [] = @{term "()"}
Andreas@58039
    65
  | mk_tuples [t] = t
Andreas@58039
    66
  | mk_tuples (t :: ts) = HOLogic.mk_prod (t, mk_tuples ts)
Andreas@58039
    67
Andreas@58039
    68
fun dest_tuples (Const (@{const_name Pair}, _) $ l $ r) = l :: dest_tuples r
Andreas@58039
    69
  | dest_tuples t = [t]
Andreas@58039
    70
Andreas@58039
    71
Andreas@58039
    72
fun map_option _ NONE = NONE
Andreas@58039
    73
  | map_option f (SOME x) = SOME (f x)
Andreas@58039
    74
Andreas@58039
    75
fun last_field sep str =
Andreas@58039
    76
  let
wenzelm@64577
    77
    val n = size sep
wenzelm@64577
    78
    val len = size str
Andreas@58039
    79
    fun find i =
Andreas@58039
    80
      if i < 0 then NONE
Andreas@58039
    81
      else if String.substring (str, i, n) = sep then SOME i
wenzelm@64577
    82
      else find (i - 1)
Andreas@58039
    83
  in
Andreas@58039
    84
    (case find (len - n) of
Andreas@58039
    85
      NONE => NONE
Andreas@58039
    86
    | SOME i => SOME (String.substring (str, 0, i), String.extract (str, i + n, NONE)))
wenzelm@64577
    87
  end
Andreas@58039
    88
Andreas@58039
    89
fun split_first_last start stop s =
wenzelm@64577
    90
  (case first_field start s of
wenzelm@64577
    91
    NONE => NONE
wenzelm@64577
    92
  | SOME (initial, rest) =>
wenzelm@64577
    93
      (case last_field stop rest of
wenzelm@64577
    94
        NONE => NONE
wenzelm@64577
    95
      | SOME (middle, tail) => SOME (initial, middle, tail)))
Andreas@58039
    96
wenzelm@64577
    97
wenzelm@64577
    98
(* data slot for drivers *)
Andreas@58039
    99
Andreas@58039
   100
structure Drivers = Theory_Data
Andreas@58039
   101
(
wenzelm@64577
   102
  type T =
wenzelm@64577
   103
    (string * ((Proof.context -> (string * string) list * string -> Path.T -> string) * string)) list
wenzelm@64577
   104
  val empty = []
wenzelm@64577
   105
  val extend = I
wenzelm@64577
   106
  fun merge data : T = AList.merge (op =) (K true) data
Andreas@58039
   107
)
Andreas@58039
   108
wenzelm@64577
   109
val add_driver = Drivers.map o AList.update (op =)
wenzelm@64577
   110
val get_driver = AList.lookup (op =) o Drivers.get
Andreas@58039
   111
Andreas@58039
   112
(*
Andreas@58039
   113
  Test drivers must produce output of the following format:
wenzelm@64577
   114
Andreas@58039
   115
  The start of the relevant data is marked with start_markerN,
Andreas@58039
   116
  its end with end_markerN.
Andreas@58039
   117
Andreas@58039
   118
  Between these two markers, every line corresponds to one test.
Andreas@58039
   119
  Lines of successful tests start with successN, failures start with failureN.
Andreas@58039
   120
  The failure failureN may continue with the YXML encoding of the evaluated term.
Andreas@58039
   121
  There must not be any additional whitespace in between.
Andreas@58039
   122
*)
Andreas@58039
   123
wenzelm@64577
   124
wenzelm@64577
   125
(* parsing of results *)
Andreas@58039
   126
Andreas@58039
   127
val successN = "True"
Andreas@58039
   128
val failureN = "False"
Andreas@58039
   129
val start_markerN = "*@*Isabelle/Code_Test-start*@*"
Andreas@58039
   130
val end_markerN = "*@*Isabelle/Code_Test-end*@*"
Andreas@58039
   131
Andreas@58039
   132
fun parse_line line =
Andreas@58039
   133
  if String.isPrefix successN line then (true, NONE)
wenzelm@64577
   134
  else if String.isPrefix failureN line then (false,
Andreas@58039
   135
    if size line > size failureN then
Andreas@58039
   136
      String.extract (line, size failureN, NONE)
Andreas@58039
   137
      |> YXML.parse_body
Andreas@58039
   138
      |> Term_XML.Decode.term
Andreas@58039
   139
      |> dest_tuples
Andreas@58039
   140
      |> SOME
Andreas@58039
   141
    else NONE)
Andreas@58039
   142
  else raise Fail ("Cannot parse result of evaluation:\n" ^ line)
Andreas@58039
   143
Andreas@58039
   144
fun parse_result target out =
wenzelm@64577
   145
  (case split_first_last start_markerN end_markerN out of
wenzelm@64577
   146
    NONE => error ("Evaluation failed for " ^ target ^ "!\nBash output:\n" ^ out)
wenzelm@64577
   147
  | SOME (_, middle, _) => middle |> trim_line |> split_lines |> map parse_line)
Andreas@58039
   148
wenzelm@64577
   149
wenzelm@64577
   150
(* pretty printing of test results *)
Andreas@58039
   151
Andreas@58039
   152
fun pretty_eval _ NONE _ = []
wenzelm@64577
   153
  | pretty_eval ctxt (SOME evals) ts =
wenzelm@64577
   154
      [Pretty.fbrk,
wenzelm@64577
   155
       Pretty.big_list "Evaluated terms"
wenzelm@64577
   156
         (map (fn (t, eval) => Pretty.block
wenzelm@64577
   157
           [Syntax.pretty_term ctxt t, Pretty.brk 1, Pretty.str "=", Pretty.brk 1,
wenzelm@64577
   158
            Syntax.pretty_term ctxt eval])
wenzelm@64577
   159
         (ts ~~ evals))]
Andreas@58039
   160
Andreas@58039
   161
fun pretty_failure ctxt target (((_, evals), query), eval_ts) =
wenzelm@60022
   162
  Pretty.block (Pretty.text ("Test in " ^ target ^ " failed for")
wenzelm@60022
   163
    @ [Pretty.brk 1, Pretty.quote (Syntax.pretty_term ctxt query)]
Andreas@58039
   164
    @ pretty_eval ctxt evals eval_ts)
Andreas@58039
   165
Andreas@58039
   166
fun pretty_failures ctxt target failures =
Andreas@58039
   167
  Pretty.blk (0, Pretty.fbreaks (map (pretty_failure ctxt target) failures))
Andreas@58039
   168
Andreas@58039
   169
wenzelm@64577
   170
(* driver invocation *)
wenzelm@64577
   171
wenzelm@64577
   172
val overlord = Attrib.setup_config_bool @{binding "code_test_overlord"} (K false)
Andreas@58039
   173
Andreas@58039
   174
fun with_overlord_dir name f =
Andreas@58039
   175
  let
wenzelm@64577
   176
    val path =
wenzelm@64577
   177
      Path.append (Path.explode "$ISABELLE_HOME_USER") (Path.basic (name ^ serial_string ()))
wenzelm@64577
   178
    val _ = Isabelle_System.mkdirs path
wenzelm@64577
   179
  in Exn.release (Exn.capture f path) end
Andreas@58039
   180
Andreas@58039
   181
fun dynamic_value_strict ctxt t compiler =
Andreas@58039
   182
  let
Andreas@58039
   183
    val thy = Proof_Context.theory_of ctxt
wenzelm@64577
   184
    val (driver, target) =
wenzelm@64577
   185
      (case get_driver thy compiler of
wenzelm@64577
   186
        NONE => error ("No driver for target " ^ compiler)
wenzelm@64577
   187
      | SOME f => f)
Andreas@58039
   188
    val debug = Config.get (Proof_Context.init_global thy) overlord
Andreas@58039
   189
    val with_dir = if debug then with_overlord_dir else Isabelle_System.with_tmp_dir
Andreas@58039
   190
    fun evaluate f = with_dir "Code_Test" (driver ctxt f) |> parse_result compiler
Andreas@58039
   191
    fun evaluator program _ vs_ty deps =
wenzelm@64577
   192
      Exn.interruptible_capture evaluate
wenzelm@64577
   193
        (Code_Target.computation_text ctxt target program deps true vs_ty)
Andreas@58039
   194
    fun postproc f = map (apsnd (map_option (map f)))
wenzelm@64577
   195
  in Exn.release (Code_Thingol.dynamic_value ctxt (Exn.map_res o postproc) evaluator t) end
Andreas@58039
   196
wenzelm@64577
   197
wenzelm@64577
   198
(* term preprocessing *)
Andreas@58039
   199
Andreas@58039
   200
fun add_eval (Const (@{const_name Trueprop}, _) $ t) = add_eval t
Andreas@58039
   201
  | add_eval (Const (@{const_name "HOL.eq"}, _) $ lhs $ rhs) = (fn acc =>
wenzelm@64577
   202
      acc
wenzelm@64577
   203
      |> add_eval rhs
wenzelm@64577
   204
      |> add_eval lhs
wenzelm@64577
   205
      |> cons rhs
wenzelm@64577
   206
      |> cons lhs)
Andreas@58039
   207
  | add_eval (Const (@{const_name "Not"}, _) $ t) = add_eval t
Andreas@58039
   208
  | add_eval (Const (@{const_name "Orderings.ord_class.less_eq"}, _) $ lhs $ rhs) = (fn acc =>
wenzelm@64577
   209
      lhs :: rhs :: acc)
Andreas@58039
   210
  | add_eval (Const (@{const_name "Orderings.ord_class.less"}, _) $ lhs $ rhs) = (fn acc =>
wenzelm@64577
   211
      lhs :: rhs :: acc)
Andreas@58039
   212
  | add_eval _ = I
Andreas@58039
   213
Andreas@58039
   214
fun mk_term_of [] = @{term "None :: (unit \<Rightarrow> yxml_of_term) option"}
Andreas@58039
   215
  | mk_term_of ts =
wenzelm@64577
   216
      let
wenzelm@64577
   217
        val tuple = mk_tuples ts
wenzelm@64577
   218
        val T = fastype_of tuple
wenzelm@64577
   219
      in
wenzelm@64577
   220
        @{term "Some :: (unit \<Rightarrow> yxml_of_term) \<Rightarrow> (unit \<Rightarrow> yxml_of_term) option"} $
wenzelm@64577
   221
          (absdummy @{typ unit} (@{const yxml_string_of_term} $
wenzelm@64577
   222
            (Const (@{const_name Code_Evaluation.term_of}, T --> @{typ term}) $ tuple)))
wenzelm@64577
   223
      end
Andreas@58039
   224
Andreas@58039
   225
fun test_terms ctxt ts target =
Andreas@58039
   226
  let
Andreas@58039
   227
    val thy = Proof_Context.theory_of ctxt
Andreas@58039
   228
Andreas@58039
   229
    fun term_of t = Sign.of_sort thy (fastype_of t, @{sort term_of})
Andreas@58039
   230
wenzelm@64577
   231
    fun ensure_bool t =
wenzelm@64577
   232
      (case fastype_of t of
wenzelm@64577
   233
        @{typ bool} => ()
wenzelm@64577
   234
      | _ => error ("Test case not of type bool: " ^ Pretty.string_of (Syntax.pretty_term ctxt t)))
Andreas@58039
   235
Andreas@58039
   236
    val _ = map ensure_bool ts
Andreas@58039
   237
Andreas@58039
   238
    val evals = map (fn t => filter term_of (add_eval t [])) ts
Andreas@58039
   239
    val eval = map mk_term_of evals
Andreas@58039
   240
wenzelm@64577
   241
    val T =
wenzelm@64577
   242
      HOLogic.mk_prodT (@{typ bool},
wenzelm@64577
   243
        Type (@{type_name option}, [@{typ unit} --> @{typ yxml_of_term}]))
Andreas@58039
   244
    val t = HOLogic.mk_list T (map HOLogic.mk_prod (ts ~~ eval))
Andreas@58039
   245
wenzelm@64577
   246
    val result = dynamic_value_strict ctxt t target
Andreas@58039
   247
Andreas@58039
   248
    val failed =
Andreas@58039
   249
      filter_out (fst o fst o fst) (result ~~ ts ~~ evals)
wenzelm@64577
   250
      handle ListPair.UnequalLengths =>
Andreas@58039
   251
        error ("Evaluation failed!\nWrong number of test results: " ^ Int.toString (length result))
wenzelm@64577
   252
    val _ = case failed of [] => ()
Andreas@58039
   253
      | _ => error (Pretty.string_of (pretty_failures ctxt target failed))
Andreas@58039
   254
  in
Andreas@58039
   255
    ()
Andreas@58039
   256
  end
Andreas@58039
   257
Andreas@58039
   258
fun test_targets ctxt = map o test_terms ctxt
Andreas@58039
   259
Andreas@58039
   260
fun test_code_cmd raw_ts targets state =
Andreas@58039
   261
  let
wenzelm@64577
   262
    val ctxt = Toplevel.context_of state
wenzelm@64577
   263
    val ts = Syntax.read_terms ctxt raw_ts
Andreas@58039
   264
    val frees = fold Term.add_free_names ts []
Andreas@58039
   265
    val _ = if frees = [] then () else
Andreas@58039
   266
      error ("Terms contain free variables: " ^
Andreas@58039
   267
      Pretty.string_of (Pretty.block (Pretty.commas (map Pretty.str frees))))
Andreas@58039
   268
  in
Andreas@58039
   269
    test_targets ctxt ts targets; ()
Andreas@58039
   270
  end
Andreas@58039
   271
Andreas@58348
   272
fun eval_term target ctxt t =
Andreas@58348
   273
  let
Andreas@58348
   274
    val frees = Term.add_free_names t []
Andreas@58348
   275
    val _ = if frees = [] then () else
Andreas@58348
   276
      error ("Term contains free variables: " ^
Andreas@58348
   277
      Pretty.string_of (Pretty.block (Pretty.commas (map Pretty.str frees))))
Andreas@58039
   278
Andreas@58039
   279
    val thy = Proof_Context.theory_of ctxt
Andreas@58039
   280
Andreas@58039
   281
    val T_t = fastype_of t
wenzelm@64577
   282
    val _ = if Sign.of_sort thy (T_t, @{sort term_of}) then () else error
wenzelm@64577
   283
      ("Type " ^ Pretty.string_of (Syntax.pretty_typ ctxt T_t) ^
Andreas@58039
   284
       " of term not of sort " ^ Pretty.string_of (Syntax.pretty_sort ctxt @{sort term_of}))
Andreas@58039
   285
Andreas@58039
   286
    val T = HOLogic.mk_prodT (@{typ bool}, Type (@{type_name option}, [@{typ unit} --> @{typ yxml_of_term}]))
Andreas@58039
   287
    val t' = HOLogic.mk_list T [HOLogic.mk_prod (@{term "False"}, mk_term_of [t])]
Andreas@58039
   288
wenzelm@64577
   289
    val result = dynamic_value_strict ctxt t' target
wenzelm@64577
   290
  in (case result of [(_, SOME [t])] => t | _ => error "Evaluation failed") end
Andreas@58039
   291
wenzelm@64577
   292
wenzelm@64577
   293
(* generic driver *)
Andreas@58039
   294
Andreas@58039
   295
fun gen_driver mk_driver env_var env_var_dest compilerN ctxt (code_files, value_name) =
Andreas@58039
   296
  let
Andreas@58039
   297
    val compiler = getenv env_var
wenzelm@64577
   298
    val _ =
wenzelm@64577
   299
      if compiler <> "" then ()
wenzelm@64577
   300
      else error (Pretty.string_of (Pretty.para
Andreas@58039
   301
         ("Environment variable " ^ env_var ^ " is not set. To test code generation with " ^
Andreas@58039
   302
         compilerN ^ ", set this variable to your " ^ env_var_dest ^ " in the settings file.")))
Andreas@58039
   303
Andreas@58039
   304
    fun compile NONE = ()
Andreas@58039
   305
      | compile (SOME cmd) =
wenzelm@64577
   306
          let
wenzelm@64577
   307
            val (out, ret) = Isabelle_System.bash_output cmd
wenzelm@64577
   308
          in
wenzelm@64577
   309
            if ret = 0 then () else error
wenzelm@64577
   310
              ("Compilation with " ^ compilerN ^ " failed:\n" ^ cmd ^ "\n" ^ out)
wenzelm@64577
   311
          end
Andreas@58039
   312
wenzelm@64577
   313
    fun run path =
Andreas@58039
   314
      let
Andreas@58039
   315
        val modules = map fst code_files
wenzelm@64577
   316
        val {files, compile_cmd, run_cmd, mk_code_file} = mk_driver ctxt path modules value_name
Andreas@58039
   317
Andreas@58039
   318
        val _ = map (fn (name, code) => File.write (mk_code_file name) code) code_files
Andreas@58039
   319
        val _ = map (fn (name, content) => File.write name content) files
Andreas@58039
   320
Andreas@58039
   321
        val _ = compile compile_cmd
Andreas@58039
   322
Andreas@58039
   323
        val (out, res) = Isabelle_System.bash_output run_cmd
Andreas@58039
   324
        val _ = if res = 0 then () else error
Andreas@58039
   325
          ("Evaluation for " ^ compilerN ^ " terminated with error code " ^ Int.toString res ^
Andreas@58039
   326
           "\nBash output:\n" ^ out)
wenzelm@64577
   327
      in out end
wenzelm@64577
   328
  in run end
Andreas@58039
   329
wenzelm@64577
   330
wenzelm@64577
   331
(* driver for PolyML *)
Andreas@58039
   332
wenzelm@58415
   333
val ISABELLE_POLYML = "ISABELLE_POLYML"
wenzelm@64577
   334
val polymlN = "PolyML"
Andreas@58039
   335
Andreas@58039
   336
fun mk_driver_polyml _ path _ value_name =
Andreas@58039
   337
  let
Andreas@58039
   338
    val generatedN = "generated.sml"
Andreas@58039
   339
    val driverN = "driver.sml"
Andreas@58039
   340
Andreas@58039
   341
    val code_path = Path.append path (Path.basic generatedN)
Andreas@58039
   342
    val driver_path = Path.append path (Path.basic driverN)
wenzelm@64577
   343
    val driver =
Andreas@58039
   344
      "fun main prog_name = \n" ^
Andreas@58039
   345
      "  let\n" ^
wenzelm@64577
   346
      "    fun format_term NONE = \"\"\n" ^
Andreas@58039
   347
      "      | format_term (SOME t) = t ();\n" ^
Andreas@58039
   348
      "    fun format (true, _) = \"" ^ successN ^ "\\n\"\n" ^
Andreas@58039
   349
      "      | format (false, to) = \"" ^ failureN ^ "\" ^ format_term to ^ \"\\n\";\n" ^
Andreas@58039
   350
      "    val result = " ^ value_name ^ " ();\n" ^
Andreas@58039
   351
      "    val _ = print \"" ^ start_markerN ^ "\";\n" ^
Andreas@58039
   352
      "    val _ = map (print o format) result;\n" ^
Andreas@58039
   353
      "    val _ = print \"" ^ end_markerN ^ "\";\n" ^
Andreas@58039
   354
      "  in\n" ^
Andreas@58039
   355
      "    ()\n" ^
Andreas@58039
   356
      "  end;\n"
Andreas@58039
   357
    val cmd =
wenzelm@64577
   358
      "echo \"use \\\"" ^ Path.implode code_path ^ "\\\"; use \\\"" ^
wenzelm@64577
   359
      Path.implode driver_path ^ "\\\"; main ();\" | " ^
wenzelm@58415
   360
      Path.implode (Path.variable ISABELLE_POLYML)
Andreas@58039
   361
  in
Andreas@58039
   362
    {files = [(driver_path, driver)], compile_cmd = NONE, run_cmd = cmd, mk_code_file = K code_path}
Andreas@58039
   363
  end
Andreas@58039
   364
wenzelm@58832
   365
fun evaluate_in_polyml ctxt =
wenzelm@58832
   366
  gen_driver mk_driver_polyml ISABELLE_POLYML "PolyML executable" polymlN ctxt
Andreas@58039
   367
wenzelm@64577
   368
wenzelm@64577
   369
(* driver for mlton *)
Andreas@58039
   370
Andreas@58039
   371
val mltonN = "MLton"
Andreas@58039
   372
val ISABELLE_MLTON = "ISABELLE_MLTON"
Andreas@58039
   373
Andreas@58039
   374
fun mk_driver_mlton _ path _ value_name =
Andreas@58039
   375
  let
Andreas@58039
   376
    val generatedN = "generated.sml"
Andreas@58039
   377
    val driverN = "driver.sml"
Andreas@58039
   378
    val projectN = "test"
Andreas@58039
   379
    val ml_basisN = projectN ^ ".mlb"
Andreas@58039
   380
Andreas@58039
   381
    val code_path = Path.append path (Path.basic generatedN)
Andreas@58039
   382
    val driver_path = Path.append path (Path.basic driverN)
Andreas@58039
   383
    val ml_basis_path = Path.append path (Path.basic ml_basisN)
wenzelm@64577
   384
    val driver =
wenzelm@64577
   385
      "fun format_term NONE = \"\"\n" ^
Andreas@58039
   386
      "  | format_term (SOME t) = t ();\n" ^
Andreas@58039
   387
      "fun format (true, _) = \"" ^ successN ^ "\\n\"\n" ^
Andreas@58039
   388
      "  | format (false, to) = \"" ^ failureN ^ "\" ^ format_term to ^ \"\\n\";\n" ^
Andreas@58039
   389
      "val result = " ^ value_name ^ " ();\n" ^
Andreas@58039
   390
      "val _ = print \"" ^ start_markerN ^ "\";\n" ^
Andreas@58039
   391
      "val _ = map (print o format) result;\n" ^
Andreas@58039
   392
      "val _ = print \"" ^ end_markerN ^ "\";\n"
Andreas@58039
   393
    val ml_basis =
Andreas@58039
   394
      "$(SML_LIB)/basis/basis.mlb\n" ^
Andreas@58039
   395
      generatedN ^ "\n" ^
Andreas@58039
   396
      driverN
Andreas@58039
   397
Andreas@58039
   398
    val compile_cmd =
wenzelm@62549
   399
      File.bash_path (Path.variable ISABELLE_MLTON) ^
wenzelm@62549
   400
      " -default-type intinf " ^ File.bash_path ml_basis_path
wenzelm@62549
   401
    val run_cmd = File.bash_path (Path.append path (Path.basic projectN))
Andreas@58039
   402
  in
Andreas@58039
   403
    {files = [(driver_path, driver), (ml_basis_path, ml_basis)],
Andreas@58039
   404
     compile_cmd = SOME compile_cmd, run_cmd = run_cmd, mk_code_file = K code_path}
Andreas@58039
   405
  end
Andreas@58039
   406
wenzelm@58832
   407
fun evaluate_in_mlton ctxt =
wenzelm@58832
   408
  gen_driver mk_driver_mlton ISABELLE_MLTON "MLton executable" mltonN ctxt
Andreas@58039
   409
wenzelm@64577
   410
wenzelm@64577
   411
(* driver for SML/NJ *)
Andreas@58039
   412
Andreas@58039
   413
val smlnjN = "SMLNJ"
Andreas@58039
   414
val ISABELLE_SMLNJ = "ISABELLE_SMLNJ"
Andreas@58039
   415
Andreas@58039
   416
fun mk_driver_smlnj _ path _ value_name =
Andreas@58039
   417
  let
Andreas@58039
   418
    val generatedN = "generated.sml"
Andreas@58039
   419
    val driverN = "driver.sml"
Andreas@58039
   420
Andreas@58039
   421
    val code_path = Path.append path (Path.basic generatedN)
Andreas@58039
   422
    val driver_path = Path.append path (Path.basic driverN)
wenzelm@64577
   423
    val driver =
Andreas@58039
   424
      "structure Test = struct\n" ^
Andreas@58039
   425
      "fun main prog_name =\n" ^
Andreas@58039
   426
      "  let\n" ^
wenzelm@64577
   427
      "    fun format_term NONE = \"\"\n" ^
Andreas@58039
   428
      "      | format_term (SOME t) = t ();\n" ^
Andreas@58039
   429
      "    fun format (true, _) = \"" ^ successN ^ "\\n\"\n" ^
Andreas@58039
   430
      "      | format (false, to) = \"" ^ failureN ^ "\" ^ format_term to ^ \"\\n\";\n" ^
Andreas@58039
   431
      "    val result = " ^ value_name ^ " ();\n" ^
Andreas@58039
   432
      "    val _ = print \"" ^ start_markerN ^ "\";\n" ^
Andreas@58039
   433
      "    val _ = map (print o format) result;\n" ^
Andreas@58039
   434
      "    val _ = print \"" ^ end_markerN ^ "\";\n" ^
Andreas@58039
   435
      "  in\n" ^
Andreas@58039
   436
      "    0\n" ^
Andreas@58039
   437
      "  end;\n" ^
Andreas@58039
   438
      "end;"
Andreas@58039
   439
    val cmd =
Andreas@58039
   440
      "echo \"Control.MC.matchRedundantError := false; Control.MC.matchRedundantWarn := false;" ^
Andreas@58039
   441
      "use \\\"" ^ Path.implode code_path ^ "\\\"; use \\\"" ^ Path.implode driver_path ^ "\\\";" ^
Andreas@58039
   442
      "Test.main ();\" | " ^ Path.implode (Path.variable ISABELLE_SMLNJ)
Andreas@58039
   443
  in
Andreas@58039
   444
    {files = [(driver_path, driver)], compile_cmd = NONE, run_cmd = cmd, mk_code_file = K code_path}
Andreas@58039
   445
  end
Andreas@58039
   446
wenzelm@58832
   447
fun evaluate_in_smlnj ctxt =
wenzelm@58832
   448
  gen_driver mk_driver_smlnj ISABELLE_SMLNJ "SMLNJ executable" smlnjN ctxt
Andreas@58039
   449
wenzelm@64577
   450
wenzelm@64577
   451
(* driver for OCaml *)
Andreas@58039
   452
Andreas@58039
   453
val ocamlN = "OCaml"
Andreas@58039
   454
val ISABELLE_OCAMLC = "ISABELLE_OCAMLC"
Andreas@58039
   455
Andreas@58039
   456
fun mk_driver_ocaml _ path _ value_name =
Andreas@58039
   457
  let
Andreas@58039
   458
    val generatedN = "generated.ml"
Andreas@58039
   459
    val driverN = "driver.ml"
Andreas@58039
   460
Andreas@58039
   461
    val code_path = Path.append path (Path.basic generatedN)
Andreas@58039
   462
    val driver_path = Path.append path (Path.basic driverN)
wenzelm@64577
   463
    val driver =
Andreas@58039
   464
      "let format_term = function\n" ^
wenzelm@64577
   465
      "  | None -> \"\"\n" ^
Andreas@58039
   466
      "  | Some t -> t ();;\n" ^
Andreas@58039
   467
      "let format = function\n" ^
Andreas@58039
   468
      "  | (true, _) -> \"" ^ successN ^ "\\n\"\n" ^
Andreas@58039
   469
      "  | (false, x) -> \"" ^ failureN ^ "\" ^ format_term x ^ \"\\n\";;\n" ^
Andreas@58039
   470
      "let result = " ^ ("Generated." ^ value_name) ^ " ();;\n" ^
Andreas@58039
   471
      "let main x =\n" ^
Andreas@58039
   472
      "  let _ = print_string \"" ^ start_markerN ^ "\" in\n" ^
Andreas@58039
   473
      "  let _ = List.map (fun x -> print_string (format x)) result in\n" ^
Andreas@58039
   474
      "  print_string \"" ^ end_markerN ^ "\";;\n" ^
Andreas@58039
   475
      "main ();;"
Andreas@58039
   476
Andreas@58039
   477
    val compiled_path = Path.append path (Path.basic "test")
Andreas@58039
   478
    val compile_cmd =
Andreas@58039
   479
      Path.implode (Path.variable ISABELLE_OCAMLC) ^ " -w pu -o " ^ Path.implode compiled_path ^
Andreas@58039
   480
      " -I " ^ Path.implode path ^
Andreas@58039
   481
      " nums.cma " ^ Path.implode code_path ^ " " ^ Path.implode driver_path
Andreas@58039
   482
wenzelm@62549
   483
    val run_cmd = File.bash_path compiled_path
Andreas@58039
   484
  in
Andreas@58039
   485
    {files = [(driver_path, driver)],
Andreas@58039
   486
     compile_cmd = SOME compile_cmd, run_cmd = run_cmd, mk_code_file = K code_path}
Andreas@58039
   487
  end
Andreas@58039
   488
wenzelm@58832
   489
fun evaluate_in_ocaml ctxt =
wenzelm@58832
   490
  gen_driver mk_driver_ocaml ISABELLE_OCAMLC "ocamlc executable" ocamlN ctxt
Andreas@58039
   491
wenzelm@64577
   492
wenzelm@64577
   493
(* driver for GHC *)
Andreas@58039
   494
Andreas@58039
   495
val ghcN = "GHC"
Andreas@58039
   496
val ISABELLE_GHC = "ISABELLE_GHC"
Andreas@58039
   497
Andreas@58039
   498
val ghc_options = Attrib.setup_config_string @{binding code_test_ghc} (K "")
Andreas@58039
   499
Andreas@58039
   500
fun mk_driver_ghc ctxt path modules value_name =
Andreas@58039
   501
  let
Andreas@58039
   502
    val driverN = "Main.hs"
Andreas@58039
   503
Andreas@58039
   504
    fun mk_code_file name = Path.append path (Path.basic (name ^ ".hs"))
Andreas@58039
   505
    val driver_path = Path.append path (Path.basic driverN)
wenzelm@64577
   506
    val driver =
Andreas@58039
   507
      "module Main where {\n" ^
Andreas@58039
   508
      String.concat (map (fn module => "import qualified " ^ module ^ ";\n") modules) ^
Andreas@58039
   509
      "main = do {\n" ^
Andreas@58039
   510
      "    let {\n" ^
wenzelm@64577
   511
      "      format_term Nothing = \"\";\n" ^
Andreas@58039
   512
      "      format_term (Just t) = t ();\n" ^
Andreas@58039
   513
      "      format (True, _) = \"" ^ successN ^ "\\n\";\n" ^
Andreas@58039
   514
      "      format (False, to) = \"" ^ failureN ^ "\" ++ format_term to ++ \"\\n\";\n" ^
Andreas@58039
   515
      "      result = " ^ value_name ^ " ();\n" ^
Andreas@58039
   516
      "    };\n" ^
Andreas@58039
   517
      "    Prelude.putStr \"" ^ start_markerN ^ "\";\n" ^
Andreas@58039
   518
      "    Prelude.mapM_ (putStr . format) result;\n" ^
Andreas@58039
   519
      "    Prelude.putStr \"" ^ end_markerN ^ "\";\n" ^
Andreas@58039
   520
      "  }\n" ^
Andreas@58039
   521
      "}\n"
Andreas@58039
   522
Andreas@58039
   523
    val compiled_path = Path.append path (Path.basic "test")
Andreas@58039
   524
    val compile_cmd =
Andreas@58039
   525
      Path.implode (Path.variable ISABELLE_GHC) ^ " " ^ Code_Haskell.language_params ^ " " ^
Andreas@58039
   526
      Config.get ctxt ghc_options ^ " -o " ^ Path.implode compiled_path ^ " " ^
Andreas@58039
   527
      Path.implode driver_path ^ " -i" ^ Path.implode path
Andreas@58039
   528
wenzelm@62549
   529
    val run_cmd = File.bash_path compiled_path
Andreas@58039
   530
  in
Andreas@58039
   531
    {files = [(driver_path, driver)],
Andreas@58039
   532
     compile_cmd = SOME compile_cmd, run_cmd = run_cmd, mk_code_file = mk_code_file}
Andreas@58039
   533
  end
Andreas@58039
   534
wenzelm@58832
   535
fun evaluate_in_ghc ctxt =
wenzelm@58832
   536
  gen_driver mk_driver_ghc ISABELLE_GHC "GHC executable" ghcN ctxt
Andreas@58039
   537
wenzelm@64577
   538
wenzelm@64577
   539
(* driver for Scala *)
Andreas@58039
   540
Andreas@58039
   541
val scalaN = "Scala"
Andreas@58039
   542
val ISABELLE_SCALA = "ISABELLE_SCALA"
Andreas@58039
   543
Andreas@58039
   544
fun mk_driver_scala _ path _ value_name =
Andreas@58039
   545
  let
Andreas@58039
   546
    val generatedN = "Generated_Code"
Andreas@58039
   547
    val driverN = "Driver.scala"
Andreas@58039
   548
Andreas@58039
   549
    val code_path = Path.append path (Path.basic (generatedN ^ ".scala"))
Andreas@58039
   550
    val driver_path = Path.append path (Path.basic driverN)
wenzelm@64577
   551
    val driver =
Andreas@58039
   552
      "import " ^ generatedN ^ "._\n" ^
Andreas@58039
   553
      "object Test {\n" ^
Andreas@58039
   554
      "  def format_term(x : Option[Unit => String]) : String = x match {\n" ^
Andreas@58039
   555
      "    case None => \"\"\n" ^
Andreas@58039
   556
      "    case Some(x) => x(())\n" ^
Andreas@58039
   557
      "  }\n" ^
Andreas@58039
   558
      "  def format(term : (Boolean, Option[Unit => String])) : String = term match {\n" ^
Andreas@58039
   559
      "      case (true, _) => \"True\\n\"\n" ^
Andreas@58039
   560
      "      case (false, x) => \"False\" + format_term(x) + \"\\n\"\n" ^
Andreas@58039
   561
      "  }\n" ^
Andreas@58039
   562
      "  def main(args:Array[String]) = {\n" ^
Andreas@58039
   563
      "    val result = " ^ value_name ^ "(());\n" ^
Andreas@58039
   564
      "    print(\"" ^ start_markerN ^ "\");\n" ^
Andreas@58039
   565
      "    result.map{test:(Boolean, Option[Unit => String]) => print(format(test))};\n" ^
Andreas@58039
   566
      "    print(\"" ^ end_markerN ^ "\");\n" ^
Andreas@58039
   567
      "  }\n" ^
Andreas@58039
   568
      "}\n"
Andreas@58039
   569
Andreas@58039
   570
    val compile_cmd =
Andreas@58039
   571
      Path.implode (Path.append (Path.variable ISABELLE_SCALA) (Path.basic "scalac")) ^
wenzelm@62549
   572
      " -d " ^ File.bash_path path ^ " -classpath " ^ File.bash_path path ^ " " ^
wenzelm@62549
   573
      File.bash_path code_path ^ " " ^ File.bash_path driver_path
Andreas@58039
   574
Andreas@58039
   575
    val run_cmd =
Andreas@58039
   576
      Path.implode (Path.append (Path.variable ISABELLE_SCALA) (Path.basic "scala")) ^
wenzelm@62549
   577
      " -cp " ^ File.bash_path path ^ " Test"
Andreas@58039
   578
  in
Andreas@58039
   579
    {files = [(driver_path, driver)],
Andreas@58039
   580
     compile_cmd = SOME compile_cmd, run_cmd = run_cmd, mk_code_file = K code_path}
Andreas@58039
   581
  end
Andreas@58039
   582
wenzelm@58832
   583
fun evaluate_in_scala ctxt =
wenzelm@58832
   584
  gen_driver mk_driver_scala ISABELLE_SCALA "Scala directory" scalaN ctxt
Andreas@58039
   585
Andreas@58039
   586
val test_codeP = Scan.repeat1 Parse.prop -- (@{keyword "in"} |-- Scan.repeat1 Parse.name)
Andreas@58039
   587
wenzelm@64577
   588
val _ =
wenzelm@59936
   589
  Outer_Syntax.command @{command_keyword test_code}
Andreas@58039
   590
    "compile test cases to target languages, execute them and report results"
Andreas@58039
   591
      (test_codeP >> (fn (raw_ts, targets) => Toplevel.keep (test_code_cmd raw_ts targets)))
Andreas@58039
   592
haftmann@59323
   593
val _ = Theory.setup (fold add_driver
haftmann@59323
   594
  [(polymlN, (evaluate_in_polyml, Code_ML.target_SML)),
haftmann@59323
   595
   (mltonN, (evaluate_in_mlton, Code_ML.target_SML)),
haftmann@59323
   596
   (smlnjN, (evaluate_in_smlnj, Code_ML.target_SML)),
haftmann@59323
   597
   (ocamlN, (evaluate_in_ocaml, Code_ML.target_OCaml)),
haftmann@59323
   598
   (ghcN, (evaluate_in_ghc, Code_Haskell.target)),
haftmann@59323
   599
   (scalaN, (evaluate_in_scala, Code_Scala.target))]
wenzelm@63806
   600
  #> fold (fn target => Value_Command.add_evaluator (target, eval_term target))
wenzelm@64577
   601
    [polymlN, mltonN, smlnjN, ocamlN, ghcN, scalaN])
haftmann@59323
   602
Andreas@58039
   603
end