-- | Possibly convenient facilities for constructing constants.
module Futhark.IR.Prop.Constants
  ( IsValue (..),
    constant,
    intConst,
    floatConst,
  )
where

import Futhark.IR.Syntax.Core (SubExp (..))
import Language.Futhark.Primitive

-- | If a Haskell type is an instance of 'IsValue', it means that a
-- value of that type can be converted to a Futhark 'PrimValue'.
-- This is intended to cut down on boilerplate when writing compiler
-- code - for example, you'll quickly grow tired of writing @Constant
-- (LogVal True) loc@.
class IsValue a where
  value :: a -> PrimValue

instance IsValue Int8 where
  value :: Int8 -> PrimValue
value = IntValue -> PrimValue
IntValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int8 -> IntValue
Int8Value

instance IsValue Int16 where
  value :: Int16 -> PrimValue
value = IntValue -> PrimValue
IntValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int16 -> IntValue
Int16Value

instance IsValue Int32 where
  value :: Int32 -> PrimValue
value = IntValue -> PrimValue
IntValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> IntValue
Int32Value

instance IsValue Int64 where
  value :: Int64 -> PrimValue
value = IntValue -> PrimValue
IntValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> IntValue
Int64Value

instance IsValue Word8 where
  value :: Word8 -> PrimValue
value = IntValue -> PrimValue
IntValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int8 -> IntValue
Int8Value forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral

instance IsValue Word16 where
  value :: Word16 -> PrimValue
value = IntValue -> PrimValue
IntValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int16 -> IntValue
Int16Value forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral

instance IsValue Word32 where
  value :: Word32 -> PrimValue
value = IntValue -> PrimValue
IntValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> IntValue
Int32Value forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral

instance IsValue Word64 where
  value :: Word64 -> PrimValue
value = IntValue -> PrimValue
IntValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> IntValue
Int64Value forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral

instance IsValue Double where
  value :: Double -> PrimValue
value = FloatValue -> PrimValue
FloatValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> FloatValue
Float64Value

instance IsValue Float where
  value :: Float -> PrimValue
value = FloatValue -> PrimValue
FloatValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> FloatValue
Float32Value

instance IsValue Bool where
  value :: Bool -> PrimValue
value = Bool -> PrimValue
BoolValue

instance IsValue PrimValue where
  value :: PrimValue -> PrimValue
value = forall a. a -> a
id

instance IsValue IntValue where
  value :: IntValue -> PrimValue
value = IntValue -> PrimValue
IntValue

instance IsValue FloatValue where
  value :: FloatValue -> PrimValue
value = FloatValue -> PrimValue
FloatValue

-- | Create a 'Constant' 'SubExp' containing the given value.
constant :: IsValue v => v -> SubExp
constant :: forall v. IsValue v => v -> SubExp
constant = PrimValue -> SubExp
Constant forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsValue a => a -> PrimValue
value

-- | Utility definition for reasons of type ambiguity.
intConst :: IntType -> Integer -> SubExp
intConst :: IntType -> Integer -> SubExp
intConst IntType
t Integer
v = forall v. IsValue v => v -> SubExp
constant forall a b. (a -> b) -> a -> b
$ forall int. Integral int => IntType -> int -> IntValue
intValue IntType
t Integer
v

-- | Utility definition for reasons of type ambiguity.
floatConst :: FloatType -> Double -> SubExp
floatConst :: FloatType -> Double -> SubExp
floatConst FloatType
t Double
v = forall v. IsValue v => v -> SubExp
constant forall a b. (a -> b) -> a -> b
$ forall num. Real num => FloatType -> num -> FloatValue
floatValue FloatType
t Double
v