[Haskell-cafe] Basic question about recursive data type...

Nicholls, Mark nicholls.mark at vimn.com
Tue Mar 10 14:54:00 UTC 2015

a long time since I did some Haskell and my brain has reverted to its OO dogma...

> {-# LANGUAGE GADTs, TypeFamilies, TypeOperators, MultiParamTypeClasses, FlexibleInstances, UndecidableInstances, FlexibleContexts #-}


> data Next input output where
>   End :: Next () ()
>   Next :: (input -> output) -> Next input output

this is a bit like curried function...
I can construct

> y :: Next Integer (Next String (Next () ()))
> y = Next (\x ->
>        if (x == 1) then
>          Next (\y ->
>            if (y == "s") then
>               End
>            else
>               End)
>        else
>          Next (\y ->
>            if (y == "t") then
>               End
>            else
>               End))

which is a bit like "Integer->String->()"
I can execute it...

> exe :: Next input output -> input -> output
> exe (Next f) i  = f i
> exe End ()      = ()

> p :: Next String (Next () ())
> p = exe y 1


but how do I constrain all the outputs to be of type Next?

I don't want this to be valid

> z :: Next Integer Integer
> z = Next (\x -> x + 1)


This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited.

While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data.

Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us.

MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe.  MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc.  Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150310/46697e8d/attachment.html>

More information about the Haskell-Cafe mailing list