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
use std::collections::BTreeMap; use std::rc::Rc; use either::Either; pub type Level = u32; /// `Exp` in Mini-TT. /// Expression language for Mini-TT. /// /// $M,\ N,\ A,\ B ::=$ #[derive(Debug, Clone, Eq, PartialEq)] pub enum Expression { /// $0$ Unit, /// $\textbf{1}$ One, /// $\textsf{U}$, /// `Type`. Extended with levels. Type(Level), /// Empty file Void, /// $x$, /// `bla` Var(String), /// $\textsf{Sum} \ S$, /// `Sum { Bla x }` Sum(Branch), /// $\textsf{fun} \ S$, /// `split { Bla x => y }` Split(Branch), /// This is an extension to Mini-TT, `A ++ B`. Merge(Box<Self>, Box<Self>), /// $\Pi p : A. B$ or $A \rightarrow B$, /// `\Pi a: b. c` or `A -> B` Pi(Typed, Box<Self>), /// $\Sigma p : A. B$ or $A \times B$, /// `\Sigma a: b. c` or `A * B` Sigma(Typed, Box<Self>), /// $\lambda p. M$, /// `\lambda a. c`, the optional value is the type of the argument.<br/> /// This cannot be specified during parsing because it's used for generated intermediate values /// during type-checking. Lambda(Pattern, Option<AnonymousValue>, Box<Self>), /// $M.1$, /// `bla.1` First(Box<Self>), /// $M.2$, /// `bla.2` Second(Box<Self>), /// $M \ N$, /// `f a` Application(Box<Self>, Box<Self>), /// $M, N$, /// `a, b` Pair(Box<Self>, Box<Self>), /// $\textsf{c}\ M$, `Cons a` Constructor(String, Box<Self>), /// `const a = b`, this is an extension: a declaration whose type-signature is inferred. /// This is very similar to a `Declaration`. Constant(Pattern, Box<Self>, Box<Self>), /// $D; M$, /// `let bla` or `rec bla` Declaration(Box<Declaration>, Box<Self>), } /// Just a wrapper for a value but does not do `Eq` comparison. /// This is an implementation detail and should not be noticed much when reading the source code. #[derive(Debug, Clone)] pub struct AnonymousValue { pub internal: Box<Value>, } impl AnonymousValue { pub fn new(value: Value) -> Self { Self { internal: Box::new(value), } } pub fn some(value: Value) -> Option<Self> { Some(Self::new(value)) } } impl Eq for AnonymousValue {} impl PartialEq<AnonymousValue> for AnonymousValue { fn eq(&self, _other: &Self) -> bool { true } } /// $S(M) ::= ()\ |\ (\textsf{c}\ M, S)$ pub type GenericBranch<T> = BTreeMap<String, Box<T>>; /// $S ::= ()\ |\ (\textsf{c}\ M, S)$, Pattern matching branch. pub type Branch = GenericBranch<Expression>; /// This function name is mysterious, but I failed to find a better name. It's for converting a /// `Branch` into a `CaseTree` by inserting the `context` to every `Case`s. pub fn branch_to_righted(branch: Branch, context: Telescope) -> CaseTree { let mut case_tree: CaseTree = Default::default(); for (name, expression) in branch.into_iter() { let case = GenericCase::new(Either::Right(*expression), context.clone()); case_tree.insert(name, Box::new(case)); } case_tree } /// $p:A$, Pattern with type explicitly specified. /// This is just a helper struct. #[derive(Debug, Clone, Eq, PartialEq)] pub struct Typed { pub pattern: Pattern, pub expression: Box<Expression>, } impl Typed { pub fn new(pattern: Pattern, expression: Expression) -> Self { Self { pattern, expression: Box::new(expression), } } pub fn destruct(self) -> (Pattern, Expression) { let pattern = self.pattern; let expression = *self.expression; (pattern, expression) } } /// `Val` in Mini-TT, value term.<br/> /// Terms are either of canonical form or neutral form. /// /// $u,v,t ::=$ #[derive(Debug, Clone)] pub enum Value { /// $\lambda\ f$. /// Canonical form: lambda abstraction. Lambda(Closure), /// $0$. /// Canonical form: unit instance. Unit, /// $\textbf{1}$. /// Canonical form: unit type. One, /// $\textsf{U}$. /// Canonical form: type universe. Type(Level), /// $\Pi \ t\ g$. /// Canonical form: pi type (type for dependent functions). Pi(Box<Self>, Closure), /// $\Sigma \ t\ g$. /// Canonical form: sigma type (type for dependent pair). Sigma(Box<Self>, Closure), /// $u,v$. /// Canonical form: Pair value (value for sigma). Pair(Box<Self>, Box<Self>), /// $c \ t$. /// Canonical form: call to a constructor. Constructor(String, Box<Self>), /// $\textsf{fun}\ s$. /// Canonical form: case-split. Split(CaseTree), /// $\textsf{Sum}\ s$. /// Canonical form: sum type. Sum(CaseTree), /// $[k]$. /// Neutral form. Neutral(Neutral), } /// Generic definition for two kinds of neutral terms. /// /// Implementing `Eq` because of `NormalExpression`. /// /// $k(v) ::=$ #[derive(Debug, Clone, Eq, PartialEq)] pub enum GenericNeutral<Value: Clone> { /// $\textsf{x}_n$. /// Neutral form: stuck on a free variable. Generated(u32), /// $k\ v$. /// Neutral form: stuck on applying on a free variable. Application(Box<Self>, Box<Value>), /// $k.1$. /// Neutral form: stuck on trying to find the first element of a free variable. First(Box<Self>), /// $k.2$. /// Neutral form: stuck on trying to find the second element of a free variable. Second(Box<Self>), /// $s\ k$. /// Neutral form: stuck on trying to case-split a free variable. Split( GenericBranch<GenericCase<Either<Value, Expression>, Value>>, Box<Self>, ), } /// $k ::= k(v)$. /// `Neut` in Mini-TT, neutral value. pub type Neutral = GenericNeutral<Value>; /// `Patt` in Mini-TT. /// /// $p ::=$ #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)] pub enum Pattern { /// $p,p$, /// Pair pattern. This sounds like trivial and useless, but we can achieve mutual recursion by /// using this pattern. Pair(Box<Self>, Box<Self>), /// \_, /// Unit pattern, used for introducing anonymous definitions. Unit, /// $x$, /// Variable name pattern, the most typical pattern. Var(String), } /// `Decl` in Mini-TT. /// $D ::= p:A=M\ |\ \textsf{rec}\ p:A=M$ /// /// It's supposed to be an `enum` because it can be rec or non-rec, but for /// coding convenience I've made it a struct with a `bool` member /// (`is_recursive`) to indicate whether it's recursive. #[derive(Debug, Clone, Eq, PartialEq)] pub struct Declaration { /// $p$ in syntax. pub pattern: Pattern, /// This is an extension -- declarations can be prefixed with some parameters. pub prefix_parameters: Vec<Typed>, /// $A$ in syntax. pub signature: Expression, /// $M$ in syntax. pub body: Expression, /// Whether the $\textsf{rec}$ is present. pub is_recursive: bool, } impl Declaration { /// Constructor pub fn new( pattern: Pattern, prefix_parameters: Vec<Typed>, signature: Expression, body: Expression, is_recursive: bool, ) -> Self { Self { pattern, prefix_parameters, signature, body, is_recursive, } } /// Non-recursive declarations pub fn simple( pattern: Pattern, prefix_parameters: Vec<Typed>, signature: Expression, body: Expression, ) -> Self { Self::new(pattern, prefix_parameters, signature, body, false) } /// Recursive declarations pub fn recursive( pattern: Pattern, prefix_parameters: Vec<Typed>, signature: Expression, body: Expression, ) -> Self { Self::new(pattern, prefix_parameters, signature, body, true) } } /// Generic definition for two kinds of telescopes.<br/> /// `Value` can be specialized with `Value` or `NormalExpression`. /// /// Implementing `Eq` because of `NormalExpression` // TODO: replace with Vec<enum {Dec, Var}> maybe? #[derive(Debug, Clone, Eq, PartialEq)] pub enum GenericTelescope<Value: Clone> { /// $()$, /// Empty telescope. Nil, /// $\rho, p=D$, /// In Mini-TT, checked declarations are put here. However, it's not possible to store a /// recursive declaration as an `Expression` (which is a member of `Declaration`) here. /// /// The problem is quite complicated and can be reproduced by checking out 0.1.5 revision and /// type-check this code (`Type` was `U` and `Sum` was `sum` at that time): /// /// ```ignore /// rec nat : U = sum { Zero 1 | Suc nat }; /// -- Inductive definition of nat /// /// let one : nat = Zero 0; /// let two : nat = Suc one; /// -- Unresolved reference /// ``` UpDec(Rc<Self>, Declaration), /// $\rho, p=v$, /// Usually a local variable, introduced in your telescope UpVar(Rc<Self>, Pattern, Value), } pub type TelescopeRc<Value> = Rc<GenericTelescope<Value>>; /// $\rho ::= \rho(v)$ /// `Rho` in Mini-TT, dependent context. pub type Telescope = Rc<GenericTelescope<Value>>; /// Just for simplifying constructing an `Rc`. /// $\rho, p=v$ pub fn up_var_rc<Value: Clone>( me: TelescopeRc<Value>, pattern: Pattern, value: Value, ) -> TelescopeRc<Value> { Rc::new(GenericTelescope::UpVar(me, pattern, value)) } /// Just for simplifying constructing an `Rc`. /// $\rho, p=D$ pub fn up_dec_rc<Value: Clone>( me: TelescopeRc<Value>, declaration: Declaration, ) -> TelescopeRc<Value> { Rc::new(GenericTelescope::UpDec(me, declaration)) } /// Because we can't `impl` a `Default` for `Rc`. /// $()$ pub fn nil_rc<Value: Clone>() -> TelescopeRc<Value> { Rc::new(GenericTelescope::Nil) } /// `Clos` in Mini-TT. /// /// $f,g ::=$ #[derive(Debug, Clone)] pub enum Closure { /// $\lang \lambda p.M,\rho \rang$, /// `cl` in Mini-TT.<br/> /// Closure that does a pattern matching. /// /// Members: pattern, parameter type (optional), body expression and the captured scope. Abstraction(Pattern, Option<Box<Value>>, Expression, Box<Telescope>), /// This is not present in Mini-TT, thus an extension.<br/> /// Sometimes the closure is already an evaluated value. Value(Box<Value>), /// $f \circ c$ /// `clCmp` in Mini-TT.<br/> /// Closure that was inside of a case-split. /// /// For example, in a definition: /// ```ignore /// f = split { TT a => bla }; /// ``` /// The part `TT a => bla` is a choice closure, where `Box<Self>` refers to the `a => bla` part /// and `TT` is the `String`. Choice(Box<Self>, String), } /// Generic definition for three kinds of case trees #[derive(Debug, Clone, Eq, PartialEq)] pub struct GenericCase<Expression, Value: Clone> { pub expression: Expression, pub context: TelescopeRc<Value>, } impl<Expression, Value: Clone> GenericCase<Expression, Value> { pub fn new(expression: Expression, context: TelescopeRc<Value>) -> Self { Self { expression, context, } } } /// One single case in case trees. pub type Case = GenericCase<Either<Value, Expression>, Value>; /// $\lang S,\rho \rang$, /// `SClos` in Mini-TT.<br/> /// Case tree. pub type CaseTree = GenericBranch<Case>; impl GenericCase<Either<Value, Expression>, Value> { pub fn reduce_to_value(self) -> Value { let GenericCase { expression, context, } = self; expression.either(|l| l, |r| r.eval(context)) } }