Copyright | (C) 2013 Amgen Inc. |
---|---|
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Provides a shallow view of a SEXP
R value as an algebraic datatype. This
is useful to define functions over R values in Haskell with pattern matching.
For example:
toPair :: SEXP a -> (SomeSEXP, SomeSEXP) toPair (hexp -> List _ (Just car) (Just cdr)) = (SomeSEXP car, SomeSEXP cdr) toPair (hexp -> Lang car (Just cdr)) = (SomeSEXP car, SomeSEXP cdr) toPair s = error $ "Cannot extract pair from object of type " ++ typeOf s
(See SomeSEXP
for why we need to use it here.)
The view is said to be shallow
because it only unfolds the head of the
R value into an algebraic datatype. In this way, functions producing views
can be written non-recursively, hence inlined at all call sites and
simplified away. When produced by a view function in a pattern match,
allocation of the view can be compiled away and hence producing a view can be
done at no runtime cost. In fact, pattern matching on a view in this way is
more efficient than using the accessor functions defined in Foreign.R,
because we avoid the overhead of calling one or more FFI functions entirely.
HExp
is the view and hexp
is the view function that projects SEXP
s
into HExp
views.
Synopsis
- data HExp :: * -> SEXPTYPE -> * where
- Nil :: HExp s 'Nil
- Symbol :: a :∈ ['Char, 'Nil] => SEXP s a -> SEXP s b -> SEXP s c -> HExp s 'Symbol
- List :: (IsPairList b, c :∈ ['Symbol, 'Nil]) => SEXP s a -> SEXP s b -> SEXP s c -> HExp s 'List
- Env :: (IsPairList a, b :∈ ['Env, 'Nil], c :∈ ['Vector, 'Nil]) => SEXP s a -> SEXP s b -> SEXP s c -> HExp s 'Env
- Closure :: IsPairList a => SEXP s a -> SEXP s b -> SEXP s 'Env -> HExp s 'Closure
- Promise :: (IsExpression b, c :∈ ['Env, 'Nil]) => SEXP s a -> SEXP s b -> SEXP s c -> HExp s 'Promise
- Lang :: (IsExpression a, IsPairList b) => SEXP s a -> SEXP s b -> HExp s 'Lang
- Special :: HExp s 'Special
- Builtin :: HExp s 'Builtin
- Char :: !(Vector 'Char Word8) -> HExp s 'Char
- Logical :: !(Vector 'Logical Logical) -> HExp s 'Logical
- Int :: !(Vector 'Int Int32) -> HExp s 'Int
- Real :: !(Vector 'Real Double) -> HExp s 'Real
- Complex :: !(Vector 'Complex (Complex Double)) -> HExp s 'Complex
- String :: !(Vector 'String (SEXP V 'Char)) -> HExp s 'String
- DotDotDot :: IsPairList a => SEXP s a -> HExp s 'List
- Vector :: !Int32 -> !(Vector 'Vector (SomeSEXP V)) -> HExp s 'Vector
- Expr :: !Int32 -> !(Vector 'Expr (SomeSEXP V)) -> HExp s 'Expr
- Bytecode :: HExp s 'Bytecode
- ExtPtr :: c :∈ ['Symbol, 'Nil] => Ptr () -> SEXP s b -> SEXP s c -> HExp s 'ExtPtr
- WeakRef :: (a :∈ ['Env, 'ExtPtr, 'Nil], c :∈ ['Closure, 'Builtin, 'Special, 'Nil], d :∈ ['WeakRef, 'Nil]) => SEXP s a -> SEXP s b -> SEXP s c -> SEXP s d -> HExp s 'WeakRef
- Raw :: !(Vector 'Raw Word8) -> HExp s 'Raw
- S4 :: a :∈ ['Symbol, 'Nil] => SEXP s a -> HExp s 'S4
- (===) :: TestEquality f => f a -> f b -> Bool
- hexp :: SEXP s a -> HExp s a
- vector :: IsVector a => SEXP s a -> Vector a (ElemRep V a)
Documentation
data HExp :: * -> SEXPTYPE -> * where Source #
A view of R's internal SEXP
structure as an algebraic datatype. Because
this is in fact a GADT, the use of named record fields is not possible here.
Named record fields give rise to functions for whom it is not possible to
assign a reasonable type (existentially quantified type variables would
escape).
See https://cran.r-project.org/doc/manuals/r-release/R-ints.html#SEXPTYPEs.
Nil :: HExp s 'Nil | The NULL value ( |
Symbol | A symbol ( |
List | A list ( |
Env | An environment ( |
Closure | A closure ( |
Promise | A promise ( |
Lang | Language objects ( |
| |
Special :: HExp s 'Special | A special (built-in) function call ( |
Builtin :: HExp s 'Builtin | A |
Char :: !(Vector 'Char Word8) -> HExp s 'Char | An internal character string ( |
Logical :: !(Vector 'Logical Logical) -> HExp s 'Logical | A logical vector ( |
Int :: !(Vector 'Int Int32) -> HExp s 'Int | An integer vector ( |
Real :: !(Vector 'Real Double) -> HExp s 'Real | A numeric vector ( |
Complex :: !(Vector 'Complex (Complex Double)) -> HExp s 'Complex | A complex vector ( |
String :: !(Vector 'String (SEXP V 'Char)) -> HExp s 'String | A character vector ( |
DotDotDot | A special type of |
| |
Vector | A list/generic vector ( |
Expr | An expression vector ( |
Bytecode :: HExp s 'Bytecode | A ‘byte-code’ object generated by R ( |
ExtPtr | An external pointer ( |
WeakRef | A weak reference ( |
Raw :: !(Vector 'Raw Word8) -> HExp s 'Raw | A raw vector ( |
S4 | An S4 class which does not consist solely of a simple type such as an atomic vector or function ( |
(===) :: TestEquality f => f a -> f b -> Bool Source #
Heterogeneous equality.