Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Types and the difference between GHCi        interactive and a
      file (Daniel Fischer)
   2. Re:  Empty 'do' constructor (jean verdier)
   3.  The Data Parallel Haskell example from the       Haskell Wiki
      won't work (Jo?o Paulo Pizani Flor)
   4. Re:  Happy generated parser fails to compile. (Rohit Garg)
   5.  matching the output of a standard function to my function
      definition (non-exhaustive pattern problem) (Martin Tomko)
   6. Re:  matching the output of a standard function to        my
      function definition (non-exhaustive pattern problem) (Daniel Fischer)
   7. Re:  matching the output of a standard function   to my
      function definition (non-exhaustive pattern problem) (Brent Yorgey)
   8. Re:  matching the output of a standard function   to my
      function definition (non-exhaustive pattern problem) (Martin Tomko)


----------------------------------------------------------------------

Message: 1
Date: Sun, 26 Sep 2010 23:29:02 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Types and the difference between GHCi
        interactive and a file
To: [email protected], [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

On Sunday 26 September 2010 22:46:35, Russ Abbott wrote:
> The following runs without a problem.
>
> Prelude> let null' xs = xs == []
> null' :: (Eq a) => [a] -> Bool
>
> Prelude> let main' = do print (null' [])
> main' :: IO ()
>
> Prelude> main'
> True
> it :: ()
>
> But if I put essentially the same code in a file and try to load the
> file I get an error.
>
> File: Null
>
> null' xs = xs == []
>
> main = do print (null' [])
>
>
> Prelude> :load "Null.hs"
> [1 of 1] Compiling Main             ( Null.hs, interpreted )
>
> Null.hs:3:17:
>     Ambiguous type variable `a' in the constraint:
>       `Eq a' arising from a use of `null'' at Null.hs:3:17-24
>     Probable fix: add a type signature that fixes these type variable(s)
> Failed, modules loaded: none.
>
> Why is that?

ghci has extended defaulting rules, a lot of things have their type 
defaulted to avoid too many "Ambiguous type variable ..." messages at the 
prompt although per the report they shouldn't be defaulted.

When a source file is compiled (whether to object code or to bytecode), the 
defaulting rules of the report are applied. The expression "null' []" has 
the ambiguous type Eq a => [a] -> Bool (there's no way to determine at 
which type the expression should be evaluated).
The defaulting rules in the language report,
http://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-790004.3.4
say such an ambiguity can be resolved by defaulting under certain 
cirumstances, a necessary condition is that there is one numeric type class 
constraint. That isn't the case here, so defaulting isn't allowed, 
compilation fails.

At the prompt, ghci merrily resolves the ambiguity by defaulting a to ().

>
>
> I had thought that the ambiguity was referring to the type of [] in the
> print statement, i.e., that GHC can't figure out the type of [].  If I
> modify the print statement to be   print ( null' ([] :: [Int]) ) 
> everything is ok.

Right.

>
> But if that's the case, why is this not a problem at the interactive
> level?

Extended defaulting rules, because otherwise there would be too many 
ambiguity errors.
At the prompt, most expressions you enter have very little context which 
would allow to determine a type, in source files there's usually much more 
context available, so ambiguities are less frequent in code.

>
> Here is a related question.  In the following what does it mean to say
> that x is of type [a] where "a" is a type variable?
>
> Prelude> let x = []
> x :: [a]

It means x is a polymorphic expression, x can belong to every list type.

Prelude> ([x,[True]],[x,"ah"],[x,[fromEnum 'u']])
([[],[True]],["","ah"],[[],[117]])

It can even appear at different types in the same expression.

>
> For example,
>
> Prelude> x == (tail [1 :: Int])
> True
> it :: Bool
>
> Prelude> x == (tail [1 :: Float])
> True
> it :: Bool
>
> Prelude> (tail [1 :: Int]) == (tail [1 :: Float])
>
> <interactive>:1:28:
>     Couldn't match expected type `Int' against inferred type `Float'
>     In the expression: 1 :: Float
>     In the first argument of `tail', namely `[1 :: Float]'
>     In the second argument of `(==)', namely `(tail [1 :: Float])'
>
> Can a value like the value of x really be of an incompletely specified
> type?

Yes, that's possible. [], Nothing and others have this property.
Although, it would be more correct to say that x can be instantiated at 
different types.

>
> I couldn't do that in the file.

You can, but there are a few things that can prevent it.
One is the monomorphism restriction (aka the dreaded MR).

>
> When I tried the following in the file,
>
> print (null' ([] :: (Eq a) => a))
>
> I got this error message on loading the file.
>
> Null.hs:3:24:
>     Couldn't match expected type `a1' against inferred type `[a]'
>       `a1' is a rigid type variable bound by
>            an expression type signature at Null.hs:3:34
>     In the first argument of `null'', namely `([] :: (Eq a) => a)'
>     In the first argument of `print', namely
>         `(null' ([] :: (Eq a) => a))'
>     In the expression: print (null' ([] :: (Eq a) => a))
>

Yes, that's not very illuminating at first.
The point of this message is that you said [] could have any type, as long 
as it belongs to Eq, in particular, it could be Bool. However, the most 
general type of [] is [a], so it could be any type, as long as it's a list 
type - Bool isn't, so you have the type mismatch, the promised type
(Eq a) => a versus the inferred type [b].

If you fix that and write

print (null' ([] :: (Eq a) => [a]))

You'll get a different error, the one you got before you added the 
expression type signature:

   Ambiguous type variable `b' in the constraint:
      `Eq b'
        arising from an expression type signature at TTest.hs:3:22-40
    Probable fix: add a type signature that fixes these type variable(s)

>
> Thanks.
>
> -- Russ



------------------------------

Message: 2
Date: Mon, 27 Sep 2010 09:50:45 +0200
From: jean verdier <[email protected]>
Subject: Re: [Haskell-beginners] Empty 'do' constructor
To: Magnus Therning <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="UTF-8"

You are expecting unlines to be String -> [String] but it's [String] ->
String. The function you're looking for is probably Data.List.lines.

The list indexing operator is !! and not !.

Your function should then probably be:

myManupulation cs = (lines cs) !! 3



On Sat, 2010-09-25 at 10:02 +0100, Magnus Therning wrote:
> On 25/09/10 08:21, Sok H. Chang wrote:
> > Thank you to your answer!
> > I indent me code as you said.
> > I tried using Tab, using spaces…
> > But can't work.
> >
> > Is there another possibility?
> > Thank you!
> 
> With the code looking like this:
> 
> main = do cs <- readFile "C:\\SPR.txt"
>           putStrLn $ myManupulation cs
> 
> myManupulation cs = (unlines cs) ! 3
> 
> Then it's indented properly, but it still doesn't compile because its types
> don't line up properly.
> 
> /M
> 
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners




------------------------------

Message: 3
Date: Mon, 27 Sep 2010 11:06:16 -0300
From: Jo?o Paulo Pizani Flor <[email protected]>
Subject: [Haskell-beginners] The Data Parallel Haskell example from
        the     Haskell Wiki won't work
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

Hello dear Haskell People! I'm currently very interested in doing some
parallel programming in Haskell, specially in using the Data Parallel
Haskell stuff...

So as a first step I tried to emulate the vector multiply example found in
the Haskell Wiki:
http://www.haskell.org/haskellwiki/GHC/Data_Parallel_Haskell#A_simple_example

The code I've written (two files) for this example is in the following
pastebins:

DPHVecMul.hs:  http://paste.org/pastebin/view/22899
Main.hs:  http://paste.org/pastebin/view/22900

I've compiled successfully both files and linked them, exactly as described
in the Wiki. But when I run the resulting executable, the following error
shows up:

"dotp: Prelude.undefined"

It's seems (from the tests I've done) that the call to the function
*fromPArrayP
*of the module *Data.Parallel.Array.Prelude *results always in undefined. In
fact, I've taken a look at the function definition in the docs, and in fact
it always returns undefined. It doesn't make ANY SENSE to me :P

Has someone been able to successfully compile and run this example code?? If
so, what am I doing wrong? Is there some obvious error I'm incurring in? :D


Regards,

João Paulo Pizani Flor
[email protected]
Computer Science - 2007/1
Federal University of Santa Catarina - Brazil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100927/52326885/attachment-0001.html

------------------------------

Message: 4
Date: Mon, 27 Sep 2010 20:14:42 +0530
From: Rohit Garg <[email protected]>
Subject: Re: [Haskell-beginners] Happy generated parser fails to
        compile.
To: Stephen Tetley <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

> runCalc :: String -> [Expr]
> runCalc ss = calc $ alexScanTokens ss

Thanks, that fixed the problem. :)
>
> Best wishes
>
> Stephen
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



-- 
Rohit Garg

http://rpg-314.blogspot.com/


------------------------------

Message: 5
Date: Mon, 27 Sep 2010 17:41:24 +0200
From: Martin Tomko <[email protected]>
Subject: [Haskell-beginners] matching the output of a standard
        function to my function definition (non-exhaustive pattern problem)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Dear all

I am sure this is a very beginner problem, but I am not sure how to 
solve it:
I have a myFunct function:
myFunct :: Int -> [a] -> a

defined as:
myFunct _ [] = error "empty list provided as arg"
myFunct a [b] | length [x|x<-[b],fid x == a] == 0 = error "no match"
                       |    otherwise = head [x|x<-[b],fid x == a]


Which works fine (may not be elegant, but works). Now, I am chaining it 
in a larger function, where at some stage I use a graph object from 
Data.Graph and check for its edges, resulting in [(Int,Int)]
trying to run this fails:

  myFunct (fst (head (edges graph))) myList

*** Exception: FunctStruct.hs:(136,1)-(138,48): Non-exhaustive patterns 
in function myFunct

contrived example, but myList is a list of [a]). The problem arrises 
from the definition of the type of Edge as (Vertex,Vertex), but type 
Vertex = Int 
(http://hackage.haskell.org/packages/archive/containers/0.2.0.1/doc/html/Data-Graph.html),
 
so that should be ok, or am I making a mistake somewhere and I should go 
from vertex to Integer explicitly?

Thanks a lot!
Martin


------------------------------

Message: 6
Date: Mon, 27 Sep 2010 17:56:46 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] matching the output of a standard
        function to     my function definition (non-exhaustive pattern problem)
To: [email protected], [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

On Monday 27 September 2010 17:41:24, Martin Tomko wrote:
> Dear all
>
> I am sure this is a very beginner problem, but I am not sure how to
> solve it:
> I have a myFunct function:
> myFunct :: Int -> [a] -> a
>
> defined as:
> myFunct _ [] = error "empty list provided as arg"
> myFunct a [b] | length [x|x<-[b],fid x == a] == 0 = error "no match"
>                        |    otherwise = head [x|x<-[b],fid x == a]

The pattern `[b]' matches only lists of length 1.
[b] is syntactic sugar for (b : [])

So when you pass a longer list to the function, no pattern matches.

Presumably you wanted the second equation to treat all nonempty lists, n 
which case you simply have to replace all three occurrences of `[b]' with a 
generic variable, e.g. b.

Since you test for empty lists first, no empty list ever reaches that 
equation.

Also, don't use length list == 0 to check for empty lists, if list is long, 
that needs a long time (and possibly a lot of space).

To check for empty lists, use null.

Or pattern match. That would also improve the style in the second equation:

myFunct _ [] = error ".."
myFunct a xs = case [x | x <- xs, fid x == a] of
                [] -> error "no match"
                (y:_) -> y

Cheers,
Daniel


------------------------------

Message: 7
Date: Mon, 27 Sep 2010 12:08:59 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] matching the output of a standard
        function        to my function definition (non-exhaustive pattern 
problem)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Mon, Sep 27, 2010 at 05:41:24PM +0200, Martin Tomko wrote:
> Dear all
> 
> I am sure this is a very beginner problem, but I am not sure how to
> solve it:
> I have a myFunct function:
> myFunct :: Int -> [a] -> a
> 
> defined as:
> myFunct _ [] = error "empty list provided as arg"
> myFunct a [b] | length [x|x<-[b],fid x == a] == 0 = error "no match"
>                       |    otherwise = head [x|x<-[b],fid x == a]

May I also suggest that instead of implementing this yourself, you
could just use

  myFunct a b = find ((==a) . fid) b

'find' is from Data.List and returns the first thing in the list
satisfying the given predicate, or Nothing if there is no such
element.

-Brent


------------------------------

Message: 8
Date: Mon, 27 Sep 2010 18:15:22 +0200
From: Martin Tomko <[email protected]>
Subject: Re: [Haskell-beginners] matching the output of a standard
        function        to my function definition (non-exhaustive pattern 
problem)
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Dear Daniel
that worked perfectly, thanks for the suggestion!

One more question: how do you use null to check for empty lists? 
something like [] == null did not work ... just chekcing, I got my 
orignal code to work, thanks again!
Martin


On 9/27/2010 5:56 PM, Daniel Fischer wrote:
> On Monday 27 September 2010 17:41:24, Martin Tomko wrote:
>    
>> Dear all
>>
>> I am sure this is a very beginner problem, but I am not sure how to
>> solve it:
>> I have a myFunct function:
>> myFunct :: Int ->  [a] ->  a
>>
>> defined as:
>> myFunct _ [] = error "empty list provided as arg"
>> myFunct a [b] | length [x|x<-[b],fid x == a] == 0 = error "no match"
>>                         |    otherwise = head [x|x<-[b],fid x == a]
>>      
> The pattern `[b]' matches only lists of length 1.
> [b] is syntactic sugar for (b : [])
>
> So when you pass a longer list to the function, no pattern matches.
>
> Presumably you wanted the second equation to treat all nonempty lists, n
> which case you simply have to replace all three occurrences of `[b]' with a
> generic variable, e.g. b.
>
> Since you test for empty lists first, no empty list ever reaches that
> equation.
>
> Also, don't use length list == 0 to check for empty lists, if list is long,
> that needs a long time (and possibly a lot of space).
>
> To check for empty lists, use null.
>
> Or pattern match. That would also improve the style in the second equation:
>
> myFunct _ [] = error ".."
> myFunct a xs = case [x | x<- xs, fid x == a] of
>                  [] ->  error "no match"
>                  (y:_) ->  y
>
> Cheers,
> Daniel
>
>    




------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 27, Issue 56
*****************************************

Reply via email to