简体   繁体   中英

Functions to Polymorphic data types

data Foo a is defined like:

data Foo a where
  Foo :: (Typeable a, Show a) => a -> Foo a
  -- perhaps more constructors

instance Show a => Show (Foo a) where
  show (Foo a) = show a

with some instances:

fiveFoo :: Foo Int
fiveFoo = Foo 5

falseFoo :: Foo Bool
falseFoo = Foo False

How can I define any function from b -> Foo a , for example:

getFoo :: (Show a, Typeable a) => String -> Foo a
getFoo "five" = fiveFoo
getFoo "false" = falseFoo

Here getFoo does not type check with Couldn't match type 'a' with 'Bool' .

The only thing that I am interested in here is for a to be of class Show so I can use getFoo like:

main = getLine >>= (print . getFoo)

You can use existential types to make a data type hide and "carry" a type class like Show around.

Note that using existential types like this is considered to be an anti-pattern in Haskell, and you probably want to consider carefully whether you really want to do this: being more explicit about your types is usually simpler, better, and less prone to bugs.

However, that being said, if you really want to do this, here is how you would use existential types with your example:

{-# LANGUAGE ExistentialQuantification #-}

-- This Foo can only be constructed with instances of Show as its argument.
data Foo = forall a. Show a => Foo a

-- Note that there is no "Show a => ..." context here:
-- Foo itself already carries that constraint around with it.
instance Show Foo where
  show (Foo a) = show a


getFoo :: String -> Foo
getFoo "five" = Foo 5
getFoo "false" = Foo False

main = print . getFoo =<< getLine

Demonstration:

ghci> main
five
5
ghci> main
false
False

Perhaps you want to omit the type parameter from Foo.

data Foo where
  Foo :: (Typeable a, Show a) => a -> Foo

instance Show Foo where
  show (Foo a) = show a

fiveFoo :: Foo
fiveFoo = Foo (5 :: Int) -- (Foo 5) doesn't work because of ambiguity

falseFoo :: Foo
falseFoo = Foo False

getFoo :: String -> Foo
getFoo "five" = fiveFoo
getFoo "false" = falseFoo

print $ getFoo "five" -- prints '5'
print $ getFoo "false" -- prints 'False'
getFoo :: (Show a, Typeable a) => String -> Foo a
getFoo "five" = fiveFoo
getFoo "false" = falseFoo

If fiveFoo :: Foo Int and falseFoo :: Foo Bool , you're essentially asking for getFoo to return a different type depending on what value you feed it at run-time. You can't do that. In Haskell, all types must be known at compile-time .

If all you want to be able to do is convert the thing to a string, why not just store it as a string in the first place? I'm guessing the answer is that this is actually a simplification of the real problem you're trying to solve...(?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM