This repository was archived by the owner on Oct 23, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathunroll.go
More file actions
174 lines (163 loc) · 4.56 KB
/
Copy pathunroll.go
File metadata and controls
174 lines (163 loc) · 4.56 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package mini
import (
"go.awalterschulze.org/gominikanren/micro"
"go.awalterschulze.org/gominikanren/sexpr/ast"
)
// defines two candidate functions for unrolling
// the idea is to do partial application and 'unroll' recursive functions
// into a disjunction/conjunction of goals, so that they can be
// executed in parallel in order to speed them up
// unrolled versions are faster even without concurrency
/*
MemberO checks membership in a list
we make a function that does partial application
i.e. given y = (1 2 3), (membero x y) unrolls to
(conde [(== x 1)][(== x 2)][(== x 3)])
(and of course then we can use a parallel version of conde)
unrolling when x is bound but y is unbound does not make sense
but we can optimise if we detect y is bound
(define (membero x y)
(fresh (a d)
(== y `(,a . ,d))
(conde
[(== x a)]
[(membero x d)]
)))
*/
func MemberO(x, y *ast.SExpr) micro.Goal {
return micro.Zzz(micro.CallFresh(func(a *ast.SExpr) micro.Goal {
return micro.CallFresh(func(d *ast.SExpr) micro.Goal {
return micro.Conj(
micro.EqualO(y, ast.Cons(a, d)),
micro.Disj(
micro.EqualO(x, a),
MemberO(x, d),
),
)
})
}))
}
// MemberOUnrolled is the partial application version of MemberO
// taking the second argument (the list) first
func MemberOUnrolled(y *ast.SExpr) func(*ast.SExpr) micro.Goal {
goals := []func(*ast.SExpr) micro.Goal{}
for {
if y == nil {
break
}
car, cdr := y.Car(), y.Cdr()
goals = append(goals, func(x *ast.SExpr) micro.Goal {
return micro.EqualO(x, car)
})
y = cdr
}
n := len(goals)
return func(x *ast.SExpr) micro.Goal {
gs := make([]micro.Goal, n)
for i, g := range goals {
gs[i] = g(x)
}
return DisjPlus(gs...)
}
}
/*
MapO defines a relation between inputs and outputs of a function f
inputs and outputs are given as equal-length lists
we make a function that inspects and unrolls this
i.e. given x = (1 2 3), (mapo f x y) unrolls to the following
(fresh (y1 y2 y3) (f 1 y1) (f 2 y2) (f 3 y3) (== x `(,y1 ,y2 ,y3)))
of which each subgoal can be evaluated in parallel
here unrolling makes sense whether x or y is bound
we can detect either and unroll before applying search
(define (mapo f x y)
(conde
[(== x '()) (== y '())]
[(fresh (xa xd ya yd)
(== `(,xa . ,xd) x)
(== `(,ya . ,yd) y)
(f xa ya)
(mapo f xd yd)
)]
))
*/
func MapO(f func(*ast.SExpr, *ast.SExpr) micro.Goal, x, y *ast.SExpr) micro.Goal {
return Conde(
[]micro.Goal{
micro.EqualO(x, nil),
micro.EqualO(y, nil),
},
[]micro.Goal{
// really missing Fresh (xa xd ya yd) here
micro.CallFresh(func(xa *ast.SExpr) micro.Goal {
return micro.CallFresh(func(xd *ast.SExpr) micro.Goal {
return micro.CallFresh(func(ya *ast.SExpr) micro.Goal {
return micro.CallFresh(func(yd *ast.SExpr) micro.Goal {
return ConjPlus(
micro.EqualO(x, ast.Cons(xa, xd)),
micro.EqualO(y, ast.Cons(ya, yd)),
f(xa, ya),
MapO(f, xd, yd),
)
})
})
})
}),
},
)
}
// MapOUnrolled is partial application of MapO with y already filled in
func MapOUnrolled(list *ast.SExpr) func(func(*ast.SExpr, *ast.SExpr) micro.Goal, *ast.SExpr) micro.Goal {
goals := []func(func(*ast.SExpr, *ast.SExpr) micro.Goal, *ast.SExpr) micro.Goal{}
for {
if list == nil {
break
}
car, cdr := list.Car(), list.Cdr()
goals = append(goals, func(f func(*ast.SExpr, *ast.SExpr) micro.Goal, x *ast.SExpr) micro.Goal {
return f(x, car)
})
list = cdr
}
n := len(goals)
return func(f func(*ast.SExpr, *ast.SExpr) micro.Goal, x *ast.SExpr) micro.Goal {
gs := make([]micro.Goal, n+1)
vars := make([]*ast.SExpr, n)
var cons *ast.SExpr = nil
for i := 0; i < n; i++ {
v := ast.NewVariable("")
vars[i] = v
gs[n-i] = goals[n-i-1](f, v)
cons = ast.Cons(v, cons)
}
gs[0] = micro.EqualO(x, cons)
return ConjPlus(gs...)
}
}
// MapODoubleUnrolled is partial application of MapO with f and y already filled in
func MapODoubleUnrolled(f func(*ast.SExpr, *ast.SExpr) micro.Goal, list *ast.SExpr) func(*ast.SExpr) micro.Goal {
goals := []func(*ast.SExpr) micro.Goal{}
for {
if list == nil {
break
}
car, cdr := list.Car(), list.Cdr()
goals = append(goals, func(x *ast.SExpr) micro.Goal {
return f(x, car)
})
list = cdr
}
n := len(goals)
return func(x *ast.SExpr) micro.Goal {
gs := make([]micro.Goal, n+1)
vars := make([]*ast.SExpr, n)
var cons *ast.SExpr = nil
for i := 0; i < n; i++ {
v := ast.NewVariable("")
vars[i] = v
gs[n-i] = goals[n-i-1](v)
cons = ast.Cons(v, cons)
}
gs[0] = micro.EqualO(x, cons)
return ConjPlus(gs...)
}
}