From d1c97e405230b6616ef834cf38be351e566a228e Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Thu, 21 Aug 2025 12:35:37 +0300 Subject: feat: implemented coproducts according to tests --- src/type/case.rs | 97 ++++++-------------------------------------------------- 1 file changed, 9 insertions(+), 88 deletions(-) (limited to 'src/type/case.rs') diff --git a/src/type/case.rs b/src/type/case.rs index d8871af..ae4eb77 100644 --- a/src/type/case.rs +++ b/src/type/case.rs @@ -120,99 +120,20 @@ impl SExp { _ => Err(OtherError) }, - _ => Err(OtherError), // maybe add nicer error? - - } - - // match (self, ty) { - - // (a, b) if a.infer_list_type(ctx.clone()) == Ok(b.clone()) => - // Ok(ctx.into_iter().collect()), - // (a, b) if a.infer_type(ctx.clone()) == Ok(b.clone()) => - // Ok(ctx.into_iter().collect()), - - // (Atom(Var(name)), t) => - // Ok(ctx.into_iter() - // .chain(iter::once((name, t))) - // .collect()), + (se, SumType(l, r)) => match (&(se.parts())[..], (*l, *r)) { + ([op, exp], (l, _)) if *op == Atom(Inl) => + exp.clone().matches_type_ctx(l, ctx.clone()), + ([op, exp], (_, r)) if *op == Atom(Inr) => + exp.clone().matches_type_ctx(r, ctx.clone()), + _ => Err(OtherError), + }, - // (exp, VecOf(ty)) => { - // let mut res: Vec<(String, Type)> = - // ctx.clone().into_iter().collect(); - // let mut exps = exp.clone().parts(); - // // TODO: Nil or empty exp - // let restpat = exps.remove(exps.len() - 1); - // for exp in exps { - // for et in exp.matches_type_ctx(*ty.clone(), ctx.clone())? { - // res.push(et); - // } - // } - // match restpat { - // Atom(RestPat(name)) => { - // res.push((name, vecof(ty))); - // Ok(res) - // }, - // t => { - // for et in t.matches_type_ctx(*ty.clone(), ctx.clone())? { - // res.push(et); - // } - // Ok(res) - // } - // } - // }, - // (SCons(e1, e2), List(typelist)) => { - // let explist = scons(e1.clone(), e2.clone()).parts(); - // let mut res: Vec<(String, Type)> = - // ctx.clone().into_iter().collect(); - // if explist.len() == typelist.len() { - // for (exp, ty) in explist.into_iter().zip(typelist) { - // for (e, t) in exp.matches_type_ctx(ty, ctx.clone())? { - // res.push((e, t)); - // } - // } - // Ok(res) - // } else { - // match explist.last().cloned() { - // Some(Atom(RestPat(name))) => { - // for (exp, ty) in explist.clone() - // .into_iter() - // .rev().skip(1).rev() - // .zip(typelist.clone()) - // { - // for (e, t) in exp.matches_type_ctx(ty, ctx.clone())? { - // res.push((e, t)); - // } - // } - // res.push(( - // name, - // List(typelist - // .into_iter() - // .skip(explist.len() - 1) - // .collect()) - // )); - // Ok(res) - // }, - // _ => Err(InvalidPattern(TypeMismatch { - // pattern: scons(e1.clone(), e2.clone()), - // expected: List(typelist), - // found: scons(e1, e2).infer_list_type(ctx)?, - // })), - // } - // } - // }, - // (e, t) => { - // let found_ty = e.infer_list_type(ctx)?; - // Err(InvalidPattern(TypeMismatch { - // pattern: e, - // expected: t, - // found: found_ty - // })) - // }, + _ => Err(OtherError), // maybe add nicer error? - // } + } } } -- cgit v1.2.3