rank-2 :(

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

rank-2 :(

Bulat Ziganshin
Hello ,

those rank-2 types wil make me mad :)

encodeHelper :: (MRef m r, Binary m a, BitStream m (StringBuffer m r))
             => a -> m String
encodeHelper x = do h <- newStringBuffer "" stringBufferDefaultCloseFunc
                    put_ h x
                    getStringBuffer h
encode x = runST (encodeHelper x)

i can't force `encode` to compile, either with or without any type
signatures. actually, signature for `encodeHelper` was developed by
Hugs, but now i don't know how to proceed further

just now tried to force Hugs tell me its type. mission impossible :)

DataAltBinaryClass> :t (runST (encodeHelper 1))
ERROR - Cannot justify constraints in application
*** Expression    : encodeHelper 1
*** Type          : ST c [Char]
*** Given context : ()
*** Constraints   : (Num b, Binary (ST c) b)


Binary class in my library includes monad in its header. May be it's
the cause of problems?

class Binary m a where
    -- | Convert value to sequence of bits and write it to the stream
    put_   :: (BitStream m h) => h -> a -> m ()

but it's needed because MArray types, for example, can be serialized
only in the appropriate monad:

instance Binary IO (IOArray a) where ...

--
Best regards,
 Bulat                          mailto:[hidden email]



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: rank-2 :(

Ross Paterson
On Tue, Jan 10, 2006 at 08:47:32PM +0300, Bulat Ziganshin wrote:

> those rank-2 types wil make me mad :)
>
> encodeHelper :: (MRef m r, Binary m a, BitStream m (StringBuffer m r))
>              => a -> m String
> encodeHelper x = do h <- newStringBuffer "" stringBufferDefaultCloseFunc
>                     put_ h x
>                     getStringBuffer h
> encode x = runST (encodeHelper x)
>
> i can't force `encode` to compile, either with or without any type
> signatures. actually, signature for `encodeHelper` was developed by
> Hugs, but now i don't know how to proceed further
>
> just now tried to force Hugs tell me its type. mission impossible :)
>
> DataAltBinaryClass> :t (runST (encodeHelper 1))
> ERROR - Cannot justify constraints in application
> *** Expression    : encodeHelper 1
> *** Type          : ST c [Char]
> *** Given context : ()
> *** Constraints   : (Num b, Binary (ST c) b)

It's telling you that your expression is ill-typed, and that

        encodeHelper 1 :: (Num b, Binary (ST s) b) => ST s String

runST is declared with an argument type in which s is unconstrained,
so this is rejected.

> Binary class in my library includes monad in its header. May be it's
> the cause of problems?
>
> class Binary m a where
>     -- | Convert value to sequence of bits and write it to the stream
>     put_   :: (BitStream m h) => h -> a -> m ()

Yes, that's the problem.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe