aboutsummaryrefslogtreecommitdiff
path: root/src/type/check.rs
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-21 12:35:37 +0300
committerJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-21 12:35:37 +0300
commitd1c97e405230b6616ef834cf38be351e566a228e (patch)
tree7c75d9353b0b3cc9f08400c5d09e0b7552afc9c8 /src/type/check.rs
parenta70dcaa949f41c585f9aea5e79f2550053d8e857 (diff)
downloadmyslip-d1c97e405230b6616ef834cf38be351e566a228e.tar.gz
myslip-d1c97e405230b6616ef834cf38be351e566a228e.zip
feat: implemented coproducts according to tests
Diffstat (limited to 'src/type/check.rs')
-rw-r--r--src/type/check.rs25
1 files changed, 20 insertions, 5 deletions
diff --git a/src/type/check.rs b/src/type/check.rs
index 96a4408..5ccd0e7 100644
--- a/src/type/check.rs
+++ b/src/type/check.rs
@@ -193,8 +193,9 @@ impl SExp {
/// let exp = parse_to_ast(
/// "(let mysum (coprod Bool 7)) \
/// (case mysum \
- /// ((inl b) (if b 0 1)) \
- /// ((inr x) x))"
+ /// ((inl b) (case b (true 0) (_ 1))) \
+ /// ((inr x) x) \
+ /// (_ 0))"
/// ).unwrap();
/// assert_eq!(
/// exp.type_check(),
@@ -300,9 +301,9 @@ impl SExp {
vecof(vt("T")),
List(vec![VecType, vecof(vt("T"))])
)),
- Atom(Coprod) => todo!(),
- Atom(Inl) => todo!(),
- Atom(Inr) => todo!(),
+ Atom(Coprod) => Err(OtherError), // TODO
+ Atom(Inl) => Err(OtherError), // TODO
+ Atom(Inr) => Err(OtherError), // TODO
Atom(Let) => Ok(LetType),
Atom(Print) => Ok(arr(vt("_"), NilType)),
Atom(Ty(_)) => Ok(TypeLit),
@@ -379,6 +380,20 @@ impl SExp {
}
}
+ // Coproducts
+ if **op == Atom(Coprod) {
+ let mut parts = (**l).clone().parts().into_iter();
+ return match (parts.next(), parts.next(), parts.next()) {
+ (Some(Atom(Ty(ty))), Some(r), None)
+ => Ok(sumtype(ty, r.infer_type(ctx)?)),
+ (Some(l), Some(Atom(Ty(ty))), None)
+ => Ok(sumtype(l.infer_type(ctx)?, ty)),
+ (Some(l), Some(r), None)
+ => Err(SumDefNoTypeLiteral(scons(l, r))),
+ _ => Err(OtherError)
+ };
+ }
+
// Normal operation
let opertype = (*op).infer_type(ctx.clone())?;
let argstype = (*l).infer_list_type(ctx)?;