Skip to content

Commit e095bc6

Browse files
committed
CWAssert for checking type
1 parent c5fdb2f commit e095bc6

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

cw_example/test.v

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ From CW Require Import Loader.
44

55
CWGroup "Tests for solution.solution".
66
CWTestCase "Type test".
7-
Example solution_test : 1 + 1 = 3.
8-
Proof. exact solution.solution. Qed.
7+
Fail CWAssert "Should fail" solution.solution : (1 + 1 = 2).
8+
CWAssert solution.solution : (1 + 1 = 3).
99
CWTestCase "Assumptions test".
1010
CWAssert "Testing solution" solution.solution Assumes test_axiom.
1111
CWEndGroup.
1212

13+
Definition solution_test := solution.solution.
14+
1315
CWGroup "Another test
1416
with line breaks".
1517
CWAssert "Testing solution_test

src/coq_cw.ml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ let begin_group tag name =
4242
group_stack := mk_group tag t :: !group_stack;
4343
display tag name
4444

45+
let locate r =
46+
try
47+
let gr = Smartlocate.locate_global_with_alias r in
48+
(gr, Globnames.printable_constr_of_global gr)
49+
with Not_found -> CErrors.user_err (str "Not found: " ++ Libnames.pr_qualid r)
50+
51+
let test_type ?(msg = "Type Test") r c_ty =
52+
let env = Global.env() in
53+
let sigma = Evd.from_env env in
54+
let tm = EConstr.of_constr (snd (locate r)) in
55+
let sigma, expected_ty = Constrintern.interp_constr_evars env sigma c_ty in
56+
let actual_ty = Retyping.get_type_of ~lax:true env sigma tm in
57+
match Reductionops.infer_conv env sigma actual_ty expected_ty with
58+
| Some _ -> passed msg
59+
| None ->
60+
let p_actual = Printer.pr_econstr_env env sigma actual_ty in
61+
let p_expected = Printer.pr_econstr_env env sigma expected_ty in
62+
failed (Printf.sprintf "%s\nActual type = %s\nExpected type = %s"
63+
msg (string_of_ppcmds p_actual) (string_of_ppcmds p_expected));
64+
CErrors.user_err (str "Incorrect Type: " ++ Printer.pr_econstr_env env sigma tm)
65+
4566
(* Based on the PrintAssumptions code from vernac/vernacentries.ml *)
4667
let assumptions r =
4768
try

src/coq_cw.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
val test_axioms : ?msg:string -> Libnames.qualid -> Libnames.qualid list -> unit
22

3+
val test_type : ?msg:string -> Libnames.qualid -> Constrexpr.constr_expr -> unit
4+
35
val test_file_size : ?fname:string -> int -> unit
46

57
val test_file_regex : ?fname:string -> bool -> string -> unit

src/g_coq_cw.ml4

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ DECLARE PLUGIN "coq_cw_plugin"
22

33
open Stdarg
44

5-
VERNAC COMMAND EXTEND CWAssert CLASSIFIED AS QUERY
5+
VERNAC COMMAND EXTEND CWAssertType CLASSIFIED AS QUERY
6+
| [ "CWAssert" string_opt(msg) ref(r) ":" constr(ty)] -> [
7+
Coq_cw.test_type ?msg r ty
8+
]
9+
END
10+
11+
VERNAC COMMAND EXTEND CWAssertAssumptions CLASSIFIED AS QUERY
612
| [ "CWAssert" string_opt(msg) ref(e) "Assumes" ref_list(axioms)] -> [
713
Coq_cw.test_axioms ?msg e axioms
814
]

0 commit comments

Comments
 (0)