-
Notifications
You must be signed in to change notification settings - Fork 5
/
terms.go
117 lines (99 loc) · 2.76 KB
/
terms.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
109
110
111
112
113
114
115
116
117
package golgi
import (
"fmt"
G "gorgonia.org/gorgonia"
)
var (
_ Term = (Layer)(nil)
_ Term = Name("")
_ Term = (*Env)(nil)
)
// Term represents a term that can be used in Golgi
type Term interface {
Name() string
}
// Name is a variable by name
type Name string
// Name will return itself as a string
func (n Name) Name() string { return string(n) }
// Env is a linked list representing an environment.
// Within the documentation, an environment is written as such:
// e := (x ↦ X)
// `x` is the name while `X` is the *gorgonia.Node
//
// A longer environment may look like this:
// e2 := (x ↦ X :: y ↦ Y)
// ^
// Here, e2 is pointing to the *Env that contains (y ↦ Y).
//
// When talking about Envs in general, it will often be written as a meta variable σ.
type Env struct {
name string
node *G.Node
prev *Env
}
// NewEnv creates a new Env.
func NewEnv(name string, node *G.Node) *Env {
return &Env{name: name, node: node}
}
func (e *Env) hinted(prealloc G.Nodes) {
prealloc = append(prealloc, e.node)
if e.prev != nil {
e.prev.hinted(prealloc)
}
}
// Extend allows users to extend the environment.
//
// Given an environment as follows:
// e := (x ↦ X)
// if `e.Extend(y, Y)` is called, the following is returned
// e2 := (x ↦ X :: y ↦ Y)
// ^
// The pointer will be pointing to the *Env starting at y
func (e *Env) Extend(name string, node *G.Node) *Env {
return &Env{name: name, node: node, prev: e}
}
// ByName returns the first node that matches the given name. It also returns the parent
//
// For example, if we have an Env as follows:
// e := (x ↦ X1 :: w ↦ W :: x ↦ X2)
// ^
//
// The caret indicates the pointer of *Env. Now, if e.ByName("x") is called,
// then the result returned will be X2 and (x ↦ X1 :: w ↦ W)
func (e *Env) ByName(name string) (*G.Node, *Env) {
if e.name == name {
return e.node, e.prev
}
if e.prev != nil {
return e.prev.ByName(name)
}
return nil, nil
}
// Model will return the gorgonia.Nodes associated with this environment
func (e *Env) Model() G.Nodes {
retVal := G.Nodes{e.node}
if e.prev != nil {
retVal = append(retVal, e.prev.Model()...)
}
return retVal
}
// HintedModel will return the gorgonia.Nodes hinted associated with this environment
func (e *Env) HintedModel(hint int) G.Nodes {
prealloc := make(G.Nodes, 0, hint)
e.hinted(prealloc)
return prealloc
}
// Name will return the name of the composition
func (e *Env) Name() string {
var name string
if e.prev != nil {
name = e.prev.Name() + " :: "
}
name += fmt.Sprintf("%v ↦ %v", e.name, e.node)
return name
}
type tag struct{ a, b Term }
func (t tag) Name() string { return fmt.Sprintf("%v@%v", t.a.Name(), t.b.Name()) }
type I struct{}
func (i I) Name() string { return "I" }