Discussion Forum

Afp0506
Participating students are encouraged to post their questions related to programming assignments, exercises, and lectures in this discussion forum, as well as answers and hints for fellow students. This serves two purposes:

  • To stimulate interaction between students during the course. A lively discussion is beneficial for everyone.
  • To minimize the startup time for the assignments (for instance, about the software we use, GHC flags that are required for compilation, etc.).

Needless to say, it is strictly prohibited to include (partial) answers to assignments.


MacOSX? users: an easy way to install ghc(i) on your Mac is to use the darwinports project. I think there is a direct (DMG) installer on the http://www.haskell.org/ghc site as well, but darwinports allows you to install (and manage!) many other useful tools (such as subversion) as well. Check it out smile

-- SanderMak - 08 Feb 2006

Urgent: Erica van den Bos is still looking for persons to team up with for the upcoming ant-challenge!

In case anyone is interested, I have joined all the html pages of the tutorial "All About Monads" by Jeff Newbern (literature for week 2) together in one big PDF (Newbern-monads.pdf) for easy printing. (This involved manually printing each page to a file and then joining them all up with ghostscript... does anyone know of an easier way of converting multiple linked html files to a single PS/PDF file?)

-- KasperBrink - 09 Feb 2006

After a little test I got the following result:

partition :: (a -> Bool) -> [a ] -> ([a], [a])
partition p xs = foldr select ([], []) xs
         where select x (ts, fs)
            | p x          = (x : ts, fs)
            | otherwise    = (ts, x : fs)

partition' :: (a -> Bool) -> [a ] -> ([a], [a])
partition' p xs = foldr select ([], []) xs
         where select x ~(ts, fs)
            | p x          = (x : ts, fs)
            | otherwise    = (ts, x : fs)

partition'' :: (a -> Bool) -> [a ] -> ([a], [a])
partition'' p xs = foldr select ([], []) xs
         where select x parts
            | p x          = (x : ts, fs)
            | otherwise    = (ts, x : fs)
              where   ts   = fst parts;
                      fs   = snd parts

List>take 10 $ fst $ partition even [1..]  
*** Exception: stack overflow
List>take 10 $ fst $ partition' even [1..]  
[2,4,6,8,10,12,14,16,18,20]
List>take 10 $ fst $ partition'' even [1..]  
[2,4,6,8,10,12,14,16,18,20]

-- MichielOvereem - 10 Feb 2006

A small note: the first implementation doesn't work because the pattern match on (ts, fs) is performed before the result tuple is constructed (the two right-hand sides of the guarded expressions). The second and third definition, on the other hand, both work because the pattern match on the second argument of select is performed after the result tuple is constructed. For partition', the pattern match is delayed because of the irrifutable pattern ~(ts, fs). For partition'', the pattern match is delegated to fst and snd, which is delayed until ts and fs are really needed.

-- BastiaanHeeren - 15 Feb 2006

For the people that do not want to print each of the papers separately, this is a PDF file(211 pages) with all the literature for week 3 and later.

-- EricBouwers - 15 Feb 2006

Compilation problem: trying to compile IO Monad Example from Newburn's tutorial (ghc on mac), but I get this error:

powerr:~/dev/priv_repo/afp/exc1-monad sbmak$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.4.1
powerr:~/dev/priv_repo/afp/exc1-monad sbmak$ ghc Test.hs
/usr/bin/ld: Undefined symbols:
_ControlziMonadziError_zdfMonadErrorIOException_closure
___stginit_ControlziMonadziError_
_ZCMain_main_closure
___stginit_ZCMain
collect2: ld returned 1 exit status

Compiling it on a Linux machine in BBL yields:

~/temp> ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.4
~/temp> ghc Test.hs
Test.o(.text+0x56): In function `r22m_info':
: undefined reference to `ControlziMonadziError_zdfMonadErrorIOException_closure'
Test.o(.text+0x8b6): In function `Test_main_info':
: undefined reference to `ControlziMonadziError_zdfMonadErrorIOException_closure'
Test.o(.text+0x8e9): In function `__stginit_Test_':
: undefined reference to `__stginit_ControlziMonadziError_'
Test.o(.data+0x0): In function `r22m_srt':
: undefined reference to `ControlziMonadziError_zdfMonadErrorIOException_closure'
Test.o(.data+0x4c): In function `Test_main_srt':
: undefined reference to `ControlziMonadziError_zdfMonadErrorIOException_closure'
/usr/lib/ghc-6.4/libHSrts.a(Main.o)(.text+0xe): In function `main':
: undefined reference to `__stginit_ZCMain'
/usr/lib/ghc-6.4/libHSrts.a(Main.o)(.text+0x28): In function `main':
: undefined reference to `ZCMain_main_closure'
collect2: ld returned 1 exit status

It seems unable to link the Control.Monad.Error stuff (and maybe more?), do I need to pass any arguments to the compiler to make this work?

Update: so, this works (I know, RTFM...):

powerr:~/dev/priv_repo/afp/exc1-monad sbmak$ ghc -c Test.hs
and this as well:
powerr:~/dev/priv_repo/afp/exc1-monad sbmak$ ghc --make Test
Chasing modules from: Test
Compiling Test             ( Test.hs, Test.o )

Now, how do I run the compiled stuff... next challenge smile

Ok, so the -c flag means 'stop before linking', so that's why I don't get the errors then... Apparently --make doesn't link either. Anyone with a clue what is going on here?

-- SanderMak - 15 Feb 2006

Bastiaan: I got the same error message on my Mac. Here is the command-line you were looking for:

ghc --make -o test Test.hs
After that you can call strip test to remove symbols from the executable test.

Unless you are using a makefile, always use the --make flag when using GHC to compile your sources. This instructs the compiler to build all the needed object files (.o) in the correct order, and to use all these files during linking (including libraries you use!). For your particular example, only the lang package is needed. Hence, ghc -package lang -o test Test.hs would also work.

The -o flag specifies the name of the executable. For our Windows users: use -o test.exe instead. If you do not specify the name of the executable, GHC will call it a.out, which is really a silly name.

One more thing: if you specify a module name in the file you are trying to compile other than Main, compilation will fail. Note that it is not necessary to call the file Main.hs in this case as would be with other modules. -- SanderMak - 17 Feb 2006

Monadic higher-order functions: Last lecture, Kasper had a nice observation: you can also define monadic higher-order functions to recurse over your data structures. The slides presented the function transform. I have rewritten this example using foldTreeM and mapTreeM. See MonadicHOF.hs.

-- BastiaanHeeren - 20 Feb 2006

I hav a lot of problems with installing wxHaskell om my Win2K? machine. I already considered installing Linux but it still want to make it work on Win2K? smile The main problem with installing wxHaskell is wxhaskell-register.bat. The first error I discovered was the fact that the %installdir% variable does not have the right value, it is 1 dir too deep. When /bin or /lib is appended to the variable, the script fails because the %installdir% variable has value ../wxhaskell-0.9.4/bin already! So the solution must be to adjust this variable. However, this does not solve the problem completely. When I run the script, or enter the commands manually, I keep getting the following error message:

ghc-pkg: cannot find libwxcore.a on library path

Does anybody have a clue what this error message is about?

Update: The problem is solved, spaces in the file names killed the script smile So, install GHC and wxHaskell to a path without spaces!

-- ThomasVanNoort - 20 Feb 2006

The .bat files take the install-dir as an argument. The following command worked for me:

wxhaskell-register.bat C:\Progra~1\GHC\wxhaskell
-- EricBouwers - 21 Feb 2006

Precompiled binary of wxHaskell for Windows/GHC 6.4.1: Martijn Schrage has compiled wxHaskell for Windows with GHC 6.4.1. The binary can be found at http://www.cs.uu.nl/wiki/Proxima/ProximaDownload. The Windows binary that can be found at the wxHaskell homepage only works with GHC 6.4.0. If the (compiled) binary of wxHaskell is not consistent with the version of GHC you have installed on your computer, you will get an error message like:

E:\ghc-6.4.1\imports\wxhaskell-0.9.4\samples\wxcore\Minimal.hs:3:0:
    Failed to load interface for `Graphics.UI.WXCore':
        Bad interface file: E:\ghc-6.4.1\imports\wxhaskell-0.9.4\lib\imports/Graphics/UI/WXCore.hi
            mismatched interface file versions: expected 6041, found 6040
Failed, modules loaded: none.
-- BastiaanHeeren - 23 Feb 2006

For the people who don't like PS (like myself...), here is a PDF of the paper on Template Haskell.

-- ThomasVanNoort - 01 Mar 2006

Does anyone has the TemplateHaskell code running in ghci or ghc (on the BBL lab machines)? I've tried a number of commands:

ghci -package Syntax -fth Test.hs
ghci -package THSyntax -fth Test.hs
ghci -package TH.Syntax -fth Test.hs
ghci -package Language.Haskell.TH.Syntax -fth Test.hs

These all result in the same message: ghc.exe: unknown package name: ...

ghci -fth Test.hs
this results in: Can't find module `Language.Haskell.TH.Syntax'

Help, what is the correct command?

-- MichielOvereem - 09 Mar 2006

On the windows lab machines an older version of ghc (6.2.1) is installed. You have to use the package

Language.Haskell.THSyntax
instead of
Language.Haskell.TH.Syntax
You could of course ssh to a linux machine, the right version of ghc (6.4.x) is installed there.

-- MichielOvereem - 10 Mar 2006

I think there are a couple mistakes in the description of TemplateHaskell (actually it is just one, I misread something):

test3 = $(permute [0,2,1])
        foldr 0 (+) [1..10]

should be

test3 = $(permute [1,0,2])
        foldr 0 (+) [1..10]

--foldr (+) 0 [1..10] = 55

Am I correct or am I missing something?

-- MichielOvereem - 13 Mar 2006

Ah, nothing like a good old-fashioned build error:

HInteractive.hs:351:12: lexical error in string/character literal
make[1]: *** [/import/enterprise/home/sbmak/afp/exc4/hmake-3.10/lib/ix86-Linux/HInteractive] Error 1
make[1]: Leaving directory `/import/enterprise/home/sbmak/afp/exc4/hmake-3.10/src/interpreter'
make: *** [targets/ix86-Linux/hi-ghc] Error 2
This is occurs while making hmake on the student pc's... anyone have a clue what's going on?

Update: Oh well, I replaced the (huge, multiline) strings which the error points to with some trivial (one-line) string, and all is well... no need for ASCII art smile Strange error though, how could something like this have slipped through in a stable distribution?

-- SanderMak - 28 Mar 2006

Here are some notes on how to build and install hmake and hat as a normal (non-root) user (sorry, no Makefile...).


1. First, set an environment variable pointing to where you want the files to be installed
   export PREFIX=~/path/to/installdir

2. Get hmake, untar it, and change into the toplevel dir. Doing this on a local filesystem (e.g. /tmp) should speed things up a bit.

   wget http://www.haskell.org/hmake/hmake-3.10.tar.gz
   tar xvzf hmake-3.10.tar.gz
   cd hmake-3.10

3. replace src/interpreter/HInteractive.hs with this file: hmake_src_interpreter_HInteractive.hs
(this will remove all newlines inside the two strings at the end of the file, i.e. banner=... and help=...)

4. (Fedora Core 4 users: edit script/confhc; change line 81. This step is not necessary on the lab machines.)

    -  grep -v '^#' ghcsym.out > $2;
    +  grep '^[0-9]\+$' ghcsym.out > $2;

5. build "as normal"

    ./configure --prefix=$PREFIX
    make
    make install

6. Remember to add $PREFIX/bin to your PATH before proceeding!
(you probably also want to do this in your ~/.bashrc, with $PREFIX expanded)

    export PATH=$PREFIX/bin:$PATH


7. Get hat, untar it, and change into the toplevel dir.
   wget ftp://ftp.cs.york.ac.uk/pub/haskell/hat/hat-2.04.tar.gz
   tar xvzf hat-2.04.tar.gz
   cd hat-2.04

8. Replace script/confhat with this file: hat_script_confhat, and make sure that it is executable (chmod +x script/confhat).
(for the curious:) This fixes several problems: libdir is now set to $PREFIX/lib from the environment; the script now uses the new ghc-pkg syntax and includes the --user flag; and Hat.PreludeBuiltin is added to the exported module list.

9. (Fedora Core 4 users: edit script/confhc-hat; change line 59. This step is not necessary on the lab machines.)

    -  grep -e '^[0-9]*$' ghcsym.out > $2
    +  grep '^[0-9]\+$' ghcsym.out > $2

10. build as normal. Note that the script you changed in step 8 depends on having $PREFIX in the environment!

    ./configure --prefix=$PREFIX
    make
    make install


Good luck!

-- KasperBrink - 29 Mar 2006

Well, I spent almost the whole evening (I know...) to get it to work on either my Mac or on a student-linux pc, but both seem to fail at actually compiling the sorting example (./Sorting/Mmain.hs). First I try this:

~/afp/exc4/files/Sorting>hmake -hat -v Mmain
<..snip..>
Hat/Mmain.hs:17:9:
    Could not find module `Hat.PreludeBuiltin':
      locations searched:
        Hat/PreludeBuiltin.hi
        Hat/PreludeBuiltin.hi-boot

and then, using the include directive I get this:

~/afp/exc4/files/Sorting>hmake -hat -i/users/sbmak/hat/lib/imports/hat/ -v Mmain
Hat/Mmain.hs:17:9:
    Interface file inconsistency:
      home-package module `Hat.PreludeBuiltin' is mentioned,
      but does not appear in the dependencies of the interface
Error messages are similar on the mac.

I'm starting to hate this thing...

-- SanderMak - 29 Mar 2006

Sander, I was able to reproduce your error on the student machines, and my own (Fedora Core 4) machine. It seems that although the PreludeBuiltin modules are being installed correctly, they are not registered properly in the Cabal package management system. By manually adding Hat.PreludeBuiltin to the exported modules list, I was able to get Mmain.hs, and several other examples, to compile without errors (plenty of warnings though!).

You could repeat the steps 7-10 I listed above with the (updated) hat_script_confhat script which I've fixed to add PreludeBuiltin in the appropriate place. Or, if you still have a hat build tree lying around, you could just update script/confhat and rerun make install. Or for a really quick (untested!!) workaround, try

ghc-pkg describe hat > hat.conf
cat hat.conf | sed -e 's/^exposed-modules:.*$/& Hat.PreludeBuiltin/' | ghc-pkg --user update -

-- KasperBrink - 29 Mar 2006

I had to add the following line to hat-20.4/lib/ix86-Linux/hatlib/ghc/hat-package.conf:

library-dirs: /var/tmp/hmake/hat-2.04/lib/ix86-Linux/hatlib/ghc

-- MichielOvereem - 30 Mar 2006

Michiel: hmmm, interesting. You seem to be pointing it at the part of the build tree that is copied to $PREFIX/lib/imports/hat after installation; but the "import-dirs" field already points to that... Are you sure that the libHShat.a file is being copied correctly when you make install? It should end up in the $PREFIX/lib directory, and then you probably won't have to touch the .conf file at all. Or maybe you've found yet another strange bug in the install scripts, it wouldn't be the first.

Of course, if this works for you, then I'd just be happy and leave it alone... But it does make it hard to throw away your build tree after installation, or work on multiple machines.

-- KasperBrink - 30 Mar 2006

Ok, on a somewhat lighter note then smile While reading up on arrows, I stumbled across this:

  • There is a preprocessor that reads as input a Haskell script augmented with arrow notation, and outputs a plain Haskell script.

  • The notation is also implemented directly in GHC, from version 6.2, where it is enabled by the -farrows option. Error reports for arrow notation are considerably clearer, and refer to the original source. The documentation accompanying GHC has more details.

('the notation' meaning the proc notation which Xiaoyu showed us earlier today, which I thought to be Yampa specific, but is for all arrows)

Source: http://www.haskell.org/arrows/syntax.html

-- SanderMak - 31 Mar 2006

ANNOUNCEMENT:

After several weeks of sleepless nights for the team consisting of Eric, John, Michiel and Thomas, the hard work finally paid off! We would like to thank Sander's team for pimpin' the simulator, Mark's team for using Monads, Elmar's team for going crazy, and of course our mothers smile

Warm, fuzzy, season greetings from The Winners!