This page requires javascript to work
  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
use voile_util::level::LiftEx;
use voile_util::uid::DBI;

use super::{CaseSplit, Closure, Neutral, Val, Variants};

/// Reducible expressions.
pub trait RedEx<T: Sized = Val>: Sized {
    /// This is primarily a private implementation-related API.
    /// Use at your own risk.
    fn reduce_with_dbi(self, arg: Val, dbi: DBI) -> T;

    /// When the argument is not likely to be used,
    /// prefer this over [`reduce_with_dbi`](reduce_with_dbi).
    fn reduce_with_dbi_borrow(self, arg: &Val, dbi: DBI) -> T;
}

impl RedEx for Val {
    fn reduce_with_dbi(self, arg: Val, dbi: DBI) -> Val {
        match self {
            Val::Pair(a, b) => Val::pair(
                a.reduce_with_dbi_borrow(&arg, dbi),
                b.reduce_with_dbi(arg, dbi),
            ),
            Val::Neut(neutral_value) => neutral_value.reduce_with_dbi(arg, dbi),
            Val::Lam(closure) => Val::Lam(closure.reduce_with_dbi(arg, dbi + 1)),
            Val::Dt(kind, param_plicit, param_type, closure) => Val::dependent_type(
                kind,
                param_plicit,
                param_type.reduce_with_dbi_borrow(&arg, dbi),
                closure.reduce_with_dbi(arg, dbi + 1),
            ),
            Val::RowPoly(kind, variants) => {
                Val::RowPoly(kind, reduce_variants_with_dbi(variants, dbi, &arg))
            }
            Val::Rec(fields) => Val::Rec(reduce_variants_with_dbi(fields, dbi, &arg)),
            Val::Cons(name, a) => Self::cons(name, a.reduce_with_dbi(arg, dbi)),
            Val::Type(n) => Val::Type(n),
            Val::RowKind(l, k, ls) => Val::RowKind(l, k, ls),
        }
    }

    fn reduce_with_dbi_borrow(self, arg: &Val, dbi: DBI) -> Val {
        match self {
            Val::Pair(a, b) => Val::pair(
                a.reduce_with_dbi_borrow(arg, dbi),
                b.reduce_with_dbi_borrow(arg, dbi),
            ),
            Val::Neut(neutral_value) => neutral_value.reduce_with_dbi_borrow(arg, dbi),
            Val::Lam(closure) => Val::Lam(closure.reduce_with_dbi_borrow(arg, dbi + 1)),
            Val::Dt(kind, param_plicit, param_type, closure) => Val::dependent_type(
                kind,
                param_plicit,
                param_type.reduce_with_dbi_borrow(arg, dbi),
                closure.reduce_with_dbi_borrow(arg, dbi + 1),
            ),
            Val::RowPoly(kind, variants) => {
                Val::RowPoly(kind, reduce_variants_with_dbi(variants, dbi, arg))
            }
            Val::Rec(fields) => Val::Rec(reduce_variants_with_dbi(fields, dbi, arg)),
            Val::Cons(name, a) => Self::cons(name, a.reduce_with_dbi_borrow(arg, dbi)),
            Val::Type(n) => Val::Type(n),
            Val::RowKind(l, k, ls) => Val::RowKind(l, k, ls),
        }
    }
}

impl RedEx for Neutral {
    fn reduce_with_dbi(self, arg: Val, dbi: DBI) -> Val {
        use Neutral::*;
        match self {
            Var(n) if dbi == n => arg.attach_dbi(dbi),
            Var(n) => Val::var(n),
            Ref(n) => Val::glob(n),
            Meta(mi) => Val::meta(mi),
            Axi(a) => Val::Neut(Axi(a)),
            App(f, args) => args
                .into_iter()
                .fold(f.reduce_with_dbi_borrow(&arg, dbi), |f, a| {
                    // Do we need to `reduce` after `apply` again?
                    f.apply(a.reduce_with_dbi_borrow(&arg, dbi))
                }),
            SplitOn(split, obj) => Val::case_tree(split)
                .apply(obj.reduce_with_dbi_borrow(&arg, dbi))
                // further reduce because the `split` is not yet reduced
                .reduce_with_dbi(arg, dbi),
            OrSplit(split, or) => Val::case_tree(split)
                .split_extend(or.reduce_with_dbi_borrow(&arg, dbi))
                .reduce_with_dbi(arg, dbi),
            Fst(pair) => pair.reduce_with_dbi(arg, dbi).first(),
            Snd(pair) => pair.reduce_with_dbi(arg, dbi).second(),
            Proj(rec, field) => rec.reduce_with_dbi(arg, dbi).project(field),
            Lift(levels, neut) => neut.reduce_with_dbi(arg, dbi).lift(levels),
            Fall(levels, neut) => neut.reduce_with_dbi(arg, dbi).fall(levels),
            Row(kind, variants, ext) => {
                let variants = reduce_variants_with_dbi(variants, dbi, &arg);
                let ext = ext.reduce_with_dbi(arg, dbi);
                Val::RowPoly(kind, variants).row_extend(ext)
            }
            Rec(fields, ext) => {
                let fields = reduce_variants_with_dbi(fields, dbi, &arg);
                let ext = ext.reduce_with_dbi(arg, dbi);
                Val::Rec(fields).rec_extend(ext)
            }
        }
    }

    fn reduce_with_dbi_borrow(self, arg: &Val, dbi: DBI) -> Val {
        use Neutral::*;
        match self {
            Var(n) if dbi == n => arg.clone().attach_dbi(dbi),
            Var(n) => Val::var(n),
            Ref(n) => Val::glob(n),
            Meta(mi) => Val::meta(mi),
            Axi(a) => Val::Neut(Axi(a)),
            App(f, args) => args
                .into_iter()
                .fold(f.reduce_with_dbi_borrow(arg, dbi), |f, a| {
                    // Do we need to `reduce` after `apply` again?
                    f.apply(a.reduce_with_dbi_borrow(arg, dbi))
                }),
            SplitOn(split, obj) => Val::case_tree(split)
                .apply(obj.reduce_with_dbi_borrow(&arg, dbi))
                // further reduce because the `split` is not yet reduced
                .reduce_with_dbi_borrow(arg, dbi),
            OrSplit(split, or) => Val::case_tree(split)
                .split_extend(or.reduce_with_dbi_borrow(arg, dbi))
                .reduce_with_dbi_borrow(arg, dbi),
            Fst(pair) => pair.reduce_with_dbi_borrow(arg, dbi).first(),
            Snd(pair) => pair.reduce_with_dbi_borrow(arg, dbi).second(),
            Proj(pair, field) => pair.reduce_with_dbi_borrow(arg, dbi).project(field),
            Lift(levels, neut) => neut.reduce_with_dbi_borrow(arg, dbi).lift(levels),
            Fall(levels, neut) => neut.reduce_with_dbi_borrow(arg, dbi).fall(levels),
            Row(kind, variants, ext) => {
                let variants = reduce_variants_with_dbi(variants, dbi, arg);
                let ext = ext.reduce_with_dbi_borrow(&arg, dbi);
                Val::RowPoly(kind, variants).row_extend(ext)
            }
            Rec(fields, ext) => {
                let fields = reduce_variants_with_dbi(fields, dbi, arg);
                let ext = ext.reduce_with_dbi_borrow(&arg, dbi);
                Val::Rec(fields).rec_extend(ext)
            }
        }
    }
}

impl RedEx<Closure> for Closure {
    fn reduce_with_dbi(self, arg: Val, dbi: DBI) -> Self {
        use Closure::*;
        match self {
            Plain(body) => Self::plain(body.reduce_with_dbi(arg, dbi)),
            Tree(split) => Tree(reduce_case_tree_with_dbi(split, dbi, &arg)),
        }
    }

    fn reduce_with_dbi_borrow(self, arg: &Val, dbi: DBI) -> Self {
        use Closure::*;
        match self {
            Plain(body) => Self::plain(body.reduce_with_dbi_borrow(arg, dbi)),
            Tree(split) => Tree(reduce_case_tree_with_dbi(split, dbi, arg)),
        }
    }
}

fn reduce_variants_with_dbi(variants: Variants, dbi: DBI, arg: &Val) -> Variants {
    variants
        .into_iter()
        .map(|(name, ty)| (name, ty.reduce_with_dbi_borrow(&arg, dbi)))
        .collect()
}

fn reduce_case_tree_with_dbi(cases: CaseSplit, dbi: DBI, arg: &Val) -> CaseSplit {
    cases
        .into_iter()
        .map(|(name, ty)| (name, ty.reduce_with_dbi_borrow(&arg, dbi)))
        .collect()
}