Skip to content

Commit

Permalink
Inline now handles (if cond then body else error) patterns. This allo…
Browse files Browse the repository at this point in the history
…ws conditions like expect x == 1 to match performance with x == 1 && ...
  • Loading branch information
MicroProofs committed Jan 9, 2025
1 parent b3de1b0 commit 9257f9e
Show file tree
Hide file tree
Showing 4 changed files with 298 additions and 127 deletions.
4 changes: 3 additions & 1 deletion crates/aiken-lang/src/gen_uplc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,11 @@ impl<'a> CodeGenerator<'a> {

fn finalize(&mut self, mut term: Term<Name>) -> Program<Name> {
term = self.special_functions.apply_used_functions(term);

println!("PROG BEFORE IS {}", term.to_pretty());
let program = aiken_optimize_and_intern(self.new_program(term));

println!("PROG IS {}", program.to_pretty());

// This is very important to call here.
// If this isn't done, re-using the same instance
// of the generator will result in free unique errors
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/aiken-project/src/export.rs
description: "Code:\n\npub type Foo<a> {\n Empty\n Bar(a, Foo<a>)\n}\n\npub fn add(a: Foo<Int>, b: Foo<Int>) -> Int {\n when (a, b) is {\n (Empty, Empty) -> 0\n (Bar(x, y), Bar(c, d)) -> x + c + add(y, d)\n (Empty, Bar(c, d)) -> c + add(Empty, d)\n (Bar(x, y), Empty) -> x + add(y, Empty)\n }\n}\n"
snapshot_kind: text
---
{
"name": "test_module.add",
Expand All @@ -25,8 +24,8 @@ snapshot_kind: text
"$ref": "#/definitions/Int"
}
},
"compiledCode": "59017d0101003232323232322232323232325333008300430093754002264a666012600a60146ea800452000132337006eb4c038004cc011300103d8798000300e300f001300b37540026018601a00a264a66601266e1d2002300a37540022646466e00cdc01bad300f002375a601e0026600a601e6020004601e602000260186ea8008c02cdd500109919b80375a601c00266008601c601e002980103d8798000300b37540046018601a00a601600860020024446464a666014600c60166ea80044c94ccc02cc01cc030dd50008a400026466e00dd69808000999803803a60103d879800030103011001300d3754002601c601e004264a66601666e1d2002300c37540022646466e00cdc01bad3011002375a60220026660100106022602400460226024002601c6ea8008c034dd500109919b80375a602000266600e00e60206022002980103d8798000300d3754004601c601e004601a002660160046601600297ae0370e90001980300119803000a5eb815cd2ab9d5573cae815d0aba201",
"hash": "c6af3f04e300cb8c1d0429cc0d8e56a0413eef9fcb338f72076b426c",
"compiledCode": "59017d0101003333332222222232323232325333008300430093754002264a666012600a60146ea800452000132337006eb4c038004cc011300103d8798000300e300f001300b37540026018601a00a264a66601266e1d2002300a37540022646466e00cdc01bad300f002375a601e0026600a601e6020004601e602000260186ea8008c02cdd500109919b80375a601c00266008601c601e002980103d8798000300b37540046018601a00a601600860020024446464a666014600c60166ea80044c94ccc02cc01cc030dd50008a400026466e00dd69808000999803803a60103d879800030103011001300d3754002601c601e004264a66601666e1d2002300c37540022646466e00cdc01bad3011002375a60220026660100106022602400460226024002601c6ea8008c034dd500109919b80375a602000266600e00e60206022002980103d8798000300d3754004601c601e004601a002660160046601600297ae0370e90001980300119803000a5eb815d12ba15740aae7955ceab9a01",
"hash": "1f7ba1be8ac6bba7b61d818e0f274b1feae61d24737dd9e833d59430",
"definitions": {
"Int": {
"dataType": "integer"
Expand Down
23 changes: 12 additions & 11 deletions crates/aiken-project/src/tests/gen_uplc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ fn assert_uplc(source_code: &str, expected: Term<Name>, should_fail: bool, verbo
version: (1, 1, 0),
term: expected,
};
println!("BEFORE OPT IS {}", expected.to_pretty());

let expected = optimize::aiken_optimize_and_intern(expected);

Expand Down Expand Up @@ -3603,7 +3604,7 @@ fn when_bool_is_true() {
assert_uplc(
src,
Term::var("subject")
.delayed_if_then_else(Term::bool(true), Term::Error)
.delayed_if_then_else(Term::bool(true), Term::Error.delay().force())
.lambda("subject")
.apply(Term::bool(true)),
false,
Expand All @@ -3627,7 +3628,7 @@ fn when_bool_is_true_switched_cases() {
assert_uplc(
src,
Term::var("subject")
.delayed_if_then_else(Term::bool(true), Term::Error)
.delayed_if_then_else(Term::bool(true), Term::Error.delay().force())
.lambda("subject")
.apply(Term::bool(true)),
false,
Expand All @@ -3651,7 +3652,7 @@ fn when_bool_is_false() {
assert_uplc(
src,
Term::var("subject")
.delayed_if_then_else(Term::bool(true), Term::Error)
.delayed_if_then_else(Term::bool(true), Term::Error.delay().force())
.lambda("subject")
.apply(Term::bool(false)),
true,
Expand Down Expand Up @@ -4088,16 +4089,16 @@ fn generic_validator_type_test() {
Term::tail_list()
.apply(Term::Var(tail_id_5.clone()))
.as_var("tail_id_6", |tail_id_6| {
Term::head_list()
Term::tail_list()
.apply(Term::Var(tail_id_6.clone()))
.as_var("__val", |val| {
Term::tail_list()
.delayed_choose_list(
Term::head_list()
.apply(Term::Var(tail_id_6))
.delayed_choose_list(
expect_b(val, Term::Var(then_delayed), trace),
Term::Error,
)
})
.as_var("__val", |val| {
expect_b(val, Term::Var(then_delayed), trace)
}),
Term::Error,
)
})
}
});
Expand Down
Loading

0 comments on commit 9257f9e

Please sign in to comment.