Skip to content

Commit 752e6c1

Browse files
committed
tests: typecheck elpi-checker and elpi2html
1 parent b44ed6f commit 752e6c1

File tree

5 files changed

+51
-39
lines changed

5 files changed

+51
-39
lines changed

src/elpi-checker.elpi

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ type wrong-arity term -> typ -> list term -> err.
5555
type unknown term -> err.
5656
type assert prop -> err -> prop.
5757

58-
type error list (pair (ctype "Loc.t") string) -> prop.
59-
:name "default-typechecking-error"
58+
type error list (pair (ctype "Loc.t") string) -> bool -> prop.
6059

61-
error Msg :- iter (x\ sigma L M\ fst x L, snd x M, print L "Error:" M) Msg.
60+
:name "default-typechecking-error"
61+
error Msg tt :- iter (x\ sigma L M\ fst x L, snd x M, print L "Error:" M) Msg.
6262

6363
type warning (ctype "Loc.t") -> string -> prop.
6464
:name "default-typechecking-warning"
@@ -69,25 +69,25 @@ assert _ (type-err T Ty ETy) :- !,
6969
checking LOC,
7070
MSG is {pp T} ^ " has type " ^ {ppt Ty} ^
7171
" but is used with type " ^ {ppt ETy},
72-
error [pr LOC MSG].
72+
error [pr LOC MSG] _.
7373
assert _ (wrong-arity T Ty A) :- !,
7474
checking LOC,
7575
MSG is {pp T} ^ " has type " ^ {ppt Ty} ^
7676
" but is applied to " ^ {pp-list A},
77-
error [pr LOC MSG].
77+
error [pr LOC MSG] _.
7878

7979
stash-new E S :- open_safe E L, ( mem L S ; stash_in_safe E S ), !.
8080

81-
report-all-failures-if-no-success P :-
81+
report-all-failures-if-no-success P RC :-
8282
new_safe E,
83-
(((pi ML\ error ML :- !, iter (stash-new E) ML, fail) => P)
83+
(((pi ML\ error ML _ :- !, iter (stash-new E) ML, fail) => P)
8484
;
85-
(error {open_safe E})).
86-
report-all-failures-and-fail-if-no-success P :-
85+
(error {open_safe E} RC)).
86+
report-all-failures-and-fail-if-no-success P RC :-
8787
new_safe E,
88-
(((pi ML\ error ML :- !, iter (stash-new E) ML, fail) => P)
88+
(((pi ML\ error ML _ :- !, iter (stash-new E) ML, fail) => P)
8989
;
90-
(error {open_safe E}, fail)).
90+
(error {open_safe E} RC, fail)).
9191

9292
mode (pp i o).
9393
type pp term -> string -> prop.
@@ -135,19 +135,20 @@ unif Y (uvar as X) :- X = Y.
135135
mode (of i o).
136136

137137
of (cdata CData) Ty :-
138-
is_cdata CData CTy, !, assert (unif Ty CTy) (type-err (cdata CData) CTy Ty).
138+
is_cdata CData (ctype CTy), !,
139+
assert (unif Ty (ctype CTy)) (type-err (cdata CData) (ctype CTy) Ty).
139140

140141
of (app [HD|ARGS]) TY :- !,
141142
report-all-failures-if-no-success % HD may have multiple types
142-
(of HD HDTY, of-app HDTY ARGS TY HD (Done - Done)).
143+
(of HD HDTY, of-app HDTY ARGS TY HD (Done - Done)) _.
143144
of (lam F) (arrow T B) :- !, pi x\
144145
(of x T :- !) => of (F x) B.
145146

146147
mode (of-app i i o o o).
147148

148149
:if "DEBUG:CHECKER"
149150
of-app Ty Args Tgt Hd _ :-
150-
print {counter "run"} "of-app" {pp Hd} ":" {pp Ty} "@" {pp-list Args} "=" {pp Tgt}, fail.
151+
print {counter "run"} "of-app" {pp Hd} ":" {ppt Ty} "@" {pp-list Args} "=" {ppt Tgt}, fail.
151152

152153
of-app (tapp [tconst "variadic", T, _] as V) [X|XS] TGT HD (B - BT) :- !,
153154
of X TX, assert (unif T TX) (type-err X TX T), BT = X :: TL, of-app V XS TGT HD (B - TL).
@@ -179,15 +180,15 @@ type checking (ctype "Loc.t") -> prop.
179180
log-tc-clause Loc Query :- !, print {counter "run"} "typecheck" Loc Query.
180181
log-tc-clause _ _.
181182

182-
typecheck [] (clause Loc Names Query) :-
183+
typecheck [] (clause Loc Names Query) RC :-
183184
log-tc-clause Loc Query,
184185
checking Loc =>
185-
report-all-failures-if-no-success (of-clause Names Query).
186-
typecheck [ (clause Loc Names Clause) |Rest] Q :-
186+
report-all-failures-if-no-success (of-clause Names Query) RC.
187+
typecheck [ (clause Loc Names Clause) |Rest] Q RC :-
187188
log-tc-clause Loc Clause,
188189
checking Loc =>
189-
report-all-failures-if-no-success (of-clause Names Clause), !,
190-
typecheck Rest Q.
190+
report-all-failures-if-no-success (of-clause Names Clause) RC, !,
191+
typecheck Rest Q RC.
191192

192193
mode (refresh i o).
193194
refresh (forall F) T :- !, refresh (F FRESH_) T.
@@ -197,8 +198,6 @@ refresh X X.
197198
safe-dest-app (app [X | A]) X A :- !.
198199
safe-dest-app X X [].
199200

200-
macro @vdash :- ":-".
201-
202201
collect-symbols-term N _ X X :- name N, !.
203202
collect-symbols-term (cdata _) _ X X :- !.
204203
collect-symbols-term (app []) _ X X :- !.
@@ -245,6 +244,9 @@ warn-undeclared Known ((const S) `: LOC) :-
245244
MSG is "constant " ^ S ^ " has no declared type." ^ H,
246245
warning LOC MSG.
247246

247+
type (`:) term -> typ -> entry.
248+
type (`:) term -> ctype "Loc.t" -> entry.
249+
248250
under-decl-env [] P :- P.
249251
under-decl-env [ X `: PT | XS ] P :-
250252
%print "Assume" X PT,
@@ -255,7 +257,7 @@ under-undecl-env [ X `: _ | XS ] P :-
255257
%print "Assume" X PT,
256258
(of X Ty_ :- !) => under-undecl-env XS P.
257259

258-
typecheck-program P Q DeclaredTypes :-
260+
typecheck-program P Q DeclaredTypes RC :-
259261
KnownTypes = [
260262
((const "pi") `: forall x\ (arrow (arrow x prop) prop)),
261263
((const "sigma") `: forall x\ (arrow (arrow x prop) prop)),
@@ -264,7 +266,7 @@ typecheck-program P Q DeclaredTypes :-
264266
collect-symbols-clause Q KnownTypes TMP Undeclared,
265267
iter (warn-undeclared KnownTypes) {rev Undeclared},
266268
under-decl-env {rev KnownTypes}
267-
(under-undecl-env Undeclared (typecheck P Q)).
269+
(under-undecl-env Undeclared (typecheck P Q RC)).
268270

269271
% ---------- warnings ------------------------------------------------------
270272

@@ -319,23 +321,13 @@ warn-linear [ (clause Loc Names Clause) |CS] :-
319321

320322
% ---------- test ----------------------------------------------------------
321323

322-
type foo int -> prop.
323-
type foo string -> prop.
324-
325-
main :- test1, test2, test3, test4, test5, test6, test7,
326-
warn1.
327-
test1 :- app lam.
328-
test2 :- lam app.
329-
test3 :- app "oops".
330-
test4 :- app [] 2.
331-
test5 :- print x 2 "3x".
332-
test6 :- foo "x", foo 1, foo app.
333-
test7 :- foo (foo app).
334-
warn1 :- LINEAR.
324+
main.
335325

336326
% ------- entry ---------------------------------------
337327

338328
check P Q DeclaredTypes :-
339-
typecheck-program P Q DeclaredTypes, !, warn-linear P.
329+
typecheck-program P Q DeclaredTypes RC, !,
330+
warn-linear P, !,
331+
if (var RC) (true) (fail).
340332

341333
% vim: set ft=lprolog:

src/elpi2html.elpi

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,17 @@ main-quoted P OUT FILTERS :-
388388
close_out OC.
389389

390390
pred main i:list string.
391+
type main prop.
391392

392393
% entry point from the command line
393394
main [IN,OUT|FILTERS] :- !,
394395
quote_syntax IN "main" P _,
395396
main-quoted P OUT FILTERS.
396397

397-
main _ :-
398-
print "usage: elpi elpi2html.elpi -exec main -- in out [filter]".
398+
main _ :- usage.
399+
main.
400+
401+
usage :-
402+
halt "usage: elpi elpi2html.elpi -exec main -- in out [filter]".
399403

400404
% vim: set ft=lprolog:

tests/sources/elpi-checker.elpi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../src/elpi-checker.elpi

tests/sources/elpi2html.elpi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../src/elpi2html.elpi

tests/suite/elpi_specific.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,17 @@ let () = declare "asclause"
156156
~description:"parsing of the .. as X clause"
157157
~expectation:Test.Success
158158
()
159+
160+
let () = declare "elpi-checker"
161+
~source_elpi:"elpi-checker.elpi"
162+
~description:"type checking the type checker"
163+
~typecheck:true
164+
~expectation:Test.Success
165+
()
166+
167+
let () = declare "elpi2html"
168+
~source_elpi:"elpi2html.elpi"
169+
~description:"type checking elpi2html"
170+
~typecheck:true
171+
~expectation:Test.Success
172+
()

0 commit comments

Comments
 (0)