module Feldspar.Core.Constructs.Num
( NUM (..)
) where
import Language.Syntactic
import Language.Syntactic.Constructs.Binding
import Data.Complex (Complex(..))
import Feldspar.Range
import Feldspar.Core.Types
import Feldspar.Core.Interpretation
import Feldspar.Core.Constructs.Literal
import Feldspar.Core.Constructs.Integral
import Feldspar.Core.Constructs.Complex
data NUM a
where
Abs :: (Type a, Num a, Num (Size a)) => NUM (a :-> Full a)
Sign :: (Type a, Num a, Num (Size a)) => NUM (a :-> Full a)
Add :: (Type a, Num a, Num (Size a)) => NUM (a :-> a :-> Full a)
Sub :: (Type a, Num a, Num (Size a)) => NUM (a :-> a :-> Full a)
Mul :: (Type a, Num a, Num (Size a)) => NUM (a :-> a :-> Full a)
instance Semantic NUM
where
semantics Abs = Sem "abs" abs
semantics Sign = Sem "signum" signum
semantics Add = Sem "(+)" (+)
semantics Sub = Sem "(-)" ()
semantics Mul = Sem "(*)" (*)
semanticInstances ''NUM
instance EvalBind NUM where evalBindSym = evalBindSymDefault
instance AlphaEq dom dom dom env => AlphaEq NUM NUM dom env
where
alphaEqSym = alphaEqSymDefault
instance Sharable NUM
instance Monotonic NUM
instance SizeProp (NUM :|| Type)
where
sizeProp (C' Abs) (WrapFull a :* Nil) = abs (infoSize a)
sizeProp (C' Sign) (WrapFull a :* Nil) = signum (infoSize a)
sizeProp (C' Add) (WrapFull a :* WrapFull b :* Nil) = infoSize a + infoSize b
sizeProp (C' Sub) (WrapFull a :* WrapFull b :* Nil) = infoSize a infoSize b
sizeProp (C' Mul) (WrapFull a :* WrapFull b :* Nil) = infoSize a * infoSize b
instance ( (NUM :|| Type) :<: dom
, (Literal :|| Type) :<: dom
, (INTEGRAL :|| Type) :<: dom
, (COMPLEX :|| Type) :<: dom
, OptimizeSuper dom
)
=> Optimize (NUM :|| Type) dom
where
constructFeatOpt _ (C' Abs) (a :* Nil)
| RangeSet r <- infoRange (getInfo a)
, isNatural r
= return a
constructFeatOpt _ (C' Sign) (a :* Nil)
| RangeSet ra <- infoRange (getInfo a)
, 0 `rangeLess` ra
= return (literalDecor 1)
constructFeatOpt _ (C' Sign) (a :* Nil)
| RangeSet ra <- infoRange (getInfo a)
, ra `rangeLess` 0
= return (literalDecor (1))
constructFeatOpt opts (C' Add) (a :* b :* Nil)
| Just 0 <- viewLiteral b = return a
| Just 0 <- viewLiteral a = return b
| alphaEq a b = constructFeatOpt opts (c' Mul) (a :* literalDecor 2 :* Nil)
constructFeatOpt opts s@(C' Add) (a :* (op :$ b :$ c) :* Nil)
| Just al <- viewLiteral a
, Just (C' Add) <- prjF op
, Just cl <- viewLiteral c
= constructFeat opts s (b :* literalDecor (al+cl) :* Nil)
constructFeatOpt opts s@(C' Add) (a :* (op :$ b :$ c) :* Nil)
| Just al <- viewLiteral a
, Just (C' Sub) <- prjF op
, Just cl <- viewLiteral c
= constructFeat opts s (b :* literalDecor (alcl) :* Nil)
constructFeatOpt opts s@(C' Add) ((op :$ a :$ b) :* c :* Nil)
| Just cl <- viewLiteral c
, Just (C' Add) <- prjF op
, Just bl <- viewLiteral b
= constructFeat opts s (a :* literalDecor (bl+cl) :* Nil)
constructFeatOpt opts s@(C' Add) ((op :$ a :$ b) :* c :* Nil)
| Just cl <- viewLiteral c
, Just (C' Sub) <- prjF op
, Just bl <- viewLiteral b
= constructFeat opts s (a :* literalDecor (clbl) :* Nil)
constructFeatOpt opts (C' Add) ((op1 :$ a :$ b) :* (op2 :$ c :$ d) :* Nil)
| Just (C' Add) <- prjF op1
, Just (C' Add) <- prjF op2
, Just bl <- viewLiteral b
, Just dl <- viewLiteral d
= do
ac <- constructFeat opts (c' Add) (a :* c :* Nil)
constructFeat opts (c' Add) (ac :* literalDecor (bl+dl) :* Nil)
constructFeatOpt opts (C' Add) ((op1 :$ a :$ b) :* (op2 :$ c :$ d) :* Nil)
| Just (C' Add) <- prjF op1
, Just (C' Sub) <- prjF op2
, alphaEq a c
, alphaEq b d
= constructFeat opts (c' Add) (a :* c :* Nil)
constructFeatOpt opts (C' Add) ((rem :$ a :$ b) :* (mul :$ c :$ (quot :$ d :$ e)) :* Nil)
| Just (C' Rem) <- prjF rem
, Just (C' Mul) <- prjF mul
, Just (C' Quot) <- prjF quot
, alphaEq a d
, alphaEq c e
, alphaEq b e
= return a
constructFeatOpt opts (C' Sub) ((op :$ a :$ b) :* c :* Nil)
| Just cl <- viewLiteral c
, Just s@(C' Add) <- prjF op
, Just bl <- viewLiteral b
= constructFeat opts s (a :* literalDecor (blcl) :* Nil)
constructFeatOpt opts s@(C' Sub) ((op :$ a :$ b) :* c :* Nil)
| Just cl <- viewLiteral c
, Just (C' Sub) <- prjF op
, Just bl <- viewLiteral b
= constructFeat opts s (a :* literalDecor (bl+cl) :* Nil)
constructFeatOpt opts (C' Sub) ((op1 :$ a :$ b) :* (op2 :$ c :$ d) :* Nil)
| Just (C' Add) <- prjF op1
, Just (C' Sub) <- prjF op2
, alphaEq a c
, alphaEq b d
= constructFeat opts (c' Add) (b :* d :* Nil)
constructFeatOpt _ (C' Sub) (a :* b :* Nil)
| Just 0 <- viewLiteral b = return a
| alphaEq a b = return $ literalDecor 0
constructFeatOpt opts (C' Mul) (a :* iunit :* Nil)
| ComplexType FloatType <- infoType (getInfo iunit)
, Just (0 :+ k) <- viewLiteral iunit
, abs k == 1
= do
ra <- constructFeat opts (c' RealPart) (a :* Nil)
ia <- constructFeat opts (c' ImagPart) (a :* Nil)
iainv <- constructFeatOpt opts (c' Mul) (literalDecor (k) :* ia :* Nil)
rainv <- constructFeatOpt opts (c' Mul) (literalDecor k :* ra :* Nil)
constructFeatOpt opts (c' MkComplex) (iainv :* rainv :* Nil)
constructFeatOpt _ (C' Mul) (a :* b :* Nil)
| Just 0 <- viewLiteral a = return a
| Just 1 <- viewLiteral a = return b
| Just 0 <- viewLiteral b = return b
| Just 1 <- viewLiteral b = return a
constructFeatOpt opts s@(C' Mul) (a :* (op :$ b :$ c) :* Nil)
| Just al <- viewLiteral a
, Just (C' Mul) <- prjF op
, Just cl <- viewLiteral c
= constructFeat opts s (b :* literalDecor (al*cl) :* Nil)
constructFeatOpt opts s@(C' Mul) ((op :$ a :$ b) :* c :* Nil)
| Just cl <- viewLiteral c
, Just (C' Mul) <- prjF op
, Just bl <- viewLiteral b
= constructFeat opts s (a :* literalDecor (bl*cl) :* Nil)
constructFeatOpt opts (C' Mul) ((op1 :$ a :$ b) :* (op2 :$ c :$ d) :* Nil)
| Just (C' Mul) <- prjF op1
, Just (C' Mul) <- prjF op2
, Just b' <- viewLiteral b
, Just d' <- viewLiteral d
= do
ac <- constructFeat opts (c' Mul) (a :* c :* Nil)
constructFeat opts (c' Mul) (ac :* literalDecor (b'*d') :* Nil)
constructFeatOpt opts (C' Add) (a :* b :* Nil)
| Just _ <- viewLiteral a = constructFeatUnOpt opts (c' Add) (b :* a :* Nil)
constructFeatOpt opts (C' Mul) (a :* b :* Nil)
| Just _ <- viewLiteral a = constructFeatUnOpt opts (c' Mul) (b :* a :* Nil)
constructFeatOpt opts a args = constructFeatUnOpt opts a args
constructFeatUnOpt opts x@(C' _) = constructFeatUnOptDefault opts x