This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
figure7.go
104 lines (79 loc) · 2.19 KB
/
figure7.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
// 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
// figure7 illustrates figure 7 of Featherweight Go.
//
// Unfortunately, it does not compile:
//
// ./figure5.go:96:6: invalid recursive type: Edge refers to itself
// ./figure5.go:96:6: invalid recursive type Edge
// ./figure5.go:96:6: Edge refers to
// ./figure5.go:100:6: Vertex refers to
// ./figure5.go:96:6: Edge
// ./figure5.go:100:6: invalid recursive type: Vertex refers to itself
package main
// Featherweight Go, Fig. 3
type Function[a any, b any] interface {
Apply(x a) b
}
type incr struct{ n int }
func (this incr) Apply(x int) int {
return x + this.n
}
type pos struct{}
func (this pos) Apply(x int) bool {
return x > 0
}
type compose[a any, b any, c any] struct {
f Function[a, b]
g Function[b, c]
}
func (this compose[a, b, c]) Apply(x a) c {
return this.g.Apply(this.f.Apply(x))
}
// Adapted from Featherweight Go, Fig. 4
type Eq[a any] interface {
Equal(a) bool
}
type Int int
func (this Int) Equal(that Int) bool {
return this == that
}
type List[a any] interface {
Match(casenil Function[Nil[a], any], casecons Function[Cons[a], any]) any
}
type Nil[a any] struct{}
func (xs Nil[a]) Match(casenil Function[Nil[a], any], casecons Function[Cons[a], any]) any {
return casenil.Apply(xs)
}
type Cons[a any] struct {
Head a
Tail List[a]
}
func (xs Cons[a]) Match(casenil Function[Nil[a], any], casecons Function[Cons[a], any]) any {
return casecons.Apply(xs)
}
type lists[a any, b any] struct{}
func (_ lists[a, b]) Map(f Function[a, b], xs List[a]) List[b] {
return xs.Match(mapNil[a, b]{}, mapCons[a, b]{f}).(List[b])
}
type mapNil[a any, b any] struct{}
func (m mapNil[a, b]) Apply(_ Nil[a]) any {
return Nil[b]{}
}
type mapCons[a any, b any] struct {
f Function[a, b]
}
func (m mapCons[a, b]) Apply(xs Cons[a]) any {
return Cons[b]{m.f.Apply(xs.Head), lists[a, b]{}.Map(m.f, xs.Tail)}
}
// Featherweight Go, Fig. 7
type Edge[e Edge[e, v], v Vertex[e, v]] interface {
Source() v
Target() v
}
type Vertex[e Edge[e, v], v Vertex[e, v]] interface {
Edges() List[e]
}
func main() {}