Created
May 15, 2016 03:17
-
-
Save cartazio/60eac732e7ac162916eaf828c9b1483c to your computer and use it in GitHub Desktop.
copattern hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GADTs, RankNTypes #-} | |
main = putStrLn "hello!" | |
-- StreamModel is what we ordinarily write as "Stream" in haskell, | |
-- though is not ideal for a variety of reasons. | |
data StreamModel a = StreamModel { head :: a , tail :: StreamModel a} | |
data StreamTag a {- param list -} res {- result type! -} where | |
HeadTag :: StreamTag a a | |
TailTag :: StreamTag a (Stream a) | |
newtype Stream a = Stream { unStream :: (forall res . StreamTag a res -> res) } | |
headStream:: Stream a -> a | |
headStream (Stream f) = f HeadTag | |
tailStream :: Stream a -> Stream a | |
tailStream (Stream f) = f TailTag | |
-- this is just zipWith for Stream, | |
rawRawZipWith :: (a -> b ->c ) -> Stream a -> Stream b -> Stream c | |
rawRawZipWith f sta stb = Stream $ \ x -> | |
case x of | |
HeadTag -> f (headStream sta) (headStream stb) | |
TailTag -> rawRawZipWith f (tailStream sta) (tailStream stb) | |
--- edit1: heres how to go from Stream (the coinductive definition) | |
--- to StreamModel (the usual stream definition in haskell) | |
stream2StreamModel :: Stream a -> StreamModel a | |
stream2StreamModel strm = StreamModel (headStream strm) (tailStream strm) | |
-- edit2 : clarified that the StreamModel type is only included for simplicity :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
discussion at https://www.reddit.com/r/haskell/comments/4aju8f/simple_example_of_emulating_copattern_matching_in/