Visit Jeremy's Blog.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Haskell: Why are these ambiguous?
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 04-26-2011, 12:42 AM #1 hydraMax Member   Registered: Jul 2010 Location: Skynet Distribution: Debian + Emacs Posts: 467 Blog Entries: 60 Rep: Haskell: Why are these ambiguous? I'm learning Haskell on my own, I would appreciate it if a Haskell programmer could clear something up for me: For learning purposes, I wrote up a few type constructors and some functions, like so: Code: ```data Point a = Point a a deriving (Show) xCoord :: (Num a) => (Point a) -> a xCoord (Point x y) = x yCoord :: (Num a) => (Point a) -> a yCoord (Point x y) = y data Line a = Line (Point a) (Point a) deriving (Show) p1 :: (Num a) => Line a -> Point a p1 (Line a b) = a p2 :: (Num a) => Line a -> Point a p2 (Line a b) = b length :: (Floating a) => (Line a) -> a length (Line a b) = sqrt (adj * adj + opp * opp) where c = Line a b adj = xCoord (p2 c) - xCoord (p1 c) opp = yCoord (p2 c) - yCoord (p1 c)``` Then I start ghci, load the file, and run some code like so: Code: ```*Main> :l plot [1 of 1] Compiling Main ( plot.hs, interpreted ) Ok, modules loaded: Main. *Main> let l1 = Line (Point 5.0 2.0) (Point 8.0 (-3.0)) *Main> length l1 :1:0: Ambiguous occurrence `length' It could refer to either `Main.length', defined at plot.hs:17:0 or `Prelude.length', imported from Prelude``` So, I get what it is saying here: In other words, ghci can't tell whether I am referring to the length function I defined or the preloaded length function. Obviously, I can just be more specific: Code: ```*Main> Main.length l1 5.830951894845301``` This makes sense. But this is what is confusing me: Why is it that Haskell can't determine which function to use based on the type of the parameter I am passing in? The function types don't leave any ambiguity: Code: ```*Main> :t Prelude.length Prelude.length :: [a] -> Int *Main> :t Main.length Main.length :: (Floating a) => Line a -> a``` I'm passing in a single Line, not a list, so why can't ghci tell the difference? Is this an inherent limitation in the language, or do I need to write my code differently somehow? BTW, I also tried the more generic function definition of length: Code: ```length :: (Floating a) => (Line a) -> a length a = sqrt (adj * adj + opp * opp) where adj = xCoord (p2 a) - xCoord (p1 a) opp = yCoord (p2 a) - yCoord (p1 a)``` But it didn't seem to make any difference regarding the ambiguity issue. Side note: I am willing to wait a few days to get a response to this post. If you do not know the answer, please do not reply, as that will destroy this thread's zero-reply status.
 04-27-2011, 12:32 PM #2 acid_kewpie Moderator   Registered: Jun 2001 Location: UK Distribution: Gentoo, RHEL, Fedora, Centos Posts: 43,415 Rep: At the risk of incurring your wrath, I'm not at all familiar with Haskel, but I would never expect a language to know which function to run in a situation like this, save for overloaded function definitions. Under various languages it's possible to define a function multiple times and have them be differentiated by, for example, the parameters being passed to them, e.g. "def myfunc()" vs "def myfunc(a, b, c)" and calling them accordingly like that, but these are functions within the same scope, so it's clearly there a positive feature of the programming language. What you're asking for might sound reasonable at a simplistic level, but it gets messy, and would certainly not be a good thing in my book. What if these two functions do utterly different things? You have no immediate way of having any idea which one is being called. You could think you're calling a function to get the length of an array, but maybe the function that's actually getting called due to the vagaries desired is actually calling an identically named function which, ooh... returnd the length of time it takes to delete your MBR... how would you know? And what if they do take the same inputs? you run a program on one environment which doesn't have those libraries and it works totally different to your own development environment. 1 members found this post helpful.
04-27-2011, 11:51 PM   #3
hydraMax
Member

Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467
Blog Entries: 60

Original Poster
Rep:
Quote:
 Originally Posted by acid_kewpie At the risk of incurring your wrath, I'm not at all familiar with Haskel, but I would never expect a language to know which function to run in a situation like this, save for overloaded function definitions. Under various languages it's possible to define a function multiple times and have them be differentiated by, for example, the parameters being passed to them, e.g. "def myfunc()" vs "def myfunc(a, b, c)" and calling them accordingly like that, but these are functions within the same scope, so it's clearly there a positive feature of the programming language. What you're asking for might sound reasonable at a simplistic level, but it gets messy, and would certainly not be a good thing in my book. What if these two functions do utterly different things? You have no immediate way of having any idea which one is being called. You could think you're calling a function to get the length of an array, but maybe the function that's actually getting called due to the vagaries desired is actually calling an identically named function which, ooh... returnd the length of time it takes to delete your MBR... how would you know? And what if they do take the same inputs? you run a program on one environment which doesn't have those libraries and it works totally different to your own development environment.
Somehow I'm guessing that my "wrath" doesn't carry much weight at linuxquestions. Nevertheless, it would have been preferable to have my zero-reply status destroyed by someone who had at least tried Haskell once.

But in all fairness, asking a specific question about a narrow language like Haskell at a generic Linux forum is kind of dumb. I guess I'll have to give in and actually join a Haskell mailing list.

04-28-2011, 12:35 AM   #4
paulsm4
Guru

Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep:
hydraMax -

Before you go off in too much of a huff , check out these links. I think you might find them interesting:

In particular:
Quote: