Skip to content

Commit 86e20a4

Browse files
committed
[tc] attribute parser for the creation of elpi predicates for tc
1 parent c7fcee1 commit 86e20a4

20 files changed

+335
-414
lines changed

apps/tc/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ In this implementation, the elpi rule for the instance `a3` is:
109109

110110
```elpi
111111
tc-A {{3}} {{a3 lp:A lp:B lp:C}} :-
112-
do-once (tc-NoBacktrack A B),
112+
once (tc-NoBacktrack A B),
113113
tc-A A C.
114114
```
115115

116-
The predicate `do-once i:prop` has
116+
The predicate `once i:prop` has
117117

118118
```prolog
119-
do-once P :- P, !.
119+
once P :- P, !.
120120
```
121121

122122
as implementation. The cut (`!`) operator is in charge to avoid backtracking on

apps/tc/elpi/WIP/deactivate_evar.elpi

Lines changed: 0 additions & 27 deletions
This file was deleted.

apps/tc/elpi/WIP/force_llam.elpi

Lines changed: 0 additions & 131 deletions
This file was deleted.

apps/tc/elpi/WIP/modes.elpi

Lines changed: 0 additions & 44 deletions
This file was deleted.

apps/tc/elpi/att_parser.elpi

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/* license: GNU Lesser General Public License Version 2.1 or later */
2+
/* ------------------------------------------------------------------------- */
3+
4+
/*
5+
This file aims to parse the attributes passed to a class. These parsed
6+
attributes are stored in the tc.pending.atts predicate.
7+
8+
A new pending.atts rule is built with the command TC.Pending_attributes
9+
10+
We call atts "pending" since atts are meant to add the wanted attributes to
11+
the signature of the incoming class declaration (remember that a class in coq
12+
is translated as a predicate in elpi). After the creation of the predicate the
13+
pending attribute rule is removed.
14+
15+
NOTE: it is forbidden to have two rules for tc.pending.atts in the same
16+
database
17+
*/
18+
namespace tc {
19+
namespace pending {
20+
21+
shorten tc.pending.{atts, mode, functionality}.
22+
shorten tc.{no-backtrack}.
23+
24+
pred default-atts o:mode, o:functionality, o:tc.search-mode.
25+
default-atts (mode []) (functionality ff) (tc.no-backtrack ff).
26+
27+
namespace internal {
28+
% returns the bool associated to an option.
29+
% by default, if not specified, ff is returned
30+
pred get-bool-opt i:string, o:bool.
31+
get-bool-opt S B :- get-option S B, !.
32+
get-bool-opt _ ff.
33+
34+
namespace modes {
35+
/*
36+
for each option returns its value. Note that no check (for now) is
37+
done to verify if the value is a valid mode. moreover, if mode is `i`
38+
(resp. `o`) with flag `ff`, we return its negated version, i.e. `o`
39+
(resp. `o`)
40+
*/
41+
pred get-option->modes-string i:prop, o:string.
42+
get-option->modes-string (get-option A tt) A :- !.
43+
get-option->modes-string (get-option "i" ff) "o" :- !.
44+
get-option->modes-string (get-option "o" ff) "i" :- !.
45+
get-option->modes-string A _ :- coq.error "[tc] cannot parse" A.
46+
47+
pred from-att o:mode.
48+
from-att (mode Modes') :-
49+
get-option "mode" L,
50+
std.map L get-option->modes-string Modes, !,
51+
% adding 'o' to the end of modes for the proof of instance search
52+
std.append Modes ["o"] Modes'.
53+
% By default, if modes are not specified, we return the empty list
54+
from-att (mode []).
55+
}
56+
57+
% [give-attribute S P R] looks for the value (of type bool) of the attribute
58+
% called S, and returns the predicate P applied to this value
59+
pred give-attribute i:string, i:(bool -> B), o:B.
60+
give-attribute S P (P B) :- get-bool-opt S B.
61+
}
62+
63+
macro @functional :- "functional".
64+
macro @no-backtrack :- "no_backtrack".
65+
macro @mode :- "mode".
66+
macro @pending-atts! :- "pending-atts".
67+
68+
/*
69+
parses the attributes list to retrive the declared mode, functionality and
70+
search-mode. This parsing operation is for attributes of new predicates.
71+
If an attribute is not provided the default value will be returned.
72+
73+
to extend the attribute parse:
74+
- add the new label in the list passed to parse-attributes
75+
- add the new value as ouput of this signature
76+
- changes will be needed for the implementation of pending.atts and
77+
declare-class-in-elpi predicates.
78+
*/
79+
pred att-parser-for-pred o:mode, o:functionality, o:tc.search-mode.
80+
att-parser-for-pred Modes Functional NoBacktrack :-
81+
coq.parse-attributes {attributes}
82+
[ att @mode attlist,
83+
att @no-backtrack bool,
84+
att @functional bool,
85+
att-ignore-unknown ] Opts, !,
86+
Opts => (
87+
internal.modes.from-att Modes,
88+
internal.give-attribute @no-backtrack no-backtrack NoBacktrack,
89+
internal.give-attribute @functional functionality Functional,
90+
true).
91+
92+
% parse attributes with att-parser-for-pred and accumulate the
93+
% corressponing tc.pending.atts clause in the database
94+
pred att-add.
95+
att-add :-
96+
atts _ _ _,
97+
coq.error "[TC] an already pending attribute instruction exists".
98+
att-add :- att-acc {att-parser-for-pred}, !.
99+
100+
% accumulates a pending.atts in the database
101+
pred att-acc i:mode, i:functionality, i:tc.search-mode.
102+
att-acc Modes Functional NoBacktrack :-
103+
tc.add-tc-db @pending-atts! _ (atts Modes Functional NoBacktrack).
104+
105+
% returns and remove the pending attributes from the database
106+
pred get o:mode, o:functionality, o:tc.search-mode.
107+
get Modes Functional NoBacktrack :-
108+
atts Modes Functional NoBacktrack, !, att-rm.
109+
get Modes Functional NoBacktrack :-
110+
default-atts Modes Functional NoBacktrack.
111+
112+
% removes the pending attributes from the database. this removal is done
113+
% after a new predicate (class) has been declared
114+
pred att-rm.
115+
att-rm :-
116+
default-atts Modes Functional NoBacktrack,
117+
tc.remove-clause @pending-atts! (atts Modes Functional NoBacktrack) [].
118+
}
119+
}

apps/tc/elpi/compiler1.elpi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ namespace tc {
8989
build-args (prod _ _ Bo) [{{0}} | TL] :- !, build-args (Bo _) TL.
9090
build-args _ [{{0}}].
9191

92-
% [remove-inst GR] remove an instance from the DB by replacing it with `dummy`
92+
% [remove-inst GR] remove an instance from the DB
9393
pred remove-inst i:gref.
9494
remove-inst InstGR :-
9595
tc.get-full-path InstGR ClauseName,

0 commit comments

Comments
 (0)