Created
May 8, 2012 06:51
-
-
Save mwotton/2633129 to your computer and use it in GitHub Desktop.
fun with forall
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 ExistentialQuantification #-} | |
{-# LANGUAGE RankNTypes #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE ImpredicativeTypes #-} | |
module Forall where | |
import Control.Applicative | |
import Data.Aeson | |
import Control.Monad | |
type Foo = forall a. (FromJSON a) => a -> String | |
data Bar = Bar Int | |
data Baz = Baz Int Int | |
instance FromJSON Baz where | |
parseJSON (Object v) = Baz <$> v .: "baz1" | |
<*> v .: "baz2" | |
parseJSON _ = mzero | |
instance FromJSON Bar where | |
parseJSON (Object v) = Bar <$> v .: "bar" | |
parseJSON _ = mzero | |
showBaz_foo o = case fromJSON o of | |
Error s -> s | |
Success (Baz a b) -> "Baz " ++ show a ++ "," ++ show b | |
showBar_foo o = case fromJSON o of | |
Error s -> s | |
Success (Bar a) -> "Bar " ++ show a | |
fooList = [showBaz_foo,showBar_foo] | |
objects = [object ["baz1" .= Number 1, "baz2" .= Number 2], | |
object ["bar" .= Number 3]] | |
-- res works fine | |
res = zipWith (\a b -> a b) fooList objects | |
-- go fails miserably | |
go :: [(String, Foo)] -> Value -> String -> String | |
go table v key = case lookup key table of | |
Nothing -> "not there" | |
Just x -> case fromJSON v of | |
Error s -> "broken: " ++ s | |
Success thing -> x thing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
*Forall> :r
[1 of 1] Compiling Forall ( forall.hs, interpreted )
forall.hs:43:22:
Couldn't match expected type
t0 -> t1' with actual type
forall a. FromJSON a => a -> String'The function
x' is applied to one argument, but its type
Foo' has noneIn the expression: x thing
In a case alternative: Success thing -> x thing
Failed, modules loaded: none.