-
Notifications
You must be signed in to change notification settings - Fork 22
/
unify.go
162 lines (151 loc) · 4.59 KB
/
unify.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
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
//
// Copyright © 2018 Aljabr, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package symbol
import (
"github.com/kocircuit/kocircuit/lang/circuit/model"
"github.com/kocircuit/kocircuit/lang/go/kit/tree"
)
func Unify(span *model.Span, x, y Type) (Type, error) {
ctx := &typingCtx{Span: span}
return ctx.Unify(x, y)
}
func UnifyTypes(span *model.Span, tt []Type) (Type, error) {
ctx := &typingCtx{Span: span}
return ctx.UnifyTypes(tt)
}
func (ctx *typingCtx) UnifyTypes(tt []Type) (unified Type, err error) {
if len(tt) == 0 {
return EmptyType{}, nil
}
unified = tt[0]
for i := 1; i < len(tt); i++ {
if unified, err = ctx.Unify(unified, tt[i]); err != nil {
return nil, err
}
}
return
}
// Unify(x, y) = Unify(y, x)
// Unify(x, Unify(y, z)) = Unify(Unify(x, y), z)
func (ctx *typingCtx) Unify(x, y Type) (Type, error) {
switch {
case IsEmptyType(x) && IsEmptyType(y):
return EmptyType{}, nil
case !IsEmptyType(x) && IsEmptyType(y):
return Optionally(x), nil
case IsEmptyType(x) && !IsEmptyType(y):
return Optionally(y), nil
}
switch xt := x.(type) {
case *OptionalType:
if elem, err := ctx.Unify(xt.Elem, y); err != nil {
return nil, err
} else {
return Optionally(elem), nil
}
case *SeriesType:
switch yt := y.(type) {
case *OptionalType:
return ctx.Unify(y, x) // symmetry
case *SeriesType:
return ctx.UnifySeries(xt, yt)
case BasicType, *OpaqueType, *MapType, *StructType, VarietyType, NamedType:
if elem, err := ctx.Unify(xt.Elem, y); err != nil {
return nil, err
} else {
return &SeriesType{elem}, nil
}
}
case BasicType:
switch yt := y.(type) {
case *OptionalType, *SeriesType:
return ctx.Unify(y, x) // symmetry
case BasicType:
return ctx.UnifyBasic(xt, yt)
}
case *OpaqueType:
switch yt := y.(type) {
case *OptionalType, *SeriesType, BasicType:
return ctx.Unify(y, x) // symmetry
case *OpaqueType:
return ctx.UnifyOpaque(xt, yt)
}
case *MapType:
switch yt := y.(type) {
case *OptionalType, *SeriesType, BasicType, *OpaqueType:
return ctx.Unify(y, x) // symmetry
case *MapType:
return ctx.UnifyMap(xt, yt)
}
case *StructType:
switch yt := y.(type) {
case *OptionalType, *SeriesType, BasicType, *OpaqueType, *MapType:
return ctx.Unify(y, x) // symmetry
case *StructType:
return ctx.UnifyStruct(xt, yt)
}
case VarietyType:
switch y.(type) {
case *OptionalType, *SeriesType, BasicType, *OpaqueType, *MapType, *StructType:
return ctx.Unify(y, x) // symmetry
case VarietyType:
return VarietyType{}, nil
}
case NamedType:
switch yt := y.(type) {
case *OptionalType, *SeriesType, BasicType, *OpaqueType, *MapType, *StructType, VarietyType:
return ctx.Unify(y, x) // symmetry
case NamedType:
return ctx.UnifyNamed(xt, yt)
}
}
return nil, ctx.Errorf(nil, "%s and %s cannot be unified", tree.Sprint(x), tree.Sprint(y))
}
func (ctx *typingCtx) UnifyBasic(x, y BasicType) (Type, error) {
if unified, ok := unifyBasic(x, y); ok {
return unified, nil
} else {
return nil, ctx.Errorf(nil, "basic types %s and %s cannot be unified", tree.Sprint(x), tree.Sprint(y))
}
}
func (ctx *typingCtx) UnifyOpaque(x, y *OpaqueType) (Type, error) {
if x.Type == y.Type {
return x, nil
} else {
return nil, ctx.Errorf(nil, "opaque types %s and %s cannot be unified", tree.Sprint(x), tree.Sprint(y))
}
}
func (ctx *typingCtx) UnifySeries(x, y *SeriesType) (*SeriesType, error) {
if xyElem, err := ctx.Refine("()").Unify(x.Elem, y.Elem); err != nil {
return nil, ctx.Errorf(nil, "cannot unify sequences %s and %s", tree.Sprint(x), tree.Sprint(y))
} else {
return &SeriesType{Elem: xyElem}, nil
}
}
func (ctx *typingCtx) UnifyNamed(x, y NamedType) (Type, error) {
if x.Type == y.Type {
return x, nil
} else {
return nil, ctx.Errorf(nil, "named types %s and %s cannot be unified", tree.Sprint(x), tree.Sprint(y))
}
}
func (ctx *typingCtx) UnifyMap(x, y *MapType) (Type, error) {
if unified, err := ctx.Unify(x.Value, y.Value); err == nil {
return &MapType{Value: unified}, nil
} else {
return nil, ctx.Errorf(nil, "map types %s and %s cannot be unified", tree.Sprint(x), tree.Sprint(y))
}
}