Safe Haskell | None |
---|---|
Language | Haskell2010 |
Typed deep embedding of simple C expressions
This is a subset of C expressions that only have simple non-compound and non-pointed types, and that don't contain any control structures.
(Of course, nothing stops one from translating CExp
to something other than
C, but its constructors and set of supported types is inspired by C.)
Synopsis
- data Unary a where
- evalUnary :: Unary a -> a
- unaryOp :: Unary a -> UnOp
- data Binary a where
- BiAdd :: Num a => Binary (a -> a -> a)
- BiSub :: Num a => Binary (a -> a -> a)
- BiMul :: Num a => Binary (a -> a -> a)
- BiDiv :: Fractional a => Binary (a -> a -> a)
- BiQuot :: Integral a => Binary (a -> a -> a)
- BiRem :: Integral a => Binary (a -> a -> a)
- BiAnd :: Binary (Bool -> Bool -> Bool)
- BiOr :: Binary (Bool -> Bool -> Bool)
- BiEq :: CType a => Binary (a -> a -> Bool)
- BiNEq :: CType a => Binary (a -> a -> Bool)
- BiLt :: (Ord a, CType a) => Binary (a -> a -> Bool)
- BiGt :: (Ord a, CType a) => Binary (a -> a -> Bool)
- BiLe :: (Ord a, CType a) => Binary (a -> a -> Bool)
- BiGe :: (Ord a, CType a) => Binary (a -> a -> Bool)
- evalBinary :: Binary a -> a
- binaryOp :: Binary a -> BinOp
- type SupportCode = forall m. MonadC m => m ()
- data Sym sig where
- Lit :: String -> a -> Sym (Full a)
- Const :: String -> a -> Sym (Full a)
- Fun :: Signature sig => String -> Denotation sig -> Sym sig
- UOp :: Unary (a -> b) -> Sym (a :-> Full b)
- Op :: Binary (a -> b -> c) -> Sym (a :-> (b :-> Full c))
- Cast :: (a -> b) -> Sym (a :-> Full b)
- Cond :: Sym (Bool :-> (a :-> (a :-> Full a)))
- Var :: VarId -> Sym (Full a)
- ArrIx :: (Integral i, Ix i) => IArr i a -> Sym (i :-> Full a)
- WithCode :: SupportCode -> Sym (a :-> Full a)
- data T sig where
- newtype CExp a = CExp {}
- evalSym :: Sym sig -> Denotation sig
- evalCExp :: CExp a -> a
- compCExp :: forall m a. MonadC m => CExp a -> m Exp
- constFold :: CExp a -> CExp a
- castAST :: forall a b. Typeable b => ASTF T a -> Maybe (ASTF T b)
- viewLit :: CExp a -> Maybe a
- pattern LitP :: forall a a1. () => (CType (DenResult (Full a)), Full a ~ Full a1) => a1 -> CExp a
- pattern LitP' :: forall sig a. () => (CType (DenResult sig), sig ~ Full a) => a -> AST T sig
- pattern NonLitP :: CExp a
- pattern NonLitP' :: ASTF T a
- pattern OpP :: forall a a1 a2 a3 b c. () => (CType (DenResult (a2 :-> (a1 :-> Full a))), (a2 :-> (a1 :-> Full a)) ~ (a3 :-> (b :-> Full c))) => Binary (a3 -> b -> c) -> AST T (Full a2) -> AST T (Full a1) -> CExp a
- pattern OpP' :: forall sig a1 a2 a3 b c. () => (CType (DenResult (a2 :-> (a1 :-> sig))), (a2 :-> (a1 :-> sig)) ~ (a3 :-> (b :-> Full c))) => Binary (a3 -> b -> c) -> AST T (Full a2) -> AST T (Full a1) -> AST T sig
- pattern UOpP :: forall a a1 a2 b. () => (CType (DenResult (a1 :-> Full a)), (a1 :-> Full a) ~ (a2 :-> Full b)) => Unary (a2 -> b) -> AST T (Full a1) -> CExp a
- pattern UOpP' :: forall sig a1 a2 b. () => (CType (DenResult (a1 :-> sig)), (a1 :-> sig) ~ (a2 :-> Full b)) => Unary (a2 -> b) -> AST T (Full a1) -> AST T sig
- isFloat :: forall a. CType a => CExp a -> Bool
- isExact :: CType a => CExp a -> Bool
- isExact' :: CType a => ASTF T a -> Bool
- value :: CType a => a -> CExp a
- constant :: CType a => String -> a -> CExp a
- variable :: CType a => VarId -> CExp a
- withCode :: CType a => (forall m. MonadC m => m ()) -> CExp a -> CExp a
- true :: CExp Bool
- false :: CExp Bool
- quot_ :: (Eq a, Integral a, CType a) => CExp a -> CExp a -> CExp a
- (#%) :: (Integral a, CType a) => CExp a -> CExp a -> CExp a
- i2n :: (Integral a, Num b, CType b) => CExp a -> CExp b
- i2b :: Integral a => CExp a -> CExp Bool
- b2i :: (Integral a, CType a) => CExp Bool -> CExp a
- not_ :: CExp Bool -> CExp Bool
- (#&&) :: CExp Bool -> CExp Bool -> CExp Bool
- (#||) :: CExp Bool -> CExp Bool -> CExp Bool
- (#==) :: (Eq a, CType a) => CExp a -> CExp a -> CExp Bool
- (#!=) :: (Eq a, CType a) => CExp a -> CExp a -> CExp Bool
- (#<) :: (Ord a, CType a) => CExp a -> CExp a -> CExp Bool
- (#>) :: (Ord a, CType a) => CExp a -> CExp a -> CExp Bool
- (#<=) :: (Ord a, CType a) => CExp a -> CExp a -> CExp Bool
- (#>=) :: (Ord a, CType a) => CExp a -> CExp a -> CExp Bool
- cond :: CType a => CExp Bool -> CExp a -> CExp a -> CExp a
- (?) :: CType a => CExp Bool -> CExp a -> CExp a -> CExp a
- (#!) :: (CType a, Integral i, Ix i) => IArr i a -> CExp i -> CExp a
Expressions
BiAdd :: Num a => Binary (a -> a -> a) | |
BiSub :: Num a => Binary (a -> a -> a) | |
BiMul :: Num a => Binary (a -> a -> a) | |
BiDiv :: Fractional a => Binary (a -> a -> a) | |
BiQuot :: Integral a => Binary (a -> a -> a) | |
BiRem :: Integral a => Binary (a -> a -> a) | |
BiAnd :: Binary (Bool -> Bool -> Bool) | |
BiOr :: Binary (Bool -> Bool -> Bool) | |
BiEq :: CType a => Binary (a -> a -> Bool) | |
BiNEq :: CType a => Binary (a -> a -> Bool) | |
BiLt :: (Ord a, CType a) => Binary (a -> a -> Bool) | |
BiGt :: (Ord a, CType a) => Binary (a -> a -> Bool) | |
BiLe :: (Ord a, CType a) => Binary (a -> a -> Bool) | |
BiGe :: (Ord a, CType a) => Binary (a -> a -> Bool) |
evalBinary :: Binary a -> a Source #
type SupportCode = forall m. MonadC m => m () Source #
Syntactic symbols for C
Lit :: String -> a -> Sym (Full a) | |
Const :: String -> a -> Sym (Full a) | |
Fun :: Signature sig => String -> Denotation sig -> Sym sig | |
UOp :: Unary (a -> b) -> Sym (a :-> Full b) | |
Op :: Binary (a -> b -> c) -> Sym (a :-> (b :-> Full c)) | |
Cast :: (a -> b) -> Sym (a :-> Full b) | |
Cond :: Sym (Bool :-> (a :-> (a :-> Full a))) | |
Var :: VarId -> Sym (Full a) | |
ArrIx :: (Integral i, Ix i) => IArr i a -> Sym (i :-> Full a) | |
WithCode :: SupportCode -> Sym (a :-> Full a) |
Instances
Equality Sym Source # | |
Render Sym Source # | |
Defined in Language.Embedded.CExp | |
StringTree Sym Source # | |
Defined in Language.Embedded.CExp | |
Symbol Sym Source # | |
Defined in Language.Embedded.CExp |
Instances
Equality T Source # | |
Render T Source # | |
Defined in Language.Embedded.CExp | |
StringTree T Source # | |
Defined in Language.Embedded.CExp | |
Symbol T Source # | |
Defined in Language.Embedded.CExp |
C expression
Instances
EvalExp CExp Source # | |
Defined in Language.Embedded.CExp | |
FreeExp CExp Source # | |
CompExp CExp Source # | |
Eq (CExp a) Source # | |
(Fractional a, Ord a, CType a) => Fractional (CExp a) Source # | |
(Num a, Ord a, CType a) => Num (CExp a) Source # | |
Syntactic (CExp a) Source # | |
type FreePred CExp Source # | |
Defined in Language.Embedded.CExp | |
type Internal (CExp a) Source # | |
Defined in Language.Embedded.CExp | |
type Domain (CExp a) Source # | |
Defined in Language.Embedded.CExp |
evalSym :: Sym sig -> Denotation sig Source #
constFold :: CExp a -> CExp a Source #
One-level constant folding: if all immediate sub-expressions are literals, the expression is reduced to a single literal
pattern LitP :: forall a a1. () => (CType (DenResult (Full a)), Full a ~ Full a1) => a1 -> CExp a Source #
pattern LitP' :: forall sig a. () => (CType (DenResult sig), sig ~ Full a) => a -> AST T sig Source #
pattern OpP :: forall a a1 a2 a3 b c. () => (CType (DenResult (a2 :-> (a1 :-> Full a))), (a2 :-> (a1 :-> Full a)) ~ (a3 :-> (b :-> Full c))) => Binary (a3 -> b -> c) -> AST T (Full a2) -> AST T (Full a1) -> CExp a Source #
pattern OpP' :: forall sig a1 a2 a3 b c. () => (CType (DenResult (a2 :-> (a1 :-> sig))), (a2 :-> (a1 :-> sig)) ~ (a3 :-> (b :-> Full c))) => Binary (a3 -> b -> c) -> AST T (Full a2) -> AST T (Full a1) -> AST T sig Source #
pattern UOpP :: forall a a1 a2 b. () => (CType (DenResult (a1 :-> Full a)), (a1 :-> Full a) ~ (a2 :-> Full b)) => Unary (a2 -> b) -> AST T (Full a1) -> CExp a Source #
pattern UOpP' :: forall sig a1 a2 b. () => (CType (DenResult (a1 :-> sig)), (a1 :-> sig) ~ (a2 :-> Full b)) => Unary (a2 -> b) -> AST T (Full a1) -> AST T sig Source #
isFloat :: forall a. CType a => CExp a -> Bool Source #
Return whether the type of the expression is a floating-point numeric type
isExact :: CType a => CExp a -> Bool Source #
Return whether the type of the expression is a non-floating-point type
isExact' :: CType a => ASTF T a -> Bool Source #
Return whether the type of the expression is a non-floating-point type
User interface
Predefined constant
quot_ :: (Eq a, Integral a, CType a) => CExp a -> CExp a -> CExp a Source #
Integer division truncated toward zero
(#%) :: (Integral a, CType a) => CExp a -> CExp a -> CExp a Source #
Integer remainder satisfying
(x `quot_` y)*y + (x #% y) == x
Conditional expression
Condition operator; use as follows:
cond1 ? a $ cond2 ? b $ cond3 ? c $ default