Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builtin.headlist fix for assoclists #799

Merged
merged 2 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions crates/aiken-lang/src/gen_uplc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3770,14 +3770,25 @@ impl<'a> CodeGenerator<'a> {
builder::special_case_builtin(builtin, 0, vec![])
}

DefaultFunction::FstPair
| DefaultFunction::SndPair
| DefaultFunction::HeadList => builder::undata_builtin(
builtin,
0,
&constructor.tipo.return_type().unwrap(),
vec![],
),
DefaultFunction::FstPair | DefaultFunction::SndPair => {
builder::undata_builtin(
builtin,
0,
&constructor.tipo.return_type().unwrap(),
vec![],
)
}

DefaultFunction::HeadList
if !constructor.tipo.return_type().unwrap().is_2_tuple() =>
{
builder::undata_builtin(
builtin,
0,
&constructor.tipo.return_type().unwrap(),
vec![],
)
}

DefaultFunction::MkCons | DefaultFunction::MkPairData => {
unimplemented!("MkCons and MkPairData should be handled by an anon function or using [] or ( a, b, .., z).\n")
Expand Down Expand Up @@ -4164,9 +4175,11 @@ impl<'a> CodeGenerator<'a> {
builder::special_case_builtin(&func, count, arg_vec)
}

DefaultFunction::FstPair
| DefaultFunction::SndPair
| DefaultFunction::HeadList => {
DefaultFunction::FstPair | DefaultFunction::SndPair => {
builder::undata_builtin(&func, count, tipo, arg_vec)
}

DefaultFunction::HeadList if !tipo.is_2_tuple() => {
builder::undata_builtin(&func, count, tipo, arg_vec)
}

Expand Down
19 changes: 8 additions & 11 deletions crates/aiken-lang/src/gen_uplc/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1543,7 +1543,14 @@ pub fn special_case_builtin(
mut args: Vec<Term<Name>>,
) -> Term<Name> {
match func {
DefaultFunction::IfThenElse
DefaultFunction::ChooseUnit if count > 0 => {
let term = args.pop().unwrap();
let unit = args.pop().unwrap();

term.lambda("_").apply(unit)
}
DefaultFunction::ChooseUnit
| DefaultFunction::IfThenElse
| DefaultFunction::ChooseList
| DefaultFunction::ChooseData
| DefaultFunction::Trace => {
Expand Down Expand Up @@ -1580,16 +1587,6 @@ pub fn special_case_builtin(

term
}
DefaultFunction::ChooseUnit => {
if count == 0 {
unimplemented!("Honestly, why are you doing this?")
} else {
let term = args.pop().unwrap();
let unit = args.pop().unwrap();

term.lambda("_").apply(unit)
}
}
DefaultFunction::UnConstrData => {
let mut term: Term<Name> = (*func).into();

Expand Down
50 changes: 50 additions & 0 deletions crates/aiken-project/src/tests/gen_uplc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5557,6 +5557,56 @@ fn expect_none() {
);
}

#[test]
fn head_list_on_map() {
let src = r#"
use aiken/builtin

test exp_none() {
let x = [(1, ""), (2, #"aa")]
builtin.head_list(x) == (1, "")
}
"#;

assert_uplc(
src,
Term::equals_data()
.apply(
Term::map_data().apply(
Term::mk_cons()
.apply(Term::head_list().apply(Term::var("x")))
.apply(Term::empty_map()),
),
)
.apply(
Term::map_data().apply(
Term::mk_cons()
.apply(Term::pair_values(
Constant::Data(Data::integer(1.into())),
Constant::Data(Data::bytestring(vec![])),
))
.apply(Term::empty_map()),
),
)
.lambda("x")
.apply(Term::map_values(vec![
Constant::ProtoPair(
Type::Data,
Type::Data,
Constant::Data(Data::integer(1.into())).into(),
Constant::Data(Data::bytestring(vec![])).into(),
),
Constant::ProtoPair(
Type::Data,
Type::Data,
Constant::Data(Data::integer(2.into())).into(),
Constant::Data(Data::bytestring(vec![170])).into(),
),
])),
false,
);
}

#[test]
fn tuple_2_match() {
let src = r#"
Expand Down