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, modes, functionality}.
22+ shorten tc.{no-backtrack}.
23+
24+ pred default-atts o:modes, o:functionality, o:tc.search-mode.
25+ default-atts (modes []) (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:modes.
48+ from-att (modes 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 (modes []).
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:modes, 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:modes, 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:modes, 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+ }
0 commit comments