summaryrefslogtreecommitdiff
path: root/dhall/src/semantics/core/visitor.rs
blob: 2191ce3757cf6a5b51815ab86c673228947a53ed (plain)
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
use std::iter::FromIterator;

use crate::semantics::{Binder, ValueKind};
use crate::syntax::Label;

/// A visitor trait that can be used to traverse `ValueKind`s. We need this pattern so that Rust lets
/// us have as much mutability as we can. See the equivalent file in `crate::syntax` for more
/// details.
pub(crate) trait ValueKindVisitor<'a, V1, V2>: Sized {
    type Error;

    fn visit_val(&mut self, val: &'a V1) -> Result<V2, Self::Error>;
    fn visit_binder(
        mut self,
        _label: &'a Binder,
        ty: &'a V1,
        val: &'a V1,
    ) -> Result<(V2, V2), Self::Error> {
        Ok((self.visit_val(ty)?, self.visit_val(val)?))
    }
    fn visit_vec(&mut self, x: &'a [V1]) -> Result<Vec<V2>, Self::Error> {
        x.iter().map(|e| self.visit_val(e)).collect()
    }
    fn visit_opt(
        &mut self,
        x: &'a Option<V1>,
    ) -> Result<Option<V2>, Self::Error> {
        Ok(match x {
            Some(x) => Some(self.visit_val(x)?),
            None => None,
        })
    }
    fn visit_map<T>(
        &mut self,
        x: impl IntoIterator<Item = (&'a Label, &'a V1)>,
    ) -> Result<T, Self::Error>
    where
        V1: 'a,
        T: FromIterator<(Label, V2)>,
    {
        x.into_iter()
            .map(|(k, x)| Ok((k.clone(), self.visit_val(x)?)))
            .collect()
    }
    fn visit_optmap<T>(
        &mut self,
        x: impl IntoIterator<Item = (&'a Label, &'a Option<V1>)>,
    ) -> Result<T, Self::Error>
    where
        V1: 'a,
        T: FromIterator<(Label, Option<V2>)>,
    {
        x.into_iter()
            .map(|(k, x)| Ok((k.clone(), self.visit_opt(x)?)))
            .collect()
    }

    fn visit(
        self,
        input: &'a ValueKind<V1>,
    ) -> Result<ValueKind<V2>, Self::Error> {
        visit_ref(self, input)
    }
}

fn visit_ref<'a, Visitor, V1, V2>(
    mut v: Visitor,
    input: &'a ValueKind<V1>,
) -> Result<ValueKind<V2>, Visitor::Error>
where
    Visitor: ValueKindVisitor<'a, V1, V2>,
{
    use ValueKind::*;
    Ok(match input {
        LamClosure {
            binder,
            annot,
            closure,
        } => LamClosure {
            binder: binder.clone(),
            annot: v.visit_val(annot)?,
            closure: closure.clone(),
        },
        PiClosure {
            binder,
            annot,
            closure,
        } => PiClosure {
            binder: binder.clone(),
            annot: v.visit_val(annot)?,
            closure: closure.clone(),
        },
        AppliedBuiltin(b, xs, types, env) => AppliedBuiltin(
            *b,
            v.visit_vec(xs)?,
            v.visit_vec(types)?,
            env.clone(),
        ),
        Var(v) => Var(*v),
        Const(k) => Const(*k),
        BoolLit(b) => BoolLit(*b),
        NaturalLit(n) => NaturalLit(*n),
        IntegerLit(n) => IntegerLit(*n),
        DoubleLit(n) => DoubleLit(*n),
        EmptyOptionalLit(t) => EmptyOptionalLit(v.visit_val(t)?),
        NEOptionalLit(x) => NEOptionalLit(v.visit_val(x)?),
        EmptyListLit(t) => EmptyListLit(v.visit_val(t)?),
        NEListLit(xs) => NEListLit(v.visit_vec(xs)?),
        RecordType(kts) => RecordType(v.visit_map(kts)?),
        RecordLit(kvs) => RecordLit(v.visit_map(kvs)?),
        UnionType(kts) => UnionType(v.visit_optmap(kts)?),
        UnionConstructor(l, kts, uniont) => UnionConstructor(
            l.clone(),
            v.visit_optmap(kts)?,
            v.visit_val(uniont)?,
        ),
        UnionLit(l, x, kts, uniont, ctort) => UnionLit(
            l.clone(),
            v.visit_val(x)?,
            v.visit_optmap(kts)?,
            v.visit_val(uniont)?,
            v.visit_val(ctort)?,
        ),
        TextLit(ts) => TextLit(
            ts.iter()
                .map(|t| t.traverse_ref(|e| v.visit_val(e)))
                .collect::<Result<_, _>>()?,
        ),
        Equivalence(x, y) => Equivalence(v.visit_val(x)?, v.visit_val(y)?),
        PartialExpr(e) => PartialExpr(e.traverse_ref(|e| v.visit_val(e))?),
    })
}

pub struct TraverseRefWithBindersVisitor<F1, F2> {
    pub visit_val: F1,
    pub visit_under_binder: F2,
}

impl<'a, V1, V2, Err, F1, F2> ValueKindVisitor<'a, V1, V2>
    for TraverseRefWithBindersVisitor<F1, F2>
where
    V1: 'a,
    F1: FnMut(&'a V1) -> Result<V2, Err>,
    F2: for<'b> FnOnce(&'a Binder, &'b V1, &'a V1) -> Result<V2, Err>,
{
    type Error = Err;

    fn visit_val(&mut self, val: &'a V1) -> Result<V2, Self::Error> {
        (self.visit_val)(val)
    }
    fn visit_binder<'b>(
        mut self,
        label: &'a Binder,
        ty: &'a V1,
        val: &'a V1,
    ) -> Result<(V2, V2), Self::Error> {
        let val = (self.visit_under_binder)(label, ty, val)?;
        let ty = (self.visit_val)(ty)?;
        Ok((ty, val))
    }
}