forked from bcmills/go2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfigure23.go
49 lines (40 loc) · 1.08 KB
/
figure23.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
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
// Adapted from Featherweight Go, Fig. 23
type Eq[a any] interface {
Equal(that a) Bool
}
type Bool interface {
Not() Bool
Equal(that Bool) Bool
cond(br Branches[any]) any
}
type Branches[a any] interface {
IfTT() a
IfFF() a
}
type Cond[a any] struct{}
func (this Cond[a]) Eval(b Bool, br Branches[a]) a {
return b.cond(anyBranches[a]{br}).(a)
}
type anyBranches[a any] struct {
typed Branches[a]
}
func (br anyBranches[a]) IfTT() any {
return br.typed.IfTT()
}
func (br anyBranches[a]) IfFF() any {
return br.typed.IfFF()
}
type TT struct{}
type FF struct{}
func (this TT) Not() Bool { return FF{} }
func (this FF) Not() Bool { return TT{} }
func (this TT) Equal(that Bool) Bool { return that }
func (this FF) Equal(that Bool) Bool { return that.Not() }
func (this TT) cond(br Branches[any]) any { return br.IfTT() }
func (this FF) cond(br Branches[any]) any { return br.IfFF() }
func main() {}