From 281ce68b3ff0605ed2e07c257a13deff691e2b1a Mon Sep 17 00:00:00 2001 From: Alan Lawrence Date: Mon, 10 Jul 2023 16:43:37 +0100 Subject: [PATCH] fmt --- src/hugr/typecheck.rs | 56 ++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/src/hugr/typecheck.rs b/src/hugr/typecheck.rs index 5f8d4d66f..781234391 100644 --- a/src/hugr/typecheck.rs +++ b/src/hugr/typecheck.rs @@ -102,34 +102,42 @@ pub fn typecheck_const(typ: &ClassicType, val: &ConstValue) -> Result<(), ConstT } (ClassicType::F64, ConstValue::F64(_)) => Ok(()), (ty @ ClassicType::Container(c), tm) => match c { - Container::Tuple(row) => if let ConstValue::Tuple(xs) = tm { - if row.len() != xs.len() { - return Err(ConstTypeError::TupleWrongLength); + Container::Tuple(row) => { + if let ConstValue::Tuple(xs) = tm { + if row.len() != xs.len() { + return Err(ConstTypeError::TupleWrongLength); + } + for (ty, tm) in row.iter().zip(xs.iter()) { + match ty { + SimpleType::Classic(ty) => typecheck_const(ty, tm)?, + _ => return Err(ConstTypeError::LinearTypeDisallowed), + } + } + Ok(()) + } else { + Err(ConstTypeError::Failed(ty.clone())) } - for (ty, tm) in row.iter().zip(xs.iter()) { + } + Container::Sum(row) => { + if let ConstValue::Sum { tag, variants, val } = tm { + if tag > &row.len() { + return Err(ConstTypeError::InvalidSumTag); + } + if **row != *variants { + return Err(ConstTypeError::TypeRowMismatch( + *row.clone(), + variants.clone(), + )); + } + let ty = variants.get(*tag).unwrap(); match ty { - SimpleType::Classic(ty) => typecheck_const(ty, tm)?, - _ => return Err(ConstTypeError::LinearTypeDisallowed), + SimpleType::Classic(ty) => typecheck_const(ty, val.as_ref()), + _ => Err(ConstTypeError::LinearTypeDisallowed), } + } else { + Err(ConstTypeError::Failed(ty.clone())) } - Ok(()) - } else {Err(ConstTypeError::Failed(ty.clone()))}, - Container::Sum(row) => if let ConstValue::Sum { tag, variants, val } = tm { - if tag > &row.len() { - return Err(ConstTypeError::InvalidSumTag); - } - if **row != *variants { - return Err(ConstTypeError::TypeRowMismatch( - *row.clone(), - variants.clone(), - )); - } - let ty = variants.get(*tag).unwrap(); - match ty { - SimpleType::Classic(ty) => typecheck_const(ty, val.as_ref()), - _ => Err(ConstTypeError::LinearTypeDisallowed), - } - } else {Err(ConstTypeError::Failed(ty.clone()))}, + } _ => Err(ConstTypeError::Unimplemented(ty.clone())), }, (ty @ ClassicType::Graph(_), _) => Err(ConstTypeError::Unimplemented(ty.clone())),