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
use std::collections::BTreeMap; use std::rc::Rc; /// `Exp` in Mini-TT. /// Expression language for Mini-TT. #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)] pub enum Expression { /// `0` Unit, /// `1` One, /// `U` Type, /// Empty file Void, /// `bla` Var(String), /// `sum { Bla x }` Sum(Branch), /// `split { Bla x => y }` Split(Branch), /// `\Pi a: b. c` Pi(Typed, Box<Self>), /// `\Sigma a: b. c` Sigma(Typed, Box<Self>), /// `\lambda a. c` Lambda(Pattern, Box<Self>), /// `bla.1` First(Box<Self>), /// `bla.2` Second(Box<Self>), /// `f a` Application(Box<Self>, Box<Self>), /// `a, b` Pair(Box<Self>, Box<Self>), /// `Cons a` Constructor(String, Box<Self>), /// `let bla` or `rec bla` Declaration(Box<Declaration>, Box<Self>), } /// Pattern matching branch. pub type Branch = BTreeMap<String, Box<Expression>>; /// Pattern with type explicitly specified pub type Typed = (Pattern, Box<Expression>); /// `Val` in Mini-TT, value term.<br/> /// Terms are either of canonical form or neutral form. #[derive(Debug, Clone)] pub enum Value { /// Canonical form: lambda abstraction. Lambda(Closure), /// Canonical form: unit instance. Unit, /// Canonical form: unit type. One, /// Canonical form: type universe. Type, /// Canonical form: pi type (type for dependent functions). Pi(Box<Self>, Closure), /// Canonical form: sigma type (type for dependent pair). Sigma(Box<Self>, Closure), /// Canonical form: Pair value (value for sigma). Pair(Box<Self>, Box<Self>), /// Canonical form: call to a constructor. Constructor(String, Box<Self>), /// Canonical form: case-split. Split(CaseTree), /// Canonical form: sum type. Sum(CaseTree), /// Neutral form. Neutral(Neutral), } /// Generic definition for two kinds of neutral terms. /// /// Implementing `Eq` because of `NormalExpression` #[derive(Debug, Clone, Eq, PartialEq)] pub enum GenericNeutral<Value: Clone> { /// Neutral form: stuck on a free variable. Generated(u32), /// Neutral form: stuck on applying on a free variable. Application(Box<Self>, Box<Value>), /// Neutral form: stuck on trying to find the first element of a free variable. First(Box<Self>), /// Neutral form: stuck on trying to find the second element of a free variable. Second(Box<Self>), /// Neutral form: stuck on trying to case-split a free variable. Split(GenericCaseTree<Value>, Box<Self>), } /// `Neut` in Mini-TT, neutral value. pub type Neutral = GenericNeutral<Value>; /// `Patt` in Mini-TT. #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)] pub enum Pattern { /// Pair pattern. This sounds like trivial and useless, but we can achieve mutual recursion by /// using this pattern. Pair(Box<Pattern>, Box<Pattern>), /// Unit pattern, used for introducing anonymous definitions. Unit, /// Variable name pattern, the most typical pattern. Var(String), } #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)] pub enum DeclarationType { Simple, Recursive, } /// `Decl` in Mini-TT. #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)] pub struct Declaration { pub pattern: Pattern, pub prefix_parameters: Vec<Typed>, pub signature: Expression, pub body: Expression, pub declaration_type: DeclarationType, } impl Declaration { /// Constructor pub fn new( pattern: Pattern, prefix_parameters: Vec<Typed>, signature: Expression, body: Expression, declaration_type: DeclarationType, ) -> Self { Self { pattern, prefix_parameters, signature, body, declaration_type, } } /// Non-recursive declarations pub fn simple( pattern: Pattern, prefix_parameters: Vec<Typed>, signature: Expression, body: Expression, ) -> Self { Self::new( pattern, prefix_parameters, signature, body, DeclarationType::Simple, ) } /// Recursive declarations pub fn recursive( pattern: Pattern, prefix_parameters: Vec<Typed>, signature: Expression, body: Expression, ) -> Self { Self::new( pattern, prefix_parameters, signature, body, DeclarationType::Recursive, ) } } /// 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, /// 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: /// /// ```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), /// Usually a local variable, introduced in your telescope UpVar(Rc<Self>, Pattern, Value), } pub type TelescopeRaw = GenericTelescope<Value>; pub type TelescopeRc<Value> = Rc<GenericTelescope<Value>>; /// `Rho` in Mini-TT, dependent context. pub type Telescope = Rc<TelescopeRaw>; /// Just for simplifying constructing an `Rc`. 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`. 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. #[derive(Debug, Clone)] pub enum Closure { /// `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.<br/> /// Sometimes the closure is already an evaluated value. Value(Box<Value>), /// `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 two kinds of case trees pub type GenericCaseTree<Value> = (Box<Branch>, Box<Rc<GenericTelescope<Value>>>); /// `SClos` in Mini-TT.<br/> /// Case tree. pub type CaseTree = GenericCaseTree<Value>;