File: substitutions.go

package info (click to toggle)
golang-github-chewxy-hm 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 216 kB
  • sloc: makefile: 2
file content (138 lines) | stat: -rw-r--r-- 2,733 bytes parent folder | download
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
package hm

import "fmt"

// Subs is a list of substitution. Internally there are two very basic substitutions - one backed by map and the other a normal slice
type Subs interface {
	Get(TypeVariable) (Type, bool)
	Add(TypeVariable, Type) Subs
	Remove(TypeVariable) Subs

	// Iter() <-chan Substitution
	Iter() []Substitution
	Size() int
	Clone() Subs
}

// A Substitution is a tuple representing the TypeVariable and the replacement Type
type Substitution struct {
	Tv TypeVariable
	T  Type
}

type sSubs struct {
	s []Substitution
}

func newSliceSubs(maybeSize ...int) *sSubs {
	var size int
	if len(maybeSize) > 0 && maybeSize[0] > 0 {
		size = maybeSize[0]
	}
	retVal := BorrowSSubs(size)
	retVal.s = retVal.s[:0]
	return retVal
}

func (s *sSubs) Get(tv TypeVariable) (Type, bool) {
	if i := s.index(tv); i >= 0 {
		return s.s[i].T, true
	}
	return nil, false
}

func (s *sSubs) Add(tv TypeVariable, t Type) Subs {
	if i := s.index(tv); i >= 0 {
		s.s[i].T = t
		return s
	}
	s.s = append(s.s, Substitution{tv, t})
	return s
}

func (s *sSubs) Remove(tv TypeVariable) Subs {
	if i := s.index(tv); i >= 0 {
		// for now we keep the order
		copy(s.s[i:], s.s[i+1:])
		s.s[len(s.s)-1].T = nil
		s.s = s.s[:len(s.s)-1]
	}

	return s
}

func (s *sSubs) Iter() []Substitution { return s.s }
func (s *sSubs) Size() int            { return len(s.s) }
func (s *sSubs) Clone() Subs {
	retVal := BorrowSSubs(len(s.s))
	copy(retVal.s, s.s)
	return retVal
}

func (s *sSubs) index(tv TypeVariable) int {
	for i, sub := range s.s {
		if sub.Tv == tv {
			return i
		}
	}
	return -1
}

func (s *sSubs) Format(state fmt.State, c rune) {
	state.Write([]byte{'{'})
	for i, v := range s.s {
		if i < len(s.s)-1 {
			fmt.Fprintf(state, "%v: %v, ", v.Tv, v.T)

		} else {
			fmt.Fprintf(state, "%v: %v", v.Tv, v.T)
		}
	}
	state.Write([]byte{'}'})
}

type mSubs map[TypeVariable]Type

func (s mSubs) Get(tv TypeVariable) (Type, bool) { retVal, ok := s[tv]; return retVal, ok }
func (s mSubs) Add(tv TypeVariable, t Type) Subs { s[tv] = t; return s }
func (s mSubs) Remove(tv TypeVariable) Subs      { delete(s, tv); return s }

func (s mSubs) Iter() []Substitution {
	retVal := make([]Substitution, len(s))
	var i int
	for k, v := range s {
		retVal[i] = Substitution{k, v}
		i++
	}
	return retVal
}

func (s mSubs) Size() int { return len(s) }
func (s mSubs) Clone() Subs {
	retVal := make(mSubs)
	for k, v := range s {
		retVal[k] = v
	}
	return retVal
}

func compose(a, b Subs) (retVal Subs) {
	if b == nil {
		return a
	}

	retVal = b.Clone()

	if a == nil {
		return
	}

	for _, v := range a.Iter() {
		retVal = retVal.Add(v.Tv, v.T)
	}

	for _, v := range retVal.Iter() {
		retVal = retVal.Add(v.Tv, v.T.Apply(a).(Type))
	}
	return retVal
}