Skip to content
Draft
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
3 changes: 3 additions & 0 deletions plutus-tx-plugin/test/Budget/9.6/guardIfErrorPath.golden.eval
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
An error has occurred:
The machine terminated because of an error, either from a built-in function or from an explicit use of 'error'.
Caused by: error
29 changes: 29 additions & 0 deletions plutus-tx-plugin/test/Budget/9.6/guardIfErrorPath.golden.pir
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
let
data Unit | Unit_match where
Unit : Unit
!traceError : all a. string -> a
= /\a ->
\(str : string) -> let !x : Unit = trace {Unit} str Unit in error {a}
in
\(x : integer) (y : integer) ->
case
(all dead. bool)
(case bool (lessThanEqualsInteger x 10) [True, False])
[ (/\dead ->
case
(all dead. bool)
(lessThanInteger y 0)
[ (/\dead ->
case
(all dead. bool)
(case
bool
(lessThanEqualsInteger (addInteger x y) 20)
[True, False])
[ (/\dead -> True)
, (/\dead -> traceError {bool} "sum too large") ]
{all dead. dead})
, (/\dead -> traceError {bool} "y negative") ]
{all dead. dead})
, (/\dead -> traceError {bool} "x too large") ]
{all dead. dead}
16 changes: 16 additions & 0 deletions plutus-tx-plugin/test/Budget/9.6/guardIfErrorPath.golden.uplc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(program
1.1.0
(\x y ->
force
((\traceError ->
case
(lessThanEqualsInteger x 10)
[ (delay (traceError "x too large"))
, (delay
(case
(lessThanInteger y 0)
[ (case
(lessThanEqualsInteger (addInteger x y) 20)
[(traceError "sum too large"), True])
, (traceError "y negative") ])) ])
(\str -> (\x -> error) (force trace str (constr 0 []))))))
6 changes: 6 additions & 0 deletions plutus-tx-plugin/test/Budget/9.6/guardIfHappyPath.golden.eval
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CPU: 778_272
Memory: 3_505
AST Size: 53
Flat Size: 101

(con bool True)
29 changes: 29 additions & 0 deletions plutus-tx-plugin/test/Budget/9.6/guardIfHappyPath.golden.pir
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
let
data Unit | Unit_match where
Unit : Unit
!traceError : all a. string -> a
= /\a ->
\(str : string) -> let !x : Unit = trace {Unit} str Unit in error {a}
in
\(x : integer) (y : integer) ->
case
(all dead. bool)
(case bool (lessThanEqualsInteger x 10) [True, False])
[ (/\dead ->
case
(all dead. bool)
(lessThanInteger y 0)
[ (/\dead ->
case
(all dead. bool)
(case
bool
(lessThanEqualsInteger (addInteger x y) 20)
[True, False])
[ (/\dead -> True)
, (/\dead -> traceError {bool} "sum too large") ]
{all dead. dead})
, (/\dead -> traceError {bool} "y negative") ]
{all dead. dead})
, (/\dead -> traceError {bool} "x too large") ]
{all dead. dead}
16 changes: 16 additions & 0 deletions plutus-tx-plugin/test/Budget/9.6/guardIfHappyPath.golden.uplc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(program
1.1.0
(\x y ->
force
((\traceError ->
case
(lessThanEqualsInteger x 10)
[ (delay (traceError "x too large"))
, (delay
(case
(lessThanInteger y 0)
[ (case
(lessThanEqualsInteger (addInteger x y) 20)
[(traceError "sum too large"), True])
, (traceError "y negative") ])) ])
(\str -> (\x -> error) (force trace str (constr 0 []))))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
An error has occurred:
The machine terminated because of an error, either from a built-in function or from an explicit use of 'error'.
Caused by: error
31 changes: 31 additions & 0 deletions plutus-tx-plugin/test/Budget/9.6/handOptIfErrorPath.golden.pir
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
let
!ifThenElse : all a. bool -> a -> a -> a
= /\a -> \(b : bool) (x : a) (y : a) -> case a b [y, x]
!greaterThanInteger : integer -> integer -> bool
= \(x : integer) (y : integer) ->
ifThenElse {bool} (lessThanEqualsInteger x y) False True
data Unit | Unit_match where
Unit : Unit
!traceError : all a. string -> a
= /\a ->
\(str : string) -> let !x : Unit = trace {Unit} str Unit in error {a}
in
\(x : integer) (y : integer) ->
ifThenElse
{unit -> bool}
(greaterThanInteger x 10)
(\(ds : unit) -> traceError {bool} "x too large")
(\(ds : unit) ->
ifThenElse
{unit -> bool}
(lessThanInteger y 0)
(\(ds : unit) -> traceError {bool} "y negative")
(\(ds : unit) ->
ifThenElse
{unit -> bool}
(greaterThanInteger (addInteger x y) 20)
(\(ds : unit) -> traceError {bool} "sum too large")
(\(ds : unit) -> True)
())
())
()
19 changes: 19 additions & 0 deletions plutus-tx-plugin/test/Budget/9.6/handOptIfErrorPath.golden.uplc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(program
1.1.0
(\x y ->
(\traceError ->
case
(lessThanEqualsInteger x 10)
[ (\ds -> traceError "x too large")
, (\ds ->
case
(lessThanInteger y 0)
[ (\ds ->
case
(lessThanEqualsInteger (addInteger x y) 20)
[(\ds -> traceError "sum too large"), (\ds -> True)]
())
, (\ds -> traceError "y negative") ]
()) ])
(\str -> (\x -> error) (force trace str (constr 0 [])))
()))
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CPU: 890_272
Memory: 4_205
AST Size: 62
Flat Size: 108

(con bool True)
31 changes: 31 additions & 0 deletions plutus-tx-plugin/test/Budget/9.6/handOptIfHappyPath.golden.pir
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
let
!ifThenElse : all a. bool -> a -> a -> a
= /\a -> \(b : bool) (x : a) (y : a) -> case a b [y, x]
!greaterThanInteger : integer -> integer -> bool
= \(x : integer) (y : integer) ->
ifThenElse {bool} (lessThanEqualsInteger x y) False True
data Unit | Unit_match where
Unit : Unit
!traceError : all a. string -> a
= /\a ->
\(str : string) -> let !x : Unit = trace {Unit} str Unit in error {a}
in
\(x : integer) (y : integer) ->
ifThenElse
{unit -> bool}
(greaterThanInteger x 10)
(\(ds : unit) -> traceError {bool} "x too large")
(\(ds : unit) ->
ifThenElse
{unit -> bool}
(lessThanInteger y 0)
(\(ds : unit) -> traceError {bool} "y negative")
(\(ds : unit) ->
ifThenElse
{unit -> bool}
(greaterThanInteger (addInteger x y) 20)
(\(ds : unit) -> traceError {bool} "sum too large")
(\(ds : unit) -> True)
())
())
()
19 changes: 19 additions & 0 deletions plutus-tx-plugin/test/Budget/9.6/handOptIfHappyPath.golden.uplc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(program
1.1.0
(\x y ->
(\traceError ->
case
(lessThanEqualsInteger x 10)
[ (\ds -> traceError "x too large")
, (\ds ->
case
(lessThanInteger y 0)
[ (\ds ->
case
(lessThanEqualsInteger (addInteger x y) 20)
[(\ds -> traceError "sum too large"), (\ds -> True)]
())
, (\ds -> traceError "y negative") ]
()) ])
(\str -> (\x -> error) (force trace str (constr 0 [])))
()))
71 changes: 71 additions & 0 deletions plutus-tx-plugin/test/Budget/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE NegativeLiterals #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
Expand All @@ -20,6 +21,7 @@ import Budget.WithoutGHCOptimisations qualified as WithoutGHCOptTest
import Data.Set qualified as Set
import PlutusTx.AsData qualified as AsData
import PlutusTx.Builtins qualified as PlutusTx hiding (null)
import PlutusTx.Builtins.Internal qualified as BI
import PlutusTx.Code
import PlutusTx.Data.List (List)
import PlutusTx.Data.List.TH (destructList)
Expand Down Expand Up @@ -102,6 +104,35 @@ tests =
goldenBundle' "andWithoutGHCOpts" compiledAndWithoutGHCOpts
, -- With the function definition in the local module
goldenBundle' "andWithLocal" compiledAndWithLocal
, -- Multi-way if vs hand-optimized BI.ifThenElse comparison
goldenBundle
"guardIfHappyPath"
compiledGuardIf
( compiledGuardIf
`unsafeApplyCode` liftCodeDef (5 :: Integer)
`unsafeApplyCode` liftCodeDef (3 :: Integer)
)
, goldenBundle
"guardIfErrorPath"
compiledGuardIf
( compiledGuardIf
`unsafeApplyCode` liftCodeDef (15 :: Integer)
`unsafeApplyCode` liftCodeDef (3 :: Integer)
)
, goldenBundle
"handOptIfHappyPath"
compiledHandOptIf
( compiledHandOptIf
`unsafeApplyCode` liftCodeDef (5 :: Integer)
`unsafeApplyCode` liftCodeDef (3 :: Integer)
)
, goldenBundle
"handOptIfErrorPath"
compiledHandOptIf
( compiledHandOptIf
`unsafeApplyCode` liftCodeDef (15 :: Integer)
`unsafeApplyCode` liftCodeDef (3 :: Integer)
)
]

compiledSum :: CompiledCode Integer
Expand Down Expand Up @@ -633,3 +664,43 @@ compiledAndWithLocal =
code = $$(compile [||f||])
in flip unsafeApplyCode (liftCodeDef (4 :: Integer)) $
unsafeApplyCode code (liftCodeDef (4 :: Integer))

-- | Multi-way if with guards (plugin-compiled).
compiledGuardIf :: CompiledCode (Integer -> Integer -> Bool)
compiledGuardIf =
$$( compile
[||
\x y ->
if
| x PlutusTx.> (10 :: Integer) -> PlutusTx.traceError "x too large"
| y PlutusTx.< (0 :: Integer) -> PlutusTx.traceError "y negative"
| (x PlutusTx.+ y) PlutusTx.> (20 :: Integer) -> PlutusTx.traceError "sum too large"
| otherwise -> True
||]
)

-- | Hand-optimized BI.ifThenElse with lambda-delay pattern.
compiledHandOptIf :: CompiledCode (Integer -> Integer -> Bool)
compiledHandOptIf =
$$( compile
[||
\x y ->
BI.ifThenElse
(x PlutusTx.> (10 :: Integer))
(\_ -> PlutusTx.traceError "x too large")
( \_ ->
BI.ifThenElse
(y PlutusTx.< (0 :: Integer))
(\_ -> PlutusTx.traceError "y negative")
( \_ ->
BI.ifThenElse
((x PlutusTx.+ y) PlutusTx.> (20 :: Integer))
(\_ -> PlutusTx.traceError "sum too large")
(\_ -> True)
BI.unitval
)
BI.unitval
)
BI.unitval
||]
)