LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 04-26-2011, 12:42 AM   #1
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467
Blog Entries: 60

Rep: Reputation: 51
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

<interactive>: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.
 
Old 04-27-2011, 12:32 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,406

Rep: Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965
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.
Old 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: Reputation: 51
Quote:
Originally Posted by acid_kewpie View Post
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.
 
Old 04-28-2011, 12:35 AM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
hydraMax -

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

http://research.microsoft.com/en-us/...rs/oo-haskell/

http://www.haskell.org/onlinereport/decls.html

In particular:
Quote:
A problem inherent with Haskell -style overloading is the possibility of an ambiguous type. For example, using the read and show functions defined in Chapter 10, and supposing that just Int and Bool are members of Read and Show, then the expression

let x = read "..." in show x -- invalid

is ambiguous, because the types for show and read,

show :: forall a. Show a =>a ->String
read :: forall a. Read a =>String ->a

could be satisfied by instantiating a as either Int in both cases, or Bool. Such expressions are considered ill-typed, a static error.

We say that an expression e has an ambiguous type if, in its type forall u. cx =>t, there is a type variable u in u that occurs in cx but not in t. Such types are invalid.

For example, the earlier expression involving show and read has an ambiguous type since its type is forall a. Show a, Read a =>String.

Ambiguous types can only be circumvented by input from the user....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Haskell: I/O Not in Order? hydraMax Programming 2 04-18-2011 07:02 PM
[SOLVED] Are there any haskell programers who can help me with learning? HajdukJanko Programming 12 01-24-2011 06:45 PM
Haskell & OpenGL Denyam Slackware 1 12-02-2009 07:59 PM
haskell question satori@sanitarium Programming 1 10-20-2009 05:35 PM
Rant: Who actually likes Haskell? Berticus General 5 10-27-2008 01:53 PM


All times are GMT -5. The time now is 01:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration