Send Beginners mailing list submissions to beginners@haskell.org 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: IO Problem (Brent Yorgey) 2. Re: Mathematical Blundering (Andrew Sackville-West) 3. requested module name differs from name found in interface file (Larry Evans) 4. A Pascal-like Language Compiler (Pranesh Srinivasan) 5. Re: A Pascal-like Language Compiler ( Joel Bj?rnson ) 6. Re: requested module name differs from name found in interface file (Larry Evans) ---------------------------------------------------------------------- Message: 1 Date: Fri, 17 Oct 2008 14:11:55 -0400 From: Brent Yorgey <[EMAIL PROTECTED]> Subject: Re: [Haskell-beginners] IO Problem To: beginners@haskell.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset=us-ascii On Fri, Oct 17, 2008 at 07:58:45PM +1300, Jamie McCloskey wrote: > > All this creates a whole load of functions with IO, even though it > should not be necessary. What I would like to know, is how I can avoid > this, possibly by modifying exec to just take a State. The short answer is: it IS necessary. Think about what exec does, for instance. It takes a Program and an initial State and does... what? Executes the program, which might result in some things getting printed to the screen or some input being read from the keyboard. That is, exec can have some I/O effects. Therefore, exec's return type must be an IO value. The same goes for all the other functions you mentioned. This is a feature, not a bug --- the type of a function tells you precisely whether it can possibly have any I/O effects. If the return type is (IO something), then it can; if the return type does not involve IO, then it is *guaranteed*[1] that it cannot have any I/O effects. -Brent [1] Except for that pesky unsafePerformIO. But you should never use that, unless you really absolutely know what you are doing and why. ------------------------------ Message: 2 Date: Fri, 17 Oct 2008 16:52:28 -0700 From: Andrew Sackville-West <[EMAIL PROTECTED]> Subject: Re: [Haskell-beginners] Mathematical Blundering To: beginners@haskell.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset="us-ascii" On Thu, Oct 16, 2008 at 09:57:39AM +0100, Paul Johnston wrote: > Bit of basic maths. > You are using a power series to approximate sine > This works by taking an expansion about a fixed point, usually zero. > It only works well around that point. > If you get far away it works badly. > You need to exploit the cyclic nature of the trignometrical functions i.e. > Sin x = sin ((2 * pi) + x) = sin ((4 * pi) + x) > Essentially consider the shift in multiples of 2 * pi and calculate the value > of x nearest to zero. > > See > http://en.wikipedia.org/wiki/Taylor_series > The diagram on the top right is very instructive. how appropriate. We're doing this in math right now ;) ISTM that OP should take x modulo pi and (per the wiki diagram) a seven term series to get a nice usable sine function. (and don't forget to watch for the sign (nopun) of the result) fact 1 = 1 fact x = x * (fact $ x - 1) term n x = (-1)**n * (theta**(2*n + 1) / fact(2*n + 1)) where theta = pi * ((x / pi) - xTrunc) * sign xTrunc = fromIntegral $ truncate (x/pi) sign = (-1)**xTrunc sine x = sum $ take 7 [term n x | n<-[0..]] Wow, that took me a lot longer to figure out because of all the mixing of numeric types. The critical part was fromIntegral in the poorly name xTrunc function. but it looks pretty accurate. *Main> maximum [sine x - sin x | x<-[1..100]] 1.2652798913048713e-5 and it's easy to boost the accuracy by taking more terms in the series. taking 9: *Main> maximum [sine x - sin x | x<-[1..100]] 1.1683279538265978e-8 taking 11: *Main> maximum [sine x - sin x | x<-[1..100]] 4.694897248747054e-12 I'm sure there must be a better way to get x modulo pi, but I don't know enough... I'd love some feedback on my solution both from a math and a haskell perspective. A > > Paul > > -----Original Message----- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Jeffrey Drake > Sent: Thursday, October 16, 2008 9:47 AM > To: Haskell Beginners > Subject: [Haskell-beginners] Mathematical Blundering > > > I have defined myself a set of functions to test: > > fact 1 = 1 > fact n = n * (fact $ n - 1) > > sine x = x - (x^3/(fact 3)) + (x^5/(fact 5)) - (x^7/(fact 7)) > > Where my code is 'sine' and the prelude's is sin: > *Main> sine 1 > 0.841468253968254 > *Main> sin 1 > 0.8414709848078965 > > *Main> sine 2 > 0.9079365079365079 > *Main> sin 2 > 0.9092974268256817 > > *Main> sine 3 > 9.107142857142847e-2 > *Main> sin 3 > 0.1411200080598672 > > *Main> sine 4 > -1.3841269841269837 > *Main> sin 4 > -0.7568024953079282 > > After 2 they seem to diverge rather rapidly, and I am not sure why. Any ideas? > > I would have thought that 4 terms would have been enough. > > - Jeff. > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 197 bytes Desc: Digital signature Url : http://www.haskell.org/pipermail/beginners/attachments/20081017/5147ce12/attachment-0001.bin ------------------------------ Message: 3 Date: Sun, 19 Oct 2008 16:59:39 -0500 From: Larry Evans <[EMAIL PROTECTED]> Subject: [Haskell-beginners] requested module name differs from name found in interface file To: beginners@haskell.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset="iso-8859-1" With a file containing: > module Main where > > import Array > import Control.Functor.Fix I get: > make > ghc -i/root/.cabal/lib/category-extras-0.53.5/ghc-6.8.2 -c > catamorphism.example.hs > > catamorphism.example.hs:19:0: > Bad interface file: > /root/.cabal/lib/category-extras-0.53.5/ghc-6.8.2/Control/Functor/Fix.hi > Something is amiss; requested module main:Control.Functor.Fix > differs from name found in the interface file > category-extras-0.53.5:Control.Functor.Fix > make: *** [all] Error 1 I used cabal to install category-extras: > /home/evansl/download/haskell/libs # cabal install category-extras > Resolving dependencies... > Downloading category-extras-0.53.5... > Configuring category-extras-0.53.5... > Preprocessing library category-extras-0.53.5... ... > /usr/bin/ar: creating dist/build/libHScategory-extras-0.53.5.a > Installing library in /root/.cabal/lib/category-extras-0.53.5/ghc-6.8.2 > Registering category-extras-0.53.5... > Reading package info from "dist/installed-pkg-config" ... done. > Saving old package config file... done. > Writing new package config file... done. > /home/evansl/download/haskell/libs # What should I do to import the Functor.Fix as shown here: http://comonad.com/reader/2008/recursion-schemes/ TIA. -Larry -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081019/08b64919/attachment-0001.htm ------------------------------ Message: 4 Date: Mon, 20 Oct 2008 17:39:50 +0530 From: "Pranesh Srinivasan" <[EMAIL PROTECTED]> Subject: [Haskell-beginners] A Pascal-like Language Compiler To: "Hasekll - Beginners" <beginners@haskell.org> Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset=ISO-8859-1 Hi all, I have been learning haskell for the last month or so. I have just started becoming familiar with monads and looking at Parsec. As part of the Language Translators Course this semester, in the university I study, we are to implement a Pascal-like toy language to C translator[1]. The aim is to take a simple subset of Pascal and translate it to an intermediate 3 operand format, before translating it to simple C. The time I have for this project is about a month. I am contemplating doing this in Haskell, since I have read in several places that Haskell (and other functional languages) is very good for writing compilers. In fact, I have started writing code for lexing the basic tokens. I do have doubts about my ability to finish it. The curiosity to do something of a moderate size in Haskell, and the extremely friendly folks on #haskell, tipped me on this decision. I looked around for similar things on the internet, in vain. I have several doubts at this stage some of which include, * how the parser will determine the scope of the variables, i.e, how can a symbol table be implemented (in the state of the parser ?) * how easy or difficult will basic code optimisation (like unused variables, and loops with no side effects) and type checking be? * how would one go about implementing basic error recovery in the parser (make it skip the current line, or block) ? I would love it if I get a few pointers from people who have been there, done that. Or am I being too ambitious too early? :) [1] One of the suggested programming exercises at the end of the "Dragon" Compilers book, by Aho, et al. -- Pranesh S ------------------------------ Message: 5 Date: Mon, 20 Oct 2008 15:47:44 +0200 From: " Joel Bj?rnson " <[EMAIL PROTECTED]> Subject: Re: [Haskell-beginners] A Pascal-like Language Compiler To: "Pranesh Srinivasan" <[EMAIL PROTECTED]> Cc: Hasekll - Beginners <beginners@haskell.org> Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset=ISO-8859-1 Oops, should have hit the replay-to-all button, so sorry Pranesh for double posting. Hi, have you considered using BNCF[1]? With BNFC you can define a grammar for your language then let the BNFC converter generate a Haskell parser (along with an ADT representing syntax trees). On Mon, Oct 20, 2008 at 2:09 PM, Pranesh Srinivasan <[EMAIL PROTECTED]> wrote: > I looked around for similar things on the internet, in vain. I have > several doubts at this stage some of which include, > > * how the parser will determine the scope of the variables, > i.e, how can a symbol table be implemented (in the state of the > parser ?) I think it is probably better to exclude the variable look up, type checking etc from the parsing phase. Here's an example of a general scheme: 1. Parse input file into an abstract syntax tree representation. 2. Perform type checking on your syntax tree. 3. Transform the syntax tree using re-write rules and optimisations. 4. Pretty print the syntax tree in order to output code of your target language. > > * how easy or difficult will basic code optimisation (like unused > variables, and loops with no side effects) and type checking be? I would say probably easier then in many other languages since Haskell has such a great support for recursive data types, pattern matching etc. > * how would one go about implementing basic error recovery in the > parser (make it skip the current line, or block) ? > Not really sure what you mean by error recovery but when using BNFC all of the tedious work of implementing the parsers is automated. Regards, Joel [1] http://www.cs.chalmers.se/Cs/Research/Language-technology/BNFC/ ------------------------------ Message: 6 Date: Mon, 20 Oct 2008 12:15:39 -0500 From: Larry Evans <[EMAIL PROTECTED]> Subject: Re: [Haskell-beginners] requested module name differs from name found in interface file To: Justin Bailey <[EMAIL PROTECTED]>, beginners@haskell.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset="iso-8859-1" On 10/20/08 11:38, Justin Bailey wrote: > On Mon, Oct 20, 2008 at 9:25 AM, Larry Evans <[EMAIL PROTECTED]> wrote: > [snip] > >> Do you mean install it manually as shown in the aforementioned >> How_to_install_a_Cabal_package? >> > > No, I mean go to http://hackage.haskell.org and download the .tar.gz > file directly > (http://hackage.haskell.org/cgi-bin/hackage-scripts/package/category-extras). > Unzip/untar that package. I haven't looked at it, but there is > probably a "src" directory. Put your example file in there and load it > in ghci. Your source file should then use the source available in that > directory tree rather than the category-extras install which seems > broken. This works because the GHC will look at the import > "Control.Functor.Fix" and use that to look for > "Control/Functor/Fix.hs" from the current directory. > Tried that but: <--- cut here --- GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude> :load "/home/evansl/download/haskell/libs/category-extras-0.53.5/src/catamorphism.example.hs" Control/Category/Dual.hs:17:7: Could not find module `Control.Category': Use -v to see a list of the files searched for. Failed, modules loaded: none. Prelude> <--- cut here --- If it's any help, the directory list for src/Control/Category is: <--- cut here --- /home/evansl/download/haskell/libs/category-extras-0.53.5/src/Control/Category: total used in directory 52 available 148798604 drwxr-xr-x 3 evansl evansl 4096 Jun 30 08:08 . drwxr-xr-x 9 evansl evansl 4096 Jun 30 08:08 .. -rw-r--r-- 1 evansl evansl 2118 Jun 30 08:08 Associative.hs -rw-r--r-- 1 evansl evansl 1668 Jun 30 08:08 Braided.hs drwxr-xr-x 2 evansl evansl 4096 Jun 30 08:08 Cartesian -rw-r--r-- 1 evansl evansl 4973 Jun 30 08:08 Cartesian.hs -rw-r--r-- 1 evansl evansl 1020 Jun 30 08:08 Discrete.hs -rw-r--r-- 1 evansl evansl 1340 Jun 30 08:08 Distributive.hs -rw-r--r-- 1 evansl evansl 668 Jun 30 08:08 Dual.hs -rw-r--r-- 1 evansl evansl 578 Jun 30 08:08 Hask.hs -rw-r--r-- 1 evansl evansl 3254 Jun 30 08:08 Monoidal.hs -rw-r--r-- 1 evansl evansl 1746 Jun 30 08:08 Object.hs >--- cut here --- > You might have more luck finding out what's going on by emailing > Haskell-Cafe directly, too. > Yes, thanks. > Justin > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081020/108edec2/attachment.htm ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 4, Issue 7 ***************************************