
Hi! I have much fun with rigid types, type signatures and GADTs. And I would like to invite also others in and share my joy. ;-) Please see the attached file and chase a solution to how to make it compile. I would like to have a function which I would call like: createNerve (Axon undefined) (AxonAny undefined) and it would return proper Nerve. Similar to how asTypeOf works. I would like to do that to remove repeating code like: from <- newChan for <- newChan let nerve = Nerve (Axon from) (AxonAny for) which I have to write again and again just to make types work out. Why I cannot move that into the function? I am using GHC 6.12.3. Is this something which will work in 7.0? Mitar

Hello Mitar, Friday, November 5, 2010, 12:45:21 PM, you wrote:
from <- newChan for <- newChan let nerve = Nerve (Axon from) (AxonAny for)
create = do from <- newChan for <- newChan return$ Nerve (Axon from) (AxonAny for) main = do nerve <- create ... -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Hi!
On Fri, Nov 5, 2010 at 10:49 AM, Bulat Ziganshin
Friday, November 5, 2010, 12:45:21 PM, you wrote:
from <- newChan for <- newChan let nerve = Nerve (Axon from) (AxonAny for)
create = do from <- newChan for <- newChan return$ Nerve (Axon from) (AxonAny for)
main = do nerve <- create ...
OK. It is necessary to check the attached file to understand. ;-) I would like to call it like "create (Axon undefined) (AxonAny undefined)" and get in that case "Nerve (Axon a) (AxonAny b)" as a result. If I would call it like "create (AxonAny undefined) (AxonAny undefined)" I would get "Nerve (AxonAny a) (AxonAny b)" as a result. And so on. So I know I can move some hard-coded combination into a function. But I would like to cover all combinations and tell with arguments which combination I want. Mitar

Hello Mitar, Friday, November 5, 2010, 2:08:52 PM, you wrote:
I would like to call it like "create (Axon undefined) (AxonAny undefined)" and get in that case "Nerve (Axon a) (AxonAny b)" as a result. If I would call it like "create (AxonAny undefined) (AxonAny undefined)" I would get "Nerve (AxonAny a) (AxonAny b)" as a result. And so on.
look into HsLua sources. it does something like you asking (converting return type into sequence of commands) so it mau be what you are looking for. it uses typeclasses for this effect. the same technique used in haskell printf implementation afaik -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Hi!
On Fri, Nov 5, 2010 at 12:12 PM, Bulat Ziganshin
look into HsLua sources. it does something like you asking (converting return type into sequence of commands) so it mau be what you are looking for. it uses typeclasses for this effect. the same technique used in haskell printf implementation afaik
While reading the source code I found that in loadstring you do not make sure that free is called even in a case of an exception. Is this not necessary? Mitar

On 05.11.2010 14:08, Mitar wrote:
So I know I can move some hard-coded combination into a function. But I would like to cover all combinations and tell with arguments which combination I want.
I'm not sure what do you exactly want. But what about applicative functors? They offer nice notation
Nerve <$> (Axon <$> newChan) <*> (AxonAny <$> newChan)

Hi, Mitar wrote:
I would like to do that to remove repeating code like:
from<- newChan for<- newChan let nerve = Nerve (Axon from) (AxonAny for)
which I have to write again and again just to make types work out. Why I cannot move that into the function?
One option is to write a little library of functions which create axons and nerves: newAxon :: Impulse i => IO (Axon (Chan i) i AxonConductive) newAxon = do chan <- newChan return (Axon chan) newAxonAny :: IO (Axon (Chan i) AnyImpulse AxonConductive) newAxonAny = do chan <- newChan return (AxonAny chan) newNoAxon :: Monad m => m (Axon (Chan i) i AxonNotConductive) newNoAxon = do return NoAxon newNerve :: Monad m => m (Axon a a' b) -> m (Axon c c' d) -> m (Nerve a a' b c c' d) newNerve newFrom newFor = do from <- newFrom for <- newFor return (Nerve from for) Note that newNerve does not take Axons, but rather monadic actions which create Axons. Now, you can use something like nerve <- newNerve newAxon newAxonAny to create a concrete nerve. Tillmann PS. It might be an interesting exercise to use either liftM and liftM2 (from Control.Monad) or the (<$>) and (<*>) operators from Control.Applicative to implement these functions without do notation. PPS. Crosspost removed. I don't want to post to mailing lists which I do not read.

Hi!
On Fri, Nov 5, 2010 at 4:01 PM, Tillmann Rendel
Note that newNerve does not take Axons, but rather monadic actions which create Axons. Now, you can use something like
nerve <- newNerve newAxon newAxonAny
to create a concrete nerve.
Thanks! I decided for this approach. It seems to me the nicest. Simple and allows me to later redefine Nerve and Axon without breaking stuff around. Mitar
participants (4)
-
Alexey Khudyakov
-
Bulat Ziganshin
-
Mitar
-
Tillmann Rendel