You are not logged in.

#1 2015-04-06 18:41:39

shpelda
Member
Registered: 2008-08-07
Posts: 59

[SOLVED] How to Haskell polymorphic IO ?

Hello, can some haskeller around here help and explain why following does not compile, and how to make it working?

import Graphics.UI.Gtk
makeBox:: Bool -> IO BoxClass
makeBox True = hBoxNew False 17
makeBox _ = vBoxNew False 17
main = do putStrLn "Hello, World!"

My goal is to have a function that returns some abstract box based on parameter, but i do propably miss some level of understanding of haskell polymorphism.

Compiler complains with :

    Expecting one more argument to ‘BoxClass’
    The first argument of ‘IO’ should have kind ‘*’,
      but ‘BoxClass’ has kind ‘* -> Constraint’
    In the type signature for ‘makeBox’: makeBox :: Bool -> IO BoxClas

Last edited by shpelda (2015-04-06 21:03:27)

Offline

#2 2015-04-06 19:40:12

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [SOLVED] How to Haskell polymorphic IO ?

Multiple issues:

  • As the error message says, BoxClass needs another argument.

  • More importantly, BoxClass is a type class, so you can't use it in a type signature in this way (only in a context/qualifier to the left of a double arrow).

  • Your makeBox has two cases and they produce values of different types. You can't do that.

Edit: you're thinking about subtype polymorphism (as in most OO languages). Haskell has parametric polymorphism and (with type classes) ad-hoc polymorphism. Those are not the same.

Last edited by Raynman (2015-04-06 19:46:08)

Offline

#3 2015-04-06 19:50:46

shpelda
Member
Registered: 2008-08-07
Posts: 59

Re: [SOLVED] How to Haskell polymorphic IO ?

My main problem is this "Your makeBox has two cases and they produce values of different types. You can't do that.", the rest are just attempts to get through this.

EDIT: Yes, i was after subtype polymorphism. I do more research. thanks.

Last edited by shpelda (2015-04-06 20:27:29)

Offline

#4 2015-04-06 20:25:19

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [SOLVED] How to Haskell polymorphic IO ?

You could say a factory method is a complex way to get something like higher-order functions in those OO languages. You can turn it into something like this:

makeBox :: BoxClass a => (Bool -> Int -> a) -> a
makeBox f = f False 17

So the argument f is a sort of constructor function (it might even be more appropriate to call that makeBox and rename the function). The code that calls makeBox gets to choose the type of box (by passing an 'f' that produces a particular 'a' -- as in "makeBox vBoxNew :: VBox"); in your example, makeBox would make the choice (although it was based on the boolean argument).

This is also more general than your example where you could only have two types of boxes because you can pass any function with the right type. But it is still unnecessarily restricted: the constraint (BoxClass a) is not needed, because makeBox doesn't use this in any way; all it does is apply the function to a particular Bool and Int.

Last edited by Raynman (2015-04-06 20:34:08)

Offline

#5 2015-04-06 21:01:56

shpelda
Member
Registered: 2008-08-07
Posts: 59

Re: [SOLVED] How to Haskell polymorphic IO ?

You saved me a lot of pain. Thank you.

Offline

Board footer

Powered by FluxBB