module Enumerate.Enum
( minBound_enumerable
, maxBound_enumerable
, toEnum_enumerable
, fromEnum_enumerable
, array_enumerable
, table_enumerable
) where
import Enumerate.Types
import Spiros.Prelude
import Numeric.Natural
import qualified Data.Array as Array --IntMap
import Data.Array (Array, (!))
import qualified Data.Map as Map
import Data.Map (Map)
minBound_enumerable :: forall a. (Enumerable a) => Array Int a -> a
minBound_enumerable as = (as ! 0)
maxBound_enumerable :: forall a. (Enumerable a) => Array Int a -> a
maxBound_enumerable as = (as ! (n1))
where n = nat2int $ cardinality ([] :: [a])
toEnum_enumerable :: forall a. (Enumerable a) => Array Int a -> (Int -> a)
toEnum_enumerable as = \i -> (as ! i)
fromEnum_enumerable :: forall a. (Enumerable a, Ord a) => Map a Int -> (a -> Int)
fromEnum_enumerable as = \x -> (__fromJust__ "fromEnum") (Map.lookup x as)
array_enumerable :: forall a. (Enumerable a) => Array Int a --TODO
array_enumerable = Array.listArray (0, n 1) enumerated
where n = nat2int $ cardinality ([] :: [a])
table_enumerable :: forall a. (Enumerable a, Ord a) => Map a Int
table_enumerable = Map.fromList (zip enumerated [0 .. n 1])
where n = nat2int $ cardinality ([] :: [a])
__fromJust__ :: String -> Maybe a -> a
__fromJust__ name = maybe (__bug__ name) id
__bug__ :: String -> a
__bug__ name = error (name ++ ": invalid Enumerable instance")
nat2int :: Natural -> Int
nat2int = fromInteger . fromIntegral