-
Notifications
You must be signed in to change notification settings - Fork 27
/
variable.go
108 lines (92 loc) · 2.29 KB
/
variable.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
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
package engine
import (
"fmt"
"io"
"sync/atomic"
)
var varCounter int64
func lastVariable() Variable {
return Variable(varCounter)
}
// Variable is a prolog variable.
type Variable int64
// NewVariable creates a new anonymous variable.
func NewVariable() Variable {
n := atomic.AddInt64(&varCounter, 1)
return Variable(n)
}
func (v Variable) WriteTerm(w io.Writer, opts *WriteOptions, env *Env) error {
x := env.Resolve(v)
v, ok := x.(Variable)
if !ok {
return x.WriteTerm(w, opts, env)
}
if a, ok := opts.variableNames[v]; ok {
return a.WriteTerm(w, opts.withQuoted(false).withLeft(operator{}).withRight(operator{}), env)
}
_, err := w.Write([]byte(fmt.Sprintf("_%d", v)))
return err
}
func (v Variable) Compare(t Term, env *Env) int {
w := env.Resolve(v)
v, ok := w.(Variable)
if !ok {
return w.Compare(t, env)
}
switch t := env.Resolve(t).(type) {
case Variable:
switch {
case v > t:
return 1
case v < t:
return -1
default:
return 0
}
default:
return -1
}
}
// variableSet is a set of variables. The key is the variable and the value is the number of occurrences.
// So if you look at the value it's a multi set of variable occurrences and if you ignore the value it's a set of occurrences (witness).
type variableSet map[Variable]int
func newVariableSet(t Term, env *Env) variableSet {
s := variableSet{}
for terms := []Term{t}; len(terms) > 0; terms, t = terms[:len(terms)-1], terms[len(terms)-1] {
switch t := env.Resolve(t).(type) {
case Variable:
s[t] += 1
case Compound:
for i := 0; i < t.Arity(); i++ {
terms = append(terms, t.Arg(i))
}
}
}
return s
}
func newExistentialVariablesSet(t Term, env *Env) variableSet {
ev := variableSet{}
for terms := []Term{t}; len(terms) > 0; terms, t = terms[:len(terms)-1], terms[len(terms)-1] {
if c, ok := env.Resolve(t).(Compound); ok && c.Functor() == atomCaret && c.Arity() == 2 {
for v, o := range newVariableSet(c.Arg(0), env) {
ev[v] = o
}
terms = append(terms, c.Arg(1))
}
}
return ev
}
func newFreeVariablesSet(t, v Term, env *Env) variableSet {
fv := variableSet{}
s := newVariableSet(t, env)
bv := newVariableSet(v, env)
for v := range newExistentialVariablesSet(t, env) {
bv[v] += 1
}
for v, n := range s {
if m, ok := bv[v]; !ok {
fv[v] = n + m
}
}
return fv
}