forked from singnet/atomspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype-signature.scm
105 lines (85 loc) · 3.01 KB
/
type-signature.scm
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
;
; A demonstration of using type signatures aka "deep types" during
; pattern matching. Type signatures are a way of specifying the type
; of a hypergraph. This can be used to restrict the search space during
; a pattrn match: by default, types are checked during a search, and
; variable groundings must respect the variable type.
;
(use-modules (opencog) (opencog query) (opencog exec))
; Populate the atomspace with some nonsense atoms.
(Inheritance (Concept "foo") (Concept "bingo"))
(Inheritance (Concept "bar") (Concept "bingo"))
(Inheritance (Concept "baz") (Concept "bonk"))
; Define a search that should return only the first inheritance link.
(define get-foo
(GetLink
(TypedVariable (Variable "$x")
(Signature (Inheritance (Concept "foo") (Type "ConceptNode"))))
; Search the entire atomspace for this.
(Variable "$x")
)
)
; Perform the search. Should return only the foo-inheritance.
(cog-execute! get-foo)
; Define a search that should return the first two inheritance links.
(define get-foobar
(GetLink
(TypedVariable (Variable "$x")
(TypeChoice
(Signature (Inheritance (Concept "foo") (Type "ConceptNode")))
(Signature (Inheritance (Concept "bar") (Type "ConceptNode")))))
; Search the entire atomspace for this.
(Variable "$x")
)
)
; Perform the search. Should return both foo and bar-inheritance.
(cog-execute! get-foobar)
; =============================================================
; A more complex example
; More data:
(EvaluationLink
(PredicateNode "foo")
(ListLink (ConceptNode "bingo") (ConceptNode "yes!")))
(EvaluationLink
(AnchorNode "bar")
(ListLink (ConceptNode "hurrah") (ConceptNode "yay!")))
(EvaluationLink
(ConceptNode "baz")
(ListLink (ConceptNode "oops") (ConceptNode "Oh no, Mr. Bill!")))
; A search pattern that looks for predicates or grounded predicates.
(define predicate-search
(GetLink
(TypedVariable
(Variable "$x")
(Signature
(EvaluationLink
(TypeChoice
(TypeNode "PredicateNode")
(TypeNode "AnchorNode"))
(ListLink
(Type "ConceptNode") (Type "ConceptNode")))))
(AndLink (Variable "$x"))))
(cog-execute! predicate-search)
; =============================================================
; The cog-value-is-type? function allows signatures to be
; directly validated. Consider these examples:
; Should evaluate to true.
(cog-value-is-type?
(Signature (Inheritance (Concept "foo") (Type "ConceptNode")))
(Inheritance (Concept "foo") (ConceptNode "bar")))
; Should evaluate to false.
(cog-value-is-type?
(Signature (Inheritance (Concept "foo") (Type "ConceptNode")))
(Inheritance (Concept "failure-mode") (ConceptNode "bar")))
; We can define new types, too.
(DefineLink
(DefinedType "My foo type")
(Signature (Inheritance (Concept "foo") (Type "ConceptNode"))))
; Should evaluate to true
(cog-value-is-type?
(DefinedType "My foo type")
(Inheritance (Concept "foo") (ConceptNode "bar")))
; Should evaluate to false.
(cog-value-is-type?
(DefinedType "My foo type")
(Inheritance (Concept "failure-mode") (ConceptNode "bar")))