Skip to content

Commit 3a7f054

Browse files
committed
Added a complete extraction example
1 parent 345974e commit 3a7f054

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

cw_example_extraction/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
all:
2+
coqc Preloaded.v
3+
coqc Solution.v
4+
coqc -I ../src -Q ../theories CW SolutionTest.v
5+
6+
clean:
7+
rm -f *.o *.cmx *.cmi *.vo *.glob a.out *.ml *.mli

cw_example_extraction/Preloaded.v

Whitespace-only changes.

cw_example_extraction/Solution.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fixpoint factorial n :=
2+
match n with
3+
| O => 1
4+
| S n => S n * factorial n
5+
end.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Require Solution.
2+
Require Import Preloaded.
3+
Require Coq.extraction.Extraction.
4+
From CW Require Import Loader.
5+
6+
Extract Inductive nat => "int"
7+
["0" "(fun x -> x + 1)"]
8+
"(fun zero succ n -> if n = 0 then zero () else succ (n - 1))".
9+
10+
Extract Constant plus => "( + )".
11+
Extract Constant mult => "( * )".
12+
13+
CWTest "Successful Extraction Test".
14+
15+
Extraction "factorial.ml" Solution.factorial.
16+
17+
CWCompileAndRun "factorial.mli" "factorial.ml" Options Driver "
18+
open Factorial
19+
let () =
20+
assert (factorial 3 = 6);
21+
assert (factorial 0 = 1);
22+
assert (factorial 5 = 120)
23+
".
24+
25+
CWTest "Extraction Test With Errors".
26+
27+
Extract Constant mult => "*".
28+
29+
Extraction "factorial.ml" Solution.factorial.
30+
31+
CWCompileAndRun "factorial.mli" "factorial.ml" Options Driver "
32+
open Factorial
33+
let () =
34+
assert (factorial 3 = 6);
35+
assert (factorial 0 = 1);
36+
assert (factorial 5 = 120)
37+
".
38+
39+
CWEndTest.
40+

src/coq_cw.ml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,25 @@ let test_file_regex ?(fname = solution_file) match_flag regex =
137137
CErrors.user_err (str "Bad match")
138138
end
139139

140-
let run_system_command args =
140+
let run_system_command ?err_msg args =
141141
let cmd = String.concat " " args in
142142
Printf.printf "Running: %s" cmd;
143143
match Unix.system cmd with
144-
| Unix.WEXITED 0 -> passed "OK"
145-
| _ -> failed "Failed"
144+
| Unix.WEXITED 0 -> ()
145+
| _ ->
146+
let msg = match err_msg with None -> "Failed" | Some msg -> msg in
147+
failed msg;
148+
CErrors.user_err (str msg)
146149

147150
let write_file fname str =
148151
let oc = open_out fname in
149152
Printf.fprintf oc "%s" str;
150153
close_out oc
151154

155+
(** Compiles and runs the given OCaml source files *)
152156
let compile_and_run files ?(options = "") driver_code =
153157
write_file driver_file driver_code;
154-
run_system_command ([ocaml_compiler; options] @ files @ [driver_file]);
158+
run_system_command ~err_msg:"Compilation failed" ([ocaml_compiler; options] @ files @ [driver_file]);
159+
passed "OK";
155160
run_system_command ["./a.out"];
156161
passed "OK"

0 commit comments

Comments
 (0)