-
Notifications
You must be signed in to change notification settings - Fork 7
/
stlc.rs
228 lines (206 loc) · 6.86 KB
/
stlc.rs
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
//! An example of using the `moniker` library to implement the simply typed
//! lambda calculus with literals
//!
//! We use [bidirectional type checking](http://www.davidchristiansen.dk/tutorials/bidirectional.pdf)
//! to get some level of type inference.
extern crate im;
#[macro_use]
extern crate moniker;
use im::HashMap;
use moniker::{Binder, BoundTerm, Embed, FreeVar, Scope, Var};
use std::rc::Rc;
/// Types
///
/// ```text
/// t ::= Int integer types
/// | Float floating point types
/// | String string types
/// | t -> t function types
/// ```
#[derive(Debug, Clone, BoundTerm)]
pub enum Type {
/// Integers
Int,
/// Floating point numbers
Float,
/// Strings
String,
/// Function types
Arrow(RcType, RcType),
}
/// Reference counted types
#[derive(Debug, Clone, BoundTerm)]
pub struct RcType {
pub inner: Rc<Type>,
}
impl From<Type> for RcType {
fn from(src: Type) -> RcType {
RcType {
inner: Rc::new(src),
}
}
}
/// Literal values
#[derive(Debug, Clone, BoundTerm)]
pub enum Literal {
/// Integer literals
Int(i32),
/// Floating point literals
Float(f32),
/// String literals
String(String),
}
/// Expressions
///
/// ```text
/// e ::= x variables
/// | e : t expressions annotated with types
/// | \x => e anonymous functions
/// | \x : t => e anonymous functions (with type annotation)
/// | e₁ e₂ function application
/// ```
#[derive(Debug, Clone, BoundTerm)]
pub enum Expr {
/// Annotated expressions
Ann(RcExpr, RcType),
/// Literals
Literal(Literal),
/// Variables
Var(Var<String>),
/// Lambda expressions, with an optional type annotation for the parameter
Lam(Scope<(Binder<String>, Embed<Option<RcType>>), RcExpr>),
/// Function application
App(RcExpr, RcExpr),
}
/// Reference counted expressions
#[derive(Debug, Clone, BoundTerm)]
pub struct RcExpr {
pub inner: Rc<Expr>,
}
impl From<Expr> for RcExpr {
fn from(src: Expr) -> RcExpr {
RcExpr {
inner: Rc::new(src),
}
}
}
impl RcExpr {
// FIXME: auto-derive this somehow!
fn subst<N: PartialEq<Var<String>>>(&self, name: &N, replacement: &RcExpr) -> RcExpr {
match *self.inner {
Expr::Ann(ref expr, ref ty) => {
RcExpr::from(Expr::Ann(expr.subst(name, replacement), ty.clone()))
},
Expr::Var(ref var) if name == var => replacement.clone(),
Expr::Var(_) | Expr::Literal(_) => self.clone(),
Expr::Lam(ref scope) => RcExpr::from(Expr::Lam(Scope {
unsafe_pattern: scope.unsafe_pattern.clone(),
unsafe_body: scope.unsafe_body.subst(name, replacement),
})),
Expr::App(ref fun, ref arg) => RcExpr::from(Expr::App(
fun.subst(name, replacement),
arg.subst(name, replacement),
)),
}
}
}
/// Evaluate an expression into its normal form
pub fn eval(expr: &RcExpr) -> RcExpr {
match *expr.inner {
Expr::Ann(ref expr, _) => eval(expr),
Expr::Literal(_) | Expr::Var(_) | Expr::Lam(_) => expr.clone(),
Expr::App(ref fun, ref arg) => match *eval(fun).inner {
Expr::Lam(ref scope) => {
let ((binder, _), body) = scope.clone().unbind();
eval(&body.subst(&binder, &eval(arg)))
},
_ => expr.clone(),
},
}
}
/// A context containing a series of type annotations
type Context = HashMap<FreeVar<String>, RcType>;
/// Check that a (potentially ambiguous) expression conforms to a given type
pub fn check(context: &Context, expr: &RcExpr, expected_ty: &RcType) -> Result<(), String> {
match (&*expr.inner, &*expected_ty.inner) {
(&Expr::Lam(ref scope), &Type::Arrow(ref param_ty, ref ret_ty)) => {
if let ((Binder(free_var), Embed(None)), body) = scope.clone().unbind() {
check(&context.update(free_var, param_ty.clone()), &body, ret_ty)?;
return Ok(());
}
},
(_, _) => {},
}
let inferred_ty = infer(&context, expr)?;
if RcType::term_eq(&inferred_ty, expected_ty) {
Ok(())
} else {
Err(format!(
"type mismatch - found `{:?}` but expected `{:?}`",
inferred_ty, expected_ty
))
}
}
/// Synthesize the types of unambiguous expressions
pub fn infer(context: &Context, expr: &RcExpr) -> Result<RcType, String> {
match *expr.inner {
Expr::Ann(ref expr, ref ty) => {
check(context, expr, ty)?;
Ok(ty.clone())
},
Expr::Literal(Literal::Int(_)) => Ok(RcType::from(Type::Int)),
Expr::Literal(Literal::Float(_)) => Ok(RcType::from(Type::Float)),
Expr::Literal(Literal::String(_)) => Ok(RcType::from(Type::String)),
Expr::Var(Var::Free(ref free_var)) => match context.get(free_var) {
Some(term) => Ok((*term).clone()),
None => Err(format!("`{}` not found in `{:?}`", free_var, context)),
},
Expr::Var(Var::Bound(ref bound_var)) => {
panic!("encountered a bound variable: {}", bound_var)
},
Expr::Lam(ref scope) => match scope.clone().unbind() {
((Binder(free_var), Embed(Some(ann))), body) => {
let body_ty = infer(&context.update(free_var, ann.clone()), &body)?;
Ok(RcType::from(Type::Arrow(ann, body_ty)))
},
((binder, Embed(None)), _) => Err(format!(
"type annotation needed for parameter `{:?}`",
binder
)),
},
Expr::App(ref fun, ref arg) => match *infer(context, fun)?.inner {
Type::Arrow(ref param_ty, ref ret_ty) => {
let arg_ty = infer(context, arg)?;
if RcType::term_eq(param_ty, &arg_ty) {
Ok(ret_ty.clone())
} else {
Err(format!(
"argument type mismatch - found `{:?}` but expected `{:?}`",
arg_ty, param_ty,
))
}
},
_ => Err(format!("`{:?}` is not a function", fun)),
},
}
}
#[test]
fn test_infer() {
use moniker::FreeVar;
let x = FreeVar::fresh_named("x");
// expr = (\x : Int -> x)
let expr = RcExpr::from(Expr::Lam(Scope::new(
(Binder(x.clone()), Embed(Some(RcType::from(Type::Int)))),
RcExpr::from(Expr::Var(Var::Free(x.clone()))),
)));
assert_term_eq!(
infer(&Context::new(), &expr).unwrap(),
RcType::from(Type::Arrow(
RcType::from(Type::Int),
RcType::from(Type::Int)
)),
);
}
// TODO: Use property testing for this!
// http://janmidtgaard.dk/papers/Midtgaard-al%3AICFP17-full.pdf
fn main() {}