
Hello, This code runs ok http://lpaste.net/91814 but only thanks to enforcing [()] on line 12. In GHCI *Main> :t (List []) (List []) :: NestedList a and *Main> :t flatten (List []) flatten (List []) :: [a] That means ghc cannot infer the type. Is there a way how to # print flatten (List []) ? Or even more general, print [] without enforcing the type? Thank you Lukas

On Tue, Aug 13, 2013 at 10:46 AM, Lukas Lehner
That means ghc cannot infer the type. Is there a way how to # print flatten (List []) ? Or even more general, print [] without enforcing the type?
If you turn on the ExtendedDefaultRules extension ( `{-# LANGUAGE ExtendedDefaultRules #-}` pragma or `-X ExtendedDefaultRules` ghc option), ghc will infer () for the type just as ghci does. Note that this reduces type safety a bit, since ghc will now accept programs that have what otherwise would be type errors. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Good to know extension. And without it?
On Tue, Aug 13, 2013 at 4:59 PM, Brandon Allbery
On Tue, Aug 13, 2013 at 10:46 AM, Lukas Lehner
wrote: That means ghc cannot infer the type. Is there a way how to # print flatten (List []) ? Or even more general, print [] without enforcing the type?
If you turn on the ExtendedDefaultRules extension ( `{-# LANGUAGE ExtendedDefaultRules #-}` pragma or `-X ExtendedDefaultRules` ghc option), ghc will infer () for the type just as ghci does. Note that this reduces type safety a bit, since ghc will now accept programs that have what otherwise would be type errors.
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

2013/8/13 Lukas Lehner
Good to know extension. And without it?
Someone correct me if I'm wrong, here's how I understand it: To execute print [], ghc has to find which instance of Show to call. How could it find an instance without knowing the type ? [] could be a list of anything, and while in theory we know that in all cases it should print "[]", in practice ghc can't call that code if it doesn't know the type of the list. David.

That I understand. My question is that I have to enforce the type that specific way (don't want to use ExtendedDefaultRules) or is there some more generic way? On Wed, Aug 14, 2013 at 12:08 PM, David Virebayre < dav.vire+haskell@gmail.com> wrote:
2013/8/13 Lukas Lehner
: Good to know extension. And without it?
Someone correct me if I'm wrong, here's how I understand it:
To execute print [], ghc has to find which instance of Show to call. How could it find an instance without knowing the type ? [] could be a list of anything, and while in theory we know that in all cases it should print "[]", in practice ghc can't call that code if it doesn't know the type of the list.
David.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

2013/8/14 Lukas Lehner
That I understand. My question is that I have to enforce the type that specific way (don't want to use ExtendedDefaultRules) or is there some more generic way?
Ah, sorry, I don't know of a more generic way. But I'm not experienced enough to be sure. My experience is than in real world code, most of the time, the way you use the values gives enough info for ghc to infer a type. Sometimes, you can avoid adding type information by using aTypeOf from the Prelude. David.

There is a way. It's print "[]" if the compiler cannot infer the type of an empty list, it's because you wrote a literal empty list []. Sorry for giving such a practical (non-theoretical) answer ;) Michael

Lukas Lehner
writes: ... My question is that I have to enforce the type that specific way (don't want to use ExtendedDefaultRules) or is there some more generic way?
print flatten (List ([] :: Show a => [a]) Even for an empty list, we have to provide evidence that the list is showable. That's what the type signature is doing: I am a list of something/anything showable. (If you want all of your NestedLists's to be showable, it's better to put a Show constraint on the data decl. But that would take us into existential types or GADT's.) AntC
participants (5)
-
AntC
-
Brandon Allbery
-
David Virebayre
-
Lukas Lehner
-
Michael Peternell