-
Notifications
You must be signed in to change notification settings - Fork 154
/
substitution.go
84 lines (76 loc) · 1.53 KB
/
substitution.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
package semantic
import (
"fmt"
"sort"
"strings"
)
// Substitution is a maping of type variables to a poly type.
type Substitution map[Tvar]PolyType
func (s Substitution) ApplyType(t PolyType) PolyType {
for tv, typ := range s {
t = t.substituteType(tv, typ)
}
return t
}
func (s Substitution) ApplyScheme(ts Scheme) Scheme {
for tv, typ := range s {
ts.T = ts.T.substituteType(tv, typ)
}
return ts
}
func (s Substitution) ApplyKind(k Kind) Kind {
for tv, typ := range s {
k = k.substituteKind(tv, typ)
}
return k
}
func (s Substitution) ApplyEnv(env *Env) *Env {
for tv, typ := range s {
env.RangeSet(func(n string, scheme Scheme) Scheme {
return scheme.Substitute(tv, typ)
})
}
return env
}
func (s Substitution) ApplyTvar(tv Tvar) Tvar {
switch t := s[tv].(type) {
case Tvar:
return t
default:
return tv
}
}
// Merge r into l.
func (l Substitution) Merge(r Substitution) {
// Apply right to all of l
for tvL, tL := range l {
l[r.ApplyTvar(tvL)] = r.ApplyType(tL)
}
// Add missing keys from r to l
for tvR, tR := range r {
if _, ok := l[tvR]; !ok {
l[tvR] = tR
}
}
}
func (s Substitution) String() string {
var builder strings.Builder
vars := make([]int, 0, len(s))
for tv := range s {
vars = append(vars, int(tv))
}
sort.Ints(vars)
builder.WriteString("{")
if len(s) > 1 {
builder.WriteString("\n")
}
for i, tvi := range vars {
tv := Tvar(tvi)
if i != 0 {
builder.WriteString(",\n")
}
fmt.Fprintf(&builder, "%v = %v", tv, s[tv])
}
builder.WriteString("}")
return builder.String()
}