{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-omit-interface-pragmas #-}
module PlutusTx.Enum (Enum(..)) where
import PlutusTx.Bool (Bool (..), otherwise)
import PlutusTx.Builtins
import PlutusTx.Eq ((==))
import PlutusTx.ErrorCodes
import PlutusTx.List
import PlutusTx.Ord (Ord (..), Ordering (..))
import PlutusTx.Trace
class Enum a where
succ :: a -> a
pred :: a -> a
toEnum :: Integer -> a
:: a -> Integer
enumFromTo :: a -> a -> [a]
instance Enum Integer where
{-# INLINABLE succ #-}
succ :: Integer -> Integer
succ Integer
x = Integer -> Integer -> Integer
addInteger Integer
x Integer
1
{-# INLINABLE pred #-}
pred :: Integer -> Integer
pred Integer
x = Integer -> Integer -> Integer
subtractInteger Integer
x Integer
1
{-# INLINABLE toEnum #-}
toEnum :: Integer -> Integer
toEnum Integer
x = Integer
x
{-# INLINABLE fromEnum #-}
fromEnum :: Integer -> Integer
fromEnum Integer
x = Integer
x
{-# INLINABLE enumFromTo #-}
enumFromTo :: Integer -> Integer -> [Integer]
enumFromTo Integer
x Integer
y
| Integer
x forall a. Ord a => a -> a -> Bool
> Integer
y = []
| Bool
otherwise = Integer
x forall a. a -> [a] -> [a]
: forall a. Enum a => a -> a -> [a]
enumFromTo (forall a. Enum a => a -> a
succ Integer
x) Integer
y
instance Enum () where
{-# INLINABLE succ #-}
succ :: () -> ()
succ ()
_ = forall a. BuiltinString -> a
traceError BuiltinString
succVoidBadArgumentError
{-# INLINABLE pred #-}
pred :: () -> ()
pred ()
_ = forall a. BuiltinString -> a
traceError BuiltinString
predVoidBadArgumentError
{-# INLINABLE toEnum #-}
toEnum :: Integer -> ()
toEnum Integer
x | Integer
x forall a. Eq a => a -> a -> Bool
== Integer
0 = ()
| Bool
otherwise = forall a. BuiltinString -> a
traceError BuiltinString
toEnumVoidBadArgumentError
{-# INLINABLE fromEnum #-}
fromEnum :: () -> Integer
fromEnum () = Integer
0
{-# INLINABLE enumFromTo #-}
enumFromTo :: () -> () -> [()]
enumFromTo ()
_ ()
_ = [()]
instance Enum Bool where
{-# INLINABLE succ #-}
succ :: Bool -> Bool
succ Bool
False = Bool
True
succ Bool
True = forall a. BuiltinString -> a
traceError BuiltinString
succBoolBadArgumentError
{-# INLINABLE pred #-}
pred :: Bool -> Bool
pred Bool
True = Bool
False
pred Bool
False = forall a. BuiltinString -> a
traceError BuiltinString
predBoolBadArgumentError
{-# INLINABLE toEnum #-}
toEnum :: Integer -> Bool
toEnum Integer
n | Integer
n forall a. Eq a => a -> a -> Bool
== Integer
0 = Bool
False
| Integer
n forall a. Eq a => a -> a -> Bool
== Integer
1 = Bool
True
| Bool
otherwise = forall a. BuiltinString -> a
traceError BuiltinString
toEnumBoolBadArgumentError
{-# INLINABLE fromEnum #-}
fromEnum :: Bool -> Integer
fromEnum Bool
False = Integer
0
fromEnum Bool
True = Integer
1
{-# INLINABLE enumFromTo #-}
enumFromTo :: Bool -> Bool -> [Bool]
enumFromTo Bool
x Bool
y = forall a b. (a -> b) -> [a] -> [b]
map forall a. Enum a => Integer -> a
toEnum (forall a. Enum a => a -> a -> [a]
enumFromTo (forall a. Enum a => a -> Integer
fromEnum Bool
x) (forall a. Enum a => a -> Integer
fromEnum Bool
y))
instance Enum Ordering where
{-# INLINABLE succ #-}
succ :: Ordering -> Ordering
succ Ordering
LT = Ordering
EQ
succ Ordering
EQ = Ordering
GT
succ Ordering
GT = forall a. BuiltinString -> a
traceError BuiltinString
succOrderingBadArgumentError
{-# INLINABLE pred #-}
pred :: Ordering -> Ordering
pred Ordering
GT = Ordering
EQ
pred Ordering
EQ = Ordering
LT
pred Ordering
LT = forall a. BuiltinString -> a
traceError BuiltinString
predOrderingBadArgumentError
{-# INLINABLE toEnum #-}
toEnum :: Integer -> Ordering
toEnum Integer
n | Integer
n forall a. Eq a => a -> a -> Bool
== Integer
0 = Ordering
LT
| Integer
n forall a. Eq a => a -> a -> Bool
== Integer
1 = Ordering
EQ
| Integer
n forall a. Eq a => a -> a -> Bool
== Integer
2 = Ordering
GT
toEnum Integer
_ = forall a. BuiltinString -> a
traceError BuiltinString
toEnumOrderingBadArgumentError
{-# INLINABLE fromEnum #-}
fromEnum :: Ordering -> Integer
fromEnum Ordering
LT = Integer
0
fromEnum Ordering
EQ = Integer
1
fromEnum Ordering
GT = Integer
2
{-# INLINABLE enumFromTo #-}
enumFromTo :: Ordering -> Ordering -> [Ordering]
enumFromTo Ordering
x Ordering
y = forall a b. (a -> b) -> [a] -> [b]
map forall a. Enum a => Integer -> a
toEnum (forall a. Enum a => a -> a -> [a]
enumFromTo (forall a. Enum a => a -> Integer
fromEnum Ordering
x) (forall a. Enum a => a -> Integer
fromEnum Ordering
y))