Re: [Haskell-cafe] Re: HDBC converting a date sql value to UTCTime
George Moschovitis wrote: Alternatively is there a way to create a UTCTime value from an epoch integer (no of seconds since epoch). I can't find a suitable constructor with Hoogle. http://www.haskell.org/ghc/docs/latest/html/libraries/time/Data-Time-Clock-POSIX.html posixSecondsToUTCTime probably in combination with fromIntegral Jules ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Access to Oracle database from Haskell
2008/6/20 Alistair Bayley [EMAIL PROTECTED]: Having just taken a closer took at what Oracle Instant Client is, I suspect that you might have some trouble getting Takusen to compile against it. The Instant Client lacks header files, while Takusen's FFI imports specify oci.h. I don't know what happens if ghc can't find the header files. Oracle do state that the Instant Client is for deployment only; developers (that means you) will need the full client installation. There's an additional download for the Instant Client which includes the OCI headers. Paul. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Access to Oracle database from Haskell
Oracle OCI interface is quite different between 7/8 and 9/10. And 10 is different from 9 in some respect. I don't know much about 11. Oracle 10.2.0.3 is a stable release, but there are some major server bugs in it, that Oracle had to release 10.2.0.4. I'd recommend Haskell community to focus on 10.2.0.x release. It should be good for another 2-3 years. It is too risky to venture into 11. That said, there is a large enterprise user base that will find Haskell useful if it works with Oracle DB (particularly on linux). On 6/21/08, Paul Moore [EMAIL PROTECTED] wrote: 2008/6/20 Alistair Bayley [EMAIL PROTECTED]: Having just taken a closer took at what Oracle Instant Client is, I suspect that you might have some trouble getting Takusen to compile against it. The Instant Client lacks header files, while Takusen's FFI imports specify oci.h. I don't know what happens if ghc can't find the header files. Oracle do state that the Instant Client is for deployment only; developers (that means you) will need the full client installation. There's an additional download for the Instant Client which includes the OCI headers. Paul. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] an example from YAHT doesn't run
Hello, On pdf page 43 of YAHT it provides a code example. I typed this into a file named Guess.hs and tried unsuccessfully to load it into ghci. I am running ghci version 6.8.2 This is the output it gave me: [1 of 1] Compiling Main ( Guess.hs, interpreted ) Guess.hs:19:12: parse error on input `doGuessing' Failed, modules loaded: none. I've attached the code module. As far as I can tell it is identical to the example in YAHT. Did I type it wrong, is the tutorial wrong, or something else? thanks again, Jared Langson Guess.hs Description: Binary data ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] an example from YAHT doesn't run
Hi Jared, 2008/6/21 [EMAIL PROTECTED]: Guess.hs:19:12: parse error on input `doGuessing' Failed, modules loaded: none. That says there's something wrong at line 19. In this case, 'doGuessing' is not properly aligned with 'putStrLn' of the previous line. The same happens at line 22. See if that helps and double-check the rules of indentation. : ) Paulo ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] ANNOUNCE: Pipe 1.0
Greetings, I hereby announce the release of the Pipe library, a library for piping data through a pipeline of processes. A web page with (hopefully) all the necessary info and a simple example can be found at http://iki.fi/matti.niemenmaa/pipe/ The package is at Hackage: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/Pipe Feel free to comment on any part of it, either here or straight to my e-mail address. I've tested it on Windows XP and Linux (Gentoo) and GHC 6.8.2 and 6.8.3. Since the code is rather low-level it'd be interesting to hear whether it works on other systems, say the BSDs or OS X. -- Matti Niemenmaa ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] message passing style like in Haskell?
On Fri, Jun 20, 2008 at 07:57:58AM +0200, Ketil Malde wrote: Albert Y. C. Lai [EMAIL PROTECTED] writes: While we are kind of on this topic, what makes the characters ħ þ prefix operator by default, while º and most other odd ones infix? alphanumeric vs non-alphanumeric Testing this, I find that isAlpha is True also for 'º', but as the OP claims, Haskell will use it as a(n infix) symbol. This is a bug in GHC. The characters = '\255' were done specially, but incorrectly for many of those = '\128'. I'll fix it, probably by just removing the specialisation for them. Thanks Ian ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Literal programming in Haskell with rst-literals
Hello Haskell community! I just did a marginally cool thing and I wanted to share it with you. rst-literals is a small program I wrote a while ago in order to write documents in reStructuredText format that would embed SQL code for data models in them, a form of literal programming for SQL if you will; I would describe my needs for the schema in prose, and reST literal-blocks were used to embed SQL code, blocks that look like this:: CLASS Employee ( firstname VARCHAR, lastname VARCHAR ) I wrote the script to be entirely generic: it parses the reST documents using the docutils code and outputs only the literal-blocks, with indentation removed; you can then run your favourite interpreter/compiler on the result (in that case, psql to initialize a database). Recently, while experimenting with Haskell, I started using both the literal (.lhs) and non-literal (.hs) styles of Haskell input, and I found the literal style a bit unpleasant to use, in particular, I don't like to have to prefix every line of code I write, despite the help that Emacs' haskell-mode provides. So I tried pulling a similar trick and embedding Haskell code in literal-blocks within reST documents, extracting that code using rst-literals, and it turns out that it works like a charm. Here is an example makefile for doing this:: .SUFFIXES: .rst .hs all: chap6 .rst.hs: rst-literals $ $@ chap6: chap6.hs ghc --make chap6.hs An example reST document with some embedded Haskell code follows this email. Note that since rst-literals is using the docutils parser, you can make use of all of the recursive reST syntax, sections, bulleted items and much more. Only the literal-blocks are extracted, anywhere they appear. You can also easily process the reST source into HTML pages or LaTeX documents using the tools that come with docutils. You can find rst-literals here: http://furius.ca/pubcode/ Enjoy, -- Martin P.S. If there is a way to output cpp-like directives for GHC, like #line filename lineno, it would be easy to modify rst-literals to generate those, so that compilation errors could refer to the source reST document instead of the extracted source. chap6.hs: -- === Exercises from Hutton book, Chapter 6 === .. contents:: .. 1 Introduction 2 Exercise 1 3 Exercise 2 4 Exercise 3 5 Exercise 4 6 Exercise 5 7 Exercise 6 Introduction Bla bla bla blablablablablabla bla bla blabla. Bla bla bla blablablablablabla bla bla blabla. Bla bla bla blablablablablabla bla bla blabla. Bla bla bla blablablablablabla bla bla blabla. Bla bla bla blablablablablabla bla bla blabla. Bla bla bla blablablablablabla bla bla blabla. Exercise 1 == :: myexp :: Int - Int - Int myexp b 0 = 1 myexp b (n+1) = b * (myexp b n) Exercise 2 == (Exercise 2 consisted in derivations, so we mark the literal blocks as another type of block with #!example, so that they don't get included in the output when only the default literal blocks get extracted. See rst-literals docstring for details.) Length:: #!example 1 + (length [2, 3]) 1 + 1 + (length [3]) 1 + 1 + (1) 3 Drop:: #!example drop 3 [1, 2, 3, 4, 5] [] ++ drop 3 [2, 3, 4, 5] [] ++ [] ++ drop 3 [3, 4, 5] [] ++ [] ++ [] ++ [4, 5] [4, 5] Init:: #!example init [1, 2, 3] [1] ++ init [2, 3] [1] ++ [2] ++ init [3] [1] ++ [2] ++ [] [1, 2] Exercise 3 == These are alternate versions of the example functions defined in the text:: and' :: [Bool] - Bool and' [x] = x and' (x:xs) = x and' xs concat' :: [[a]] - [a] concat' [] = [] concat' (x:xs) = x ++ concat' xs replicate' :: Int - a - [a] replicate' 0 x = [] replicate' (n+1) x = (x : replicate' n x) select' :: [a] - Int - a select' (x:xs) 0 = x select' (x:xs) (n+1) = select' xs n elem' :: Eq a = a - [a] - Bool elem' _ [] = False elem' y (x:xs) | x == y = True | otherwise = elem' y xs Exercise 4 == The exercise asked to implement a function to merge two lists:: merge :: Ord a = [a] - [a] - [a] merge xs [] = xs merge [] xs = xs merge (x:xs) (y:ys) | x y = (x : merge xs (y:ys)) | otherwise = (y : merge (x:xs) ys) Exercise 5 == :: msort :: Ord a = [a] - [a] msort [] = [] msort [x] = [x] -- This is necessary to end the recursion. msort xs = merge (msort (fst hh)) (msort (snd hh)) where hh = halve xs halve :: [a] - ([a], [a]) halve xs = (take n xs, drop n xs) where n = (length xs) `div` 2 Some main program:: main = (putStrLn . show) (halve [1..17]) Exercise 6 == (Too basic, I didn't bother.) -- ___
Re: [Haskell-cafe] another FFI question
You just have the arguments to poke wrong: poke :: Storable a = Ptr a - a - IO () So you are missing the pointer argument poke p Signal = poke p signal_int_value I didn't know about the (#const) syntax. Interesting. Also, alignment of signal should match the alignment of the underlying type (although I think that isn't really used at the moment). -- ryan 2008/6/20 Galchin, Vasili [EMAIL PROTECTED]: I am still reading the web pages. Here is what I tried: data SigNotify = Signal | None | Thread | ThreadId instance Storable SigNotify where sizeOf _ = (#const sizeof (int)) alignment _ = 1 poke Signal = (#const SIGEV_SIGNAL) poke None = poke (#const SIGEV_NONE) poke Thread = poke (#const SIGEV_THREAD) poke ThreadId = poke (#const SIGEV_THREAD_ID) but I got ... Couldn't match expected type `Ptr SigNotify' against inferred type `SigNotify' In the pattern: Signal In the definition of `poke': poke Signal = poke (0) In the definition for method `poke' Basically I want to marshall SigInfo constructors to CInt values. ?? Regards, Vasili On Fri, Jun 20, 2008 at 3:20 PM, Bulat Ziganshin [EMAIL PROTECTED] wrote: Hello Vasili, Friday, June 20, 2008, 11:51:11 PM, you wrote: data Bonzo = A | B |C How do I write the poke functions and call them? instance Storable Bonzo poke A = poke 0 poke B = poke 1 poke C = poke 4 call as poke x probably, you don't understand differences between OOP classes and type classes. look at http://haskell.org/haskellwiki/OOP_vs_type_classes and papers mentioned there -- Best regards, Bulatmailto:[EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] ANNOUNCE: Pipe 1.0
matti.niemenmaa+news: Greetings, I hereby announce the release of the Pipe library, a library for piping data through a pipeline of processes. A web page with (hopefully) all the necessary info and a simple example can be found at http://iki.fi/matti.niemenmaa/pipe/ The package is at Hackage: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/Pipe Feel free to comment on any part of it, either here or straight to my e-mail address. I've tested it on Windows XP and Linux (Gentoo) and GHC 6.8.2 and 6.8.3. Since the code is rather low-level it'd be interesting to hear whether it works on other systems, say the BSDs or OS X. Interesting. Does it depend on an unreleased version of the process library? -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Safe way to parse arguments?
Hi, I'm wondering how usually you parse command line arguments list safely. If the given argument is wrong, the program can still print out some error information instead of giving something like Prelude.read: no parse Let's make the discussion concrete. Suppose we have the following code. --8--8-- import System.Environment data Conf = Conf { number :: Int } parseArgs [] = error need at least one argument parseArgs (x:_) = Conf { number = read x } work (Conf { number = n }) = n * (n-1) `div` 2 main = print . work . parseArgs = getArgs --8--8-- If the read x fails in line 8, the program dies with Prelude.read: no parse What is the standard way to make the error message useful? Xiao-Yong -- c/*__o/* \ * (__ */\ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Safe way to parse arguments?
Hi I'm wondering how usually you parse command line arguments list safely. If the given argument is wrong, the program can still print out some error information instead of giving something like Either use reads instead, and deal with the case where there is no parse. Or use the safe library, on hackage, and something like: import Safe readNote please enter a number x Where if x is test, you'll get: Program error: Prelude.read: no parse, please enter a number, on test Or use readDef: readDef (error Please enter a number) x Which will just give you: Program error: Please enter a number Or, readMay, which will return a Nothing if you fail, which you can deal with as you wish. Thanks Neil ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Safe way to parse arguments?
xj2106: Hi, I'm wondering how usually you parse command line arguments list safely. If the given argument is wrong, the program can still print out some error information instead of giving something like Prelude.read: no parse Let's make the discussion concrete. Suppose we have the following code. --8--8-- import System.Environment data Conf = Conf { number :: Int } parseArgs [] = error need at least one argument parseArgs (x:_) = Conf { number = read x } work (Conf { number = n }) = n * (n-1) `div` 2 main = print . work . parseArgs = getArgs --8--8-- If the read x fails in line 8, the program dies with Prelude.read: no parse What is the standard way to make the error message useful? Xiao-Yong -- The main thing is to define a safe read. This will be in the base library soon, maybeRead :: Read a = String - Maybe a maybeRead s = case reads s of [(x, )] - Just x _ - Nothing Then you can pattern match on the failure case as Nothing. Cheers, Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] another FFI question
On Sat, Jun 21, 2008 at 1:28 PM, Ryan Ingram [EMAIL PROTECTED] wrote: You just have the arguments to poke wrong: poke :: Storable a = Ptr a - a - IO () So you are missing the pointer argument poke p Signal = poke p signal_int_value ^^ what is signal_int_value? I didn't know about the (#const) syntax. Interesting. Also, alignment of signal should match the alignment of the underlying type (although I think that isn't really used at the moment). -- ryan 2008/6/20 Galchin, Vasili [EMAIL PROTECTED]: I am still reading the web pages. Here is what I tried: data SigNotify = Signal | None | Thread | ThreadId instance Storable SigNotify where sizeOf _ = (#const sizeof (int)) alignment _ = 1 poke Signal = (#const SIGEV_SIGNAL) poke None = poke (#const SIGEV_NONE) poke Thread = poke (#const SIGEV_THREAD) poke ThreadId = poke (#const SIGEV_THREAD_ID) but I got ... Couldn't match expected type `Ptr SigNotify' against inferred type `SigNotify' In the pattern: Signal In the definition of `poke': poke Signal = poke (0) In the definition for method `poke' Basically I want to marshall SigInfo constructors to CInt values. ?? Regards, Vasili On Fri, Jun 20, 2008 at 3:20 PM, Bulat Ziganshin [EMAIL PROTECTED] wrote: Hello Vasili, Friday, June 20, 2008, 11:51:11 PM, you wrote: data Bonzo = A | B |C How do I write the poke functions and call them? instance Storable Bonzo poke A = poke 0 poke B = poke 1 poke C = poke 4 call as poke x probably, you don't understand differences between OOP classes and type classes. look at http://haskell.org/haskellwiki/OOP_vs_type_classes and papers mentioned there -- Best regards, Bulatmailto:[EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: ANNOUNCE: Pipe 1.0
Don Stewart wrote: Interesting. Does it depend on an unreleased version of the process library? Indeed it does. Actually most of the code was written using the current released version, I just jumped the gun a bit when I saw how nice the new interface was. I'm hoping that the release of the new process library (GHC ticket #2233) isn't that far off, which is why I figured I'd shove Pipe out in this state. If that's not the case and it turns out that people want to use Pipe without the hassle, I might be persuaded to release a version that doesn't need it. The only 'real' dependency that can't be worked around is the handlePipe function, which is only a non-exported optimization that gets applied via RULES. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Safe way to parse arguments?
Don Stewart [EMAIL PROTECTED] writes: The main thing is to define a safe read. This will be in the base library soon, maybeRead :: Read a = String - Maybe a maybeRead s = case reads s of [(x, )] - Just x _ - Nothing Then you can pattern match on the failure case as Nothing. When will it be in the base library? X-Y -- c/*__o/* \ * (__ */\ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Access to Oracle database from Haskell
Lanny Ripple wrote: I had luck with this the other day using Database.HDBC.ODBC. For Ubuntu's Hardy I found that Oracle's 10.2.0.3 worked best. (10.2.0.4 and 11 seemed to have problems for me at least.) http://www.oracle.com/technology/software/tech/oci/instantclient/htdocs/linuxsoft.html Grab the basic (not basic-lite), odbc, and sqlplus (to test) zips. Oops. And the sdk zip. The odbc_update_ini.sh should be run from inside the instantclient_10_2 directory as root sudo /bin/sh odbc_update_ini.sh / (assuming a standard unixODBC install.) You'll also need a tnsnames.ora file to describe your connection(s) to the DB(s). You'll also need to define some environment variables to run against all this. I use a small script: #!/bin/sh oracle_home=/opt/lib/oracle/instantclient_10_2 export TNS_ADMIN=$oracle_home export LD_LIBRARY_PATH=$oracle_home [ $# = 0 ] exit 1 exec $@ Best of luck, -ljr Steve Lihn wrote: You may want to check this out. http://www.orafaq.com/wiki/ODBC_FAQ#Where_can_one_get_ODBC_drivers_for_Oracle_and_Rdb.3F As Oracle is a commercial company who is not interested in open source historically, it is little chance that you will get robust software for free -- from someone with many years of Oracle DBA experience :-) On 6/19/08, Henning Thielemann [EMAIL PROTECTED] wrote: Is there a way of accessing a remote Oracle database by one of the common Haskell database interfaces (HaskellDB, Takusen, etc.) ? I tried to get unixODBC and Oracle's Instant Client running on a Linux machine, but I'm trapped in the notorious error: $ isql USER -v [IM004][unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_HENV failed [ISQL]ERROR: Could not SQLConnect This error message is discussed in various web forums, but there seems to be no systematic way to track down the problem. So I wonder whether there is another way to access the Oracle data base from Haskell. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Safe way to parse arguments?
On Saturday 21 June 2008, Don Stewart wrote: maybeRead :: Read a = String - Maybe a maybeRead s = case reads s of [(x, )] - Just x _ - Nothing Note, if you want to match the behavior of read, you'll probably want something like: maybeRead :: Read a = String - Maybe a maybeRead s = case reads s of [(x, str)] | all isSpace str - Just x _- Nothing Otherwise, trailing spaces will yield a bad parse. -- Dan ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Pretty little definitions of left and right folds
On Fri, Jun 20, 2008 at 09:52:36PM -0500, Derek Elkins wrote: On Fri, 2008-06-20 at 22:31 -0400, Brent Yorgey wrote: On Fri, Jun 20, 2008 at 06:15:20PM -0500, George Kangas wrote: foldright (+) [1, 2, 3] 0 == ( (1 +).(2 +).(3 +).id ) 0 foldleft (+) [1, 2, 3] 0 == ( id.(3 +).(2 +).(1 +) ) 0 Hi George, This is very cool! I have never thought of folds in quite this way before. It makes a lot of things (such as the identities you point out) obvious and elegant. We can also see the following identities: foldright f as == foldright (.) (map f as) id foldleft f as == foldright (flip (.)) (map f as) id I like that second one, after trying to read another definition of left fold in terms of right fold (in the web book Real World Haskell). The type signature, which could be written (a - (b - b)) - ([a] - (b - b)), suggests generalization to another type constructor C: (a - (b - b)) - (C a - (b - b)). Would a foldable typeclass make any sense? As Brandon points out, you have rediscovered Data.Foldable. =) There's nothing wrong with that, congratulations on discovering it for yourself! But again, I like this way of organizing the type signature: I had never thought of a fold as a sort of 'lift' before. If f :: a - b - b, then foldright 'lifts' f to foldright f :: [a] - b - b (or C a - b - b, more generally). Okay, it goes without saying that this is useless dabbling, but have I entertained anyone? Or have I just wasted your time? I eagerly await comments on this, my first posting. Not at all! Welcome, and thanks for posting. Look into the theory of monoids, monoid homomorphisms, M-sets and free monoids. Thanks for the pointers! Here's what I've come up with, after re-reading some Barr-Wells lecture notes. First, given finite sets A (representing an 'alphabet') and S (representing 'states'), we can describe a finite state machine by a function phi : A x S - S, which gives 'transition rules' giving a new state for each combination of alphabet character and state. If we squint and wave our hands and ignore the fact that types aren't exactly sets, and most of the types we care about have infinitely many values, this is very much like the Haskell type (a,s) - s, or (curried) a - s - s, i.e. a - (s - s). So we can think of a Haskell function phi :: a - (s - s) as a sort of 'state machine'. Also, for a monoid M and set S, an action of M on S is given by a function f : M x S - S for which (1) f(1,s) = s, and (2) f(mn,s) = f(m,f(n,s)). Of course, in Haskell we would write f :: m - (s - s), and we would write criteria (1) and (2) as (1) f mempty = id (2) f (m `mappend` n) = f m . f n Now look at the type of foldright: foldright :: (a - (s - s)) - ([a] - (s - s)) We can see that foldright exactly corresponds to the observation that any state machine with alphabet a and states s induces a monoid action on s by the free monoid [a]. It's not hard to check that the function produced by foldright indeed satisfies the requirements to be a monoid action. First, recall that foldright f = chain where chain (a:as) = (f a).(chain as) chain _ = id Now, we can easily prove (1): (foldright f) [] = chain [] = id The proof of (2) is by induction on the length of m. The base case is obvious, given the proof of (1). (foldright f) (m ++ n) = { defn. of foldright, assume m = x:xs } chain ((x:xs) ++ n) = { defn. of (++) } chain (x:(xs ++ n)) = { defn. of chain } f x . chain (xs ++ n) = { inductive hypothesis } f x . chain xs . chain n = { defn. of chain, associativity of (.) } chain (x:xs) . chain n = { defn. of foldright, m } (foldright f) m . (foldright f) n How'd I do? I'm still trying to figure out how the generalization to Traversable fits in here. I'm guessing this is where the monoid homomorphisms come in. -Brent ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Pretty little definitions of left and right folds
On Sat, Jun 21, 2008 at 7:11 PM, Brent Yorgey [EMAIL PROTECTED] wrote: First, given finite sets A (representing an 'alphabet') and S (representing 'states'), we can describe a finite state machine by a function phi : A x S - S, which gives 'transition rules' giving a new state for each combination of alphabet character and state. If we squint and wave our hands and ignore the fact that types aren't exactly sets, and most of the types we care about have infinitely many values, this is very much like the Haskell type (a,s) - s, or (curried) a - s - s, i.e. a - (s - s). So we can think of a Haskell function phi :: a - (s - s) as a sort of 'state machine'. Also, for a monoid M and set S, an action of M on S is given by a function f : M x S - S for which (1) f(1,s) = s, and (2) f(mn,s) = f(m,f(n,s)). Of course, in Haskell we would write f :: m - (s - s), and we would write criteria (1) and (2) as (1) f mempty = id (2) f (m `mappend` n) = f m . f n Now look at the type of foldright: foldright :: (a - (s - s)) - ([a] - (s - s)) Wow! I commend you on this excellent, enlightening post! Luke We can see that foldright exactly corresponds to the observation that any state machine with alphabet a and states s induces a monoid action on s by the free monoid [a]. It's not hard to check that the function produced by foldright indeed satisfies the requirements to be a monoid action. First, recall that foldright f = chain where chain (a:as) = (f a).(chain as) chain _ = id Now, we can easily prove (1): (foldright f) [] = chain [] = id The proof of (2) is by induction on the length of m. The base case is obvious, given the proof of (1). (foldright f) (m ++ n) = { defn. of foldright, assume m = x:xs } chain ((x:xs) ++ n) = { defn. of (++) } chain (x:(xs ++ n)) = { defn. of chain } f x . chain (xs ++ n) = { inductive hypothesis } f x . chain xs . chain n = { defn. of chain, associativity of (.) } chain (x:xs) . chain n = { defn. of foldright, m } (foldright f) m . (foldright f) n How'd I do? I'm still trying to figure out how the generalization to Traversable fits in here. I'm guessing this is where the monoid homomorphisms come in. -Brent ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Pretty little definitions of left and right folds
On Sat, 2008-06-21 at 21:11 -0400, Brent Yorgey wrote: On Fri, Jun 20, 2008 at 09:52:36PM -0500, Derek Elkins wrote: On Fri, 2008-06-20 at 22:31 -0400, Brent Yorgey wrote: On Fri, Jun 20, 2008 at 06:15:20PM -0500, George Kangas wrote: foldright (+) [1, 2, 3] 0 == ( (1 +).(2 +).(3 +).id ) 0 foldleft (+) [1, 2, 3] 0 == ( id.(3 +).(2 +).(1 +) ) 0 Hi George, This is very cool! I have never thought of folds in quite this way before. It makes a lot of things (such as the identities you point out) obvious and elegant. We can also see the following identities: foldright f as == foldright (.) (map f as) id foldleft f as == foldright (flip (.)) (map f as) id I like that second one, after trying to read another definition of left fold in terms of right fold (in the web book Real World Haskell). The type signature, which could be written (a - (b - b)) - ([a] - (b - b)), suggests generalization to another type constructor C: (a - (b - b)) - (C a - (b - b)). Would a foldable typeclass make any sense? As Brandon points out, you have rediscovered Data.Foldable. =) There's nothing wrong with that, congratulations on discovering it for yourself! But again, I like this way of organizing the type signature: I had never thought of a fold as a sort of 'lift' before. If f :: a - b - b, then foldright 'lifts' f to foldright f :: [a] - b - b (or C a - b - b, more generally). Okay, it goes without saying that this is useless dabbling, but have I entertained anyone? Or have I just wasted your time? I eagerly await comments on this, my first posting. Not at all! Welcome, and thanks for posting. Look into the theory of monoids, monoid homomorphisms, M-sets and free monoids. Thanks for the pointers! Here's what I've come up with, after re-reading some Barr-Wells lecture notes. First, given finite sets A (representing an 'alphabet') and S (representing 'states'), we can describe a finite state machine by a function phi : A x S - S, which gives 'transition rules' giving a new state for each combination of alphabet character and state. If we squint and wave our hands and ignore the fact that types aren't exactly sets, and most of the types we care about have infinitely many values, this is very much like the Haskell type (a,s) - s, or (curried) a - s - s, i.e. a - (s - s). So we can think of a Haskell function phi :: a - (s - s) as a sort of 'state machine'. Also, for a monoid M and set S, an action of M on S is given by a function f : M x S - S for which (1) f(1,s) = s, and (2) f(mn,s) = f(m,f(n,s)). Of course, in Haskell we would write f :: m - (s - s), This change is not completely trivial. and we would write criteria (1) and (2) as (1) f mempty = id (2) f (m `mappend` n) = f m . f n So what does this make f? Hint: What is (s - s)? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Pretty little definitions of left and right folds
On Sat, Jun 21, 2008 at 09:36:06PM -0500, Derek Elkins wrote: On Sat, 2008-06-21 at 21:11 -0400, Brent Yorgey wrote: On Fri, Jun 20, 2008 at 09:52:36PM -0500, Derek Elkins wrote: On Fri, 2008-06-20 at 22:31 -0400, Brent Yorgey wrote: On Fri, Jun 20, 2008 at 06:15:20PM -0500, George Kangas wrote: foldright (+) [1, 2, 3] 0 == ( (1 +).(2 +).(3 +).id ) 0 foldleft (+) [1, 2, 3] 0 == ( id.(3 +).(2 +).(1 +) ) 0 Hi George, This is very cool! I have never thought of folds in quite this way before. It makes a lot of things (such as the identities you point out) obvious and elegant. We can also see the following identities: foldright f as == foldright (.) (map f as) id foldleft f as == foldright (flip (.)) (map f as) id I like that second one, after trying to read another definition of left fold in terms of right fold (in the web book Real World Haskell). The type signature, which could be written (a - (b - b)) - ([a] - (b - b)), suggests generalization to another type constructor C: (a - (b - b)) - (C a - (b - b)). Would a foldable typeclass make any sense? As Brandon points out, you have rediscovered Data.Foldable. =) There's nothing wrong with that, congratulations on discovering it for yourself! But again, I like this way of organizing the type signature: I had never thought of a fold as a sort of 'lift' before. If f :: a - b - b, then foldright 'lifts' f to foldright f :: [a] - b - b (or C a - b - b, more generally). Okay, it goes without saying that this is useless dabbling, but have I entertained anyone? Or have I just wasted your time? I eagerly await comments on this, my first posting. Not at all! Welcome, and thanks for posting. Look into the theory of monoids, monoid homomorphisms, M-sets and free monoids. Thanks for the pointers! Here's what I've come up with, after re-reading some Barr-Wells lecture notes. First, given finite sets A (representing an 'alphabet') and S (representing 'states'), we can describe a finite state machine by a function phi : A x S - S, which gives 'transition rules' giving a new state for each combination of alphabet character and state. If we squint and wave our hands and ignore the fact that types aren't exactly sets, and most of the types we care about have infinitely many values, this is very much like the Haskell type (a,s) - s, or (curried) a - s - s, i.e. a - (s - s). So we can think of a Haskell function phi :: a - (s - s) as a sort of 'state machine'. Also, for a monoid M and set S, an action of M on S is given by a function f : M x S - S for which (1) f(1,s) = s, and (2) f(mn,s) = f(m,f(n,s)). Of course, in Haskell we would write f :: m - (s - s), This change is not completely trivial. Hmm... why is that? Is it because of the types-aren't-really-sets thing? Or are there other reasons as well? and we would write criteria (1) and (2) as (1) f mempty = id (2) f (m `mappend` n) = f m . f n So what does this make f? Hint: What is (s - s)? Aha! f is a monoid homomorphism to the monoid of endomorphisms on s! Right? -Brent ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Pretty little definitions of left and right folds
On Sat, 2008-06-21 at 22:48 -0400, Brent Yorgey wrote: On Sat, Jun 21, 2008 at 09:36:06PM -0500, Derek Elkins wrote: On Sat, 2008-06-21 at 21:11 -0400, Brent Yorgey wrote: On Fri, Jun 20, 2008 at 09:52:36PM -0500, Derek Elkins wrote: On Fri, 2008-06-20 at 22:31 -0400, Brent Yorgey wrote: On Fri, Jun 20, 2008 at 06:15:20PM -0500, George Kangas wrote: foldright (+) [1, 2, 3] 0 == ( (1 +).(2 +).(3 +).id ) 0 foldleft (+) [1, 2, 3] 0 == ( id.(3 +).(2 +).(1 +) ) 0 Hi George, This is very cool! I have never thought of folds in quite this way before. It makes a lot of things (such as the identities you point out) obvious and elegant. We can also see the following identities: foldright f as == foldright (.) (map f as) id foldleft f as == foldright (flip (.)) (map f as) id I like that second one, after trying to read another definition of left fold in terms of right fold (in the web book Real World Haskell). The type signature, which could be written (a - (b - b)) - ([a] - (b - b)), suggests generalization to another type constructor C: (a - (b - b)) - (C a - (b - b)). Would a foldable typeclass make any sense? As Brandon points out, you have rediscovered Data.Foldable. =) There's nothing wrong with that, congratulations on discovering it for yourself! But again, I like this way of organizing the type signature: I had never thought of a fold as a sort of 'lift' before. If f :: a - b - b, then foldright 'lifts' f to foldright f :: [a] - b - b (or C a - b - b, more generally). Okay, it goes without saying that this is useless dabbling, but have I entertained anyone? Or have I just wasted your time? I eagerly await comments on this, my first posting. Not at all! Welcome, and thanks for posting. Look into the theory of monoids, monoid homomorphisms, M-sets and free monoids. Thanks for the pointers! Here's what I've come up with, after re-reading some Barr-Wells lecture notes. First, given finite sets A (representing an 'alphabet') and S (representing 'states'), we can describe a finite state machine by a function phi : A x S - S, which gives 'transition rules' giving a new state for each combination of alphabet character and state. If we squint and wave our hands and ignore the fact that types aren't exactly sets, and most of the types we care about have infinitely many values, this is very much like the Haskell type (a,s) - s, or (curried) a - s - s, i.e. a - (s - s). So we can think of a Haskell function phi :: a - (s - s) as a sort of 'state machine'. Also, for a monoid M and set S, an action of M on S is given by a function f : M x S - S for which (1) f(1,s) = s, and (2) f(mn,s) = f(m,f(n,s)). Of course, in Haskell we would write f :: m - (s - s), This change is not completely trivial. Hmm... why is that? Is it because of the types-aren't-really-sets thing? Or are there other reasons as well? No, it's just that though these types are isomorphic (in our squinted vision), they are still not identical and each is different way of viewing the same thing. and we would write criteria (1) and (2) as (1) f mempty = id (2) f (m `mappend` n) = f m . f n So what does this make f? Hint: What is (s - s)? Aha! f is a monoid homomorphism to the monoid of endomorphisms on s! Right? Yep. So the monoid action, MxS - S can be curried to give M - (S-S), a monoid homomorphism, or further we can swap the arguments and curry giving S - (M-S); one name for this last form, connected to a totally different field, is a flow. In this case, let M be the monoid of time (the non-negative reals under addition) and S be points in space (R^3 say). Usually, in this case, the monoid action will be the solution of a differential equation. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] message passing style like in Haskell?
After some fiddling with this style, here is what I came up with for the 8 queens problem in the 99 problem set. It's quite entertaining ... ( note: it's brute force and requires a combination library ) queens2 n = n.permutations.filter all_satisfied where all_satisfied queens = queens.diff_col queens.diff_diag diff_col queens = queens.unique.is queens diff_diag queens = n .combinations 2 .map (map (subtract 1)) .map (id flip cherry_pick queens) .any same_dist.not where same_dist (row_pair, col_pair) = row_pair.foldl1 (-).abs == col_pair.foldl1 (-).abs -- generic helper cherry_pick ids xs = ids.map (xs !!) is a b = a == b unique xs = nub xs Guess this can conclude this experiment :) jinjing On Sun, Jun 22, 2008 at 1:10 AM, Ian Lynagh [EMAIL PROTECTED] wrote: On Fri, Jun 20, 2008 at 07:57:58AM +0200, Ketil Malde wrote: Albert Y. C. Lai [EMAIL PROTECTED] writes: While we are kind of on this topic, what makes the characters ħ þ prefix operator by default, while º and most other odd ones infix? alphanumeric vs non-alphanumeric Testing this, I find that isAlpha is True also for 'º', but as the OP claims, Haskell will use it as a(n infix) symbol. This is a bug in GHC. The characters = '\255' were done specially, but incorrectly for many of those = '\128'. I'll fix it, probably by just removing the specialisation for them. Thanks Ian ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] IMAP and NNTP libraries
Hi, Are there mature libraries for IMAP and NNTP available to Haskell? Thanks, Maurício ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe