Refactor AssignmentKind to allow backpassing on both let and expect.

The 3rd kind of assignment kind (Bind) is gone and now reflected through a boolean parameter. Note that this parameter is completely erased by the type-checker so that the rest of the pipeline (i.e. code-generation) doesn't have to make any assumption. They simply can't see a backpassing let or expect.
This commit is contained in:
KtorZ 2024-03-10 23:04:07 +01:00
parent df898bf239
commit 435dd0d213
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
27 changed files with 198 additions and 113 deletions

View File

@ -1436,31 +1436,59 @@ impl Default for Bls12_381Point {
}
}
pub type UntypedAssignmentKind = AssignmentKind<bool>;
pub type TypedAssignmentKind = AssignmentKind<()>;
#[derive(Debug, Clone, PartialEq, Eq, Copy, serde::Serialize, serde::Deserialize)]
pub enum AssignmentKind {
Let,
Bind,
Expect,
pub enum AssignmentKind<T> {
Let { backpassing: T },
Expect { backpassing: T },
}
impl AssignmentKind {
impl From<UntypedAssignmentKind> for TypedAssignmentKind {
fn from(kind: UntypedAssignmentKind) -> TypedAssignmentKind {
match kind {
AssignmentKind::Let { .. } => AssignmentKind::Let { backpassing: () },
AssignmentKind::Expect { .. } => AssignmentKind::Expect { backpassing: () },
}
}
}
impl<T> AssignmentKind<T> {
pub fn is_let(&self) -> bool {
matches!(self, AssignmentKind::Let)
matches!(self, AssignmentKind::Let { .. })
}
pub fn is_expect(&self) -> bool {
matches!(self, AssignmentKind::Expect)
matches!(self, AssignmentKind::Expect { .. })
}
pub fn location_offset(&self) -> usize {
match self {
AssignmentKind::Let => 3,
AssignmentKind::Bind => 3,
AssignmentKind::Expect => 6,
AssignmentKind::Let { .. } => 3,
AssignmentKind::Expect { .. } => 6,
}
}
}
impl AssignmentKind<bool> {
pub fn is_backpassing(&self) -> bool {
match self {
Self::Let { backpassing } | Self::Expect { backpassing } => *backpassing,
}
}
}
impl AssignmentKind<()> {
pub fn let_() -> Self {
AssignmentKind::Let { backpassing: () }
}
pub fn expect() -> Self {
AssignmentKind::Expect { backpassing: () }
}
}
pub type MultiPattern<PatternConstructor, Type> = Vec<Pattern<PatternConstructor, Type>>;
pub type UntypedMultiPattern = MultiPattern<(), ()>;

View File

@ -1,10 +1,10 @@
use crate::{
ast::{
self, Annotation, Arg, ArgName, AssignmentKind, BinOp, Bls12_381Point,
ByteArrayFormatPreference, CallArg, Curve, DataType, DataTypeKey, DefinitionLocation,
IfBranch, Located, LogicalOpChainKind, ParsedCallArg, Pattern, RecordConstructorArg,
RecordUpdateSpread, Span, TraceKind, TypedClause, TypedDataType, TypedRecordUpdateArg,
UnOp, UntypedClause, UntypedRecordUpdateArg,
self, Annotation, Arg, ArgName, BinOp, Bls12_381Point, ByteArrayFormatPreference, CallArg,
Curve, DataType, DataTypeKey, DefinitionLocation, IfBranch, Located, LogicalOpChainKind,
ParsedCallArg, Pattern, RecordConstructorArg, RecordUpdateSpread, Span, TraceKind,
TypedAssignmentKind, TypedClause, TypedDataType, TypedRecordUpdateArg, UnOp,
UntypedAssignmentKind, UntypedClause, UntypedRecordUpdateArg,
},
builtins::void,
parser::token::Base,
@ -106,7 +106,7 @@ pub enum TypedExpr {
tipo: Rc<Type>,
value: Box<Self>,
pattern: Pattern<PatternConstructor, Rc<Type>>,
kind: AssignmentKind,
kind: TypedAssignmentKind,
},
Trace {
@ -519,7 +519,7 @@ pub enum UntypedExpr {
location: Span,
value: Box<Self>,
pattern: Pattern<(), ()>,
kind: AssignmentKind,
kind: UntypedAssignmentKind,
annotation: Option<Annotation>,
},

View File

@ -4,9 +4,9 @@ use crate::{
CallArg, ClauseGuard, Constant, CurveType, DataType, Definition, Function, IfBranch,
LogicalOpChainKind, ModuleConstant, Pattern, RecordConstructor, RecordConstructorArg,
RecordUpdateSpread, Span, TraceKind, TypeAlias, TypedArg, UnOp, UnqualifiedImport,
UntypedArg, UntypedArgVia, UntypedClause, UntypedClauseGuard, UntypedDefinition,
UntypedFunction, UntypedModule, UntypedPattern, UntypedRecordUpdateArg, Use, Validator,
CAPTURE_VARIABLE,
UntypedArg, UntypedArgVia, UntypedAssignmentKind, UntypedClause, UntypedClauseGuard,
UntypedDefinition, UntypedFunction, UntypedModule, UntypedPattern, UntypedRecordUpdateArg,
Use, Validator, CAPTURE_VARIABLE,
},
docvec,
expr::{FnStyle, UntypedExpr, DEFAULT_ERROR_STR, DEFAULT_TODO_STR},
@ -681,15 +681,16 @@ impl<'comments> Formatter<'comments> {
&mut self,
pattern: &'a UntypedPattern,
value: &'a UntypedExpr,
kind: AssignmentKind,
kind: UntypedAssignmentKind,
annotation: &'a Option<Annotation>,
) -> Document<'a> {
let (keyword, equal) = match kind {
AssignmentKind::Let => ("let", "="),
AssignmentKind::Bind => ("let", "<-"),
AssignmentKind::Expect => ("expect", "="),
let keyword = match kind {
AssignmentKind::Let { .. } => "let",
AssignmentKind::Expect { .. } => "expect",
};
let symbol = if kind.is_backpassing() { "<-" } else { "=" };
match pattern {
UntypedPattern::Constructor {
name, module: None, ..
@ -710,7 +711,7 @@ impl<'comments> Formatter<'comments> {
.append(" ")
.append(pattern.append(annotation).group())
.append(" ")
.append(equal)
.append(symbol)
.append(self.case_clause_value(value))
}
}

View File

@ -559,7 +559,7 @@ impl<'a> CodeGenerator<'a> {
&subject_type,
AssignmentProperties {
value_type: subject.tipo(),
kind: AssignmentKind::Let,
kind: AssignmentKind::let_(),
remove_unused: false,
full_check: false,
msg_func: None,
@ -843,7 +843,7 @@ impl<'a> CodeGenerator<'a> {
) -> AirTree {
assert!(
match &value {
AirTree::Var { name, .. } if props.kind == AssignmentKind::Let => {
AirTree::Var { name, .. } if props.kind.is_let() => {
name != "_"
}
_ => true,
@ -2812,7 +2812,7 @@ impl<'a> CodeGenerator<'a> {
&actual_type,
AssignmentProperties {
value_type: data(),
kind: AssignmentKind::Expect,
kind: AssignmentKind::expect(),
remove_unused: false,
full_check: true,
msg_func,

View File

@ -4,8 +4,8 @@ use super::{
};
use crate::{
ast::{
AssignmentKind, BinOp, ClauseGuard, Constant, DataTypeKey, FunctionAccessKey, Pattern,
Span, TraceLevel, TypedArg, TypedClause, TypedClauseGuard, TypedDataType, TypedPattern,
BinOp, ClauseGuard, Constant, DataTypeKey, FunctionAccessKey, Pattern, Span, TraceLevel,
TypedArg, TypedAssignmentKind, TypedClause, TypedClauseGuard, TypedDataType, TypedPattern,
UnOp,
},
builtins::{bool, data, function, int, list, void},
@ -68,7 +68,7 @@ pub enum HoistableFunction {
#[derive(Clone, Debug)]
pub struct AssignmentProperties {
pub value_type: Rc<Type>,
pub kind: AssignmentKind,
pub kind: TypedAssignmentKind,
pub remove_unused: bool,
pub full_check: bool,
pub msg_func: Option<AirMsg>,

View File

@ -24,7 +24,9 @@ Test(
with_spread: false,
tipo: (),
},
kind: Expect,
kind: Expect {
backpassing: false,
},
annotation: None,
},
Var {

View File

@ -29,7 +29,9 @@ Fn(
location: 17..18,
name: "x",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
doc: None,

View File

@ -22,10 +22,8 @@ pub fn let_(
location: span,
value: Box::new(value),
pattern,
kind: if kind == Token::LArrow {
ast::AssignmentKind::Bind
} else {
ast::AssignmentKind::Let
kind: ast::AssignmentKind::Let {
backpassing: kind == Token::LArrow,
},
annotation,
}
@ -39,24 +37,27 @@ pub fn expect(
.ignore_then(
pattern()
.then(just(Token::Colon).ignore_then(annotation()).or_not())
.then_ignore(just(Token::Equal))
.then(choice((just(Token::Equal), just(Token::LArrow))))
.or_not(),
)
.then(r.clone())
.validate(move |(opt_pattern, value), span, emit| {
let (pattern, annotation) = opt_pattern.unwrap_or_else(|| {
let ((pattern, annotation), kind) = opt_pattern.unwrap_or_else(|| {
(
ast::UntypedPattern::Constructor {
is_record: false,
location: span,
name: "True".to_string(),
arguments: vec![],
module: None,
constructor: (),
with_spread: false,
tipo: (),
},
None,
(
ast::UntypedPattern::Constructor {
is_record: false,
location: span,
name: "True".to_string(),
arguments: vec![],
module: None,
constructor: (),
with_spread: false,
tipo: (),
},
None,
),
Token::Equal,
)
});
@ -68,7 +69,9 @@ pub fn expect(
location: span,
value: Box::new(value),
pattern,
kind: ast::AssignmentKind::Expect,
kind: ast::AssignmentKind::Expect {
backpassing: kind == Token::LArrow,
},
annotation,
}
})

View File

@ -20,7 +20,9 @@ Assignment {
location: 16..17,
name: "x",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
BinOp {
@ -44,6 +46,8 @@ Assignment {
location: 4..5,
name: "b",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
}

View File

@ -31,6 +31,8 @@ Assignment {
with_spread: false,
tipo: (),
},
kind: Expect,
kind: Expect {
backpassing: false,
},
annotation: None,
}

View File

@ -30,6 +30,8 @@ Assignment {
with_spread: false,
tipo: (),
},
kind: Expect,
kind: Expect {
backpassing: false,
},
annotation: None,
}

View File

@ -20,7 +20,9 @@ Assignment {
location: 13..14,
name: "a",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
],
@ -35,6 +37,8 @@ Assignment {
with_spread: false,
tipo: (),
},
kind: Expect,
kind: Expect {
backpassing: false,
},
annotation: None,
}

View File

@ -20,7 +20,9 @@ Assignment {
location: 14..15,
name: "b",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
],
@ -29,6 +31,8 @@ Assignment {
location: 4..5,
name: "a",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
}

View File

@ -20,7 +20,9 @@ Assignment {
location: 14..15,
name: "b",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
],
@ -29,6 +31,8 @@ Assignment {
location: 4..5,
name: "a",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
}

View File

@ -20,7 +20,9 @@ Assignment {
location: 16..17,
name: "b",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
Var {
@ -33,6 +35,8 @@ Assignment {
location: 4..5,
name: "a",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
}

View File

@ -21,6 +21,8 @@ Assignment {
with_spread: false,
tipo: (),
},
kind: Expect,
kind: Expect {
backpassing: false,
},
annotation: None,
}

View File

@ -31,7 +31,9 @@ Sequence {
location: 4..5,
name: "x",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
Assignment {
@ -115,7 +117,9 @@ Sequence {
location: 24..33,
name: "map_add_x",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
Call {

View File

@ -18,7 +18,9 @@ Sequence {
location: 8..9,
name: "i",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
Assignment {
@ -34,7 +36,9 @@ Sequence {
location: 28..29,
name: "j",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
Assignment {
@ -54,7 +58,9 @@ Sequence {
location: 48..49,
name: "k",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
],

View File

@ -32,6 +32,8 @@ Assignment {
location: 4..9,
name: "thing",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
}

View File

@ -44,7 +44,9 @@ Sequence {
location: 4..9,
name: "tuple",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
BinOp {

View File

@ -31,7 +31,9 @@ Sequence {
location: 4..5,
name: "a",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
Tuple {

View File

@ -89,7 +89,9 @@ When {
location: 55..62,
name: "amazing",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
Var {

View File

@ -23,7 +23,9 @@ Module {
location: 19..20,
name: "a",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
UInt {
@ -61,7 +63,9 @@ Module {
location: 56..57,
name: "a",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
UInt {
@ -110,7 +114,9 @@ Module {
location: 93..94,
name: "a",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
doc: None,
@ -155,7 +161,9 @@ Module {
location: 126..127,
name: "a",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
BinOp {

View File

@ -28,7 +28,9 @@ Module {
location: 17..18,
name: "x",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
Var {

View File

@ -26,7 +26,9 @@ Module {
location: 17..18,
name: "x",
},
kind: Let,
kind: Let {
backpassing: false,
},
annotation: None,
},
Var {

View File

@ -12,8 +12,8 @@ use crate::{
ByteArrayFormatPreference, CallArg, ClauseGuard, Constant, Curve, IfBranch,
LogicalOpChainKind, Pattern, RecordUpdateSpread, Span, TraceKind, TraceLevel, Tracing,
TypedArg, TypedCallArg, TypedClause, TypedClauseGuard, TypedIfBranch, TypedPattern,
TypedRecordUpdateArg, UnOp, UntypedArg, UntypedClause, UntypedClauseGuard, UntypedIfBranch,
UntypedPattern, UntypedRecordUpdateArg,
TypedRecordUpdateArg, UnOp, UntypedArg, UntypedAssignmentKind, UntypedClause,
UntypedClauseGuard, UntypedIfBranch, UntypedPattern, UntypedRecordUpdateArg,
},
builtins::{
bool, byte_array, function, g1_element, g2_element, int, list, string, tuple, void,
@ -916,7 +916,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
&mut self,
untyped_pattern: UntypedPattern,
untyped_value: UntypedExpr,
kind: AssignmentKind,
kind: UntypedAssignmentKind,
annotation: &Option<Annotation>,
location: Span,
) -> Result<TypedExpr, Error> {
@ -978,16 +978,12 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
// If `expect` is explicitly used, we still check exhaustiveness but instead of returning an
// error we emit a warning which explains that using `expect` is unnecessary.
match kind {
AssignmentKind::Bind => {
unreachable!("monadic-bind should have been desugared earlier.")
}
AssignmentKind::Let => {
AssignmentKind::Let { .. } => {
self.environment
.check_exhaustiveness(&[&pattern], location, true)?
}
AssignmentKind::Expect => {
AssignmentKind::Expect { .. } => {
let is_exaustive_pattern = self
.environment
.check_exhaustiveness(&[&pattern], location, false)
@ -1007,7 +1003,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
location: Span::empty(),
value: Box::new(untyped_value),
pattern: untyped_pattern,
kind: AssignmentKind::Let,
kind: AssignmentKind::Let { backpassing: false },
annotation: None,
},
});
@ -1018,7 +1014,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
Ok(TypedExpr::Assignment {
location,
tipo: value_typ,
kind,
kind: kind.into(),
pattern,
value: Box::new(typed_value),
})
@ -1739,7 +1735,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
}
.into(),
pattern,
kind: AssignmentKind::Let,
kind: AssignmentKind::Let { backpassing: false },
annotation,
}];
with_assignment.extend(continuation);
@ -1830,15 +1826,15 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
let mut prefix = Vec::with_capacity(untyped.len());
let mut suffix = Vec::with_capacity(untyped.len());
for expression in untyped.into_iter() {
match expression {
_ if breakpoint.is_some() => suffix.push(expression),
UntypedExpr::Assignment {
kind: AssignmentKind::Bind,
..
} => {
breakpoint = Some(expression);
if breakpoint.is_some() {
suffix.push(expression);
} else {
match expression {
UntypedExpr::Assignment { kind, .. } if kind.is_backpassing() => {
breakpoint = Some(expression);
}
_ => prefix.push(expression),
}
_ => prefix.push(expression),
}
}
if let Some(breakpoint) = breakpoint {
@ -2130,7 +2126,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
location: Span::empty(),
value: Box::new(subject.clone()),
pattern: clauses[0].patterns[0].clone(),
kind: AssignmentKind::Let,
kind: AssignmentKind::Let { backpassing: false },
annotation: None,
},
});
@ -2258,7 +2254,7 @@ fn assert_assignment(expr: TypedExpr) -> Result<TypedExpr, Error> {
with_spread: false,
tipo: void(),
},
kind: AssignmentKind::Let,
kind: AssignmentKind::let_(),
});
}

View File

@ -1,18 +1,15 @@
use std::{ops::Deref, rc::Rc};
use vec1::Vec1;
use crate::{
ast::{AssignmentKind, CallArg, Pattern, Span, PIPE_VARIABLE},
builtins::function,
expr::{TypedExpr, UntypedExpr},
};
use super::{
error::{Error, UnifyErrorSituation},
expr::ExprTyper,
Type, ValueConstructor, ValueConstructorVariant,
};
use crate::{
ast::{AssignmentKind, CallArg, Pattern, Span, PIPE_VARIABLE},
builtins::function,
expr::{TypedExpr, UntypedExpr},
};
use std::{ops::Deref, rc::Rc};
use vec1::Vec1;
#[derive(Debug)]
pub(crate) struct PipeTyper<'a, 'b, 'c> {
@ -184,7 +181,7 @@ impl<'a, 'b, 'c> PipeTyper<'a, 'b, 'c> {
let assignment = TypedExpr::Assignment {
location,
tipo: expression.tipo(),
kind: AssignmentKind::Let,
kind: AssignmentKind::let_(),
value: Box::new(expression),
pattern: Pattern::Var {
location,