-
Notifications
You must be signed in to change notification settings - Fork 5
/
ptg_simple.jl
134 lines (104 loc) · 3.67 KB
/
ptg_simple.jl
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# # Slice Bread
using PrettyTables
using Catlab
using AlgebraicRewriting
#=
Create an ontology by defining a finite presentation of a freely generated category using @present macro
About the world: The Bread World Ontology has the types Thing, BreadLoaf, Countertop, and Stool. The Breadloaf, Countertop, and Stool types have morphisms to Thing that represent is-a relationships. The InOn type can be used to encode a set relation (as opposed to a function) that was two morphisms going to Thing. One morphism points out the LHS of the relation and the other morphism point out the RHS of the relation.
=#
@present OntBreadWorld(FreeSchema) begin
Thing::Ob
BreadLoaf::Ob
Countertop::Ob
Stool::Ob
BreadLoafIsThing::Hom(BreadLoaf, Thing) # is-a
CountertopIsThing::Hom(Countertop, Thing) # is-a
StoolIsThing::Hom(Stool, Thing) # is-a
InOn::Ob
inOn_l::Hom(InOn, Thing)
inOn_r::Hom(InOn, Thing)
end
# Visualize the ontology
to_graphviz(OntBreadWorld)
# Make the ontology an acset type
@acset_type BreadWorld(OntBreadWorld)
#=
Construct rule by defining a span in the category of ACSets
Use the @acset macro to define an ACSet functor. The LHS refers to a type (or object) in our ontology and the RHS defines the set assignment using FinFunctions. For this, you need to completely specify the ACSet functor, i.e. every object and morphism in the index category must be specified.
=#
# **About the rule:** This rule moves a breadloaf from a countertop to a stool.
# ### Left ACSet
L = @acset BreadWorld begin
Thing = 3
BreadLoaf = 1
Countertop = 1
Stool = 1
BreadLoafIsThing = [1]
CountertopIsThing = [2]
StoolIsThing = [3]
InOn = 1
inOn_l = [1]
inOn_r = [2] # breadloaf is on the countertop
end
#=
### Middle/Keep ACSet
The Thing, Breadloaf, Countertop, and Stool types should be held constant. The InOn type will change because we are changing the underlying set function.
=#
K = @acset BreadWorld begin
Thing = 3
BreadLoaf = 1
Countertop = 1
Stool = 1
BreadLoafIsThing = [1]
CountertopIsThing = [2]
StoolIsThing = [3]
end
# ### Right ACSet
R = @acset BreadWorld begin
Thing = 3
BreadLoaf = 1
Countertop = 1
Stool = 1
BreadLoafIsThing = [1]
CountertopIsThing = [2]
StoolIsThing = [3]
InOn = 1
inOn_l = [1]
inOn_r = [3] # breadloaf is on the stool
end
# ### Left leg of span
l = ACSetTransformation(K, L, Thing=[1, 2, 3], BreadLoaf=[1], Countertop=[1], Stool=[1])
# ### Right leg of span
r = ACSetTransformation(K, R, Thing=[1, 2, 3], BreadLoaf=[1], Countertop=[1], Stool=[1])
#=
Use AlgebraicRewriting.Rule wrapper to add a rule interface
=#
moveBreadRule = Rule(l, r)
#=
## WORLD STATE
Define a world state using the @acset macro. This is the ACSet way of specifying an ACSet. For this, you need to completely specify the ACSet functor, i.e. every object and morphism in the index category must be specified. The ACSets must be specified in terms of FinFunctions.
=#
#=
**About the world state:** In this world state, there are two countertops, one stool, and one breadloaf. All of these amount to four things. The breadloaf is on the first countertop.
=#
state = @acset BreadWorld begin
Thing = 4
BreadLoaf = 1
Countertop = 2
Stool = 1
BreadLoafIsThing = [1]
CountertopIsThing = [2, 3] # there are two countertops
StoolIsThing = [4]
InOn = 1
inOn_l = [1] # breadloaf is on the countertop 1
inOn_r = [2]
end
#=
## Apply Rule
Use the AlgebraicRewriting.get_matches(::Rule{T}, ::ACSet) utility function to find matches between the rule and the state.
=#
matches = get_matches(moveBreadRule, state)
# Take the first match
match = matches[1]
# Compute the new world state after rewriting
new_state = rewrite_match(moveBreadRule, match)