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
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
use core::fmt::Write;
use std::fmt::{Display, Error as FmtError, Formatter};

use crate::ast::*;
use crate::check::read_back::*;

impl Display for Value {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        match self {
            Value::Lambda(closure) => {
                f.write_str("\u{03BB} ")?;
                closure.fmt_with_type(f, None)
            }
            Value::Pair(first, second) => write!(f, "({}, {})", first, second),
            Value::Unit => f.write_str("0"),
            Value::One => f.write_str("1"),
            Value::Pi(input, output) => {
                f.write_str("\u{03A0}")?;
                f.write_str(" ")?;
                output.fmt_with_type(f, Some(&**input))
            }
            Value::Type(level) => write!(f, "Type{}", level),
            Value::Sigma(first, second) => {
                f.write_str("\u{03A3}")?;
                f.write_str(" ")?;
                second.fmt_with_type(f, Some(&**first))
            }
            Value::Constructor(name, arguments) => write!(f, "{} {}", name, arguments),
            // Don't print context
            Value::Split(branches) => {
                f.write_str("split {")?;
                fmt_branch(branches, f)?;
                f.write_char('}')
            }
            // Don't print the context
            Value::Sum(constructors) => {
                f.write_str("Sum {")?;
                fmt_branch(constructors, f)?;
                f.write_char('}')
            }
            Value::Neutral(neutral) => write!(f, "[{}]", neutral),
        }
    }
}

impl Display for Expression {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        match self {
            Expression::Var(name) => name.fmt(f),
            Expression::First(pair) => write!(f, "({}.1)", pair),
            Expression::Second(pair) => write!(f, "({}.2)", pair),
            Expression::Application(function, argument) => write!(f, "({} {})", function, argument),
            Expression::Lambda(pattern, parameter_type, body) => {
                f.write_str("\u{03BB} ")?;
                pattern.fmt(f)?;
                if let Some(parameter_type) = parameter_type {
                    f.write_str(": ")?;
                    parameter_type.internal.fmt(f)?;
                }
                f.write_str(". ")?;
                body.fmt(f)
            }
            Expression::Pair(first, second) => write!(f, "({}, {})", first, second),
            Expression::Unit => f.write_str("0"),
            Expression::One => f.write_str("1"),
            Expression::Pi(input, output) => {
                f.write_str("\u{03A0}")?;
                write!(f, " {}. {}", input, output)
            }
            Expression::Type(level) => write!(f, "Type{}", level),
            Expression::Sigma(first, second) => {
                f.write_str("\u{03A3}")?;
                write!(f, " {}. {}", first, second)
            }
            Expression::Constructor(name, arguments) => write!(f, "{} {}", name, arguments),
            Expression::Split(clauses) => {
                f.write_str("split {")?;
                let mut started = false;
                for (name, clause) in clauses.iter() {
                    if started {
                        f.write_str(" | ")?;
                    } else {
                        started = true;
                    }
                    name.fmt(f)?;
                    f.write_char(' ')?;
                    match &**clause {
                        Expression::Lambda(pattern, _, body) => {
                            pattern.fmt(f)?;
                            f.write_str(" => ")?;
                            body.fmt(f)
                        }
                        rest => rest.fmt(f),
                    }?;
                }
                f.write_char('}')
            }
            // Don't print the context
            Expression::Sum(constructors) => {
                f.write_str("Sum")?;
                f.write_str(" {")?;
                fmt_branch(constructors, f)?;
                f.write_char('}')
            }
            Expression::Declaration(declaration, rest) => writeln!(f, "{};\n{}", declaration, rest),
            Expression::Constant(pattern, body, rest) => {
                write!(f, "const {} = {};\n{}", pattern, body, rest)
            }
            Expression::Void => Ok(()),
            Expression::Merge(lhs, rhs) => {
                lhs.fmt(f)?;
                f.write_str(" ++ ")?;
                rhs.fmt(f)
            }
        }
    }
}

impl<Expr: Display, Value: Clone + Display> Display for GenericCase<Expr, Value> {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        self.expression.fmt(f)
    }
}

impl Display for Typed {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        write!(f, "{}: {}", self.pattern, self.expression)
    }
}

fn fmt_branch<E: Display>(branch: &GenericBranch<E>, f: &mut Formatter) -> Result<(), FmtError> {
    let mut started = false;
    for (name, clause) in branch.iter() {
        if started {
            f.write_str(" | ")?;
        } else {
            started = true;
        }
        name.fmt(f)?;
        f.write_char(' ')?;
        clause.fmt(f)?;
    }
    Ok(())
}

impl Display for Pattern {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        match self {
            Pattern::Pair(first, second) => write!(f, "({}, {})", first, second),
            Pattern::Unit => f.write_char('_'),
            Pattern::Var(name) => f.write_str(name.as_str()),
        }
    }
}

impl Display for Declaration {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        f.write_str(if self.is_recursive { "rec" } else { "let" })?;
        f.write_char(' ')?;
        self.pattern.fmt(f)?;
        for typed in self.prefix_parameters.iter() {
            write!(f, "({})", typed)?;
        }
        f.write_str(": ")?;
        self.signature.fmt(f)?;
        f.write_str(" = ")?;
        self.body.fmt(f)
    }
}

impl Display for Closure {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        match self {
            // Don't print the scope
            Closure::Abstraction(_, pt, _, _) => self.fmt_with_type(f, pt.as_ref().map(|t| &**t)),
            e => e.fmt_with_type(f, None),
        }
    }
}

impl Closure {
    /// Actual implementation of `fmt` for `Closure`
    pub fn fmt_with_type(&self, f: &mut Formatter, t: Option<&Value>) -> Result<(), FmtError> {
        match self {
            Closure::Abstraction(pattern, _, body, _) => {
                pattern.fmt(f)?;
                if let Some(t) = t {
                    f.write_str(": ")?;
                    t.fmt(f)?;
                }
                f.write_str(". ")?;
                body.fmt(f)
            }
            Closure::Value(value) => value.fmt(f),
            Closure::Choice(rest, name) => write!(f, "{}. {}", name, rest),
        }
    }
}

impl<Value: Display + Clone> Display for GenericNeutral<Value> {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        match self {
            GenericNeutral::Generated(index) => write!(f, "<{}>", index),
            GenericNeutral::Application(function, argument) => {
                write!(f, "({} {})", function, argument)
            }
            GenericNeutral::First(pair) => write!(f, "({}.1)", pair),
            GenericNeutral::Second(pair) => write!(f, "({}.2)", pair),
            GenericNeutral::Split(clauses, argument) => {
                write!(f, "app {} {{", argument)?;
                fmt_branch(clauses, f)?;
                f.write_char('}')
            }
        }
    }
}

impl Display for NormalExpression {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        use crate::check::read_back::NormalExpression as Expression;
        match self {
            Expression::Lambda(index, expression) => {
                f.write_str("\u{03BB} <")?;
                write!(f, "{}> {}", index, expression)
            }
            Expression::Pair(first, second) => write!(f, "({}, {})", first, second),
            Expression::Unit => f.write_str("0"),
            Expression::One => f.write_str("1"),
            Expression::Pi(input, index, output) => {
                f.write_str("\u{03A0}")?;
                write!(f, " <{}> {}. {}", index, input, output)
            }
            Expression::Type(level) => write!(f, "Type{}", level),
            Expression::Sigma(first, index, second) => {
                f.write_str("\u{03A3}")?;
                write!(f, " <{}> {}. {}", index, first, second)
            }
            Expression::Constructor(name, arguments) => write!(f, "{} {}", name, arguments),
            Expression::Split(clauses) => {
                f.write_str("split {")?;
                fmt_branch(clauses, f)?;
                f.write_char('}')
            }
            // Don't print the context
            Expression::Sum(constructors) => {
                f.write_str("Sum {")?;
                fmt_branch(constructors, f)?;
                f.write_char('}')
            }
            Expression::Neutral(neutral) => write!(f, "[{}]", neutral),
        }
    }
}

/// Actually it's for NeutralTelescope
impl<Value: Clone + Display> Display for GenericTelescope<Value> {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        match self {
            GenericTelescope::Nil => Ok(()),
            GenericTelescope::UpDec(previous, declaration) => {
                write!(f, "{};\n{}", declaration, previous)
            }
            GenericTelescope::UpVar(previous, pattern, value) => {
                write!(f, "var {}: {};\n{}", pattern, value, previous)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::ast::Declaration;
    use crate::ast::Expression;
    use crate::ast::Pattern;

    #[test]
    fn simple_expr() {
        let expr = Expression::Second(Box::new(Expression::Pair(
            Box::new(Expression::One),
            Box::new(Expression::Unit),
        )));
        println!("{}", expr);
    }

    #[test]
    fn simple_decl() {
        let var = "a".to_string();
        let expr = Expression::Declaration(
            Box::new(Declaration::simple(
                Pattern::Unit,
                vec![],
                Expression::One,
                Expression::Second(Box::new(Expression::Pair(
                    Box::new(Expression::Unit),
                    Box::new(Expression::Unit),
                ))),
            )),
            Box::new(Expression::Declaration(
                Box::new(Declaration::recursive(
                    Pattern::Var(var.clone()),
                    vec![],
                    Expression::One,
                    Expression::First(Box::new(Expression::Pair(
                        Box::new(Expression::Unit),
                        Box::new(Expression::Var(var)),
                    ))),
                )),
                Box::new(Expression::Void),
            )),
        );
        println!("{}", expr);
    }
}