-
Notifications
You must be signed in to change notification settings - Fork 3
/
backward_rule.n3
66 lines (58 loc) · 1.24 KB
/
backward_rule.n3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
@prefix : <urn:example:> .
@prefix log: <http://www.w3.org/2000/10/swap/log#> .
@prefix list: <http://www.w3.org/2000/10/swap/list#> .
:Alice a :Person .
{
:Alice :built-in-1 :Ok .
:Alice :built-in-3 :Ok .
?Who :built-in-3 :Ok .
?Who log:equalTo :Alice .
:Alice :built-in-4 ?X .
:Alice :built-in-4 (:Alice) .
}
=>
{
:built-in-4 :has ?X .
:test :is true .
} .
# A backward rule is like a user defined built-in. In the body you specify
# specify what must be true, in the head you define the built-in template.
# It works a bit like stating in an imperative language:
#
# function (:Alice,:built-in-1,:Ok) {
# :Alice a :Person .
# }
{
:Alice :built-in-1 :Ok .
}
<= # Notice the backward arrow here
{
:Alice a :Person .
} .
# The head can only contain one triple. The example below will not work!
#{
# :Alice :built-in-2 :NotOk;
# :test 123 .
#}
#<=
#{
# :Alice a :Person .
#} .
# More interesting backward rules can be created by adding variab;es
{
?Who :built-in-3 :Ok .
}
<=
{
?Who a :Person .
} .
# Or, adding other built-ins
{
?Who :built-in-4 ?Result .
}
<=
{
?Who a :Person .
( () (?Who)) list:append ?Result .
# The result will be bound to the result in the head
} .