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