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
use std::fmt;
use std::ops::Deref;
use std::sync::Arc;
use symbol::SymbolRef;
use types::Walker;
pub trait KindEnv {
fn find_kind(&self, type_name: &SymbolRef) -> Option<ArcKind>;
}
impl<'a, T: ?Sized + KindEnv> KindEnv for &'a T {
fn find_kind(&self, id: &SymbolRef) -> Option<ArcKind> {
(**self).find_kind(id)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum Kind {
Variable(u32),
Type,
Row,
Function(ArcKind, ArcKind),
}
impl Kind {
pub fn variable(v: u32) -> ArcKind {
ArcKind::new(Kind::Variable(v))
}
pub fn typ() -> ArcKind {
ArcKind::new(Kind::Type)
}
pub fn row() -> ArcKind {
ArcKind::new(Kind::Row)
}
pub fn function(l: ArcKind, r: ArcKind) -> ArcKind {
ArcKind::new(Kind::Function(l, r))
}
}
#[derive(PartialEq, Copy, Clone, PartialOrd)]
enum Prec {
Top,
Function,
}
struct DisplayKind<'a>(Prec, &'a Kind);
impl fmt::Display for Kind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", DisplayKind(Prec::Top, self))
}
}
impl<'a> fmt::Display for DisplayKind<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self.1 {
Kind::Variable(i) => i.fmt(f),
Kind::Type => "Type".fmt(f),
Kind::Row => "Row".fmt(f),
Kind::Function(ref arg, ref ret) => {
match self.0 {
Prec::Function => {
write!(f, "({} -> {})", DisplayKind(Prec::Function, arg), ret)
}
Prec::Top => write!(f, "{} -> {}", DisplayKind(Prec::Function, arg), ret),
}
}
}
}
}
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct ArcKind(Arc<Kind>);
impl Deref for ArcKind {
type Target = Kind;
fn deref(&self) -> &Kind {
&self.0
}
}
impl fmt::Debug for ArcKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl fmt::Display for ArcKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl ArcKind {
pub fn new(k: Kind) -> ArcKind {
ArcKind(Arc::new(k))
}
}
impl<F: ?Sized> Walker<ArcKind> for F
where F: FnMut(&ArcKind),
{
fn walk(&mut self, typ: &ArcKind) {
self(typ);
walk_kind(typ, self)
}
}
pub fn walk_kind<F: ?Sized>(k: &ArcKind, f: &mut F)
where F: Walker<ArcKind>,
{
match **k {
Kind::Function(ref a, ref r) => {
f.walk(a);
f.walk(r);
}
Kind::Variable(_) |
Kind::Type |
Kind::Row => (),
}
}