-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdb.go
45 lines (40 loc) · 1.19 KB
/
db.go
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
package gologic
// a db is sort of like an clojure agent containing a linked list
func Db () DB {
var db *DBCons = nil
c := make(chan func (*DBCons) *DBCons)
go func () {
for {
f := <- c
db = f(db)
}
}()
return DB{c}
}
func (db DB) write(r db_record) DB {
db.c <- func (d *DBCons) *DBCons {
return &DBCons{r,d}
}
return db
}
func (d DB) Deref() DBValue {
x := make(chan *DBCons)
d.c <- func (d *DBCons) *DBCons {
x <- d
return d
}
return DBValue{<- x}
}
func (d DB) Assert (entity interface{}, attribute interface{}, value interface{}) {
d.write(db_record{Entity:entity,Attribute:attribute,Value:value})
}
func (d DBValue) Find (entity interface{}, attribute interface{}, value interface{}) Goal {
r := db_record{Entity:entity,Attribute:attribute,Value:value}
return func (s S) R {
g := Fail()
for e := d.d; e != nil; e = e.cdr {
g = Or(g,Unify(r,e.car))
}
return g(s)
}
}