Skip to content

Commit b691ee1

Browse files
committed
Remove prismatic error handling from Ck/Cek machines
1 parent f17c02c commit b691ee1

File tree

25 files changed

+197
-341
lines changed

25 files changed

+197
-341
lines changed

plutus-core/cost-model/budgeting-bench/Benchmarks/Nops.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import PlutusCore.Evaluation.Machine.BuiltinCostModel hiding (BuiltinCostModel)
2222
import PlutusCore.Evaluation.Machine.ExBudgetingDefaults
2323
import PlutusCore.Evaluation.Machine.ExMemoryUsage (ExMemoryUsage)
2424
import PlutusCore.Evaluation.Machine.MachineParameters
25-
import PlutusCore.Evaluation.Result (evaluationFailure)
2625
import PlutusCore.Pretty
2726
import PlutusPrelude
2827
import UntypedPlutusCore.Evaluation.Machine.Cek
@@ -137,7 +136,7 @@ infixr >:
137136
n >: k =
138137
case n of
139138
SomeConstant (Some (ValueOf DefaultUniInteger _)) -> k
140-
_ -> evaluationFailure
139+
_ -> builtinResultFailure
141140
{-# INLINE (>:) #-}
142141

143142
{- | The meanings of the builtins. Each one takes a number of arguments and

plutus-core/docs/BuiltinsOverview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ Here's an example instance (where `Term` is the type of TPLC terms):
402402
```haskell
403403
instance HasConstant (Term TyName Name uni fun ()) where
404404
asConstant (Constant _ val) = pure val
405-
asConstant _ = throwNotAConstant
405+
asConstant _ = throwError notAConstant
406406
```
407407

408408
Unlifting of constants then is just a matter of unwrapping a value as a constant and checking that the constant is of the right type, which is what the default implementation of `readKnown` does:
@@ -452,5 +452,5 @@ instance
452452
( TypeError ('Text "‘BuiltinResult’ cannot appear in the type of an argument")
453453
, uni ~ UniOf val
454454
) => ReadKnownIn uni val (BuiltinResult a) where
455-
readKnown _ = throwUnderTypeError
455+
readKnown _ = throwError underTypeError
456456
```

plutus-core/plutus-core/examples/PlutusCore/Examples/Builtins.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ import PlutusCore.Data
2424
import PlutusCore.Evaluation.Machine.BuiltinCostModel
2525
import PlutusCore.Evaluation.Machine.ExBudget
2626
import PlutusCore.Evaluation.Machine.ExBudgetStream
27-
import PlutusCore.Evaluation.Result (evaluationFailure)
2827
import PlutusCore.Pretty
2928

3029
import PlutusCore.StdLib.Data.ScottList qualified as Plc
3130

3231
import Control.Concurrent.MVar
3332
import Control.Exception
3433
import Control.Monad
34+
import Control.Monad.Except
3535
import Data.Default.Class
3636
import Data.Either
3737
import Data.Kind qualified as GHC (Type)
@@ -199,7 +199,7 @@ instance tyname ~ TyName => KnownTypeAst tyname DefaultUni Void where
199199
instance UniOf term ~ DefaultUni => MakeKnownIn DefaultUni term Void where
200200
makeKnown = absurd
201201
instance UniOf term ~ DefaultUni => ReadKnownIn DefaultUni term Void where
202-
readKnown _ = throwing _StructuralUnliftingError "Can't unlift to 'Void'"
202+
readKnown _ = throwError $ structuralUnliftingError "Can't unlift to 'Void'"
203203

204204
data BuiltinErrorCall = BuiltinErrorCall
205205
deriving stock (Show, Eq)
@@ -286,7 +286,7 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni ExtensionFun where
286286
idAssumeCheckBoolPlc val =
287287
case asConstant val of
288288
Right (Some (ValueOf DefaultUniBool b)) -> pure b
289-
_ -> evaluationFailure
289+
_ -> builtinResultFailure
290290

291291
toBuiltinMeaning _semvar IdSomeConstantBool =
292292
makeBuiltinMeaning
@@ -296,7 +296,7 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni ExtensionFun where
296296
idSomeConstantBoolPlc :: SomeConstant uni Bool -> BuiltinResult Bool
297297
idSomeConstantBoolPlc = \case
298298
SomeConstant (Some (ValueOf DefaultUniBool b)) -> pure b
299-
_ -> evaluationFailure
299+
_ -> builtinResultFailure
300300

301301
toBuiltinMeaning _semvar IdIntegerAsBool =
302302
makeBuiltinMeaning
@@ -306,7 +306,7 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni ExtensionFun where
306306
idIntegerAsBool :: SomeConstant uni Integer -> BuiltinResult (SomeConstant uni Integer)
307307
idIntegerAsBool = \case
308308
con@(SomeConstant (Some (ValueOf DefaultUniBool _))) -> pure con
309-
_ -> evaluationFailure
309+
_ -> builtinResultFailure
310310

311311
toBuiltinMeaning _semvar IdFInteger =
312312
makeBuiltinMeaning
@@ -397,7 +397,7 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni ExtensionFun where
397397

398398
toBuiltinMeaning _semvar ErrorPrime =
399399
makeBuiltinMeaning
400-
(evaluationFailure :: forall a. BuiltinResult a)
400+
(builtinResultFailure :: forall a. BuiltinResult a)
401401
whatever
402402

403403
toBuiltinMeaning _semvar Comma =

plutus-core/plutus-core/src/PlutusCore/Bitwise.hs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ module PlutusCore.Bitwise (
2424
maximumOutputLength
2525
) where
2626

27-
import PlutusCore.Builtin (BuiltinResult, emit)
28-
import PlutusCore.Evaluation.Result (evaluationFailure)
27+
import PlutusCore.Builtin (BuiltinResult, builtinResultFailure, emit)
2928

3029
import ByteString.StrictBuilder (Builder)
3130
import ByteString.StrictBuilder qualified as Builder
@@ -64,15 +63,15 @@ integerToByteString endiannessArg lengthArg input
6463
| lengthArg < 0 = do
6564
emit "integerToByteString: negative length argument"
6665
emit $ "Length requested: " <> (pack . show $ input)
67-
evaluationFailure
66+
builtinResultFailure
6867
-- Check that the requested length does not exceed the limit. *NB*: if we remove the limit we'll
6968
-- still have to make sure that the length fits into an Int.
7069
| lengthArg > maximumOutputLength = do
7170
emit . pack $ "integerToByteString: requested length is too long (maximum is "
7271
++ show maximumOutputLength
7372
++ " bytes)"
7473
emit $ "Length requested: " <> (pack . show $ lengthArg)
75-
evaluationFailure
74+
builtinResultFailure
7675
-- If the requested length is zero (ie, an explicit output size is not
7776
-- specified) we still have to make sure that the output won't exceed the size
7877
-- limit. If the requested length is nonzero and less than the limit,
@@ -86,7 +85,7 @@ integerToByteString endiannessArg lengthArg input
8685
++ show (8 * maximumOutputLength)
8786
++ "-1)"
8887
emit $ "Length required: " <> (pack . show $ bytesRequiredFor input)
89-
evaluationFailure
88+
builtinResultFailure
9089
| otherwise = let endianness = endiannessArgToByteOrder endiannessArg in
9190
-- We use fromIntegral here, despite advice to the contrary in general when defining builtin
9291
-- denotations. This is because, if we've made it this far, we know that overflow or truncation
@@ -98,14 +97,14 @@ integerToByteString endiannessArg lengthArg input
9897
-- This does work proportional to the size of input. However, we're in a failing case
9998
-- anyway, and the user's paid for work proportional to this size in any case.
10099
emit $ "Input: " <> (pack . show $ input)
101-
evaluationFailure
100+
builtinResultFailure
102101
NotEnoughDigits -> do
103102
emit "integerToByteString: cannot represent Integer in given number of bytes"
104103
-- This does work proportional to the size of input. However, we're in a failing case
105104
-- anyway, and the user's paid for work proportional to this size in any case.
106105
emit $ "Input: " <> (pack . show $ input)
107106
emit $ "Bytes requested: " <> (pack . show $ lengthArg)
108-
evaluationFailure
107+
builtinResultFailure
109108
Right result -> pure result
110109

111110
-- | Conversion from 'Integer' to 'ByteString', as per
@@ -528,11 +527,11 @@ readBit bs ix
528527
| ix < 0 = do
529528
emit "readBit: index out of bounds"
530529
emit $ "Index: " <> (pack . show $ ix)
531-
evaluationFailure
530+
builtinResultFailure
532531
| ix >= len * 8 = do
533532
emit "readBit: index out of bounds"
534533
emit $ "Index: " <> (pack . show $ ix)
535-
evaluationFailure
534+
builtinResultFailure
536535
| otherwise = do
537536
let (bigIx, littleIx) = ix `quotRem` 8
538537
let flipIx = len - bigIx - 1
@@ -548,7 +547,7 @@ writeBits bs ixs bit = case unsafeDupablePerformIO . try $ go of
548547
Left (WriteBitsException i) -> do
549548
emit "writeBits: index out of bounds"
550549
emit $ "Index: " <> (pack . show $ i)
551-
evaluationFailure
550+
builtinResultFailure
552551
Right result -> pure result
553552
where
554553
-- This is written in a somewhat strange way. See Note [writeBits and
@@ -588,13 +587,13 @@ replicateByte :: Integer -> Word8 -> BuiltinResult ByteString
588587
replicateByte len w8
589588
| len < 0 = do
590589
emit "replicateByte: negative length requested"
591-
evaluationFailure
590+
builtinResultFailure
592591
| len > maximumOutputLength = do
593592
emit . pack $ "replicateByte: requested length is too long (maximum is "
594593
++ show maximumOutputLength
595594
++ " bytes)"
596595
emit $ "Length requested: " <> (pack . show $ len)
597-
evaluationFailure
596+
builtinResultFailure
598597
| otherwise = pure . BS.replicate (fromIntegral len) $ w8
599598

600599
-- | Wrapper for calling 'unsafesShiftByteString' safely. Specifically, we avoid various edge cases:

plutus-core/plutus-core/src/PlutusCore/Builtin/HasConstant.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
module PlutusCore.Builtin.HasConstant
77
( BuiltinError (..)
8-
, throwNotAConstant
8+
, notAConstant
99
, HasConstant (..)
1010
, HasConstantIn
1111
, fromValueOf
@@ -18,6 +18,8 @@ import PlutusCore.Name.Unique
1818

1919
import Universe
2020

21+
import Control.Monad.Except
22+
2123
{- Note [Existence of HasConstant]
2224
We don't really need 'HasConstant' and could get away with only having 'HasConstantIn', however
2325
defining the latter directly as a @class@ instead of a type synonym in terms of the former is
@@ -56,6 +58,6 @@ fromValue = fromValueOf knownUni
5658

5759
instance HasConstant (Term TyName Name uni fun ()) where
5860
asConstant (Constant _ val) = pure val
59-
asConstant _ = throwNotAConstant
61+
asConstant _ = throwError notAConstant
6062

6163
fromConstant = Constant ()

plutus-core/plutus-core/src/PlutusCore/Builtin/KnownType.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
module PlutusCore.Builtin.KnownType
1919
( BuiltinError
20-
, throwBuiltinErrorWithCause
2120
, KnownBuiltinTypeIn
2221
, KnownBuiltinType
2322
, BuiltinResult (..)
@@ -39,10 +38,11 @@ import PlutusCore.Builtin.HasConstant
3938
import PlutusCore.Builtin.Polymorphism
4039
import PlutusCore.Builtin.Result
4140
import PlutusCore.Core
42-
import PlutusCore.Evaluation.ErrorWithCause
41+
import PlutusCore.Evaluation.Machine.Exception
4342
import PlutusCore.Evaluation.Result
4443
import PlutusCore.Pretty
4544

45+
import Control.Monad.Except
4646
import Data.Either.Extras
4747
import Data.Functor.Identity
4848
import Data.String
@@ -276,7 +276,7 @@ readKnownConstant val = asConstant val >>= oneShot \case
276276
-- optimize some of the matching away.
277277
case uniExp `geq` uniAct of
278278
Just Refl -> pure x
279-
Nothing -> throwing _UnliftingEvaluationError $ typeMismatchError uniExp uniAct
279+
Nothing -> throwError $ BuiltinUnliftingEvaluationError $ typeMismatchError uniExp uniAct
280280
{-# INLINE readKnownConstant #-}
281281

282282
-- | A non-empty spine. Isomorphic to 'NonEmpty', except is strict and is defined as a single
@@ -363,9 +363,9 @@ makeKnownOrFail x = case makeKnown x of
363363

364364
-- | Same as 'readKnown', but the cause of a potential failure is the provided term itself.
365365
readKnownSelf
366-
:: (ReadKnown val a, AsUnliftingEvaluationError err, AsEvaluationFailure err)
367-
=> val -> Either (ErrorWithCause err val) a
368-
readKnownSelf val = fromRightM (throwBuiltinErrorWithCause val) $ readKnown val
366+
:: (ReadKnown val a, BuiltinErrorToEvaluationError structural operational)
367+
=> val -> Either (ErrorWithCause (EvaluationError structural operational) val) a
368+
readKnownSelf val = fromRightM (flip throwErrorWithCause val . builtinErrorToEvaluationError) $ readKnown val
369369
{-# INLINE readKnownSelf #-}
370370

371371
instance MakeKnownIn uni val a => MakeKnownIn uni val (BuiltinResult a) where
@@ -382,21 +382,21 @@ instance
382382
( TypeError ('Text "‘BuiltinResult’ cannot appear in the type of an argument")
383383
, uni ~ UniOf val
384384
) => ReadKnownIn uni val (BuiltinResult a) where
385-
readKnown _ = throwUnderTypeError
385+
readKnown _ = throwError underTypeError
386386
{-# INLINE readKnown #-}
387387

388388
instance
389389
( TypeError ('Text "Use ‘BuiltinResult’ instead of ‘EvaluationResult’")
390390
, uni ~ UniOf val
391391
) => MakeKnownIn uni val (EvaluationResult a) where
392-
makeKnown _ = throwUnderTypeError
392+
makeKnown _ = throwError underTypeError
393393
{-# INLINE makeKnown #-}
394394

395395
instance
396396
( TypeError ('Text "Use ‘BuiltinResult’ instead of ‘EvaluationResult’")
397397
, uni ~ UniOf val
398398
) => ReadKnownIn uni val (EvaluationResult a) where
399-
readKnown _ = throwUnderTypeError
399+
readKnown _ = throwError underTypeError
400400
{-# INLINE readKnown #-}
401401

402402
instance HasConstantIn uni val => MakeKnownIn uni val (SomeConstant uni rep) where

0 commit comments

Comments
 (0)