{-# LANGUAGE GADTs #-}
module Copilot.Compile.C99.Translate where
import Control.Monad.State
import qualified Data.List.NonEmpty as NonEmpty
import Copilot.Core
import Copilot.Compile.C99.Error (impossible)
import Copilot.Compile.C99.Util
import qualified Language.C99.Simple as C
transexpr :: Expr a -> State FunEnv C.Expr
transexpr :: forall a. Expr a -> State FunEnv Expr
transexpr (Const Type a
ty a
x) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Type a -> a -> Expr
constty Type a
ty a
x
transexpr (Local Type a1
ty1 Type a
_ Name
name Expr a1
e1 Expr a
e2) = do
Expr
e1' <- forall a. Expr a -> State FunEnv Expr
transexpr Expr a1
e1
let cty1 :: Type
cty1 = forall a. Type a -> Type
transtype Type a1
ty1
init :: Maybe Init
init = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Expr -> Init
C.InitExpr Expr
e1'
forall m. Monoid m => m -> State m ()
statetell [Maybe StorageSpec -> Type -> Name -> Maybe Init -> Decln
C.VarDecln forall a. Maybe a
Nothing Type
cty1 Name
name Maybe Init
init]
forall a. Expr a -> State FunEnv Expr
transexpr Expr a
e2
transexpr (Var Type a
_ Name
n) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Name -> Expr
C.Ident Name
n
transexpr (Drop Type a
_ DropIdx
amount Id
sid) = do
let accessvar :: Name
accessvar = Id -> Name
streamaccessorname Id
sid
index :: Expr
index = Integer -> Expr
C.LitInt (forall a b. (Integral a, Num b) => a -> b
fromIntegral DropIdx
amount)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Name -> [Expr] -> Expr
funcall Name
accessvar [Expr
index]
transexpr (ExternVar Type a
_ Name
name Maybe [a]
_) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Name -> Expr
C.Ident (Name -> Name
excpyname Name
name)
transexpr (Label Type a
_ Name
_ Expr a
e) = forall a. Expr a -> State FunEnv Expr
transexpr Expr a
e
transexpr (Op1 Op1 a1 a
op Expr a1
e) = do
Expr
e' <- forall a. Expr a -> State FunEnv Expr
transexpr Expr a1
e
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. Op1 a b -> Expr -> Expr
transop1 Op1 a1 a
op Expr
e'
transexpr (Op2 Op2 a1 b a
op Expr a1
e1 Expr b
e2) = do
Expr
e1' <- forall a. Expr a -> State FunEnv Expr
transexpr Expr a1
e1
Expr
e2' <- forall a. Expr a -> State FunEnv Expr
transexpr Expr b
e2
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b c. Op2 a b c -> Expr -> Expr -> Expr
transop2 Op2 a1 b a
op Expr
e1' Expr
e2'
transexpr (Op3 Op3 a1 b c a
op Expr a1
e1 Expr b
e2 Expr c
e3) = do
Expr
e1' <- forall a. Expr a -> State FunEnv Expr
transexpr Expr a1
e1
Expr
e2' <- forall a. Expr a -> State FunEnv Expr
transexpr Expr b
e2
Expr
e3' <- forall a. Expr a -> State FunEnv Expr
transexpr Expr c
e3
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b c d. Op3 a b c d -> Expr -> Expr -> Expr -> Expr
transop3 Op3 a1 b c a
op Expr
e1' Expr
e2' Expr
e3'
transop1 :: Op1 a b -> C.Expr -> C.Expr
transop1 :: forall a b. Op1 a b -> Expr -> Expr
transop1 Op1 a b
op Expr
e =
case Op1 a b
op of
Op1 a b
Not -> Expr -> Expr
(C..!) Expr
e
Abs Type a
ty -> forall a. Type a -> Expr -> Expr
transAbs Type a
ty Expr
e
Sign Type a
ty -> forall a. Type a -> Expr -> Expr
transSign Type a
ty Expr
e
Recip Type a
ty -> (forall a. Type a -> Integer -> Expr
constNumTy Type a
ty Integer
1) Expr -> Expr -> Expr
C../ Expr
e
Acos Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"acos") [Expr
e]
Asin Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"asin") [Expr
e]
Atan Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"atan") [Expr
e]
Cos Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"cos") [Expr
e]
Sin Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"sin") [Expr
e]
Tan Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"tan") [Expr
e]
Acosh Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"acosh") [Expr
e]
Asinh Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"asinh") [Expr
e]
Atanh Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"atanh") [Expr
e]
Cosh Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"cosh") [Expr
e]
Sinh Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"sinh") [Expr
e]
Tanh Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"tanh") [Expr
e]
Exp Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"exp") [Expr
e]
Log Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"log") [Expr
e]
Sqrt Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"sqrt") [Expr
e]
Ceiling Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"ceil") [Expr
e]
Floor Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"floor") [Expr
e]
BwNot Type a
_ -> Expr -> Expr
(C..~) Expr
e
Cast Type a
_ Type b
ty -> TypeName -> Expr -> Expr
C.Cast (forall a. Type a -> TypeName
transtypename Type b
ty) Expr
e
GetField (Struct a
_) Type b
_ a -> Field s b
f -> Expr -> Name -> Expr
C.Dot Expr
e (forall a (s :: Symbol) t.
(Struct a, KnownSymbol s) =>
(a -> Field s t) -> Name
accessorname a -> Field s b
f)
transop2 :: Op2 a b c -> C.Expr -> C.Expr -> C.Expr
transop2 :: forall a b c. Op2 a b c -> Expr -> Expr -> Expr
transop2 Op2 a b c
op Expr
e1 Expr
e2 = case Op2 a b c
op of
Op2 a b c
And -> Expr
e1 Expr -> Expr -> Expr
C..&& Expr
e2
Op2 a b c
Or -> Expr
e1 Expr -> Expr -> Expr
C..|| Expr
e2
Add Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..+ Expr
e2
Sub Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..- Expr
e2
Mul Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..* Expr
e2
Mod Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..% Expr
e2
Div Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C../ Expr
e2
Fdiv Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C../ Expr
e2
Pow Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"pow") [Expr
e1, Expr
e2]
Logb Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"log") [Expr
e2] Expr -> Expr -> Expr
C../
Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"log") [Expr
e1]
Atan2 Type a
ty -> Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"atan2") [Expr
e1, Expr
e2]
Eq Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..== Expr
e2
Ne Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..!= Expr
e2
Le Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..<= Expr
e2
Ge Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..>= Expr
e2
Lt Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..< Expr
e2
Gt Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..> Expr
e2
BwAnd Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..& Expr
e2
BwOr Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..| Expr
e2
BwXor Type a
_ -> Expr
e1 Expr -> Expr -> Expr
C..^ Expr
e2
BwShiftL Type a
_ Type b
_ -> Expr
e1 Expr -> Expr -> Expr
C..<< Expr
e2
BwShiftR Type a
_ Type b
_ -> Expr
e1 Expr -> Expr -> Expr
C..>> Expr
e2
Index Type (Array n c)
_ -> Expr -> Expr -> Expr
C.Index Expr
e1 Expr
e2
transop3 :: Op3 a b c d -> C.Expr -> C.Expr -> C.Expr -> C.Expr
transop3 :: forall a b c d. Op3 a b c d -> Expr -> Expr -> Expr -> Expr
transop3 Op3 a b c d
op Expr
e1 Expr
e2 Expr
e3 = case Op3 a b c d
op of
Mux Type b
_ -> Expr -> Expr -> Expr -> Expr
C.Cond Expr
e1 Expr
e2 Expr
e3
transAbs :: Type a -> C.Expr -> C.Expr
transAbs :: forall a. Type a -> Expr -> Expr
transAbs Type a
ty Expr
e
| forall a. Type a -> Bool
typeIsFloating Type a
ty
= Name -> [Expr] -> Expr
funcall (forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
"fabs") [Expr
e]
| Bool
otherwise
= (Expr
e Expr -> Expr -> Expr
C..+ (Expr
e Expr -> Expr -> Expr
C..>> Expr
tyBitSizeMinus1)) Expr -> Expr -> Expr
C..^ (Expr
e Expr -> Expr -> Expr
C..>> Expr
tyBitSizeMinus1)
where
tyBitSizeMinus1 :: C.Expr
tyBitSizeMinus1 :: Expr
tyBitSizeMinus1 = case Type a
ty of
Type a
Int8 -> Integer -> Expr
C.LitInt Integer
7
Type a
Int16 -> Integer -> Expr
C.LitInt Integer
15
Type a
Int32 -> Integer -> Expr
C.LitInt Integer
31
Type a
Int64 -> Integer -> Expr
C.LitInt Integer
63
Type a
Word8 -> Integer -> Expr
C.LitInt Integer
7
Type a
Word16 -> Integer -> Expr
C.LitInt Integer
15
Type a
Word32 -> Integer -> Expr
C.LitInt Integer
31
Type a
Word64 -> Integer -> Expr
C.LitInt Integer
63
Type a
_ -> forall a. Name -> Name -> a
impossible
Name
"transAbs"
Name
"copilot-c99"
Name
"Abs applied to unexpected types."
transSign :: Type a -> C.Expr -> C.Expr
transSign :: forall a. Type a -> Expr -> Expr
transSign Type a
ty Expr
e = Expr -> Expr
positiveCase forall a b. (a -> b) -> a -> b
$ Expr -> Expr
negativeCase Expr
e
where
positiveCase :: C.Expr
-> C.Expr
positiveCase :: Expr -> Expr
positiveCase =
Expr -> Expr -> Expr -> Expr
C.Cond (BinaryOp -> Expr -> Expr -> Expr
C.BinaryOp BinaryOp
C.GT Expr
e (forall a. Type a -> Integer -> Expr
constNumTy Type a
ty Integer
0)) (forall a. Type a -> Integer -> Expr
constNumTy Type a
ty Integer
1)
negativeCase :: C.Expr
-> C.Expr
negativeCase :: Expr -> Expr
negativeCase =
Expr -> Expr -> Expr -> Expr
C.Cond (BinaryOp -> Expr -> Expr -> Expr
C.BinaryOp BinaryOp
C.LT Expr
e (forall a. Type a -> Integer -> Expr
constNumTy Type a
ty Integer
0)) (forall a. Type a -> Integer -> Expr
constNumTy Type a
ty (-Integer
1))
constty :: Type a -> a -> C.Expr
constty :: forall a. Type a -> a -> Expr
constty Type a
ty = case Type a
ty of
Type a
Bool -> Bool -> Expr
C.LitBool
Type a
Int8 -> forall a. Type a -> Expr -> Expr
explicitty Type a
ty forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Expr
C.LitInt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
Type a
Int16 -> forall a. Type a -> Expr -> Expr
explicitty Type a
ty forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Expr
C.LitInt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
Type a
Int32 -> forall a. Type a -> Expr -> Expr
explicitty Type a
ty forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Expr
C.LitInt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
Type a
Int64 -> forall a. Type a -> Expr -> Expr
explicitty Type a
ty forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Expr
C.LitInt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
Type a
Word8 -> forall a. Type a -> Expr -> Expr
explicitty Type a
ty forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Expr
C.LitInt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
Type a
Word16 -> forall a. Type a -> Expr -> Expr
explicitty Type a
ty forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Expr
C.LitInt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
Type a
Word32 -> forall a. Type a -> Expr -> Expr
explicitty Type a
ty forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Expr
C.LitInt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
Type a
Word64 -> forall a. Type a -> Expr -> Expr
explicitty Type a
ty forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Expr
C.LitInt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
Type a
Float -> forall a. Type a -> Expr -> Expr
explicitty Type a
ty forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Expr
C.LitFloat
Type a
Double -> forall a. Type a -> Expr -> Expr
explicitty Type a
ty forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Expr
C.LitDouble
Struct a
_ -> \a
v ->
TypeName -> NonEmpty InitItem -> Expr
C.InitVal (forall a. Type a -> TypeName
transtypename Type a
ty) (forall a. [Value a] -> NonEmpty InitItem
constStruct (forall a. Struct a => a -> [Value a]
toValues a
v))
Array Type t
ty' -> \a
v ->
TypeName -> NonEmpty InitItem -> Expr
C.InitVal (forall a. Type a -> TypeName
transtypename Type a
ty) (forall a. Type a -> [a] -> NonEmpty InitItem
constarray Type t
ty' (forall (n :: Nat) a. Array n a -> [a]
arrayelems a
v))
constinit :: Type a -> a -> C.Init
constinit :: forall a. Type a -> a -> Init
constinit Type a
ty a
val = case Type a
ty of
Array Type t
ty' -> NonEmpty InitItem -> Init
C.InitList forall a b. (a -> b) -> a -> b
$ forall a. Type a -> [a] -> NonEmpty InitItem
constarray Type t
ty' forall a b. (a -> b) -> a -> b
$ forall (n :: Nat) a. Array n a -> [a]
arrayelems a
val
Struct a
_ -> NonEmpty InitItem -> Init
C.InitList forall a b. (a -> b) -> a -> b
$ forall a. [Value a] -> NonEmpty InitItem
constStruct (forall a. Struct a => a -> [Value a]
toValues a
val)
Type a
_ -> Expr -> Init
C.InitExpr forall a b. (a -> b) -> a -> b
$ forall a. Type a -> a -> Expr
constty Type a
ty a
val
constfieldinit :: Value a -> C.InitItem
constfieldinit :: forall a. Value a -> InitItem
constfieldinit (Value Type t
ty (Field t
val)) = Maybe Name -> Init -> InitItem
C.InitItem forall a. Maybe a
Nothing forall a b. (a -> b) -> a -> b
$ forall a. Type a -> a -> Init
constinit Type t
ty t
val
constStruct :: [Value a] -> NonEmpty.NonEmpty C.InitItem
constStruct :: forall a. [Value a] -> NonEmpty InitItem
constStruct [Value a]
val = forall a. [a] -> NonEmpty a
NonEmpty.fromList forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a. Value a -> InitItem
constfieldinit [Value a]
val
constarray :: Type a -> [a] -> NonEmpty.NonEmpty C.InitItem
constarray :: forall a. Type a -> [a] -> NonEmpty InitItem
constarray Type a
ty =
forall a. [a] -> NonEmpty a
NonEmpty.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (Maybe Name -> Init -> InitItem
C.InitItem forall a. Maybe a
Nothing forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Type a -> a -> Init
constinit Type a
ty)
explicitty :: Type a -> C.Expr -> C.Expr
explicitty :: forall a. Type a -> Expr -> Expr
explicitty Type a
ty = TypeName -> Expr -> Expr
C.Cast (forall a. Type a -> TypeName
transtypename Type a
ty)
transtype :: Type a -> C.Type
transtype :: forall a. Type a -> Type
transtype Type a
ty = case Type a
ty of
Type a
Bool -> TypeSpec -> Type
C.TypeSpec forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"bool"
Type a
Int8 -> TypeSpec -> Type
C.TypeSpec forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"int8_t"
Type a
Int16 -> TypeSpec -> Type
C.TypeSpec forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"int16_t"
Type a
Int32 -> TypeSpec -> Type
C.TypeSpec forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"int32_t"
Type a
Int64 -> TypeSpec -> Type
C.TypeSpec forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"int64_t"
Type a
Word8 -> TypeSpec -> Type
C.TypeSpec forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"uint8_t"
Type a
Word16 -> TypeSpec -> Type
C.TypeSpec forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"uint16_t"
Type a
Word32 -> TypeSpec -> Type
C.TypeSpec forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"uint32_t"
Type a
Word64 -> TypeSpec -> Type
C.TypeSpec forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.TypedefName Name
"uint64_t"
Type a
Float -> TypeSpec -> Type
C.TypeSpec TypeSpec
C.Float
Type a
Double -> TypeSpec -> Type
C.TypeSpec TypeSpec
C.Double
Array Type t
ty' -> Type -> Maybe Expr -> Type
C.Array (forall a. Type a -> Type
transtype Type t
ty') Maybe Expr
length
where
length :: Maybe Expr
length = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Integer -> Expr
C.LitInt forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall (n :: Nat) t. KnownNat n => Type (Array n t) -> Id
tylength Type a
ty
Struct a
s -> TypeSpec -> Type
C.TypeSpec forall a b. (a -> b) -> a -> b
$ Name -> TypeSpec
C.Struct (forall a. Struct a => a -> Name
typename a
s)
transtypename :: Type a -> C.TypeName
transtypename :: forall a. Type a -> TypeName
transtypename Type a
ty = Type -> TypeName
C.TypeName forall a b. (a -> b) -> a -> b
$ forall a. Type a -> Type
transtype Type a
ty
constNumTy :: Type a -> Integer -> C.Expr
constNumTy :: forall a. Type a -> Integer -> Expr
constNumTy Type a
ty =
case Type a
ty of
Type a
Float -> Float -> Expr
C.LitFloat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger
Type a
Double -> Double -> Expr
C.LitDouble forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger
Type a
_ -> Integer -> Expr
C.LitInt
specializeMathFunName :: Type a -> String -> String
specializeMathFunName :: forall a. Type a -> Name -> Name
specializeMathFunName Type a
ty Name
s
| Name -> Bool
isMathFPArgs Name
s
, Type a
Float <- Type a
ty
= Name
s forall a. [a] -> [a] -> [a]
++ Name
"f"
| Bool
otherwise
= Name
s
where
isMathFPArgs :: String -> Bool
isMathFPArgs :: Name -> Bool
isMathFPArgs = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem
[ Name
"acos", Name
"asin", Name
"atan", Name
"atan2", Name
"cos", Name
"sin"
, Name
"tan", Name
"acosh", Name
"asinh", Name
"atanh", Name
"cosh", Name
"sinh"
, Name
"tanh", Name
"exp", Name
"exp2", Name
"expm1", Name
"frexp", Name
"ilogb"
, Name
"ldexp", Name
"log", Name
"log10", Name
"log1p", Name
"log2", Name
"logb"
, Name
"modf", Name
"scalbn", Name
"scalbln", Name
"cbrt", Name
"fabs", Name
"hypot"
, Name
"pow", Name
"sqrt", Name
"erf", Name
"erfc", Name
"lgamma", Name
"tgamma"
, Name
"ceil", Name
"floor", Name
"nearbyint", Name
"rint", Name
"lrint", Name
"llrint"
, Name
"round", Name
"lround", Name
"llround", Name
"trunc", Name
"fmod", Name
"remainder"
, Name
"remquo", Name
"copysign", Name
"nextafter", Name
"nexttoward", Name
"fdim"
, Name
"fmax", Name
"fmin", Name
"fma"
]
typeIsFloating :: Type a -> Bool
typeIsFloating :: forall a. Type a -> Bool
typeIsFloating Type a
Float = Bool
True
typeIsFloating Type a
Double = Bool
True
typeIsFloating Type a
_ = Bool
False