src/HOL/Library/code_test.ML
changeset 64578 7b20f9f94f4e
parent 64577 0288a566c966
child 64579 38a1d8b41189
     1.1 --- a/src/HOL/Library/code_test.ML	Sat Dec 17 12:24:13 2016 +0100
     1.2 +++ b/src/HOL/Library/code_test.ML	Sat Dec 17 13:42:25 2016 +0100
     1.3 @@ -17,7 +17,7 @@
     1.4    val start_markerN: string
     1.5    val end_markerN: string
     1.6    val test_terms: Proof.context -> term list -> string -> unit
     1.7 -  val test_targets: Proof.context -> term list -> string list -> unit list
     1.8 +  val test_targets: Proof.context -> term list -> string list -> unit
     1.9    val test_code_cmd: string list -> string list -> Toplevel.state -> unit
    1.10  
    1.11    val eval_term: string -> Proof.context -> term -> term
    1.12 @@ -69,9 +69,6 @@
    1.13    | dest_tuples t = [t]
    1.14  
    1.15  
    1.16 -fun map_option _ NONE = NONE
    1.17 -  | map_option f (SOME x) = SOME (f x)
    1.18 -
    1.19  fun last_field sep str =
    1.20    let
    1.21      val n = size sep
    1.22 @@ -191,7 +188,7 @@
    1.23      fun evaluator program _ vs_ty deps =
    1.24        Exn.interruptible_capture evaluate
    1.25          (Code_Target.computation_text ctxt target program deps true vs_ty)
    1.26 -    fun postproc f = map (apsnd (map_option (map f)))
    1.27 +    fun postproc f = map (apsnd (Option.map (map f)))
    1.28    in Exn.release (Code_Thingol.dynamic_value ctxt (Exn.map_res o postproc) evaluator t) end
    1.29  
    1.30  
    1.31 @@ -233,15 +230,14 @@
    1.32          @{typ bool} => ()
    1.33        | _ => error ("Test case not of type bool: " ^ Pretty.string_of (Syntax.pretty_term ctxt t)))
    1.34  
    1.35 -    val _ = map ensure_bool ts
    1.36 +    val _ = List.app ensure_bool ts
    1.37  
    1.38      val evals = map (fn t => filter term_of (add_eval t [])) ts
    1.39      val eval = map mk_term_of evals
    1.40  
    1.41 -    val T =
    1.42 -      HOLogic.mk_prodT (@{typ bool},
    1.43 -        Type (@{type_name option}, [@{typ unit} --> @{typ yxml_of_term}]))
    1.44 -    val t = HOLogic.mk_list T (map HOLogic.mk_prod (ts ~~ eval))
    1.45 +    val t =
    1.46 +      HOLogic.mk_list @{typ "bool \<times> (unit \<Rightarrow> yxml_of_term) option"}
    1.47 +        (map HOLogic.mk_prod (ts ~~ eval))
    1.48  
    1.49      val result = dynamic_value_strict ctxt t target
    1.50  
    1.51 @@ -249,42 +245,44 @@
    1.52        filter_out (fst o fst o fst) (result ~~ ts ~~ evals)
    1.53        handle ListPair.UnequalLengths =>
    1.54          error ("Evaluation failed!\nWrong number of test results: " ^ Int.toString (length result))
    1.55 -    val _ = case failed of [] => ()
    1.56 -      | _ => error (Pretty.string_of (pretty_failures ctxt target failed))
    1.57    in
    1.58 -    ()
    1.59 +    (case failed of [] =>
    1.60 +      ()
    1.61 +    | _ => error (Pretty.string_of (pretty_failures ctxt target failed)))
    1.62    end
    1.63  
    1.64 -fun test_targets ctxt = map o test_terms ctxt
    1.65 +fun test_targets ctxt = List.app o test_terms ctxt
    1.66  
    1.67  fun test_code_cmd raw_ts targets state =
    1.68    let
    1.69      val ctxt = Toplevel.context_of state
    1.70      val ts = Syntax.read_terms ctxt raw_ts
    1.71      val frees = fold Term.add_free_names ts []
    1.72 -    val _ = if frees = [] then () else
    1.73 -      error ("Terms contain free variables: " ^
    1.74 -      Pretty.string_of (Pretty.block (Pretty.commas (map Pretty.str frees))))
    1.75 -  in
    1.76 -    test_targets ctxt ts targets; ()
    1.77 -  end
    1.78 +    val _ =
    1.79 +      if null frees then ()
    1.80 +      else error ("Terms contain free variables: " ^
    1.81 +        Pretty.string_of (Pretty.block (Pretty.commas (map Pretty.str frees))))
    1.82 +  in test_targets ctxt ts targets end
    1.83  
    1.84  fun eval_term target ctxt t =
    1.85    let
    1.86      val frees = Term.add_free_names t []
    1.87 -    val _ = if frees = [] then () else
    1.88 -      error ("Term contains free variables: " ^
    1.89 -      Pretty.string_of (Pretty.block (Pretty.commas (map Pretty.str frees))))
    1.90 +    val _ =
    1.91 +      if null frees then ()
    1.92 +      else error ("Term contains free variables: " ^
    1.93 +        Pretty.string_of (Pretty.block (Pretty.commas (map Pretty.str frees))))
    1.94  
    1.95      val thy = Proof_Context.theory_of ctxt
    1.96  
    1.97      val T_t = fastype_of t
    1.98 -    val _ = if Sign.of_sort thy (T_t, @{sort term_of}) then () else error
    1.99 -      ("Type " ^ Pretty.string_of (Syntax.pretty_typ ctxt T_t) ^
   1.100 +    val _ =
   1.101 +      if Sign.of_sort thy (T_t, @{sort term_of}) then ()
   1.102 +      else error ("Type " ^ Pretty.string_of (Syntax.pretty_typ ctxt T_t) ^
   1.103         " of term not of sort " ^ Pretty.string_of (Syntax.pretty_sort ctxt @{sort term_of}))
   1.104  
   1.105 -    val T = HOLogic.mk_prodT (@{typ bool}, Type (@{type_name option}, [@{typ unit} --> @{typ yxml_of_term}]))
   1.106 -    val t' = HOLogic.mk_list T [HOLogic.mk_prod (@{term "False"}, mk_term_of [t])]
   1.107 +    val t' =
   1.108 +      HOLogic.mk_list @{typ "bool \<times> (unit \<Rightarrow> yxml_of_term) option"}
   1.109 +        [HOLogic.mk_prod (@{term "False"}, mk_term_of [t])]
   1.110  
   1.111      val result = dynamic_value_strict ctxt t' target
   1.112    in (case result of [(_, SOME [t])] => t | _ => error "Evaluation failed") end
   1.113 @@ -298,16 +296,16 @@
   1.114      val _ =
   1.115        if compiler <> "" then ()
   1.116        else error (Pretty.string_of (Pretty.para
   1.117 -         ("Environment variable " ^ env_var ^ " is not set. To test code generation with " ^
   1.118 -         compilerN ^ ", set this variable to your " ^ env_var_dest ^ " in the settings file.")))
   1.119 +        ("Environment variable " ^ env_var ^ " is not set. To test code generation with " ^
   1.120 +          compilerN ^ ", set this variable to your " ^ env_var_dest ^ " in the settings file.")))
   1.121  
   1.122      fun compile NONE = ()
   1.123        | compile (SOME cmd) =
   1.124            let
   1.125              val (out, ret) = Isabelle_System.bash_output cmd
   1.126            in
   1.127 -            if ret = 0 then () else error
   1.128 -              ("Compilation with " ^ compilerN ^ " failed:\n" ^ cmd ^ "\n" ^ out)
   1.129 +            if ret = 0 then ()
   1.130 +            else error ("Compilation with " ^ compilerN ^ " failed:\n" ^ cmd ^ "\n" ^ out)
   1.131            end
   1.132  
   1.133      fun run path =
   1.134 @@ -315,15 +313,16 @@
   1.135          val modules = map fst code_files
   1.136          val {files, compile_cmd, run_cmd, mk_code_file} = mk_driver ctxt path modules value_name
   1.137  
   1.138 -        val _ = map (fn (name, code) => File.write (mk_code_file name) code) code_files
   1.139 -        val _ = map (fn (name, content) => File.write name content) files
   1.140 +        val _ = List.app (fn (name, code) => File.write (mk_code_file name) code) code_files
   1.141 +        val _ = List.app (fn (name, content) => File.write name content) files
   1.142  
   1.143          val _ = compile compile_cmd
   1.144  
   1.145          val (out, res) = Isabelle_System.bash_output run_cmd
   1.146 -        val _ = if res = 0 then () else error
   1.147 -          ("Evaluation for " ^ compilerN ^ " terminated with error code " ^ Int.toString res ^
   1.148 -           "\nBash output:\n" ^ out)
   1.149 +        val _ =
   1.150 +          if res = 0 then ()
   1.151 +          else error ("Evaluation for " ^ compilerN ^ " terminated with error code " ^
   1.152 +            Int.toString res ^ "\nBash output:\n" ^ out)
   1.153        in out end
   1.154    in run end
   1.155