Debian Codegraph @

examples/example_data_base.v

raw
1 From elpi Require Import elpi.
2
3 (** Data bases are accumulated by name, hence can be shared among multiple
4 commands or across successive executions of the same command.
5
6 Let's start with a db containing international phone prefixes *)
7
8 Elpi Db phonebook.db lp:{{
9 pred phone_prefix o:string, o:int.
10 phone_prefix "USA" 1.
11 }}.
12
13 Elpi Command print_db.
14 Elpi Accumulate Db phonebook.db.
15 Elpi Accumulate lp:{{
16 main [] :- std.findall (phone_prefix S_ N_) L, !, coq.say "The Db contains" L.
17 main [str S] :- coq.say "Phone prefix for" S "is" {phone_prefix S}, !.
18 main [str S] :- coq.error "No prefix for" S.
19 }}.
20
21
22 Elpi print_db.
23 Elpi print_db USA.
24 Fail Elpi print_db Italy.
25
26 Elpi Command add_db.
27 Elpi Accumulate Db phonebook.db.
28 Elpi Accumulate lp:{{
29 main [str S, int N] :-
30 coq.elpi.accumulate _ "phonebook.db" (clause _ _ (phone_prefix S N)).
31 }}.
32
33
34 Elpi add_db France 33.
35 Elpi add_db Italy 39.
36 Elpi print_db.
37 Elpi print_db France.
38
39 (** Data bases don't need to be that simple, they can contain
40 arbitrary clauses, in particular conditional ones. *)
41
42 Elpi Db food.db lp:{{
43
44 pred sweet o:string.
45 sweet "apricot".
46
47 pred tasty o:string.
48 tasty "salmon".
49
50 }}.
51 Elpi Command add_recipy.
52 Elpi Accumulate Db food.db.
53 Elpi Accumulate lp:{{
54 pred test-sweetness i:argument, o:prop.
55 test-sweetness (str X) (sweet X).
56
57 pred test-tastiness i:argument, o:prop.
58 test-tastiness (str X) (tasty X).
59
60 main [str Name|Ingredients] :-
61 std.map Ingredients test-sweetness SweetConditions,
62 % trick: ":-" accepts a list of propositions on the RHS
63 coq.elpi.accumulate _ "food.db" (clause _ _ (sweet Name :- SweetConditions)),
64
65 std.map Ingredients test-tastiness TastyConditions,
66 std.forall TastyConditions (i\
67 coq.elpi.accumulate _ "food.db" (clause _ _ (tasty Name :- i))).
68 }}.
69
70
71 Elpi Command is_sweet.
72 Elpi Accumulate Db food.db.
73 Elpi Accumulate lp:{{
74 main [str Name] :- if (sweet Name) (coq.say "sweet!") (coq.say "brr").
75 }}.
76
77
78 Elpi Command is_tasty.
79 Elpi Accumulate Db food.db.
80 Elpi Accumulate lp:{{
81 main [str Name] :- if (tasty Name) (coq.say "yummy!") (coq.say "bohf").
82 }}.
83
84
85 Elpi add_recipy "pie" "potato" "salmon".
86 Elpi add_recipy "jam" "apricot".
87 Elpi add_recipy "tart" "jam".
88
89 Elpi is_sweet "tart".
90 Elpi is_sweet "pie".
91 Elpi is_tasty "pie".
92
93