aboutsummaryrefslogtreecommitdiff
path: root/src/ast.rs
blob: 6b3826a1a84255ced47e8dd5916fc4a718ca50e3 (plain) (blame)
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
use std::collections::{BTreeMap, HashMap};

pub type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>;

#[derive(Debug, Clone, PartialEq, Default)]
pub struct Context(HashMap<Identifier, Term>, HashMap<Signature, Expression>);

pub type Identifier = String;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Signature {
    pub name: Identifier,
    pub from: Type,
    pub to: Type
}

/// Fundamental expressions for the lambda calculus.
/// To be extended: bindings, loops/continuations, typedefs?
// note: built-in functions do NOT go here!
// note: we keep parameters as an Identifier because we annotate the WHOLE Abstraction
#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
    Annotation{expr: Box<Expression>, kind: Type},
    Constant{term: Term},
    Variable{id: Identifier},
    Abstraction{param: Identifier, func: Box<Expression>},
    Application{func: Box<Expression>, arg: Box<Expression>},
    Conditional{if_cond: Box<Expression>, if_then: Box<Expression>, if_else: Box<Expression>}
}

/// All supported types.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Type {
    Empty, Error, Unit, Boolean,                    // primitive types
    Natural, Integer, Float, String,
    List(Box<Type>),
    Array(Box<Type>, usize),                        // todo: replace with dependent types
    Slice(Box<Type>),                               // potentially fucky lifetime stuff too
    Union(Vec<Type>),                               // unordered
    Struct(BTreeMap<Identifier, Type>),             // unordered
    Tuple(Vec<Type>, Vec<Option<Identifier>>),      // ordered with labels (vectors must be same length)
    Function(Box<Type>, Box<Type>),                 // from, to: multiple params expressed via tuples
    Interface(Vec<Signature>, Option<Box<Type>>),   // typeclasses "interfaces"
    Oneself,                                        // "Self" type associated with interfaces. replaced by subtyping checks
    Generic(Option<Vec<Type>>),                     // stand in generic type. fully generic, or a list of valid types.
}

/// Data associated with a type.
// Note: no Interfaces, Slices, Empty, Error: those cannot be constructed.
// Note: no Functions: those inhabit a separate context. it's just easier
#[derive(Debug, Clone, PartialEq)]
pub enum Term {
    Unit(), Boolean(bool),
    Natural(usize), Integer(isize),
    Float(f32), String(String),
    List(Vec<Term>),
    Array(Vec<Term>),
    Union(Box<Term>),
    Struct(BTreeMap<Identifier, Term>),
    Tuple(Vec<Term>, Vec<Option<Identifier>>),
}

impl Term {
    /// Convert a term into its corresponding type.
    // Empty lists/arrays and unions currently cannot be inferred.
    pub fn convert(&self) -> Result<Type> {
        match self {
            Term::Unit() => Ok(Type::Unit),
            Term::Boolean(_) => Ok(Type::Boolean),
            Term::Natural(_) => Ok(Type::Natural),
            Term::Integer(_) => Ok(Type::Integer),
            Term::Float(_) => Ok(Type::Float),
            Term::String(_) => Ok(Type::String),
            Term::List(data) => match data.len() {
                0 => Err("attempting to infer the type of an empty list!".into()),
                _ => Ok(Type::List(Box::new(data.get(0).unwrap().convert()?))),
            },
            Term::Array(data) => match data.len() {
                0 => Err("attempting to infer the type of an empty array!".into()),
                _ => Ok(Type::Array(Box::new(data.get(0).unwrap().convert()?), data.len()))
            },
            Term::Union(data) => Err("attempting to infer the type of a union variant!".into()),
            Term::Struct(data) => {
                let mut result = BTreeMap::new();
                for (key, val) in data {
                    result.insert(key.clone(), val.convert()?);
                }
                Ok(Type::Struct(result))
            },
            Term::Tuple(data, fields) => {
                let mut result = Vec::new();
                for val in data {
                    result.push(val.convert()?);
                }
                Ok(Type::Tuple(result, fields.clone()))
            },
        }
    }
}

impl Type {
    /// Get the default value of a type. Throws an error if it doesn't exist.
    // Unions are invalid as they are not ordered.
    // Empty, Error, Slice, Function, Interface, Onself, Generic are invalid as they cannot be constructed.
    pub fn default(&self) -> Result<Term> {
        match self {
            Type::Empty => Err("attempting to take the default term for empty".into()),
            Type::Error => Err("attempting to take the default term for error".into()),
            Type::Unit => Ok(Term::Unit()),
            Type::Boolean => Ok(Term::Boolean(false)),
            Type::Natural => Ok(Term::Natural(0)),
            Type::Integer => Ok(Term::Integer(0)),
            Type::Float => Ok(Term::Float(0.0)),
            Type::String => Ok(Term::String(String::new())),
            Type::List(data) => Ok(Term::List(Vec::<Term>::new())),
            Type::Array(data, len) => Ok(Term::Array(vec![data.default()?; *len])),
            Type::Slice(_) => Err("attempting to take the default term of a slice".into()),
            Type::Union(_) => Err("attempting to take the default term of a union".into()),
            Type::Struct(data) => {
                let mut result = BTreeMap::new();
                for (key, val) in data {
                    result.insert(key.clone(), val.default()?);
                }
                Ok(Term::Struct(result))
            },
            Type::Tuple(data, fields) => {
                let mut result = Vec::new();
                for kind in data {
                    result.push(kind.default()?)
                }
                Ok(Term::Tuple(result, fields.clone()))
            },
            Type::Function(from, to) =>
                Err("attempting to take the default term of a function type".into()),
            Type::Interface(_, _) =>
                Err("attempting to take the default term of an interface".into()),
            Type::Oneself =>
                Err("attempting to take the default term of Self".into()),
            Type::Generic(_) =>
                Err("attempting to take the default term of a generic".into()),
        }
    }
}

impl core::fmt::Display for Expression {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Expression::Annotation { expr, kind } => write!(f, "({}: {})", expr, kind),
            Expression::Constant { term } => write!(f, "'{:?}", term),
            Expression::Variable { id } => write!(f, "{}", id),
            Expression::Abstraction { param, func } => write!(f, "(λ{}.{})", param, func),
            Expression::Application { func, arg } => write!(f, "({} {})", func, arg),
            Expression::Conditional { if_cond, if_then, if_else } => write!(f, "(if {} then {} else {})", if_cond, if_then, if_else),
        }
    }
}

impl core::fmt::Display for Type {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Type::Empty => write!(f, "⊤"),
            Type::Error => write!(f, "⊥"),
            Type::Unit => write!(f, "unit"),
            Type::Boolean => write!(f, "bool"),
            Type::Natural => write!(f, "nat"),
            Type::Integer => write!(f, "int"),
            Type::Float => write!(f, "float"),
            Type::String => write!(f, "str"),
            Type::List(data) => write!(f, "list[{}]", data),
            Type::Array(data, len) => write!(f, "array[{}, {}]", data, len),
            Type::Slice(data) => write!(f, "slice[{}]", data),
            Type::Union(data) => {
                write!(f, "union[")?;
                for (i, val) in data.iter().enumerate() {
                    write!(f, "{}", val)?;
                    if i != data.len() - 1 {
                        write!(f, ", ")?;
                    }
                }
                write!(f, "]")
            },
            Type::Struct(data) => {
                write!(f, "struct[")?;
                for (i, (key, val)) in data.iter().enumerate() {
                    write!(f, "{}: {}", key, val)?;
                    if i != data.len() - 1 {
                        write!(f, ", ")?;
                    }
                }
                write!(f, "]")
            },
            Type::Tuple(data, fields) => {
                write!(f, "tuple[")?;
                for (i, (val, ident)) in std::iter::zip(data, fields).enumerate() {
                    match ident {
                        Some(key) => write!(f, "{}: {}", key, val)?,
                        None => write!(f, "{}", val)?
                    }
                    if i != data.len() - 1 {
                        write!(f, ", ")?;
                    }
                }
                write!(f, "]")
            },
            Type::Function(from, to) => write!(f, "{}->{}", from, to),
            Type::Interface(data, kind) => {
                write!(f, "interface[")?;
                for (i, sig) in data.iter().enumerate() {
                    write!(f, "func {}({}): {}", sig.name, sig.from, sig.to)?;
                    if i != data.len() - 1 {
                        write!(f, ", ")?;
                    }
                }
                if let Some(data) = kind {
                    write!(f, " for {}", data)?;
                }
                write!(f, "]")
            },
            Type::Oneself => write!(f, "Self"),
            Type::Generic(data) =>  {
                write!(f, "generic[")?;
                if let Some(data) = data {
                    for (i, kind) in data.iter().enumerate() {
                        write!(f, "{}", kind)?;
                        if i != data.len() - 1 {
                            write!(f, ", ")?;
                        }
                    }
                }
                write!(f, "]")
            },
        }
    }
}

// hatehatehate that you can't implement a trait for foreign types
// let me impl display for vec god dammit
impl core::fmt::Display for Term {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Term::Unit() => write!(f, "∅"),
            Term::Boolean(term) => write!(f, "{}", term),
            Term::Natural(term) => write!(f, "{}", term),
            Term::Integer(term) => write!(f, "{}", term),
            Term::Float(term) => write!(f, "{}", term),
            Term::String(data) => write!(f, "\"{}\"", data),
            Term::List(data) => write!(f, "[{:?}]", data),
            Term::Array(data) => write!(f, "[{:?}]", data),
            Term::Union(data) => write!(f, "{{{:?}}}", data),
            Term::Struct(term) => write!(f, "{{{:?}}}", term),
            Term::Tuple(data, fields) => write!(f, "({:?})", data),
        }
    }
}

/// expose necessary functions for the underlying HashMaps
impl Context {
    pub fn new() -> Self {
        Context(HashMap::new(), HashMap::new())
    }
    pub fn get_term(&self, k: &Identifier) -> Option<&Term> {
        self.0.get(k)
    }
    pub fn insert_term(&mut self, k: Identifier, v: Term) -> Option<Term> {
        self.0.insert(k, v)
    }
    pub fn get_func(&self, k: &Signature) -> Option<&Expression> {
        self.1.get(k)
    }
    pub fn insert_func(&mut self, k: Signature, v: Expression) -> Option<Expression> {
        self.1.insert(k, v)
    }
    pub fn contains_term(&self, k: &Identifier) -> bool {
        self.0.contains_key(k)
    }
    pub fn contains_sig(&self, k: &Signature) -> bool {
        self.1.contains_key(k)
    }
}