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:  Re: Re: testing and the culture of Haskell (Alexander Dunlap)
   2.  Stack Overflow with foldl' (Cyril Pichard)
   3. Re:  QuickCheck 2 entering infinite loop (Dominic Mulligan)
   4. Re:  Stack Overflow with foldl' (Daniel Fischer)
   5.  if statements (John Moore)
   6. Re:  if statements (Roel van Dijk)
   7. Re:  if statements (Marc Weber)


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

Message: 1
Date: Fri, 22 Jan 2010 21:22:49 -0800
From: Alexander Dunlap <[email protected]>
Subject: Re: [Haskell-beginners] Re: Re: testing and the culture of
        Haskell
To: Adrian Adshead <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

When you put actions in a "do" block, what you are really doing under
the hood is using the ">>=" function to string actions together. (>>=)
:: m a -> (a -> m b) -> m b, so, when m ~ IO, (>>=) :: IO a -> (a ->
IO b) -> IO b. Thus, a do block of IO actions will itself be an IO
action. However, consider the function:

f :: IO a -> Int
f _ = 10

Now the value f (putStrLn "foo") is totally pure, and has the value of
10. A similar thing is going on with the greetAdrian function.

Hope that helps.

Alex

On Fri, Jan 22, 2010 at 7:34 PM, Adrian Adshead
<[email protected]> wrote:
>
> OK, so I get the idea that all functions are pure because they
> just return actions.
>
> So as I understand it now...
>
> All functions are pure.
> The evaluation of all functions not declared as IO results in no side effects.
> The evaluation of all functions declared as IO may or may not have side 
> effects.
>
> But how can the function greetAdrian from the example below not be an IO
> operation?
>
>
>
>
> From: Maciej Piechotka <uzytkownik2 <at> gmail.com>
> Subject: Re: Re: testing and the culture of Haskell
> Newsgroups: gmane.comp.lang.haskell.beginners
> Date: 2010-01-23 00:31:42 GMT (2 hours and 52 minutes ago)
>
> On Fri, 2010-01-22 at 20:59 +0000, Adrian Adshead wrote:
> > >All Haskell functions are pure without exception.  For example:
> > >
> > >greet :: String -> IO ()
> > >greet name = putStrLn $ "Hello, "++name
> > >
> > >This is a pure function from String to IO ().  This function (like all
> > >Haskell functions) has no side effects.  Its return value of type IO ()
> > >merely _represents_ an IO action.  The runtime system knows how to act
> > >on this representation.
> > >
> > >This also means that there is no such thing in Haskell as marking a
> > >function as side-effecting.
> > >
> > >This distinction may be subtle, but it's important.
> > >
> > >
> > >Steve
> >
> > Steve,
> >
> > Please could you clarify this for me since you are making exactly
> > the opposite assertion than I have
>  understood.
> >
> > I am confused by you stating "All Haskell functions are pure
> > without
> >  exception.".
> >
> > Pure functions have no impact on 'anything'. They take input
> > parameters (which they don't change) and return exactly the
> > same result whenever the same input parameters are given.
> >
> > >greet :: String -> IO ()
> > >greet name = putStrLn $ "Hello, "++name
> >
> > This example you gave is not a pure function since it does have
> > the side effect that the screen is changed by outputting the string
> > "Hello, " and the name passed in.
> >
> >
>
> greatAdrian :: String
> greetAdrian = let x = greet "Adrian"
>               in x `seq` f x
>
> greet can be consider a pure function and value IO () is evaluated by
> seq. IO () represents an action(s) not execution of action(s). If f of x
> does not use any tricks nothing will be
>  printed.
>
> IO a value it can be:
> - cast unsafely into a. However I guess we omit this shame for a moment
> - binded with other action. But the resultant type is now again IO b. So
> we still get a something
> - returned as main. Then we might consider whole Haskell program as
> metalanguage which returns single thing - other program. In similar way
> as:
>
> type PythonProgram = String
> main :: PythonProgram
> main = "print \"Hello World\""
>
> is pure add_impure is pure. What we do with the result is other thing.
>
> Regards
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>


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

Message: 2
Date: Sat, 23 Jan 2010 14:05:42 +0100
From: Cyril Pichard <[email protected]>
Subject: [Haskell-beginners] Stack Overflow with foldl'
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Hi,

I don't understand why the following code always result in a Stack space 
overflow error. 
I'm using foldl' and think the main algorithm is tail recursive. Am I wrong ?
I've read the http://www.haskell.org/haskellwiki/Stack_overflow
Any help would be greatly appreciated.

Thanks


module Main where

import Data.List

-- Data structures
data Point = Point { x::Double, y::Double, z::Double } deriving(Eq, Show)
data Triangle = Triangle { p1::Point, p2::Point, p3::Point } deriving(Eq, Show)
data BBox = BBox { bbmax::Point,  bbmin::Point } deriving(Eq,Show)

-- Construct a default triangle
newTriangle :: Triangle
newTriangle = Triangle (Point 1 0 0) (Point 0 1 0) (Point 0 0 1)

-- Construct a default BBox
newBBox = BBox (Point 0 0 0) (Point 0 0 0)

-- Defines min and max for Point
instance Ord Point where
   max a b = Point ( max (x a ) (x b) ) ( max (y a) (y b) ) ( max (z a) (z b) )
   min a b = Point ( min (x a ) (x b) ) ( min (y a) (y b) ) ( min (z a) (z b) )

-- Create a list of triangles
makeTriangles :: Double -> [Triangle]
makeTriangles 0 = []
makeTriangles n = [ Triangle (Point v 0 0) (Point 0 v 0) (Point 0 0 v) | v <- 
[1..n] ]

-- Compute the bounding box of a triangle 
boundingBox :: Triangle -> BBox
boundingBox t = BBox pmax pmin
                where pmax = max (p3 t) (max (p1 t) (p2  t))
                      pmin = min (p3 t) (min (p1 t) (p2  t))

-- Compute the bounding box of two bounding boxes                            
bboxAddb :: BBox -> BBox -> BBox
bboxAddb (BBox bb1max bb1min) (BBox bb2max bb2min) = BBox (max bb1max bb2max) 
(min bb1min bb2min)                             
                              
-- Compute the bounding box of a triangle and a bounding box 
bboxAddt :: BBox -> Triangle -> BBox
bboxAddt b t = bboxAddb b (boundingBox t)

main = do print $ foldl' bboxAddt newBBox triangles;
                  where triangles = makeTriangles 610000



error returned : 
Stack space overflow: current size 8388608 bytes.
Use `+RTS -Ksize' to increase it.

ghc version 6.10.3 on MacOsX Snow Leopard




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

Message: 3
Date: Fri, 22 Jan 2010 16:47:14 +0000
From: Dominic Mulligan <[email protected]>
Subject: Re: [Haskell-beginners] QuickCheck 2 entering infinite loop
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Dear Brent,

Thanks for the reply.  The type of args is [Type a].  Ascribing a 
specific type, like so:

  prop_InjectTypeConFreeVars :: [Type String] -> Bool
  prop_InjectTypeConFreeVars args =
    (freeTypeVars . (tyCon "Fun")) args == (concatMap freeTypeVars args)

Leads to the same problem; quickCheck is brought to its knees after 
about 6 tests.

Regards,
Dominic


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

Message: 4
Date: Sat, 23 Jan 2010 15:49:39 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Stack Overflow with foldl'
To: [email protected]
Cc: Cyril Pichard <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Samstag 23 Januar 2010 14:05:42 schrieb Cyril Pichard:
> Hi,
>
> I don't understand why the following code always result in a Stack space
> overflow error. I'm using foldl' and think the main algorithm is tail
> recursive. Am I wrong ? I've read the
> http://www.haskell.org/haskellwiki/Stack_overflow Any help would be
> greatly appreciated.
>
> Thanks
>

Because the 'seq' in foldl' evaluates its argument only to the outermost 
constructor.

So in foldl' bboxAddt bb triangles@(t1:tail), it starts evaluating

bboxAddt bb t1

~> bboxAddb bb (boundingBox t)

now bb must be matched against (BBox mxb mnb) and boundingBox t must be 
matched against (BBox mxt mnt) if the first match succeeds (which it does).
Now boundingBox t = BBox calculatelater1 calculatelater2, so that match 
succeeds too.
Then the definition of bboxAddb says the result of

bboxAddt bb t1

is z' = BBox (max mxb calculatelater1) (min mnb calculatelater2)

The next step in foldl' is

z' `seq` foldl' bboxAddt z' tail

but z' `seq` something only evaluates z' far enough to see it's a 
(BBox _ _), the components remain happily unevaluated.
And so on. At the end of the list, you have the result

BBox hugeThunk1 hugeThunk2

When you try to evaluate one of these thunks, the stack overflows.

You need to force the evaluation of the components in each step.
The easiest way is to make the data structures strict. For this case, it is 
sufficient to make BBox and Point strict:

data Point = Point { x, y, z :: !Double } deriving (Eq, Show)
data BBox = BBox { bbmax, bbmin :: !Point } deriving (Eq, Show)

though it's probably a good idea to make Triangle strict, too.
Then BBox can't have undefined components, evaluating a BBox to the 
outermost constructor (weak head normal form) forces the evaluation of its 
component Points to their outermost constructor, which in turn forces the 
evaluation of their respective components; those are Doubles and thus 
evaluating them to their outerost constructor evaluates them completely.

Now bb`seq` something evaluates bb completely (to normal form) when 
something is demanded, no huge thunks, no stack overflow anymore :)

Of course, if you need the possibility to have (BBox undefined undefined) 
without the programme aborting, you can't make the datatypes strict and 
have to make the folding function stricter, e.g.

import Control.DeepSeq -- from package deepseq

instance NFData Point where
    rnf (Point x y z) = x `seq` y `seq` z `seq` ()

instance NFData BBox where
    rnf (BBox mx mn) = rnf mx `seq` rnf mn

bbAddt' bb t = bb `deepseq` bbAddt bb t

foldl' bbAddt' newBBox triangles

>
> module Main where
>
> import Data.List
>
> -- Data structures
> data Point = Point { x::Double, y::Double, z::Double } deriving(Eq,
> Show) data Triangle = Triangle { p1::Point, p2::Point, p3::Point }
> deriving(Eq, Show) data BBox = BBox { bbmax::Point,  bbmin::Point }
> deriving(Eq,Show)
>
> -- Construct a default triangle
> newTriangle :: Triangle
> newTriangle = Triangle (Point 1 0 0) (Point 0 1 0) (Point 0 0 1)
>
> -- Construct a default BBox
> newBBox = BBox (Point 0 0 0) (Point 0 0 0)
>
> -- Defines min and max for Point
> instance Ord Point where
>    max a b = Point ( max (x a ) (x b) ) ( max (y a) (y b) ) ( max (z a)
> (z b) ) min a b = Point ( min (x a ) (x b) ) ( min (y a) (y b) ) ( min
> (z a) (z b) )
>
> -- Create a list of triangles
> makeTriangles :: Double -> [Triangle]
> makeTriangles 0 = []
> makeTriangles n = [ Triangle (Point v 0 0) (Point 0 v 0) (Point 0 0 v) |
> v <- [1..n] ]
>
> -- Compute the bounding box of a triangle
> boundingBox :: Triangle -> BBox
> boundingBox t = BBox pmax pmin
>                 where pmax = max (p3 t) (max (p1 t) (p2  t))
>                       pmin = min (p3 t) (min (p1 t) (p2  t))
>
> -- Compute the bounding box of two bounding boxes
> bboxAddb :: BBox -> BBox -> BBox
> bboxAddb (BBox bb1max bb1min) (BBox bb2max bb2min) = BBox (max bb1max
> bb2max) (min bb1min bb2min)
>
> -- Compute the bounding box of a triangle and a bounding box
> bboxAddt :: BBox -> Triangle -> BBox
> bboxAddt b t = bboxAddb b (boundingBox t)
>
> main = do print $ foldl' bboxAddt newBBox triangles;
>                 where triangles = makeTriangles 610000
>
>
>
> error returned :
> Stack space overflow: current size 8388608 bytes.
> Use `+RTS -Ksize' to increase it.
>
> ghc version 6.10.3 on MacOsX Snow Leopard




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

Message: 5
Date: Sat, 23 Jan 2010 17:40:10 +0000
From: John Moore <[email protected]>
Subject: [Haskell-beginners] if statements
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi
   I'm trying to get a if statement to work but not sure if this is the
right approach, what I want to do in simple english is to evaluate  one
expression at a time for example. (3+2)+(4+5) I want it to return 5 + (4+5)
then 5+9.

data Expression = Val Integer
                | Add Expression Expression
                | Subtract Expression Expression
                | Multiply Expression Expression
                | Divide Expression Expression
         deriving Show
evalStep :: Expression ->  Expression
evalStep (Val x)=  (Val x)

evalStep (Add x y) = do

  if x = isDigit
           then if y isDigit
             else evalStep x,y

I thinking about using recursion down a tree left then right but that seems
very complicated.
Any thoughts welcome!
John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100123/9ac013b2/attachment-0001.html

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

Message: 6
Date: Sat, 23 Jan 2010 19:17:55 +0100
From: Roel van Dijk <[email protected]>
Subject: Re: [Haskell-beginners] if statements
To: John Moore <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On Sat, Jan 23, 2010 at 6:40 PM, John Moore <[email protected]> wrote:
> Hi
>    I'm trying to get a if statement to work but not sure if this is the
> right approach, what I want to do in simple english is to evaluate  one
> expression at a time for example. (3+2)+(4+5) I want it to return 5 + (4+5)
> then 5+9.
>
> data Expression = Val Integer
>                 | Add Expression Expression
>                 | Subtract Expression Expression
>                 | Multiply Expression Expression
>                 | Divide Expression Expression
>          deriving Show
> evalStep :: Expression ->  Expression
> evalStep (Val x)=  (Val x)
> evalStep (Add x y) = do
>
>   if x = isDigit
>            then if y isDigit
>              else evalStep x,y
>
> I thinking about using recursion down a tree left then right but that seems
> very complicated.
> Any thoughts welcome!
> John


I renamed your Expression data type to simply E. Saves some typing :-)

data E = Val Integer
       | Add E E
       | Sub E E
       | Mul E E
       | Div E E
         deriving Show

evalStep :: E ->  E
evalStep (Val x) = (Val x)
-- Note that with a pattern match you can look deeply into a structure.
-- in this case we match on the addition of two values.
evalStep (Add (Val a) (Val b)) = Val (a + b)
evalStep (Add x@(Val _) y) = Add x (evalStep y)
evalStep (Add x y@(Val _)) = Add (evalStep x) y

You can copy the equations with "Add" and substitute with Sub, Mul and
Div and their related functions (-), (*) and div to get the complete
evalStep function.


Regards,
Roel


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

Message: 7
Date: Sat, 23 Jan 2010 19:27:17 +0100
From: Marc Weber <[email protected]>
Subject: Re: [Haskell-beginners] if statements
To: beginners <[email protected]>
Message-ID: <1264270210-sup-...@nixos>
Content-Type: text/plain; charset=UTF-8

Hi John,

You've several errors.
What is isDigit?

Let's start reading your code like the compiler does:

> evalStep (Add x y) = do
Fine Add x y thus both x and y are of type Expression
because the constructor Add has been defined this way:
>                 | Add Expression Expression

Now let's have a look at this line:
>   if x = isDigit

if x = isDigit

What is isDigit ?
Hoogle shows there is a isDigit function you didn't import.
http://haskell.org/ghc/docs/latest/html/libraries/base-4.2.0.0/Data-Char.html#v:isDigit
It's type is
  isDigit :: Char -> Bool

So your if statement reads as
if x is a function ?
You can't compare a function with a value "x".

Anyway you want == not = to compare for equality.

At this point I stop parsing your code because it doesn't make sense at
all. You usually use pattern matching for this kind of stuff:

evalStep (Add x y) = case x of
  Val xval -> case y of 
    Val yval -> (evalStep xval) + (evalStep yval)
    _ -> error "don't know what to do"
  _ -> error "don't know what to do"

This is too verbose. It doesn't make sense to write it this way.

Let's do it right ( I feel like doing ones homework here...) but I don't
implement every case so I don't feel guilty :)

Step by step:

evalStep (Add x y) =
  let xEvaled = evalStep x
      yEvaled = evalStep y
  in Val (xEvaled + yEvaled)
evalStep( Multiply  a b) = error "TODO implement multiply"
evalStep _               = error "TODO (everything else I didn't match 
explicitely)"

shorter (that's what I would do:)

evalStep (Add x y) = Val $ (evalStep x) + (evalStep y)
or even shorter:
evalStep (Add x y) = Val $ evalStep `on` (+) x y
`on` applies a function to both operands before applying the operand.

Summary: Whenever you want to use if .. else or a switch .. case ..
thing remember that Haskell does have pattern matching and you should
use it.

Your isDigit function could be implemented using pattern matching (in
fact it's the only way I know about). It looks like this:

isDigit (Val _) = true -- Val is a digit
isDigit _ = false      -- everything else is not

Then you could use if isDigit val then .. else ..
but it still doesn't make much sense because you still have to get the
val out of the constructor.

Example:
if isDigit val then
  let Val x = val -- now you can use x (which is Int) to use primitive +/-* 
operations
  in x * 2
else ..

It doesn't hurt visiting the haskell.org page and reading some
tutorials.

HTH
Marc Weber


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

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


End of Beginners Digest, Vol 19, Issue 21
*****************************************

Reply via email to