Re: Loading package GHC in GHCi

2005-01-09 Thread Andre Pang
On 09/01/2005, at 5:49 PM, Sean Seefried wrote:
I have managed to build package GHC *and* load it into GHCi.  
Initially this did not work. When I loaded up ghci with the -package 
ghc flag I was assaulted with the following error message.

 GHCi runtime linker: fatal error: I found a duplicate definition for 
symbol
 _OutOfHeapHook
 whilst processing object file
 /Users/sseefried/cvs-ghc/PLUGGABLE/working/ghc/compiler/HSghc.o
 This could be caused by:
 * Loading two different object files which export the same symbol
 * Specifying the same object file twice on the GHCi command line
 * An incorrect `package.conf' entry, causing some object to be
 loaded twice.
 GHCi cannot safely continue in this situation. Exiting now. Sorry.
Just to clarify -- this is exactly the same problem that you posted 
about a day or two earlier?  The error message seems to be the same, 
anyway :).

I tracked down this symbol in the GHC source and found it in 
ghc/compiler/parser/hschooks.c.  The purpose of redefining the 
functions within this file is apparently to improve the quality of the 
error messages. That is, the symbols generated are meant to override 
those in the RTS. Unfortunately GHCi doesn't like this at all. At the 
moment it prohibits loading a symbol that is already in the RTS, which 
seems very reasonable from a certain perspective - I can see that a 
duplicate symbol would usually be an error. Except that in the case we 
really *do* want to load it so that it overrides the old one.

The only solution I can come up with is a modification to the 
package.conf syntax so that one can specify symbols which are part of 
the RTS package and the package in question.  We could then modify the 
dynamic linker of GHC so that these symbols were removed from the 
RTS's symbol table.  The symbols would then be loaded back in again in 
with the package thus overriding the old symbols.
This is non-portable, and is only possible due to GHC doing its own 
object loading.  (I'm still not 100% sure whether GHC's linker is able 
unload things on a per-symbol basis, especially portably.)  Even if 
this does work, it won't be possible whenever GHC uses platform-native 
shared libraries, in the far far future :).

It's probably best to follow the other thread for feasible solutions to 
this, since it's already being discussed there.

--
% Andre Pang : trust.in.love.to.save  
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: Newbie question

2005-01-09 Thread Dmitri Pissarenko
Thanks all for the help!
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: Newbie question

2005-01-09 Thread Robert Dockins
comments inline...

> module Main
>   where
> 
> import IO
> 
> main = do
>   hSetBuffering stdin LineBuffering
>   words <- askForNumbers
>   printWords words
>   map read words
>   putStrLn "The sum is"
>   foldl (+) 0 words

as you noted "map read words" is a problematic line.  The problem that
GHC tells you about is that "map read words" creates a value of type 
(Read a) => [a], where but each line in an IO do block needs to have
type IO a.  THe "foldl (+) 0 words" line has the same problem; it's not
an IO value.  A trick you can use is to use "let" to bind a pure value
inside a do block, see below.

I seems to me that you are thinking that "map read" will alter "words"
from a list of Strings to a list of Ints.  This is a fundamental no-no
in a pure functional language like Haskell.  What you want is to create
a new list of integers from your "words" list.  The following changes
makes your program work.

main = do
hSetBuffering stdin LineBuffering
words <- askForNumbers
printWords words
--map read words
let ints = map read words
putStrLn "The sum is"
--foldl (+) 0 words
putStrLn (show (foldl (+) 0 ints))

> askForNumbers = do
>   putStrLn "Please enter a number:"
>   text <- getLine
>   if text == ""
>   then return []
>   else if text == "0"
>   then
>   return []
>   else do
>   rest <- askForNumbers
>   return (text : rest)

This is pretty verbose and (IMO) hard to read. I'd probably write it
this way:

askForNumbers = do
putStrLn "Pleas enter a number:"
text <- getLine
case text of
   ""  -> return []
   "0" -> return []
   _   -> askForNumbers >>= return . (text:)


> printWords [] = putStrLn "EOL"
> printWords (h : t) = do
>   putStrLn h
>   printWords t

This could also be more succinct with "sequence_", although increased
redability is arguable.

printWords x = sequence_ (map putStrLn x) >> putStrLn "EOL"





___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: Newbie question

2005-01-09 Thread Einar Karttunen
Dmitri Pissarenko <[EMAIL PROTECTED]> writes:
> a) asking the user to enter several numbers (while the end of the sequence is
> indicated by entering 0)
> b) calculate the sum of those numbers.
>
> ...
> 

Here is a corrected version:

> module Main where
>
> import IO

Delete this.

> main = do
>   hSetBuffering stdin LineBuffering

This is extraneous - stdin should be line buffered by default.

>   words <- askForNumbers
>   printWords words
>   map read words
>   putStrLn "The sum is"
>   foldl (+) 0 words

Here you map read over words and discard the result.
Then you wold over the words and produce an interger,
whereas you should produce an IO value from the do block.

Here is a complete implementation (untested):

main = do 
   words <- askForNumbers
   mapM_ putStrLn words
   putStrLn "EOL"
   let nums = map read words
   print nums
   putStrLn "The sum is"
   print (foldl (+) 0 nums)

- Einar Karttunen
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Newbie question

2005-01-09 Thread Dmitri Pissarenko
Hello!

I am learning Haskell according to the "Yet Another Haskell Tutorial" by Hal
Daume Ill.

One of the exercises involves

a) asking the user to enter several numbers (while the end of the sequence is
indicated by entering 0)
b) calculate the sum of those numbers.

The program given below tries to do that.

I can read a list of strings. Then, I try to convert the list of strings into
a list of numbers by means of map.

That is, given a list ["1","2","3"], I want to convert it into [1,2,3].

In GHCi, this is done via

map read ["1","2","3"]

However, when I apply this same call in my program below, I'm getting
following error when loading the program in GHCi:


Loading package base ... linking ... done.
Compiling Main ( AskForNumbers.hs, interpreted )

AskForNumbers.hs:10:
Couldn't match `IO' against `[]'
Expected type: IO t
Inferred type: [b]
In the application `map read words'
In a 'do' expression: map read words
Failed, modules loaded: none.


Line 10 corresponds to the statement

map read words,

which seems to work in GHCi.

What am I doing wrongly?

Thanks in advance

Dmitri Pissarenko

PS: What follows is my Haskell program.

module Main
where

import IO

main = do
hSetBuffering stdin LineBuffering
words <- askForNumbers
printWords words
map read words
putStrLn "The sum is"
foldl (+) 0 words

askForNumbers = do
putStrLn "Please enter a number:"
text <- getLine
if text == ""
then return []
else if text == "0"
then
return []
else do
rest <- askForNumbers
return (text : rest)

printWords [] = putStrLn "EOL"
printWords (h : t) = do
putStrLn h
printWords t
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users