aboutsummaryrefslogtreecommitdiff
path: root/src/type/check.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/type/check.rs')
-rw-r--r--src/type/check.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/type/check.rs b/src/type/check.rs
index c0f4b1f..96a4408 100644
--- a/src/type/check.rs
+++ b/src/type/check.rs
@@ -60,6 +60,25 @@ impl SExp {
/// ```
/// ...so please don't ask what their type is.
///
+ /// **Sum types**
+ ///
+ /// Instantiation types:
+ /// ```rust
+ /// use myslip::{
+ /// r#type::{*, Type::*, TypeError::*, util::*},
+ /// sexp::{SExp::*, SLeaf::*, util::*},
+ /// };
+ /// assert_eq!(
+ /// scons(Coprod, scons(7, scons(Ty(Boolean), Nil))).type_check(),
+ /// Ok(sumtype(Integer, Boolean))
+ /// );
+ /// assert_eq!(
+ /// scons(Coprod, scons(Ty(Boolean), scons(False, Nil))).type_check(),
+ /// Ok(sumtype(Boolean, Boolean))
+ /// );
+ /// ```
+ /// Destructuring is tested in the tests of case.
+ ///
/// Some common operators get arrow types:
/// ```rust
/// use myslip::{
@@ -161,7 +180,28 @@ impl SExp {
/// Ok(Integer)
/// );
/// ```
+ ///
+ /// Here's a test on pattern matching on sum types:
+ /// ```rust
+ /// use myslip::{
+ /// r#type::{*, Type::*, TypeError::*, util::*},
+ /// sexp::{SExp::*, SLeaf::*, util::*},
+ /// parse::parsetree::parse_to_ast
+ /// };
///
+ /// fn main() {
+ /// let exp = parse_to_ast(
+ /// "(let mysum (coprod Bool 7)) \
+ /// (case mysum \
+ /// ((inl b) (if b 0 1)) \
+ /// ((inr x) x))"
+ /// ).unwrap();
+ /// assert_eq!(
+ /// exp.type_check(),
+ /// Ok(Integer)
+ /// );
+ /// }
+ /// ```
///
/// Though perhaps the most important task of the type system
/// is to increase safety by being able to warn about errors
@@ -260,6 +300,9 @@ impl SExp {
vecof(vt("T")),
List(vec![VecType, vecof(vt("T"))])
)),
+ Atom(Coprod) => todo!(),
+ Atom(Inl) => todo!(),
+ Atom(Inr) => todo!(),
Atom(Let) => Ok(LetType),
Atom(Print) => Ok(arr(vt("_"), NilType)),
Atom(Ty(_)) => Ok(TypeLit),