{-# 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.Ord (Ordering (..))
import PlutusTx.Trace

-- | Class 'Enum' defines operations on sequentially ordered types.
class Enum a where
  -- | the successor of a value.  For numeric types, 'succ' adds 1.
  succ :: a -> a
  -- | the predecessor of a value.  For numeric types, 'pred' subtracts 1.
  pred :: a -> a
  -- | Convert from an 'Integer'.
  toEnum :: Integer -> a
  -- | Convert to an 'Integer'.
  fromEnum :: a -> Integer

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

instance Enum () where
  {-# INLINABLE succ #-}
  succ :: () -> ()
succ ()
_ = BuiltinString -> ()
forall a. BuiltinString -> a
traceError BuiltinString
succVoidBadArgumentError

  {-# INLINABLE pred #-}
  pred :: () -> ()
pred ()
_ = BuiltinString -> ()
forall a. BuiltinString -> a
traceError BuiltinString
predVoidBadArgumentError

  {-# INLINABLE toEnum #-}
  toEnum :: Integer -> ()
toEnum Integer
x | Integer
x Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 = ()
           | Bool
otherwise = BuiltinString -> ()
forall a. BuiltinString -> a
traceError BuiltinString
toEnumVoidBadArgumentError

  {-# INLINABLE fromEnum #-}
  fromEnum :: () -> Integer
fromEnum () = Integer
0

instance Enum Bool where
  {-# INLINABLE succ #-}
  succ :: Bool -> Bool
succ Bool
False = Bool
True
  succ Bool
True  = BuiltinString -> Bool
forall a. BuiltinString -> a
traceError BuiltinString
succBoolBadArgumentError

  {-# INLINABLE pred #-}
  pred :: Bool -> Bool
pred Bool
True  = Bool
False
  pred Bool
False = BuiltinString -> Bool
forall a. BuiltinString -> a
traceError BuiltinString
predBoolBadArgumentError

  {-# INLINABLE toEnum #-}
  toEnum :: Integer -> Bool
toEnum Integer
n | Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0    = Bool
False
           | Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1    = Bool
True
           | Bool
otherwise = BuiltinString -> Bool
forall a. BuiltinString -> a
traceError BuiltinString
toEnumBoolBadArgumentError

  {-# INLINABLE fromEnum #-}
  fromEnum :: Bool -> Integer
fromEnum Bool
False = Integer
0
  fromEnum Bool
True  = Integer
1

instance Enum Ordering where
  {-# INLINABLE succ #-}
  succ :: Ordering -> Ordering
succ Ordering
LT = Ordering
EQ
  succ Ordering
EQ = Ordering
GT
  succ Ordering
GT = BuiltinString -> Ordering
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 = BuiltinString -> Ordering
forall a. BuiltinString -> a
traceError BuiltinString
predOrderingBadArgumentError

  {-# INLINABLE toEnum #-}
  toEnum :: Integer -> Ordering
toEnum Integer
n | Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 = Ordering
LT
           | Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1 = Ordering
EQ
           | Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
2 = Ordering
GT
  toEnum Integer
_ = BuiltinString -> Ordering
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