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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use arret_syntax::datum::DataStr;
use arret_syntax::span::Span;
use crate::id_type::ArcId;
use crate::ty;
use crate::ty::purity;
use crate::ty::purity::Purity;
use crate::ty::ty_args::TyArgs;
use crate::ty::var_usage::Variance;
use crate::ty::Ty;
#[derive(PartialEq, Debug, Clone)]
pub struct Field {
span: Span,
name: DataStr,
ty_ref: ty::Ref<ty::Poly>,
}
impl Field {
pub fn new(span: Span, name: DataStr, ty_ref: ty::Ref<ty::Poly>) -> Self {
Self { span, name, ty_ref }
}
pub fn span(&self) -> Span {
self.span
}
pub fn name(&self) -> &DataStr {
&self.name
}
pub fn ty_ref(&self) -> &ty::Ref<ty::Poly> {
&self.ty_ref
}
pub fn accessor_fun_type(&self, cons_id: &ConsId) -> ty::Fun {
let ty_args = cons_id.identity_ty_args();
let pvars: purity::PVars = ty_args.pvar_purities().keys().cloned().collect();
let tvars: ty::TVars = ty_args.tvar_types().keys().cloned().collect();
let top_fun = ty::TopFun::new(Purity::Pure.into(), self.ty_ref().clone());
let params =
ty::List::new_tuple(Box::new([Instance::new(cons_id.clone(), ty_args).into()]));
ty::Fun::new(pvars, tvars, top_fun, params)
}
}
#[derive(PartialEq, Debug, Clone)]
pub enum PolyParam {
PVar(Variance, purity::PVarId),
Pure(Span),
TVar(Variance, ty::TVarId),
TFixed(Span, ty::Ref<ty::Poly>),
}
impl PolyParam {
pub fn variance(&self) -> Variance {
match self {
PolyParam::PVar(variance, _) => *variance,
PolyParam::TVar(variance, _) => *variance,
PolyParam::Pure(_) | PolyParam::TFixed(_, _) => Variance::Invariant,
}
}
}
#[derive(PartialEq, Debug, Clone)]
pub struct Cons {
span: Span,
ty_cons_name: DataStr,
value_cons_name: DataStr,
poly_params_list: Option<Box<[PolyParam]>>,
fields: Box<[Field]>,
}
impl Cons {
pub fn new(
span: Span,
ty_cons_name: DataStr,
value_cons_name: DataStr,
poly_params_list: Option<Box<[PolyParam]>>,
fields: Box<[Field]>,
) -> ConsId {
ConsId::new(Self {
span,
ty_cons_name,
value_cons_name,
poly_params_list,
fields,
})
}
pub fn span(&self) -> Span {
self.span
}
pub fn ty_cons_name(&self) -> &DataStr {
&self.ty_cons_name
}
pub fn value_cons_name(&self) -> &DataStr {
&self.value_cons_name
}
pub fn poly_params(&self) -> &[PolyParam] {
match self.poly_params_list {
Some(ref poly_params) => poly_params.as_ref(),
None => &[],
}
}
pub fn is_singleton(&self) -> bool {
self.poly_params_list.is_none()
}
pub fn fields(&self) -> &[Field] {
self.fields.as_ref()
}
pub fn identity_ty_args(&self) -> TyArgs<ty::Poly> {
use std::collections::HashMap;
let mut pvar_purities = HashMap::new();
let mut tvar_types = HashMap::new();
for poly_param in self.poly_params() {
match poly_param {
PolyParam::PVar(_, pvar) => {
pvar_purities.insert(pvar.clone(), pvar.clone().into());
}
PolyParam::TVar(_, tvar) => {
tvar_types.insert(tvar.clone(), tvar.clone().into());
}
PolyParam::Pure(_) | PolyParam::TFixed(_, _) => {}
}
}
TyArgs::new(pvar_purities, tvar_types)
}
pub fn value_cons_fun_type(cons_id: &ConsId) -> ty::Fun {
let ty_args = cons_id.identity_ty_args();
let pvars: purity::PVars = ty_args.pvar_purities().keys().cloned().collect();
let tvars: ty::TVars = ty_args.tvar_types().keys().cloned().collect();
let ret_type = Instance::new(cons_id.clone(), ty_args).into();
let top_fun = ty::TopFun::new(Purity::Pure.into(), ret_type);
let params = ty::List::new_tuple(
cons_id
.fields
.iter()
.map(|field| field.ty_ref.clone())
.collect(),
);
ty::Fun::new(pvars, tvars, top_fun, params)
}
}
pub type ConsId = ArcId<Cons>;
impl<M: ty::Pm> From<ConsId> for Ty<M> {
fn from(cons_id: ConsId) -> Self {
Ty::RecordClass(cons_id)
}
}
impl<M: ty::Pm> From<ConsId> for ty::Ref<M> {
fn from(cons_id: ConsId) -> Self {
ty::Ref::Fixed(Ty::RecordClass(cons_id))
}
}
#[derive(PartialEq, Debug, Clone)]
pub struct Instance<M: ty::Pm> {
cons: ConsId,
ty_args: TyArgs<M>,
}
impl<M: ty::Pm> Instance<M> {
pub fn new(cons: ConsId, ty_args: TyArgs<M>) -> Self {
Self { cons, ty_args }
}
pub fn cons(&self) -> &ConsId {
&self.cons
}
pub fn ty_args(&self) -> &TyArgs<M> {
&self.ty_args
}
}
impl<M: ty::Pm> From<Instance<M>> for Ty<M> {
fn from(instance: Instance<M>) -> Self {
Ty::Record(Box::new(instance))
}
}
impl<M: ty::Pm> From<Instance<M>> for ty::Ref<M> {
fn from(instance: Instance<M>) -> Self {
ty::Ref::Fixed(Ty::Record(Box::new(instance)))
}
}