nash-can 0.3.2

Canonicalize a nash source tree, resolving symbols, re-ordering definitions, and preparing a module for type inference.
Documentation
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use bumpalo::Bump;
use nash_ast::{
    Alias as CanAlias, AliasArgument, AliasType, Annotation, Associativity, Binop as CanBinop,
    Ctor as CanCtor, CtorOpts, Decls, Def, Export, Exports, FieldType, Module as CanModule,
    ModuleName, PackageName, Precedence, QualifiedName, Type as CanType, Union as CanUnion,
};
use nash_region::Located;

/// An exported value with its type, mirroring Elm's `I.Interface` values
/// map (`Map Name Can.Annotation`). Every entry comes from the type
/// solver, so interfaces can only be produced for solved modules.
#[derive(Clone, Copy, Debug)]
pub struct InterfaceValue<'a> {
    pub name: &'a str,
    pub annotation: &'a Annotation<'a>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum UnionVisibility {
    Open,
    Closed,
    Private,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AliasVisibility {
    Public,
    Private,
}

#[derive(Clone, Copy, Debug)]
pub struct Interface<'a> {
    pub home: ModuleName<'a>,
    pub values: &'a [InterfaceValue<'a>],
    pub aliases: &'a [InterfaceAlias<'a>],
    pub unions: &'a [InterfaceUnion<'a>],
    pub binops: &'a [InterfaceBinop<'a>],
}

#[derive(Clone, Copy, Debug)]
pub struct InterfaceUnion<'a> {
    pub name: &'a str,
    pub parameters: &'a [&'a str],
    pub ctors: &'a [&'a CanCtor<'a>],
    pub alternatives: u16,
    pub options: CtorOpts,
    pub visibility: UnionVisibility,
}

#[derive(Clone, Copy, Debug)]
pub struct InterfaceAlias<'a> {
    pub name: &'a str,
    pub parameters: &'a [&'a str],
    pub typ: &'a Located<CanType<'a>>,
    pub visibility: AliasVisibility,
}

/// Mirrors Elm's `I.Binop op annotation associativity precedence`: the
/// annotation is the underlying function's, from the solver.
#[derive(Clone, Copy, Debug)]
pub struct InterfaceBinop<'a> {
    pub symbol: &'a str,
    pub annotation: &'a Annotation<'a>,
    pub associativity: Associativity,
    pub precedence: Precedence,
    pub function: &'a str,
}

/// Type annotations for every top-level value of a module, as produced by
/// the type solver (Elm's `Map Name Can.Annotation`).
pub type Annotations<'a> = std::collections::BTreeMap<&'a str, &'a Annotation<'a>>;

/// Mirrors Elm's `I.fromModule home canModule annotations`. Like Elm's
/// partial `annotations ! name` lookup, a top-level value or operator
/// function missing from `annotations` is a bug in the caller: interfaces
/// exist only for fully solved modules.
pub fn from_module<'a>(
    bump: &'a Bump,
    module: &CanModule<'a>,
    annotations: &Annotations<'a>,
) -> Interface<'a> {
    Interface {
        home: module.name,
        values: extract_values(bump, &module.exports, module.decls, annotations),
        unions: extract_unions(bump, &module.exports, module.unions),
        aliases: extract_aliases(bump, &module.exports, module.aliases),
        binops: extract_binops(bump, &module.exports, module.binops, annotations),
    }
}

impl<'a> InterfaceUnion<'a> {
    pub fn to_public(&self) -> Option<InterfaceUnion<'a>> {
        match self.visibility {
            UnionVisibility::Open => Some(*self),
            UnionVisibility::Closed => Some(InterfaceUnion {
                ctors: &[],
                alternatives: 0,
                ..*self
            }),
            UnionVisibility::Private => None,
        }
    }
}

impl<'a> InterfaceAlias<'a> {
    pub fn to_public(&self) -> Option<InterfaceAlias<'a>> {
        match self.visibility {
            AliasVisibility::Public => Some(*self),
            AliasVisibility::Private => None,
        }
    }
}

/// Mirrors Elm's `restrict exports annotations`: every top-level value's
/// annotation, filtered by the export list.
fn extract_values<'a>(
    bump: &'a Bump,
    exports: &Exports<'a>,
    decls: &Decls<'a>,
    annotations: &Annotations<'a>,
) -> &'a [InterfaceValue<'a>] {
    let mut names = Vec::new();
    collect_decl_names(decls, &mut names);

    let to_value = |name: &'a str| InterfaceValue {
        name,
        annotation: annotations
            .get(name)
            .copied()
            .expect("solver annotations cover every top-level value"),
    };

    match exports {
        Exports::Everything(_) => bump.alloc_slice_fill_iter(names.into_iter().map(to_value)),
        Exports::Explicit(exports) => bump.alloc_slice_fill_iter(
            names
                .into_iter()
                .filter(|name| is_exported_value(exports, name))
                .map(to_value)
                .collect::<Vec<_>>(),
        ),
    }
}

fn collect_decl_names<'a>(decls: &Decls<'a>, names: &mut Vec<&'a str>) {
    match decls {
        Decls::Declare { definition, next } => {
            names.push(def_name(definition));
            collect_decl_names(next, names);
        }
        Decls::DeclareRec {
            definition,
            following,
            next,
        } => {
            names.push(def_name(definition));
            for def in *following {
                names.push(def_name(def));
            }
            collect_decl_names(next, names);
        }
        Decls::Empty => {}
    }
}

fn def_name<'a>(def: &Def<'a>) -> &'a str {
    match def {
        Def::Def { name, .. } | Def::TypedDef { name, .. } => name.value,
    }
}

fn is_exported_value(exports: &[&Located<Export<'_>>], name: &str) -> bool {
    exports
        .iter()
        .any(|export| matches!(&export.value, Export::Value(n) if *n == name))
}

fn extract_unions<'a>(
    bump: &'a Bump,
    exports: &Exports<'a>,
    unions: &'a [&'a Located<CanUnion<'a>>],
) -> &'a [InterfaceUnion<'a>] {
    bump.alloc_slice_fill_iter(unions.iter().map(|union| {
        let name = union.value.name.value;
        InterfaceUnion {
            name,
            parameters: union.value.parameters,
            ctors: union.value.ctors,
            alternatives: union.value.alternatives,
            options: union.value.options,
            visibility: union_visibility(exports, name),
        }
    }))
}

fn extract_aliases<'a>(
    bump: &'a Bump,
    exports: &Exports<'a>,
    aliases: &'a [&'a Located<CanAlias<'a>>],
) -> &'a [InterfaceAlias<'a>] {
    bump.alloc_slice_fill_iter(aliases.iter().map(|alias| {
        let name = alias.value.name.value;
        InterfaceAlias {
            name,
            parameters: alias.value.parameters,
            typ: alias.value.typ,
            visibility: alias_visibility(exports, name),
        }
    }))
}

fn extract_binops<'a>(
    bump: &'a Bump,
    exports: &Exports<'a>,
    binops: &'a [&'a Located<CanBinop<'a>>],
    annotations: &Annotations<'a>,
) -> &'a [InterfaceBinop<'a>] {
    bump.alloc_slice_fill_iter(
        binops
            .iter()
            .filter_map(|binop| {
                if is_exported_binop(exports, binop.value.symbol) {
                    // Elm's `toOp` uses `annotations ! name`: the operator's
                    // function must be a solved top-level value.
                    let annotation = annotations
                        .get(binop.value.function)
                        .copied()
                        .expect("solver annotations cover every operator function");
                    Some(InterfaceBinop {
                        symbol: binop.value.symbol,
                        annotation,
                        associativity: binop.value.associativity,
                        precedence: binop.value.precedence,
                        function: binop.value.function,
                    })
                } else {
                    None
                }
            })
            .collect::<Vec<_>>(),
    )
}

fn union_visibility(exports: &Exports<'_>, name: &str) -> UnionVisibility {
    match exports {
        Exports::Everything(_) => UnionVisibility::Open,
        Exports::Explicit(exports) => {
            for export in exports.iter() {
                match &export.value {
                    Export::UnionOpen(n) if *n == name => return UnionVisibility::Open,
                    Export::UnionClosed(n) if *n == name => return UnionVisibility::Closed,
                    _ => {}
                }
            }
            UnionVisibility::Private
        }
    }
}

fn alias_visibility(exports: &Exports<'_>, name: &str) -> AliasVisibility {
    match exports {
        Exports::Everything(_) => AliasVisibility::Public,
        Exports::Explicit(exports) => {
            if exports
                .iter()
                .any(|e| matches!(&e.value, Export::Alias(n) if *n == name))
            {
                AliasVisibility::Public
            } else {
                AliasVisibility::Private
            }
        }
    }
}

fn is_exported_binop(exports: &Exports<'_>, symbol: &str) -> bool {
    match exports {
        Exports::Everything(_) => true,
        Exports::Explicit(exports) => exports
            .iter()
            .any(|export| matches!(&export.value, Export::Binop(s) if *s == symbol)),
    }
}

// ---- deep copy helpers ----

fn copy_str<'d>(dst: &'d Bump, s: &str) -> &'d str {
    dst.alloc_str(s)
}

fn copy_module_name<'d>(dst: &'d Bump, m: &ModuleName<'_>) -> ModuleName<'d> {
    ModuleName {
        package: m.package.map(|p| PackageName {
            author: copy_str(dst, p.author),
            project: copy_str(dst, p.project),
        }),
        name: copy_str(dst, m.name),
    }
}

fn copy_qualified_name<'d>(dst: &'d Bump, q: &QualifiedName<'_>) -> QualifiedName<'d> {
    QualifiedName {
        home: copy_module_name(dst, &q.home),
        name: copy_str(dst, q.name),
    }
}

fn copy_located_type<'d>(dst: &'d Bump, lt: &Located<CanType<'_>>) -> &'d Located<CanType<'d>> {
    dst.alloc(Located::at(lt.region, copy_type(dst, &lt.value)))
}

fn copy_annotation<'d>(dst: &'d Bump, a: &Annotation<'_>) -> &'d Annotation<'d> {
    dst.alloc(Annotation {
        free_vars: dst.alloc_slice_fill_iter(a.free_vars.iter().map(|v| copy_str(dst, v))),
        typ: copy_located_type(dst, a.typ),
    })
}

fn copy_type<'d>(dst: &'d Bump, t: &CanType<'_>) -> CanType<'d> {
    match t {
        CanType::Lambda { from, to } => CanType::Lambda {
            from: copy_located_type(dst, from),
            to: copy_located_type(dst, to),
        },
        CanType::Var(name) => CanType::Var(copy_str(dst, name)),
        CanType::Named { reference, args } => CanType::Named {
            reference: copy_qualified_name(dst, reference),
            args: dst.alloc_slice_fill_iter(args.iter().map(|a| copy_located_type(dst, a))),
        },
        CanType::Record { fields, ext } => CanType::Record {
            fields: dst.alloc_slice_fill_iter(fields.iter().map(|f| FieldType {
                index: f.index,
                field: copy_str(dst, f.field),
                typ: copy_located_type(dst, f.typ),
            })),
            ext: ext.map(|e| copy_str(dst, e)),
        },
        CanType::Unit => CanType::Unit,
        CanType::Tuple {
            first,
            second,
            rest,
        } => CanType::Tuple {
            first: copy_located_type(dst, first),
            second: copy_located_type(dst, second),
            rest: dst.alloc_slice_fill_iter(rest.iter().map(|r| copy_located_type(dst, r))),
        },
        CanType::Alias {
            reference,
            arguments,
            target,
        } => CanType::Alias {
            reference: copy_qualified_name(dst, reference),
            arguments: dst.alloc_slice_fill_iter(arguments.iter().map(|a| AliasArgument {
                name: copy_str(dst, a.name),
                typ: copy_located_type(dst, a.typ),
            })),
            target: match target {
                AliasType::Open(t) => AliasType::Open(copy_located_type(dst, t)),
                AliasType::Filled(t) => AliasType::Filled(copy_located_type(dst, t)),
            },
        },
    }
}

fn copy_ctor<'d>(dst: &'d Bump, c: &CanCtor<'_>) -> &'d CanCtor<'d> {
    dst.alloc(CanCtor {
        name: copy_str(dst, c.name),
        index: c.index,
        arity: c.arity,
        arguments: dst.alloc_slice_fill_iter(c.arguments.iter().map(|a| copy_located_type(dst, a))),
    })
}

/// Deep-copy an `Interface` into a different bump arena.
pub fn deep_copy<'d>(dst: &'d Bump, src: &Interface<'_>) -> Interface<'d> {
    Interface {
        home: copy_module_name(dst, &src.home),
        values: dst.alloc_slice_fill_iter(src.values.iter().map(|v| InterfaceValue {
            name: copy_str(dst, v.name),
            annotation: copy_annotation(dst, v.annotation),
        })),
        aliases: dst.alloc_slice_fill_iter(src.aliases.iter().map(|a| InterfaceAlias {
            name: copy_str(dst, a.name),
            parameters: dst.alloc_slice_fill_iter(a.parameters.iter().map(|p| copy_str(dst, p))),
            typ: copy_located_type(dst, a.typ),
            visibility: a.visibility,
        })),
        unions: dst.alloc_slice_fill_iter(src.unions.iter().map(|u| InterfaceUnion {
            name: copy_str(dst, u.name),
            parameters: dst.alloc_slice_fill_iter(u.parameters.iter().map(|p| copy_str(dst, p))),
            ctors: dst.alloc_slice_fill_iter(u.ctors.iter().map(|c| copy_ctor(dst, c))),
            alternatives: u.alternatives,
            options: u.options,
            visibility: u.visibility,
        })),
        binops: dst.alloc_slice_fill_iter(src.binops.iter().map(|b| InterfaceBinop {
            symbol: copy_str(dst, b.symbol),
            annotation: copy_annotation(dst, b.annotation),
            associativity: b.associativity,
            precedence: b.precedence,
            function: copy_str(dst, b.function),
        })),
    }
}