Cloud Haskell - How to write "clean" for closures?

I played with Cloud Haskell . I noticed a kind of applicative interface in the hackage documentation there. But in particular, I'm trying to find or write a function closurePurewith the following signature:

closurePure :: (Typeable a, Binary a) => a -> Closure a

This is basically a limited version of pure.

While the type itself Closureis abstract, the following Closureprovided:

closure :: Static (ByteString -> a) -> ByteString -> Closure a

So, I can go this far:

closurePure :: (Typeable a, Binary a) => a -> Closure a
closurePure x = closure ??? (encode x)

The problem is what to put there ???.

My first attempt was as follows:

myDecode :: (Typeable a, Binary a) => Static (ByteString -> a)
myDecode = staticPtr (static decode)

, GHC , show , , Typeable. , Dict:

myDecode :: Typeable a => Static (Dict (Binary a) -> ByteString -> a)
myDecode = staticPtr (static (\Dict -> decode))

, Closure .

closurePure - ( Cloud Haskell)? binary Closure , , .

, :

class StaticDecode a where
  staticPtrDecode :: StaticPtr (ByteString -> a)

instance StaticDecode Int where
  staticPtrDecode = static Data.Binary.decode

instance StaticDecode Float where
  staticPtrDecode = static Data.Binary.decode

instance StaticDecode Integer where
  staticPtrDecode = static Data.Binary.decode

-- More instances etc...

myPure :: forall a. (Typeable a, StaticDecode a, Binary a) => a -> Closure a
myPure x = closure (staticPtr staticPtrDecode) (encode x)

, binary. , .

+6
2

, Closure - , , distributed-closure. , pure , - .

. , , . , , , . , , , . , ? , :

class GimmeStaticPtr c where
  gimmeStaticPtr :: StaticPtr (Dict c)

. StaticPtr - *, Constraint. constraints, (Dict ), *. , GimmeStaticPtr, .

, . StaticPtr , Closure is. , distributed-closure , , ,

class GimmeClosure c where
  gimmeClosure :: Closure (Dict c)   

closurePure :

closurePure :: (Typeable a, GimmeClosure (Binary a)) => a -> Closure a

, GimmeClosure , . - Template Haskell. GimmeClosure (Cls a) Cls. . withStatic .

,

+4

, . , . :

data BinaryDict a = BinaryDict 
  { bdEncode :: a -> ByteString
  , bdDecode :: ByteString -> a
  }

:

closurePure :: (Typeable a) => BinaryDict a -> a -> Closure a

:

closurePure bdict = closure (staticPtr (static (bdDecode bdict))) . bdEncode bdict

, , , , static . BinaryDict willy nilly, , , . :

closurePure :: (Typeable a) => Static (BinaryDict a) -> a -> Closure a

Binary . , , . , , .

, , , . ( , reflection? ). , , , ( ).

, , (déjà vu?).

class c => StaticConstraint c where
    staticConstraint :: StaticPtr (Dict c)
instance StaticConstraint (Show Int) where
    staticConstraint = static Dict
-- a handful more lines...

, ( ), , , :

closurePure :: (Typeable a, Binary a) => StaticPtr (ByteString -> a) -> a -> Closure a
closurePure decodePtr = closure (staticPtr decodePtr) . encode

someClosure :: Closure Int
someClosure = closurePure (static decode) 42

, static " ", , - , , Binary Int .

, {-# LANGUAGE CPP #-}

-- PURE :: (Binary a, Typeable a) => a -> Closure a, I promise
#define PURE (closurePure (static decode))

someClosure :: Closure Int
someClosure = PURE 42

, - Haskell Segmentation fault (core dumped) , .

+2

All Articles