module Feldspar.Core.Constructs.Conversion
( Conversion (..)
) where
import Language.Syntactic
import Language.Syntactic.Constructs.Binding
import Feldspar.Range
import Feldspar.Lattice
import Feldspar.Core.Types
import Feldspar.Core.Interpretation
data Conversion a
where
F2I :: (Type a, Integral a, RealFloat b) => Conversion (b :-> Full a)
I2N :: (Type a, Type b, Integral a, Num b
,Size a ~ Range a
) =>
Conversion (a :-> Full b)
B2I :: (Type a, Integral a)
=> Conversion (Bool :-> Full a)
Round :: (Type a, Integral a, RealFloat b)
=> Conversion (b :-> Full a)
Ceiling :: (Type a, Integral a, RealFloat b)
=> Conversion (b :-> Full a)
Floor :: (Type a, Integral a, RealFloat b)
=> Conversion (b :-> Full a)
rangeToSize :: Lattice (Size a) => TypeRep a -> Range Integer -> Size a
rangeToSize (IntType _ _) r = rangeProp r
rangeToSize _ _ = universal
rangeProp :: forall a . (Bounded a, Integral a) => Range Integer -> Range a
rangeProp (Range l u)
| withinBounds l && withinBounds u
= range (fromIntegral l) (fromIntegral u)
| otherwise = range minBound maxBound
where withinBounds i = toInteger (minBound :: a) <= i &&
i <= toInteger (maxBound :: a)
instance Semantic Conversion
where
semantics F2I = Sem "f2i" truncate
semantics I2N = Sem "i2n" (fromInteger.toInteger)
semantics B2I = Sem "b2i" (\b -> if b then 1 else 0)
semantics Round = Sem "round" round
semantics Ceiling = Sem "ceiling" ceiling
semantics Floor = Sem "floor" floor
semanticInstances ''Conversion
instance EvalBind Conversion where evalBindSym = evalBindSymDefault
instance AlphaEq dom dom dom env => AlphaEq Conversion Conversion dom env
where
alphaEqSym = alphaEqSymDefault
instance Sharable Conversion
instance Monotonic Conversion
instance SizeProp (Conversion :|| Type)
where
sizeProp (C' F2I) _ = universal
sizeProp (C' i2n@I2N) (WrapFull a :* Nil)
= rangeToSize (resultType i2n) (mapMonotonic toInteger (infoSize a))
sizeProp (C' b2i@B2I) _ = rangeToSize (resultType b2i) $ range 0 1
sizeProp (C' Round) _ = universal
sizeProp (C' Ceiling) _ = universal
sizeProp (C' Floor) _ = universal
instance ( (Conversion :|| Type) :<: dom
, OptimizeSuper dom)
=> Optimize (Conversion :|| Type) dom
where
constructFeatOpt _ (C' i2n@I2N) (a :* Nil)
| Just TypeEq <- typeEq (resultType i2n) (infoType $ getInfo a)
= return a
constructFeatOpt opts a args = constructFeatUnOpt opts a args
constructFeatUnOpt opts a@(C' _) = constructFeatUnOptDefault opts a