2 Replies - 992 Views - Last Post: 25 July 2011 - 04:45 PM

#1 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Haskell: is there a way to use a list of type (Show a) => [a]?

Posted 25 July 2011 - 03:29 PM

ok so i was reading this and an idea struck me in Haskell how to make an sprintf that excepted as list of values that except type Show and and a formatting string to print them. however it seems it's impossible to construct a list of different types even if you are storing them as implementers of Show.

i tried the following.

test = ["test", 10] :: (Show a) => [a] --here, why can't i do this? it seems legit

sprintf' :: (Show a) => [Char] -> [a] -> [Char]
sprintf' (x:xs) [email protected](y:ys)
    | x == '%' && (head xs) /= '%' = (show y) ++ (sprintf' xs ys)
    | x == '%' && (head xs) == '%' = '%' : (sprintf' (tail xs) yn)
    | otherwise                    = x : (sprintf' xs yn)
sprintf' _ [] = ""
sprintf' [] _ = ""



but i get the following error
Could not deduce (a ~ [Char])
from the context (Show a)
  bound by an expression type signature: Show a => [a]
  at C:\Users\ishka\Desktop\haskhell\test3.hs:1:8-38
  `a' is a rigid type variable bound by
      an expression type signature: Show a => [a]
      at C:\Users\ishka\Desktop\haskhell\test3.hs:1:8
In the expression: "test"
In the expression: ["test", 10] :: Show a => [a]
In an equation for `test': test = ["test", 10] :: Show a => [a]



so why can't it be inferred that "test" is meant to be of type Show? is there a way to do this?

edit:
btw if your testing this the formatting of strings is a bit different from a normal sprintf. normally you have something like %s or %i, but becuase the way i was thinking Show was polymorphic i decided the extra bit was not necessary so rather i just used a single % in place of all types. %% adds a single % to the string.

This post has been edited by ishkabible: 25 July 2011 - 03:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Haskell: is there a way to use a list of type (Show a) => [a]?

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 4,210
  • Joined: 21-June 11

Re: Haskell: is there a way to use a list of type (Show a) => [a]?

Posted 25 July 2011 - 04:38 PM

If you declare a value x to be of type Foo a => a, this means that for all a which are instances of Foo, x can be used as a value of that type. For example 42 has the type Num a => a because it can be used as an Int, an Integer, a Double or whatever else instance of Num you want.

The same is not true for your test list. Even if it were possible to create heterogeneous lists in Haskell, you couldn't just pass it as an argument to a function which takes for example a list of strings as its argument, so this would clearly be the wrong type for it.

That being said, it's simply not possible to create heterogeneous lists in Haskell, so it doesn't matter what the correct type would be if it were possible. You just can't do it.

The closest thing you could get to what you want, would be to use GHC's extension for existential types to create an existential wrapper type, which contains a value of any Show instance. You could then create a list of that type. However using it would look like this: "[MyExistentialWrapper "test", MyExistentialWrapper 10]", so this isn't quite what you want, since this isn't any less verbose than just calling show on all the elements of the list and making your function accept a list of strings.
Was This Post Helpful? 1
  • +
  • -

#3 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: Haskell: is there a way to use a list of type (Show a) => [a]?

Posted 25 July 2011 - 04:45 PM

that makes perfect sense now that you explain it that way. thanks :)

basically what was happening is i had 2 diffrent types Show [Char] and Show Int, right? so they actually are different types unlike i was thinking.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1