[Haskell-cafe] sequence and sequence_ for iteratees

2011-01-17 Thread Maciej Wos
Hello Cafe!

I have written some iteratee functions that I found to be very very
useful and I hope they will soon make it to the iteratee library.
However, I'd like to get some feedback first, particularly about the
error handling.

I'm sending this here rather than to iteratee mailing list at
projects.haskell.org because the latter is (and has been for a while!)
down...

Anyway, enumSequence and enumSequence_ are inspired by Prelude's
sequence and sequence_. They are useful when one has to deal with
several iteratees consuming the same data. For instance, one could use
enumSequence as follows:

> run $ joinIM $ enumPureNChunk [1..100] 3 $
enumSequence [I.head, I.head >>= \x -> I.head >>= \y -> return
(x+y), I.last]

which produces:

[1,3,100]

Each iteratee in the list is given the same input stream. Also,
enumSequence consumes as much of the stream as the iteratee in the
list that consumes the most. In the above example there is no stream
left after enumSequence finishes because I.last consumes everything.

As an another example, enumSequence below consumes only the first two
elements of the stream and the remainder is passed to stream2list:

> run $ joinIM $ enumPureNChunk [1..10] 3 $
(enumSequence [I.head, I.head >> I.head] >> stream2list)

[3,4,5,6,7,8,9,10]

The code for enumSequence is enclosed below (enumSequence_ is almost
identical!) To make it complete though I should add some sort of error
handling. I'm not quite sure however what would be the best thing to
do. For instance, what should happen if the stream is finished, but
one of the iteratees is not done yet? Should the whole enumSequence
fail? Similarly, should the whole enumSequence fail if one of the
iteratees throws an error?

I guess throwing some sort of recoverable error could work. But I
still need to figure out how to do that!

-- Maciej

## code ##

enumSequence :: forall m s a el . (Monad m, LL.ListLike s el, Nullable s)
 => [Iteratee s m a]
 -> Iteratee s m [a]
enumSequence is = liftI step
where
step :: Stream s -> Iteratee s m [a]
step s@(Chunk xs) | LL.null xs = liftI step
  | otherwise  = do
  let is'  = map (joinIM . enumPure1Chunk xs) is
  allDone <- lift (checkIfDone is')
  if allDone then uncurry idone =<< lift (collectResults is')
 else enumSequence (updateChunk s is')
-- TODO: should return an error if not all iteratees are done
step (EOF _) = uncurry idone =<< lift (collectResults . map
(joinIM . enumEof) $ is)

-- returns true if *all* iteratees are done; otherwise returns false
checkIfDone :: [Iteratee s m a] -> m Bool
checkIfDone = liftM and . mapM (\i -> runIter i onDone onCont)
where
onDone _ _ = return True
onCont _ _ = return False

-- returns a list of result values and the unconsumed part of the stream
collectResults :: [Iteratee s m a] -> m ([a], Stream s)
collectResults = liftM (id *** foldl1 shortest)
 . mapAndUnzipM (\i -> runIter i onDone onCont)
where
onDone a s = return (a,s)
onCont _ _ = error "enumSequence: collectResults
failed; all iteratees should be done"

shortest :: Stream s -> Stream s -> Stream s
shortest (Chunk xs) (Chunk ys)
| LL.length xs > LL.length ys = Chunk ys
| otherwise   = Chunk xs
shortest s@(EOF _) _ = s
shortest _ s@(EOF _) = s


-- iteratee in *done* state holds the unconsumed part of the chunk it
-- was given; this chunk needs to be discarded when we move further in
-- the stream
updateChunk :: Stream s -> [Iteratee s m a] -> [Iteratee s m a]
updateChunk s = map (\i -> joinIM $ runIter i (\a _ -> return
$ idone a s) icontM)

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] A question about monad based on http://en.wikipedia.org/wiki/Monad_(category_theory)

2011-01-17 Thread C K Kashyap
Hi,
I was going through http://en.wikipedia.org/wiki/Monad_(category_theory) -
under Formal Definition I notice that monad is a Functor T:C -> C

My question is - when we think of Maybe as a functor T:C -> C  should we
think that C here refers to Hakell types? As in,
(Int and Maybe Int are objects in C) and (Int -> Int and Maybe Int -> Maybe
Int are arrows in C) and T is an arrow between them. Is that right? For some
reason, I was
imagining them as two different categories C and D.

I am not able to fully understand how those diagrams translate to haskell -
I can guess that T^2 -> T referes to things like concat operation but not
able to relate it to bind.

Regards,
Kashyap
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] HDBC, postgresql, bytestrings and embedded NULLs

2011-01-17 Thread Michael Snoyman
On Mon, Jan 17, 2011 at 11:38 PM, John Goerzen  wrote:
> On 01/17/2011 03:16 PM, Michael Snoyman wrote:
>>
>> I've brought up before my problem with the convertible package: it
>> encourages usage of partial functions. I would prefer two typeclasses,
>> one for guaranteed conversions and one for conversions which may fail.
>> In fact, that is precisely why convertible-text[1] exists.
>
> I would be open to making that change in convertible.  The unfortunate
> reality with databases, however, is that many times we put things into
> strings for sending to the DB engine, and get things back from it in the
> form of strings, which must then be parsed into numeric types and the like.
>  We can't, as a matter of type system principles, guarantee that a String
> can be converted to an Integer.  How were you thinking the separation into
> these typeclasses would be applied in the context of databases/
>
>> As a related issue, there are a large number of data constructors in
>> HDBC for SqlValue. I would not argue with the presence of any of them:
>> for your purposes, every one of them is necessary. But for someone
>> writing a cross-backend package with a more limited set of datatypes,
>> it gets to be a problem. I know I can use convertible for this, but
>> see my previous paragraph ;).
>
> How about using an import...hiding statement?  Perhaps even your own module
> that only re-exports the constructors you like?

In Persistent, we already have a very good idea of what the datatype
will be (integral, date/time, etc), and therefore dealing with the raw
bytestring would probably be preferable for us. (This is a bit of a
simplification, but close enough to reality.) I'm not actually
requesting you change HDBC to return ByteStrings, I'm simply stating
that, while a high-level API using Haskell datatypes is correct for
*most* uses, there are use cases whee a low-level API makes more
sense.

And the many-constructor issue isn't a matter of convenience or being
overwhelmed by constructors, it's a matter of correctness: if I want
to map a SqlValue onto a UTCTime value, I need to check a number of
different constructors, as opposed to using a ByteString and reading
out the value directly. In Persistent, I always know which type of
DATE or DATETIME was used for creating tables, so I know which version
to anticipate. In theory, this argument applies to HDBC's constructors
as well; however, I think I was bitten by a bug previously to do with
timezoning issues.

>> I also don't like using the lazy result functions. I'm sure for many
>> people, they are precisely what is needed. However, in my
>> applications, I try to avoid it whenever possible. I've had bugs crop
>> up because I accidently used the lazy instead of strict version of a
>> function. I would prefer using an interface that uses enumerators[2].
>
> It would be pretty simple to add an option to the API to force the use of
> the strict versions of functions in all cases (or perhaps to generate an
> exception if a lazy version is attempted.)  Would that address the concern?
>  Or perhaps separating them into separate modules?

Again, I *personally* think that would be better, but I'm sure many
other HDBC users would consider this a change for the worse: there are
a lot of Haskellers who have no problem with lazy IO, and I don't want
to adversely affect their programming on a whim.

> I took a quick look at the enumerators library, but it doesn't seem to have
> the necessary support for handling data that comes from arbitrary C API
> function calls rather than handles or sockets.

It does support this, for prior art see yaml[1] or yajl-enumerator[2].
I'd be happy to help you with this, as having some enumerator
experience is a big help here.

>> For none of these do I actually think that HDBC should change. I think
>> it is a great library with a well-thought-out API. All I'm saying is
>> that I doubt there will ever be a single high-level API that will suit
>> everyone's need, and I see a huge amount of value in splitting out the
>> low-level code into a separate package. That way, *everyone* can share
>> that code together, *everyone* can find the bugs in it, and *everyone*
>> can benefit from improvements.
>
> Splitting out the backend code is quite reasonable, and actually that was
> one of the goals with the HDBC v2 API.  I would have no objection if people
> take, say, HDBC-postgresql and add a bunch of non-HDBC stuff to it, or even
> break off the C bindings to a separate package and then make HDBC-postgresql
> an interface atop that.
>
> I hope that we can, however, agree upon one low-level database API.  The
> Java, Python, and Perl communities, at least, have.  Failing to do so
> produces unnecessary incompatibility.
>
> I would also hope that this database API would be good enough that there is
> rarely call to bypass it and use a database backend directly.

I agree 100%. The question is whether or not we can all agree on what
is low-level. I think the di

Re: [Haskell-cafe] A question about monad laws

2011-01-17 Thread C K Kashyap
Thanks Tobias,

Without the associativity law, it would be very hard to determine the
> current state of the monad.
> Since the compiler, on "desugaring" do-blocks, will insert brackets, there
> is no guarantee that the results are the same as for the brace-less and
> sugar-free version of the code.
>

Indeed this example helps me.
Regards,
Kashyap

>
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Denotational semantics for the lay man.

2011-01-17 Thread Claus Reinke

I've recently had the opportunity to explain in prose what denotational
semantics are to a person unfamiliar with it. I was trying to get across
the concept of distilling the essence out of some problem domain. I
wasn't able to get the idea across so I'm looking for some simple ways
to explain it.


Trying to explain things to a layperson is often a useful exercise.
So here goes my attempt:

Semantics are all about relating two partially understood structures
to each other, in the hope of reaching a better understanding of both.

Often, the plan is to translate a problem systematically from a less
well-understood domain to a domain whose tools are more amenable
to solving the problem, then to translate the solution back to the 
original domain (and originally, just about anything was better

understood than programming).

In the beginning, the meaning of a program was what it caused a 
certain machine to do - nothing to relate to but experience. Then 
there were specifications of how conforming machines were 
supposed to execute a given program, so one could compare a

given machine against the specification. Mathematicians tried to
relate programs to mathematical objects (traditionally functions),
which they thought they understood better, only to learn a thing
or two about both programs and functions. Programs for concrete
machines were related to programs for abstract machines, or
concrete program runs to abstract program executions, in order
to focus only on those aspects of program executions relevant
to the problem at hand. Programs were related to conditions in 
a logic, to allow reasoning about programs, or programming 
from proofs. And so on..


The two main questions are always: how well do the semantics
model reality, and how well do programs express the semantics?
It helps if the mappings back and forth preserve all relevant
structure and elide any irrelevant details.

Claus

anecdotal semantics:
   "you know, once I wrote this program, and it just fried the printer.."

barometric semantics:
   I think it is getting clearer..

conventional semantics:
   usually, this means..

detonational semantics:
   what does this button do?

existential semantics:
   I'm sure it means something.

forensic semantics:
   I think this was meant to prevent..

game semantics:
   let the dice decide

historical semantics:
   I'm sure this used to work

idealistic semantics:
   this can only mean..

jovial semantics:
   oh, sure, it can mean that.

knotty semantics:
   hm, this part over here probably means..

looking glass semantics:
   when I use a program, it means just what I choose it to mean, 
   neither more nor less


musical semantics:
   it don't mean a thing if it ain't got that swing.

nihilistic semantics:
   this means nothing.

optimistic semantics:
   this could mean..

probabilistic semantics:
   often, this means that..

quantum semantics:
   you can't ask what it means.

reactionary semantics:
   this means something else.

sherlockian semantics:
   since it cannot possibly mean anything else, ..

transitional semantics:
   for the moment, this means that..

utilitarian semantics:
   this means we can use it to..

venerable semantics:
   this has always meant..

weary semantics:
I guess that means..

xenophobic semantics:
   for us here, this means..

yogic semantics:
   we shall meditate on the meaning of this.

zen semantics:
   ah!



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Browser Game Engine

2011-01-17 Thread Luke Palmer
On Sun, Jan 16, 2011 at 8:26 PM, Tom Hawkins  wrote:
> I want to create a simple browser game using Haskell.  It would be
> nothing complicated: basic 2D graphics, limited sound, and minimal
> network traffic.
>
> What is the recommended medium?  Flash or JavaScript+SVG?

Unity3D is scriptable with Javascript and runs in browser.  Not sure
how difficult it would be to get the existing Javascript backends to
interface with that library, but it's worth a shot.  I highly
recommend Unity3D, it's a very complete game development tool (and is
just fine for 2D despite the name).

Luke

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Mailing lists on projects.haskell.org?

2011-01-17 Thread Erik de Castro Lopo
Erik de Castro Lopo wrote:

> Hi all,
> 
> I tried sending mail to the haskell-llvm mailing list ( AT
> projects.haskell.org) several days ago and today I received a bounce
> message.
> 
> Looking into the issue a little further, I find that DNS has no MX
> record for projects.haskell.org and projects.haskell.org doesn't
> seem to be listening on port 25.
> 
> I would email the administrator, but that is also an @projects.h.o
> address and I doubt it would get through.
> 
> Posting here in the hope that someone who sees this can fix it.


Anyone?

There is still no mx record and still nothing listening on port 25.

Erik
-- 
--
Erik de Castro Lopo
http://www.mega-nerd.com/

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] HDBC, postgresql, bytestrings and embedded NULLs

2011-01-17 Thread John Goerzen

On 01/17/2011 03:16 PM, Michael Snoyman wrote:

I've brought up before my problem with the convertible package: it
encourages usage of partial functions. I would prefer two typeclasses,
one for guaranteed conversions and one for conversions which may fail.
In fact, that is precisely why convertible-text[1] exists.


I would be open to making that change in convertible.  The unfortunate 
reality with databases, however, is that many times we put things into 
strings for sending to the DB engine, and get things back from it in the 
form of strings, which must then be parsed into numeric types and the 
like.  We can't, as a matter of type system principles, guarantee that a 
String can be converted to an Integer.  How were you thinking the 
separation into these typeclasses would be applied in the context of 
databases/



As a related issue, there are a large number of data constructors in
HDBC for SqlValue. I would not argue with the presence of any of them:
for your purposes, every one of them is necessary. But for someone
writing a cross-backend package with a more limited set of datatypes,
it gets to be a problem. I know I can use convertible for this, but
see my previous paragraph ;).


How about using an import...hiding statement?  Perhaps even your own 
module that only re-exports the constructors you like?



I also don't like using the lazy result functions. I'm sure for many
people, they are precisely what is needed. However, in my
applications, I try to avoid it whenever possible. I've had bugs crop
up because I accidently used the lazy instead of strict version of a
function. I would prefer using an interface that uses enumerators[2].


It would be pretty simple to add an option to the API to force the use 
of the strict versions of functions in all cases (or perhaps to generate 
an exception if a lazy version is attempted.)  Would that address the 
concern?  Or perhaps separating them into separate modules?


I took a quick look at the enumerators library, but it doesn't seem to 
have the necessary support for handling data that comes from arbitrary C 
API function calls rather than handles or sockets.



For none of these do I actually think that HDBC should change. I think
it is a great library with a well-thought-out API. All I'm saying is
that I doubt there will ever be a single high-level API that will suit
everyone's need, and I see a huge amount of value in splitting out the
low-level code into a separate package. That way, *everyone* can share
that code together, *everyone* can find the bugs in it, and *everyone*
can benefit from improvements.


Splitting out the backend code is quite reasonable, and actually that 
was one of the goals with the HDBC v2 API.  I would have no objection if 
people take, say, HDBC-postgresql and add a bunch of non-HDBC stuff to 
it, or even break off the C bindings to a separate package and then make 
HDBC-postgresql an interface atop that.


I hope that we can, however, agree upon one low-level database API.  The 
Java, Python, and Perl communities, at least, have.  Failing to do so 
produces unnecessary incompatibility.


I would also hope that this database API would be good enough that there 
is rarely call to bypass it and use a database backend directly.


-- John

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] is datetime actively maintained?

2011-01-17 Thread John MacFarlane
I've already modified the dev versions of filestore and gitit so
they don't depend on datetime. This should solve the problem. I
expect to be releasing new versions of both before long.

John

+++ Max Bolingbroke [Jan 17 11 07:50 ]:
> Hi,
> 
> I also wanted to build gitit on Windows and encountered the datetime
> issue. I sent the maintainer (Eric Sessoms) a request to bump his
> version bounds on the 22nd December, but haven't received a reply. (I
> had to bump a lot of other gitit version bounds, but those bumps
> *have* been taken upstream)
> 
> I'm not sure what to do in a situation like this - since Hackage has
> no security I could just upload a new version of datetime with the
> bumped bounds, but this hardly seems polite, and I'm not really keen
> on maintaining datetime long term. The change would also be lost if
> Eric decides to upload a new version of his own.
> 
> Cheers,
> Max
> 
> On 17 January 2011 02:05, Carter Schonwald  wrote:
> > Hey All,
> > I can't tell if datetime is actively maintained, and its version constraints
> > need to be adjusted to allow base4 (which would then allow a whole mess of
> > other packages including gitit build under ghc7). I've been sucessful
> >  building gitit with ghc7 64 on OS X and using it if that one constraint on
> > datetime  is modified.
> > any thoughts?
> > cheers,
> > -Carter
> >
> >
> > ___
> > 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] Denotational semantics for the lay man.

2011-01-17 Thread Gregg Reynolds
On Mon, Jan 17, 2011 at 12:55 PM, David Sankel  wrote:

> Hello All,
>
> I've recently had the opportunity to explain in prose what denotational
> semantics are to a person unfamiliar with it. I was trying to get across the
> concept of distilling the essence out of some problem domain. I wasn't able
> to get the idea across so I'm looking for some simple ways to explain it.
>
> Does anyone know of a way to explain what's the meaning and objective of
> "distilling the essence" without introducing more jargon. One thing that
> comes to mind is how Newton's equations for gravity were a distillation of
> the essence of the way things fall.
>

A stick figure as an iconic image of a person serves as a symbol of
"person"; i.e. it denotes person.  It works because of the parts and their
arrangement.  Ditto for mathematical expressions: the whole is equal to the
sum of its parts (and the way they are arranged.)  The individual parts
(e.g. the symbol '3') are not iconic, but  the way the whole system works
corresponds exactly with the way we imagine mathematical objects work.

Google around for "correspondence theory of truth" and you might find some
useful material.

It might be useful to introduce Platonism (see wikipedia.)  Also try boiling
it down to compositionality, substitutability, and equality.

It might also help to draw a contrast with what is not denotational.
 Denotational semantics depends essentially on the fiction of univocality,
namely that every symbol has one delimited meaning: it means what it means,
and nothing else.  But in fact univocality is not possible even in theory;
you cannot circumscribe the the proliferation of meanings.   A simple
example:  2+3 denotes (the same thing as) 5; it may also be taken to denote
a computation that yields 5.  But it does not denote the energy consumed in
carrying out that computation.  A horribly inefficient algorithm may have
the same denotation as a very efficient one.  Another way of putting it is
that denotation is about extensionality to the exclusion of intensional
meanings.  Going beyond extensionality is where Category Theory enters the
picture.

HTH
-Gregg Reynolds
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] HDBC, postgresql, bytestrings and embedded NULLs

2011-01-17 Thread Michael Snoyman
On Mon, Jan 17, 2011 at 10:52 PM, John Goerzen  wrote:
> On 01/17/2011 10:07 AM, Michael Snoyman wrote:
>>
>> On Mon, Jan 17, 2011 at 4:49 PM, Leon Smith
>>  wrote:
>>>
>>> On Sat, Jan 8, 2011 at 11:55 AM, Michael Snoyman
>>>  wrote:
>>>
 In general I think it would be a good thing to have solid, low-level
 bindings to PostgreSQL.
>>>
>>> Well, there is PostgreSQL and libpq on hackage:
>>>
>>> http://hackage.haskell.org/package/libpq
>>> http://hackage.haskell.org/package/PostgreSQL
>>>
>>> The PostgreSQL looks like it's in need of maintenance,  and hasn't
>>> been updated in a few years.   libpq is new,  and looks promising.   I
>>> haven't really used either one, so I can't really say too much about
>>> either.
>>>
>>> Best,
>>> Leon
>>>
>>
>> I've tried PostgreSQL before, and if I remember correctly I couldn't
>> even build it. libpq looks interesting, I'd like to try it out.
>> Unfortunately it depends on unix, which would be a problem for Windows
>> users. If it looks like a good fit for persistent-postgresql, maybe I
>> can convince the author to replace the unix dep with something else
>> (unix-compat might be sufficient).
>
> I would also like to know what things people find are deficient in HDBC or
> HDBC-postgresql.  If the API isn't good enough for some uses, that could be
> fixed.  I would like to avoid a proliferation of database libraries as that
> is unnecessary duplication of work.  HDBC does have an easy way for DB
> backends to implement more functionality than the HDBC API supports, or an
> alternative could also be to make HDBC-postgresql a thin binding over libpq
> or some such.

I've brought up before my problem with the convertible package: it
encourages usage of partial functions. I would prefer two typeclasses,
one for guaranteed conversions and one for conversions which may fail.
In fact, that is precisely why convertible-text[1] exists.

As a related issue, there are a large number of data constructors in
HDBC for SqlValue. I would not argue with the presence of any of them:
for your purposes, every one of them is necessary. But for someone
writing a cross-backend package with a more limited set of datatypes,
it gets to be a problem. I know I can use convertible for this, but
see my previous paragraph ;).

I also don't like using the lazy result functions. I'm sure for many
people, they are precisely what is needed. However, in my
applications, I try to avoid it whenever possible. I've had bugs crop
up because I accidently used the lazy instead of strict version of a
function. I would prefer using an interface that uses enumerators[2].

For none of these do I actually think that HDBC should change. I think
it is a great library with a well-thought-out API. All I'm saying is
that I doubt there will ever be a single high-level API that will suit
everyone's need, and I see a huge amount of value in splitting out the
low-level code into a separate package. That way, *everyone* can share
that code together, *everyone* can find the bugs in it, and *everyone*
can benefit from improvements.

Michael

[1] http://hackage.haskell.org/package/convertible-text
[2] http://hackage.haskell.org/package/enumerator

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] HDBC, postgresql, bytestrings and embedded NULLs

2011-01-17 Thread John Goerzen

On 01/17/2011 10:07 AM, Michael Snoyman wrote:

On Mon, Jan 17, 2011 at 4:49 PM, Leon Smith  wrote:

On Sat, Jan 8, 2011 at 11:55 AM, Michael Snoyman  wrote:


In general I think it would be a good thing to have solid, low-level bindings 
to PostgreSQL.


Well, there is PostgreSQL and libpq on hackage:

http://hackage.haskell.org/package/libpq
http://hackage.haskell.org/package/PostgreSQL

The PostgreSQL looks like it's in need of maintenance,  and hasn't
been updated in a few years.   libpq is new,  and looks promising.   I
haven't really used either one, so I can't really say too much about
either.

Best,
Leon



I've tried PostgreSQL before, and if I remember correctly I couldn't
even build it. libpq looks interesting, I'd like to try it out.
Unfortunately it depends on unix, which would be a problem for Windows
users. If it looks like a good fit for persistent-postgresql, maybe I
can convince the author to replace the unix dep with something else
(unix-compat might be sufficient).


I would also like to know what things people find are deficient in HDBC 
or HDBC-postgresql.  If the API isn't good enough for some uses, that 
could be fixed.  I would like to avoid a proliferation of database 
libraries as that is unnecessary duplication of work.  HDBC does have an 
easy way for DB backends to implement more functionality than the HDBC 
API supports, or an alternative could also be to make HDBC-postgresql a 
thin binding over libpq or some such.


-- John



Thanks for the pointer,
Michael

___
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] Happstack events and web page refreshing

2011-01-17 Thread Jeremy Shaw


On Jan 17, 2011, at 2:19 PM, Corentin Dupont wrote:


Indeed, I tried with  ?
and it's unusable.
It make blink the page, ungrey the "stop" button for a second and  
make the fields loose the focus

so it's impossible to type in.

I'll try with XMLHTTPRequest.



Right. Using the jQuery library should make it easier to do ajax  
requests and modify the DOM on the fly,


http://jquery.com/

- jeremy___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] A question about monad laws

2011-01-17 Thread Tobias Schoofs

Perhaps this might help:

I wrote a kind of logger monad that inserted messages with a context.
Behind was an algebraic data type, let's say "LoggerState".

The API provided methods to add a message like this:

addError :: String -> Logger ()
addError "Fatal error occurred"
addWarning "Some warning"
etc.

There were methods to enter and exit the context:

enterContext :: String -> Logger ()
exitContext :: Logger ()
enterContext "System x"
enterContext "SubSystem x.y"
enterContext "Module x.y.z"
exitContext
exitContext
exitContext

Well, the context was stored in an attribute of LoggerState, let's call 
it ctx.

The add function would pick the current ctx and add it to the message.
A message was defined as: ([Context], (State, String)) where State was 
Ok, Warning, Error.


This, however, violates the associativity. In consequence, the context 
of messages would depend on parentheses, e.g.:


someFunction :: Logger ()
someFunction = do
  enterContext "A"
  addError "Some Error"
  callSomeOtherFunction
  exitContext
  enterContext "B"
  ...

produces a different result than:

someFunction =
  enterContext "A" >>
  addError "Some Error" >>
  callSomeOtherFunction >>
  exitContext >>
  enterContext "B" >>
  ...

Imagine:

someFunction =
  enterContext A >>
  (addError "Some Error" >>
   callSomeOtherFunction >>
   exitContext)
  enterContext "B"
  ...

Here, addError and callSomeOtherFunction would operate on a LoggerState 
without any Context,

whereas in the previous example, there is context "A".

Without the associativity law, it would be very hard to determine the 
current state of the monad.
Since the compiler, on "desugaring" do-blocks, will insert brackets, 
there is no guarantee that the results are the same as for the 
brace-less and sugar-free version of the code.


Hope this helps!

Tobias

On 01/17/2011 04:21 AM, C K Kashyap wrote:

Hi,
I am trying to get a better understanding of the "monad laws" - right 
now, it seems too obvious
and pointless. I had similar feelings about identity function when I 
first saw it but understood its use

when I saw it in action.
Could someone please help me get a better understanding of the 
necessity of monads complying with these laws?
I think, examples of somethings stop working if the monad does not 
obey these laws will help me understand it better.

Regards,
Kashyap


___
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] Happstack events and web page refreshing

2011-01-17 Thread Corentin Dupont
Indeed, I tried with  ?
and it's unusable.
It make blink the page, ungrey the "stop" button for a second and make the
fields loose the focus
so it's impossible to type in.

I'll try with XMLHTTPRequest.


On Mon, Jan 17, 2011 at 6:30 PM, Chris Smith  wrote:

> On Mon, 2011-01-17 at 18:25 +0100, Corentin Dupont wrote:
> > Thanks a lot for your response Jeremy.
> > I can see a lot of site that does update infos without the user to
> > have to click "refresh" (I think Facebook does?).
> > Do they do polling?
>
> While I'm not familiar with Facebook, I'd guess that today, most such
> sites are all doing polling.  Especially the high-volume ones, for which
> leaving a connection open for every current user would be impractical.
>
> Polling doesn't have to be done with page refreshes.  It can also be
> done with JavaScript using, e.g., the XMLHTTPRequest object.  Then it
> would be pretty transparent to you.
>
> --
> Chris
>
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Denotational semantics for the lay man.

2011-01-17 Thread Albert Y. C. Lai

On 11-01-17 01:55 PM, David Sankel wrote:

I've recently had the opportunity to explain in prose what denotational
semantics are to a person unfamiliar with it. I was trying to get across
the concept of distilling the essence out of some problem domain. I
wasn't able to get the idea across so I'm looking for some simple ways
to explain it.

Does anyone know of a way to explain what's the meaning and objective of
"distilling the essence" without introducing more jargon. One thing that
comes to mind is how Newton's equations for gravity were a distillation
of the essence of the way things fall.


(I'm afraid Newton's equations introduced more jargon too.)

A denotational semantics maps programs to math things. The mapping has 
to be by structural recursion over program syntax.


Why math: it's the universal modelling language. Why do we model 
programs by a math model: to explain and predict.


Why structural recursion over syntax: to be compositional, the same 
reason we stay close to CFGs for syntax: you can build bottom-up and 
analyze top-down.


It also happens that the math things are given a partial order to help 
answer "what to do with loop constructs and/or cyclic definitions". A 
moment of trying to re-invent denotational semantics reveals that this 
is the only hard part, and so learning denotational semantics typically 
takes 90% of the time on this part.


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Zipper with two focii for concurrent fun

2011-01-17 Thread Yaakov M. Nemoy
Ah, many thanks. this looks about right. I'll go through it later
today and see if i have any questions about it.

-Yaakov

On Mon, Jan 17, 2011 at 06:22:34PM +1030, John Lask wrote:
> 
> 
> see http://okmij.org/ftp/continuations/Continuations.html,
> "Zipper-based file server/OS", aka ZFS
> 
> where Oleg addresses concurrent operations on a shared data
> structure with multiple mutable cursors implemented via delimited
> continuations with varying isolation levels  ...
> 
> your issues are specifically addressed.
> 
> jvl
> 
> 
> 
> On 17/01/2011 5:20 PM, Yaakov M. Nemoy wrote:
> 
> >Hey List,
> >
> >So i've been pondering zippers in a concurrent situation, where more
> >than one thread has access to the same data structure, but with
> >different focal points. I've gotten a bit stuck though trying to
> >figure out how to do this using a zipper. The use case i'm thinking of
> >is slightly more complex, but using a basic example below should
> >suffice for an explanation.
> >
> >Say we have a list of characters in a text file which we're using in
> >the form of a zipper to show the current user's cursor position in the
> >text file. This means a data type such as:
> >
> >data ZipperList a = ZL [a] a [a]
> >
> >where the second element is the current position of the cursor. Now as
> >long as one user is editing the text file, we're fine.
> >
> >Now let's say we're working on a newfangled multipointer desktop where
> >two users have a keyboard and mouse each and want to work on the same
> >open file. We could have a list like this:
> >
> >data ZipperList a = ZL [a] a [a] a [a]
> >
> >which leaves us with a number of limitations. We're now limited to two
> >cursors rather than one, but once we start assuming multipointer, two
> >seems like a bigger limitation than just one cursor. Also, we can't
> >tell which user is at which focal point of the zipper easily.
> >
> >We could replace the user with:
> >
> >data User a = User String a
> >
> >data ZipperList a = ZL [a] (User a) [a] (User a) [a]
> >
> >but that gets into some ugly pattern matching. It still leaves us
> >stuck with a two user limit.
> >
> >Say we create a zipper that can contain other zippers. Such as:
> >
> >data ZipperList a = ZL (ZipperList a) a (ZipperList a) | L [a]
> >
> >which lets us include as many users as we want. Now this creates a
> >very different problem. Each cursor in our fictional text editor has a
> >totally different data structure. This is how any calculation at any
> >cursor knows where in the text file it is. If one user edits the
> >character at the cursor, every other zipper-like data structure has to
> >be updated for each other users. Thus a huge concurrency issue.
> >
> >This is where i hit a block. Am i totally crazy in thinking i can
> >solve this problem using a zipper? Or has someone thought about this
> >problem already and perhaps there's even a paper on it? Please advise.
> >
> >-Yaakov
> >
> >___
> >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] Denotational semantics for the lay man.

2011-01-17 Thread caseyh

You probably want to bring up other forms of semantics.

Axiomatic semantics:
Makes no distinction between a phrase's meaning and the logical  
formulas that describe it; its meaning is exactly what can be proven  
about it in some logic.


Operational semantics:
The execution of the language is described directly (rather than by  
translation).

Operational semantics loosely corresponds to "interpretation".
Can be defined via syntactic transformations on phrases of the  
language itself.



Denotational semantics:
each phrase in the language is translated into a denotation, i.e. a  
phrase in some other language. Denotational semantics loosely  
corresponds to "compilation", although the "target language" is  
usually a mathematical formalism rather than another computer language.



Above from Wikipedia.


Quoting David Sankel :


Hello All,

I've recently had the opportunity to explain in prose what denotational
semantics are to a person unfamiliar with it. I was trying to get across the
concept of distilling the essence out of some problem domain. I wasn't able
to get the idea across so I'm looking for some simple ways to explain it.

Does anyone know of a way to explain what's the meaning and objective of
"distilling the essence" without introducing more jargon. One thing that
comes to mind is how Newton's equations for gravity were a distillation of
the essence of the way things fall.

Thanks in advance,

David

--
David Sankel
Sankel Software
www.sankelsoftware.com






___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] is datetime actively maintained?

2011-01-17 Thread Henk-Jan van Tuyl
On Mon, 17 Jan 2011 08:50:52 +0100, Max Bolingbroke  
 wrote:



Hi,

I also wanted to build gitit on Windows and encountered the datetime
issue. I sent the maintainer (Eric Sessoms) a request to bump his
version bounds on the 22nd December, but haven't received a reply. (I
had to bump a lot of other gitit version bounds, but those bumps
*have* been taken upstream)

I'm not sure what to do in a situation like this - since Hackage has
no security I could just upload a new version of datetime with the
bumped bounds, but this hardly seems polite, and I'm not really keen
on maintaining datetime long term. The change would also be lost if
Eric decides to upload a new version of his own.

Cheers,
Max



I found Eric Sessoms' blog[0], he writes (via speech recognition  
software), that he cannot type because of tendinitis in his hands. That  
probably explains why he does not answer; maybe someone willing to take  
over maintenance can contact him via Skype or something like that. Other  
packages with his name in the maintainer field:

 - erlang
 - fitsio
 - scgi

Regards,
Henk-Jan van Tuyl


[0] http://esessoms.posterous.com/


--
http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
--

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Denotational semantics for the lay man.

2011-01-17 Thread David Sankel
Hello All,

I've recently had the opportunity to explain in prose what denotational
semantics are to a person unfamiliar with it. I was trying to get across the
concept of distilling the essence out of some problem domain. I wasn't able
to get the idea across so I'm looking for some simple ways to explain it.

Does anyone know of a way to explain what's the meaning and objective of
"distilling the essence" without introducing more jargon. One thing that
comes to mind is how Newton's equations for gravity were a distillation of
the essence of the way things fall.

Thanks in advance,

David

-- 
David Sankel
Sankel Software
www.sankelsoftware.com
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Happstack events and web page refreshing

2011-01-17 Thread Chris Smith
On Mon, 2011-01-17 at 18:25 +0100, Corentin Dupont wrote:
> Thanks a lot for your response Jeremy.
> I can see a lot of site that does update infos without the user to
> have to click "refresh" (I think Facebook does?).
> Do they do polling?

While I'm not familiar with Facebook, I'd guess that today, most such
sites are all doing polling.  Especially the high-volume ones, for which
leaving a connection open for every current user would be impractical.

Polling doesn't have to be done with page refreshes.  It can also be
done with JavaScript using, e.g., the XMLHTTPRequest object.  Then it
would be pretty transparent to you.

-- 
Chris


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Happstack events and web page refreshing

2011-01-17 Thread Corentin Dupont
Thanks a lot for your response Jeremy.
I can see a lot of site that does update infos without the user to have to
click "refresh" (I think Facebook does?).
Do they do polling?
I think this approach would be fine for me, my app if not very fast paced.
Then I don't need to add a event handler to happstack-state ;)

This is done with something like:  ?

Thanks,
Corentin


On Mon, Jan 17, 2011 at 5:41 PM, Jeremy Shaw  wrote:

> Hello,
>
> The problem is that clients are not 'connected' to the web server. The way
> it works (more or less) is:
>
>  1. client connects to server and sends a Request
>  2. server sends a Response
>  3. connection is terminated.
>
> So, once the page has been loaded there is no connection between the
> web-server and the client. Hence there is no way for the server to send
> anything to the clients.
>
> In HTML 5, there is something known as web sockets with does allow you to
> have a persistent connection to the clients. However, it does not yet have
> broad browser support. In fact, some browsers have temporarily removed
> support due to a security flaw in the design specification.
>
> There are a variety of hacks collectively known as 'comet' for trying to
> create a persistent connection on top of the existing HTTP 1.1 standard:
>
> http://en.wikipedia.org/wiki/Comet_(programming)
>
> That deals with trying to have the server push updates to the client.
>
> A different approach is to have the clients pull new data from the server
> by polling the server for changes every now and then. But that depends on
> how much latency you can afford for the updates. For example,  if updating
> only once a minute is fine versus must update every second.
>
> Once you decide how to get the data to the client, then you can figure out
> how to track changes to the MACID database.
>
> - jeremy
>
> On Jan 17, 2011, at 4:58 AM, Corentin Dupont wrote:
>
> Hello again,
> I have another question for happstack users/experts:
>
> I have a program with happstack-state and a web server with
> happstack-server.
> What I'd like is, whenever the MACID state is changed, that the web page is
> refreshed for every clients
> connected on the web server.
>
> So I think the question is 2 fold:
> - How to add an event handler on happstack-state for that?
> - How to ask to the web server to refresh every clients?
>
> I did not found infos about that in the API.
>
> Thanks a lot,
> Corentin
>
>
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Browser Game Engine

2011-01-17 Thread Jeremy Shaw


On Jan 16, 2011, at 9:26 PM, Tom Hawkins wrote:


I want to create a simple browser game using Haskell.  It would be
nothing complicated: basic 2D graphics, limited sound, and minimal
network traffic.

What is the recommended medium?  Flash or JavaScript+SVG?


I think your options are: flash or html5+canvas+javascript.

The must be at least a million flash games, so clearly it can be used  
for that. If you create a flash game, nobody gets excited. But if you  
create an html5 based game, then people still get excited about that.  
So, I assume it must be hard :)


For flash, you can use the free flex SDK. Flash supports bitmap and  
vector graphics, network IO and realtime audio. The runtime it  
supported almost everywhere except iOS and you pretty much only have  
to worry about supporting one version of flash.


HTML5 is trendy and cool, and can be made to do what you want. It has  
less support than flash since you need to be running a modern browser  
-- which many people are not. And you have to deal with trying to  
support multiple browsers.


So you need to decide:

 1. what technology do you want to invest in learning
 2. how important is it that people can run your game with out having  
to install a new browser

 3. how much hackery are you willing to do


Any recommended Haskell libraries for the server-side logic or client
code generation?


I have not found any way to satisfyingly generate the client-side  
code. Ultimately, it has just turned out to be easiest to write the  
client-side code in javascript/actionscript by hand.


For the server-side, it would be useful to know what sort of game  
logic is going to be run in the server itself. For many browser based  
games, the server just needs to serve static files, and apache would  
be sufficient.


In other games some of the game logic does reside on the server.

The nice way to do that would be to have some nice Haskell datatypes  
to represent the game state and then functions that get called to  
update the game state and return values to the client.


The tricky party is that you don't really want to lose the whole game  
state every time you restart the server. You could try checkpointing  
the state now and then, but that still leaves things open to data  
loss. If someone finally defeats the big boss and then you lose the  
last few minutes of the game state, they are not going to be happy.


You could keep all your state stored in a SQL database. But that just  
does not sound like a good match for game logic ?


Personally, I would recommend checking out happstack-state.

happstack-state (also known as MACID), allows you to use normal  
haskell datatypes and functions for your game state and game logic.  
But it also uses write ahead logging to ensure that every update to  
the state is logged so that you can restart the server with the state  
intact (even if the server crashed). It also provides support for  
replication across multiple servers.


There is a short tutorial here that will help you get a feel for it,

http://www.kuliniewicz.org/blog/archives/2009/04/05/happstackstate-the-basics/


You also need an API for talking to the server from the client.

You can probably make just about any of the Haskell web servers do the  
trick. But I think happstack-server is a fine choice. The darcs  
version of happstack-server is well documented, and provides a rich,  
complete API.


For example, let's say that an api call requires you to pass three key/ 
value pairs in via the QUERY_STRING. All the web frameworks have calls  
for looking up the key/value pairs. But happstack goes beyond that and  
provides an environment which can accumulate and report all the  
missing or incorrect query parameters at once, not just the first one.  
This makes it much easier to debug your client-side code since you get  
all the errors at once -- instead of just one at a time.


(http://happstack.com/docs/crashcourse/RqData.html#rqdataerror)

Check out the crash course to get a feel for the API coverage,

http://happstack.com/docs/crashcourse/index.html

the haddock documentation in the darcs code has very good coverage.

- jeremy

p.s. The darcs code is quite stable. The main holdup is just making  
the haddock docs even better.




 ___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] class-instance

2011-01-17 Thread Patrick Browne
A functional dependency seems to allow me to express my rather strange
requirement.

class Person i n | i  -> n where
 pid :: i
 name :: i -> n

instance Person Int String where
 pid = 1
 name(1) = "john"


-- name(pid::Int) will produce john

Thanks for your help

Pat



On 17/01/2011 14:07, Patrick Browne wrote:
> On 17/01/2011 13:04, Ketil Malde wrote:
>>> So other PERSONs would have different types, say:
> 
> I think the problem is there is just one constant p1 in the class and
> there needs to be multiple constants in the implementation (one for each
> person). It seems difficult to specify this using type classes So, some
> data declaration as you suggest will probably be needed.
> 
> Thanks,
> Pat
> 
> 
> 
> This message has been scanned for content and viruses by the DIT Information 
> Services E-Mail Scanning Service, and is believed to be clean. 
> http://www.dit.ie
> 
> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe


This message has been scanned for content and viruses by the DIT Information 
Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Happstack events and web page refreshing

2011-01-17 Thread Jeremy Shaw

Hello,

The problem is that clients are not 'connected' to the web server. The  
way it works (more or less) is:


 1. client connects to server and sends a Request
 2. server sends a Response
 3. connection is terminated.

So, once the page has been loaded there is no connection between the  
web-server and the client. Hence there is no way for the server to  
send anything to the clients.


In HTML 5, there is something known as web sockets with does allow you  
to have a persistent connection to the clients. However, it does not  
yet have broad browser support. In fact, some browsers have  
temporarily removed support due to a security flaw in the design  
specification.


There are a variety of hacks collectively known as 'comet' for trying  
to create a persistent connection on top of the existing HTTP 1.1  
standard:


http://en.wikipedia.org/wiki/Comet_(programming)

That deals with trying to have the server push updates to the client.

A different approach is to have the clients pull new data from the  
server by polling the server for changes every now and then. But that  
depends on how much latency you can afford for the updates. For  
example,  if updating only once a minute is fine versus must update  
every second.


Once you decide how to get the data to the client, then you can figure  
out how to track changes to the MACID database.


- jeremy

On Jan 17, 2011, at 4:58 AM, Corentin Dupont wrote:


Hello again,
I have another question for happstack users/experts:

I have a program with happstack-state and a web server with  
happstack-server.
What I'd like is, whenever the MACID state is changed, that the web  
page is refreshed for every clients

connected on the web server.

So I think the question is 2 fold:
- How to add an event handler on happstack-state for that?
- How to ask to the web server to refresh every clients?

I did not found infos about that in the API.

Thanks a lot,
Corentin



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] HDBC, postgresql, bytestrings and embedded NULLs

2011-01-17 Thread Michael Snoyman
On Mon, Jan 17, 2011 at 4:49 PM, Leon Smith  wrote:
> On Sat, Jan 8, 2011 at 11:55 AM, Michael Snoyman  wrote:
>
>> In general I think it would be a good thing to have solid, low-level 
>> bindings to PostgreSQL.
>
> Well, there is PostgreSQL and libpq on hackage:
>
> http://hackage.haskell.org/package/libpq
> http://hackage.haskell.org/package/PostgreSQL
>
> The PostgreSQL looks like it's in need of maintenance,  and hasn't
> been updated in a few years.   libpq is new,  and looks promising.   I
> haven't really used either one, so I can't really say too much about
> either.
>
> Best,
> Leon
>

I've tried PostgreSQL before, and if I remember correctly I couldn't
even build it. libpq looks interesting, I'd like to try it out.
Unfortunately it depends on unix, which would be a problem for Windows
users. If it looks like a good fit for persistent-postgresql, maybe I
can convince the author to replace the unix dep with something else
(unix-compat might be sufficient).

Thanks for the pointer,
Michael

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] ANNOUNCE: Haskell binding to the ANTLR parser generator C runtime library

2011-01-17 Thread Mark Wright
Hi,

I'm pleased to announce the release of a Haskell binding to the
ANTLR LL(*) parser generator C runtime library.

The book on ANTLR is "The Definitive ANTLR Reference: Building Domain-Specific 
Languages"
by Terence Parr:

http://www.pragprog.com/titles/tpantlr/the-definitive-antlr-reference

ANTLR is a very powerful parser generator.  It has support for predicated
parsing, where dis-ambiguating semantic predicates can be used to look
up information in the symbol table to handle context sensitive programming
languages.

The lexing and parsing handled in C.  The C parser actions call
Haskell code to do things such as:

- enter data into the symbol table

- look up data in the symbol table to implement dis-ambiguating semantic
  predicates for parsing context sensitive programming languages.

- create Abstract Symbol Table entries.

The ANTLR parser generator site is at:

http://antlr.org/

The Haskell binding to the ANTLR parser generator C runtime library is at:

http://hackage.haskell.org/package/antlrc

An example based on the implementation of arithmetic expressions example
from "Types and Programming Languages" by Benjamin Pierce, chapter 4, is
on github:

https://github.com/markwright/antlrc-examples/tree/master/src/tapl/arith

Building the example is somewhat challenging.  I provide a Makefile,
however it is specific to my system and will need to be customised.

Documentation for the ANTLR C runtime library is at:

http://www.antlr.org/wiki/display/ANTLR3/ANTLR3+Code+Generation+-+C

Documentation for the ANTLR parser generator is at:

http://www.antlr.org/wiki/display/ANTLR3/ANTLR+v3+documentation

Thanks, Mark

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] HDBC, postgresql, bytestrings and embedded NULLs

2011-01-17 Thread Leon Smith
On Sat, Jan 8, 2011 at 11:55 AM, Michael Snoyman  wrote:

> In general I think it would be a good thing to have solid, low-level bindings 
> to PostgreSQL.

Well, there is PostgreSQL and libpq on hackage:

http://hackage.haskell.org/package/libpq
http://hackage.haskell.org/package/PostgreSQL

The PostgreSQL looks like it's in need of maintenance,  and hasn't
been updated in a few years.   libpq is new,  and looks promising.   I
haven't really used either one, so I can't really say too much about
either.

Best,
Leon

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Haskell, C and Matrix Multiplication

2011-01-17 Thread Pedro Vasconcelos
On Mon, 17 Jan 2011 10:38:30 +
Blake Rain  wrote:

> So sorry, I meant:
> 
> mult :: (Num a) => [[a]] -> [[a]] -> [a]
> mult m1 m2 = [foldl (+) 0 $ zipWith (*) x y | x <- m1, y <- transpose
> m2]
> 

Shouldn't the result of multiplying an (n,k)-matrix by an (k,m)-matrix
be an (n,m)-matrix? The way you've written it the result will be a list
of length n*m.

> 
> > 
> > Regarding performance: did you make sure you're forcing the
> > evaluation of the result matrix? 
> 
> I thought I was. It's printing the results as expected. Could be I'm
> just imagining I am and suffering a delusion or misunderstanding.
> 

Another issue to look out for is let-floating: if you perform the same
matrix multiplication repeatedly, the compiler might very well lift
the matrix calculation outside the loop.

Pedro

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] class-instance

2011-01-17 Thread Ketil Malde

Patrick Browne  writes:

> I think the problem is there is just one constant p1 in the class and
> there needs to be multiple constants in the implementation (one for each
> person). It seems difficult to specify this using type classes So, some
> data declaration as you suggest will probably be needed.

Yes.  I'm going to assume you're a beginner here (always a dangerous
assumption on this list, no offense if I just misinterpret what you're
trying to do).  The (one?) difference between a Haskell class and a typical OO
class, is that an instance of a class in Haskell is a *type*, but an
instance of a class in, say, C++, is an object (which we like to call a
*value*, to dissociate ourselves from That Other Crowd :-).

Let's ignore the 'name' for now.

  class Person p where
 id :: p

means that a *type* p is in the Person class if you can identify a value
of type p that is the person-id for that type.  So Int is a person-type
if you say 

  instance Person Int where
 id = 42

and (id::Int) will return 42 in subsequent code.  Similarly, you can
make other Person-types by instantiating them and defining id¹ for them.

A function can take a Person-type as a parameter, for instance

  foo :: Person p => p -> Bool
  foo x = bla blah (id x) blah

Alternatively, 

  data Person = P Int

on the other hand declares a new type called "Person", with a data
constructor (tag) P, and containing just an Int.  This means you can
make multiple values of this type, so 

  P 4, P 42, and P 911 

are all Persons your program can juggle around in your program.  A
function can take these persons:

  foo :: Person -> Int -> Bool
  foo (P x) y = bla bla apply int functions on x and y

Hope that was helpful?

-k

¹ Dumb name, as this is the identity function.  Sorry.
-- 
If I haven't seen further, it is by standing in the footprints of giants

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] class-instance

2011-01-17 Thread Patrick Browne
On 17/01/2011 13:04, Ketil Malde wrote:
>> So other PERSONs would have different types, say:

I think the problem is there is just one constant p1 in the class and
there needs to be multiple constants in the implementation (one for each
person). It seems difficult to specify this using type classes So, some
data declaration as you suggest will probably be needed.

Thanks,
Pat



This message has been scanned for content and viruses by the DIT Information 
Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] class-instance

2011-01-17 Thread Ketil Malde
Henning Thielemann  writes:

>> class PERSON a b where
>>  p1 :: a
>>  name :: a -> b

A multi-parameter typeclass is a relation over types...

>> instance PERSON  Int String where
>>  p1 = 1
>>  name p1 = "john"
 ^--note that this is just an unused paramter,
  it is clearer to specify 'name _ = "john"'

...so this instance gives the PERSON relation for Int and String.

So other PERSONs would have different types, say:

  instance PERSON Double Int where
 p1 = 3.14
 name = 200

Does that make sense?  It looks a bit strange to me.

> The problem is certainly, that a Haskell interpreter has no way to
> infer types, thus you have to annotate type of both argument and
> result of 'name', and type annotation for 'p1' is not possible at all,
> because 'b' does not occur in its type. You have to write
>
> name (12::Int) :: String

And (correct me if I'm wrong) there's no way to do that for p1, since
there is no way to specify which type 'b' you want.  You could have

  instance PERSON Int String where
p1 = 1
name = "john"

and

  instance PERSON Int Double where
p1 = 24
name = 3.14

then the system would have no way to figure out which value you want
even if you specify (p1::Int).

Are you sure you didn't want:

  data Person = P Int String

?

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] class-instance

2011-01-17 Thread Patrick Browne
Henning,
The code is not intended to fit into a larger application.
I am trying to understand instance-implements-class relation.
My experiment consists of writing simple English sentences and then
seeing how they could be specified and implemented in Haskell.
I am sure that this simple requirement could easily be coded in Haskell,
but I studying how to specify the requirement using type classes.

The bottom line is I want to stay with plain vanilla type classes.
If there is no simple way to do this using type classes then I am
obviously using the wrong technique.


Thanks,
Pat

On 17/01/2011 11:39, Henning Thielemann wrote:

> 
> But I suspect, what you try to do is not what you need. If you tell us
> what you want to do, we can certainly give more helpful answers.
> 
> Maybe you want to do something with functional dependencies, such that
> 'String' result type is automatically chosen if the argument type is
> 'Int'. But before suggesting this type extension, I'd like to know more
> about your problem.

I am trying to understand instance-implements-class relation.

This message has been scanned for content and viruses by the DIT Information 
Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Haskell, C and Matrix Multiplication

2011-01-17 Thread Francesco Guerrieri
On Mon, Jan 17, 2011 at 12:50 PM, Henning Thielemann <
lemm...@henning-thielemann.de> wrote:

> Blake Rain wrote:
>

>   Adjoint is even more of a fiddle, but actually a one-liner after
>>  reusing  bits   of  the  determinant   function  (including  the
>>  determinant function).
>>
>
> Isn't Adjoint just 'transpose' followed by complex conjugate?
>
>
I think he refers to the matrix of cofactors.

Francesco
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Haskell, C and Matrix Multiplication

2011-01-17 Thread Henning Thielemann

Ketil Malde wrote:

Blake Rain  writes:


Here is the one in C:

void multiplyM4 (float m1[4][4], float m2[4][4], float m3[4][4]) {
m1[0][0] = m2[0][0] * m3[0][0] + m2[0][1] * m3[1][0] + m2[0][2] * m3[2][0] + 
m2[0][3] * m3[3][0];
m1[0][1] = m2[0][0] * m3[0][1] + m2[0][1] * m3[1][1] + m2[0][2] * m3[2][1] + 
m2[0][3] * m3[3][1];

(etc)
:


Some points about the C version:

:

  6. It's pretty fast (being written in C):

 Performing  100,000,000 matrix  multiples  takes on  average
 6.117s (with gcc -O2).

:


Haskell version:

[foldl (+) 0 $ zipWith (*) x y | x <- m1, y <- transpose m2]

:

  5. It's very fast (being written in Haskell):

 Performing  100,000,000  matrix  multiples takes  on  average
 1.435s  (with ghc  -O2).


This is surprising: a C program doing an explicit list of arithmetic
operation is beaten by a factor of 4 or so by a polymorphic, lazy,
list-consuming Haskell program?  Are you sure? What am I missing? 


Constant folding? The numbers certainly depend on the kind of the test 
program.


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Haskell, C and Matrix Multiplication

2011-01-17 Thread Henning Thielemann

Blake Rain wrote:


  Determinant is  a bit fiddly,  but quite trivial. However  it is
  woefully slow at  O(n!). I need to make  use of LU decomposition
  (in which the  determinant is the sum of  the diagonal values in
  U).


product of diagonal values


  Adjoint is even more of a fiddle, but actually a one-liner after
  reusing  bits   of  the  determinant   function  (including  the
  determinant function).


Isn't Adjoint just 'transpose' followed by complex conjugate?

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Haskell, C and Matrix Multiplication

2011-01-17 Thread Henning Thielemann

Blake Rain wrote:

Oh yeah, it does. You're right. I haven't used it in so long that I
forgot about them! No need to be sorry.

Well, +2 for C, eh? :)

And I'd just make a mess if I used the preprocessor.


C optimizer should also unroll loops.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] class-instance

2011-01-17 Thread Henning Thielemann


On Mon, 17 Jan 2011, Patrick Browne wrote:


-- My intension is that the PERSON class should *specify*
-- that a person has a constant id called p1
-- and that a person has a name that can be found from the id.
class PERSON a b where
 p1 :: a
 name :: a -> b

-- My intension is that the instance should  implement the PERSON class
instance PERSON  Int String where
 p1 = 1
 name p1 = "john"

-- Why does the evaluations of p1 or name p1 produce errors?
-- how do I fix them and still keep the basic instance-implements-class 
relation?


The problem is certainly, that a Haskell interpreter has no way to infer 
types, thus you have to annotate type of both argument and result of 
'name', and type annotation for 'p1' is not possible at all, because 'b' 
does not occur in its type. You have to write


name (12::Int) :: String


But I suspect, what you try to do is not what you need. If you tell us 
what you want to do, we can certainly give more helpful answers.


Maybe you want to do something with functional dependencies, such that 
'String' result type is automatically chosen if the argument type is 
'Int'. But before suggesting this type extension, I'd like to know more 
about your problem.


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] class-instance

2011-01-17 Thread Patrick Browne
-- My intension is that the PERSON class should *specify*
-- that a person has a constant id called p1
-- and that a person has a name that can be found from the id.
class PERSON a b where
  p1 :: a
  name :: a -> b

-- My intension is that the instance should  implement the PERSON class
instance PERSON  Int String where
  p1 = 1
  name p1 = "john"

-- 
-- Why does the evaluations of p1 or name p1 produce errors?
-- how do I fix them and still keep the basic instance-implements-class
relation?
-- Thanks,
-- Pat

This message has been scanned for content and viruses by the DIT Information 
Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Haskell, C and Matrix Multiplication

2011-01-17 Thread John Lato
Rather than commenting on the code provided, here are the two biggest wins
I've had recently with Haskell.  Both of these are part of a physical
modeling project.

1) uu-parsinglib - simple to use and automatic error correction.  I really,
really enjoy working with this library, and how often can you say that?

2) STM - the original design reads a system and all inputs from a config
file, then runs the system and produces output.  I wanted to build the
system from a file but collect inputs and produce output in real-time.
 After a few small adjustments, a few TVar's and a TMVar, and three
forkIO's, it was working.

One concern is that the engine (which is computation-intensive) shouldn't
block waiting for input, it should just continue on and catch up later.
 With STM, this can be expressed as:

atomically $ getInputs inputTVar `orElse` return ()

and there's the desired behavior.  Couldn't be easier.

John


From: Blake Rain 
>
> Dear Haskellers,
>
> I thought I'd take some time to share something of my weekend with
> you  all.   Not because  of  anything new,  but  because  it is  a
> feel-good example  of Haskell  being win. A  good read  for Monday
> morning, perhaps.
>




> Now over to you guys! What experiences have you had which make you
> glad you use Haskell? Anything make you smile recently?
>
> - Blake
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] H98, OOHaskell - getting started with objects in Haskell

2011-01-17 Thread Luke Palmer
On Mon, Jan 17, 2011 at 1:44 AM, Wolfgang Jeltsch
 wrote:
> Am Sonntag, den 16.01.2011, 14:48 -0800 schrieb gutti:
>> Looking at life u probably could save time, if u only would evaluate
>> code on cells, where the neighbors have changed status. So rather than
>> triggering them all centrally and each checks its neighbours, we could
>> use the concept:
>>
>> - let the active ones trigger the neighbours & so pass on activity
>
> But then you would have to take care to avoid cycles. You could also use
> on-demand updates with a centralized approach, and a centralized
> approach would probably make cycle detection easier.

Don't forget about the double buffering you will need to do if you
have a simultaneous rule.  It doesn't take long before the dragons in
imperative programming start messing with your "easy" specification.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Happstack events and web page refreshing

2011-01-17 Thread Corentin Dupont
Hello again,
I have another question for happstack users/experts:

I have a program with happstack-state and a web server with
happstack-server.
What I'd like is, whenever the MACID state is changed, that the web page is
refreshed for every clients
connected on the web server.

So I think the question is 2 fold:
- How to add an event handler on happstack-state for that?
- How to ask to the web server to refresh every clients?

I did not found infos about that in the API.

Thanks a lot,
Corentin




On Thu, Jan 13, 2011 at 9:40 PM, Corentin Dupont
wrote:

> Hello,
>
> I'm using the combination happstack + digestive-functors + web-routes +
> blazeHTML.
> I'm not finding any examples on the net...
>
> I've tried to adapt your example (thanks):
>
> type NomicForm a = HappstackForm IO String BlazeFormHtml a
>
> demoForm :: NomicForm (Text, Text)
> demoForm =
> (,) <$> ((TDB.label "greeting: " ++> inputNonEmpty Nothing) <* br)
> <*> ((TDB.label "noun: " ++> inputNonEmpty Nothing) <* br)
> <*  (submit "submit")
> where
>   br :: NomicForm ()
>   br = view H.br
>   -- make sure the fields are not blank, show errors in line if they
> are
>   inputNonEmpty :: Maybe Text -> NomicForm Text
>   inputNonEmpty v =
>   (inputText v `validate` (TD.check "You can not leave this field
> blank." (not . T.null)) <++ errors)
>
>
> But I've got a problem on submit and inputText. I don't see how they are
> compatible with HappstackForm.
> NomicForm a reduces to:
> Form (ServerPartT IO) Input String BlazeFormHtml a
>
> whereas the type of submit is:
>
> submit :: Monad m
>
>=> String-- ^ Text on the submit button
>
>-> Form m String e BlazeFormHtml ()  -- ^ Submit button
>
>
> Maybe I miss some instance?
>
> BTW, I also tried to execute your exemple, but I can't install some packages.
>
> > cabal install digestive-functors-hsp
>
> cabal: Unknown build tool trhsx
>
> Whereas trhsx is in my PATH (under linux).
>
> You said I need the latest happstack from darcs, why?
>
> Cheers,
> Corentin
>
>
> On Sun, Jan 9, 2011 at 8:36 PM, Jeremy Shaw  wrote:
>
>> Hello,
>>
>> newRule also needs to have the type, RoutedNomicServer. The
>> transformation of RoutedNomicServer into NomicServer is done in the
>> handleSite function. Something like this:
>>
>>
>> nomicSpec :: ServerHandle -> Site Route (ServerPartT IO Response)
>> nomicSpec sh =
>>  Site { handleSite  = \f url -> unRouteT (nomicSite sh url) f
>> ...
>>
>> main =
>>do ...
>>  simpleHTTP nullConf $ siteImpl (nomicSpec sh)
>>
>> Or something like that -- it's hard to tell exactly what is going on
>> in your app based on the snippets you provided.
>>
>> Also, I highly recommend using digestive functors instead of formlets.
>> It is the successor to formlets. Same core idea, better implementation
>> and actively maintained.
>>
>> I have attached a quick demo of using:
>>
>> happstack+digestive-functors+web-routes+HSP
>>
>> To use it you will need the latest happstack from darcs plus:
>>
>>  hsp
>>  web-routes
>>  web-routes-hsp
>>  web-routes-happstack
>>  web-routes-mtl
>>  digestive-functors
>>  digestive-functors-hsp
>>
>> I plan to clean up this example and document it better in the crash
>> course for the upcoming release. Clearly things like the FormInput
>> instance and the formPart function belong a library.
>>
>> let me know if you have more questions.
>> - jeremy
>>
>> On Sat, Jan 8, 2011 at 6:44 PM, Corentin Dupont
>>  wrote:
>> > Hello,
>> >
>> > I have difficulties mixing web-routes and forms:
>> > I have put routes in all my site, except for forms which remains with
>> the
>> > type ServerPartT IO Response.
>> > How to make them work together?
>> >
>> > I have:
>> > type NomicServer = ServerPartT IO
>> > type RoutedNomicServer = RouteT PlayerCommand NomicServer
>> >
>> > newRule :: ServerHandle -> NomicServer Response
>> > newRule sh = do
>> >methodM POST -- only accept a post method
>> >mbEntry <- getData -- get the data
>> >case mbEntry of
>> >   Nothing -> error $ "error: newRule"
>> >   Just (NewRule name text code pn) -> do
>> >  html <- nomicPageComm pn sh (submitRule name text code pn))
>> >  ok $ toResponse html
>> >
>> >
>> > nomicPageComm :: PlayerNumber -> ServerHandle -> Comm () ->
>> > RoutedNomicServer Html
>> > nomicPageComm pn sh comm =
>> > (..)
>> >
>> >
>> > launchWebServer :: ServerHandle -> IO ()
>> > launchWebServer sh = do
>> >putStrLn "Starting web server...\nTo connect, drive your browser to
>> > \"http://localhost:8000/Login\ ""
>> >d <- liftIO getDataDir
>> >simpleHTTP nullConf $ mconcat [dir "postLogin" $ postLogin,
>> >   fileServe [] d,
>> >   dir "Login" $ ok $ toResponse $
>> loginPage,
>> >   dir "NewRule" $ newRule sh,
>> >

[Haskell-cafe] Announcement: SiteBridge version 1.0

2011-01-17 Thread Sukit Tretriluxana
Dear all,

I am proudly announcing SiteBridge version 1.0.

Briefly about it, SiteBridge is a site bridging system that allows
developers to connect his/her locally developed app to an externally
accessible site. It's aimed to get rid of the pain of the fact that you are
still internally developing an app but want to test how it looks and feels
when externally accessible. An obvious example of this is a web app for
mobile devices. Usually these devices are on a different network than that
used in development. So you won't be able to test it with real devices
unless you have a test site, or you have control over everything including
providing WIFI network that you mobile devices can get on. However, so many
people do not have this luxury especially those working in enterprise
environment. This is a pain and even you have a test site it's often still
very awkward to incorporate it into the rapid development cycle. Sometimes
you just want a quick test to see how things go such as troubleshooting.

See this short introduction video.

http://www.youtube.com/watch?v=UxhLthKSr_E

For more information, check out

http://code.google.com/p/sitebridgeserver/

Enjoy!
Ed
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Haskell, C and Matrix Multiplication

2011-01-17 Thread Blake Rain
On Mon, 2011-01-17 at 10:13 +, Pedro Vasconcelos wrote: 
> On Mon, 17 Jan 2011 07:45:04 +
> Blake Rain  wrote:
> 
> > So, after drinking  some coffee and having a  smoke, I started the
> > Haskell version:
> > 
> > [foldl (+) 0 $ zipWith (*) x y | x <- m1, y <- transpose m2]
> > 
> 
> I don't think this is correct; it's type is 
> 
>   (Num a) => [[a]] -> [[a]] -> [a]
> 
> rather than the expected
> 
>   (Num a) => [[a]] -> [[a]] -> [[a]]
> 
> How about:
> 
> mult m1 m2 = [[foldl (+) 0 $ zipWith (*) x y |  y<-transpose m2] | x<-m1]

So sorry, I meant:

mult :: (Num a) => [[a]] -> [[a]] -> [a]
mult m1 m2 = [foldl (+) 0 $ zipWith (*) x y | x <- m1, y <- transpose
m2]

Lack of sleep.

> 
> Regarding performance: did you make sure you're forcing the
> evaluation of the result matrix? 

I thought I was. It's printing the results as expected. Could be I'm
just imagining I am and suffering a delusion or misunderstanding.

> 
> Regards,
> 
> Pedro
> 
> ___
> 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] Haskell, C and Matrix Multiplication

2011-01-17 Thread Pedro Vasconcelos
On Mon, 17 Jan 2011 07:45:04 +
Blake Rain  wrote:

> So, after drinking  some coffee and having a  smoke, I started the
> Haskell version:
> 
> [foldl (+) 0 $ zipWith (*) x y | x <- m1, y <- transpose m2]
> 

I don't think this is correct; it's type is 

(Num a) => [[a]] -> [[a]] -> [a]

rather than the expected

(Num a) => [[a]] -> [[a]] -> [[a]]

How about:

mult m1 m2 = [[foldl (+) 0 $ zipWith (*) x y |  y<-transpose m2] | x<-m1]

Regarding performance: did you make sure you're forcing the
evaluation of the result matrix? 

Regards,

Pedro

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Haskell, C and Matrix Multiplication

2011-01-17 Thread Blake Rain
Oh yeah, it does. You're right. I haven't used it in so long that I
forgot about them! No need to be sorry.

Well, +2 for C, eh? :)

And I'd just make a mess if I used the preprocessor.



On Mon, 2011-01-17 at 11:23 +0300, Miguel Mitrofanov wrote: 
> Sorry, but last time I've checked, C did have loops, is that correct? And 
> even if you don't want loops, there is a preprocessor.
> 
> 17.01.2011 10:45, Blake Rain пишет:
> > Dear Haskellers,
> >
> >
> >
> > ___
> > 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] H98, OOHaskell - getting started with objects in Haskell

2011-01-17 Thread Wolfgang Jeltsch
Am Sonntag, den 16.01.2011, 14:48 -0800 schrieb gutti:
> Looking at life u probably could save time, if u only would evaluate 
> code on cells, where the neighbors have changed status. So rather than
> triggering them all centrally and each checks its neighbours, we could
> use the concept: 
> 
> - let the active ones trigger the neighbours & so pass on activity 

But then you would have to take care to avoid cycles. You could also use
on-demand updates with a centralized approach, and a centralized
approach would probably make cycle detection easier.

Best wishes,
Wolfgang


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Haskell, C and Matrix Multiplication

2011-01-17 Thread Miguel Mitrofanov

 Sorry, but last time I've checked, C did have loops, is that correct? And even 
if you don't want loops, there is a preprocessor.

17.01.2011 10:45, Blake Rain пишет:

Dear Haskellers,

I thought I'd take some time to share something of my weekend with
you  all.   Not because  of  anything new,  but  because  it is  a
feel-good example  of Haskell  being win. A  good read  for Monday
morning, perhaps.

What  feels  like  decades  ago,   I  used  to  work  in  computer
graphics.   Mostlymy   work   was   onsoftware   for   the
amateur/semi-professional  film   industry  -  developing  various
filters and effects for the likes of Adobe AfterEffects and so on.

I  have   not  touched   computer  graphics  from   a  programming
perspective for quite a long time  now, and I thought it was about
time that  I flexed those  old muscles. So,  I have taken  it upon
myself to write an SVG renderer for Blender.

To cut what is going to be  a very long and humdrum story short, I
needed to write some matrix arithmetic code.

Having not done  this in a long  time, I thought I'd see  if I can
still actually remember what matrix multiplication is by writing a
function to multiply two 4x4 matrices.  I thought I'd write one in
C and then one in Haskell, and see how long they took and how easy
they were to write.

Here is the one in C:

void multiplyM4 (float m1[4][4], float m2[4][4], float m3[4][4]) {
m1[0][0] = m2[0][0] * m3[0][0] + m2[0][1] * m3[1][0] + m2[0][2] *
m3[2][0] + m2[0][3] * m3[3][0];
m1[0][1] = m2[0][0] * m3[0][1] + m2[0][1] * m3[1][1] + m2[0][2] *
m3[2][1] + m2[0][3] * m3[3][1];
m1[0][2] = m2[0][0] * m3[0][2] + m2[0][1] * m3[1][2] + m2[0][2] *
m3[2][2] + m2[0][3] * m3[3][2];
m1[0][3] = m2[0][0] * m3[0][3] + m2[0][1] * m3[1][3] + m2[0][2] *
m3[2][3] + m2[0][3] * m3[3][3];

m1[1][0] = m2[1][0] * m3[0][0] + m2[1][1] * m3[1][0] + m2[1][2] *
m3[2][0] + m2[1][3] * m3[3][0];
m1[1][1] = m2[1][0] * m3[0][1] + m2[1][1] * m3[1][1] + m2[1][2] *
m3[2][1] + m2[1][3] * m3[3][1];
m1[1][2] = m2[1][0] * m3[0][2] + m2[1][1] * m3[1][2] + m2[1][2] *
m3[2][2] + m2[1][3] * m3[3][2];
m1[1][3] = m2[1][0] * m3[0][3] + m2[1][1] * m3[1][3] + m2[1][2] *
m3[2][3] + m2[1][3] * m3[3][3];

m1[2][0] = m2[2][0] * m3[0][0] + m2[2][1] * m3[1][0] + m2[2][2] *
m3[2][0] + m2[2][3] * m3[3][0];
m1[2][1] = m2[2][0] * m3[0][1] + m2[2][1] * m3[1][1] + m2[2][2] *
m3[2][1] + m2[2][3] * m3[3][1];
m1[2][2] = m2[2][0] * m3[0][2] + m2[2][1] * m3[1][2] + m2[2][2] *
m3[2][2] + m2[2][3] * m3[3][2];
m1[2][3] = m2[2][0] * m3[0][3] + m2[2][1] * m3[1][3] + m2[2][2] *
m3[2][3] + m2[2][3] * m3[3][3];

m1[3][0] = m2[3][0] * m3[0][0] + m2[3][1] * m3[1][0] + m2[3][2] *
m3[2][0] + m2[3][3] * m3[3][0];
m1[3][1] = m2[3][0] * m3[0][1] + m2[3][1] * m3[1][1] + m2[3][2] *
m3[2][1] + m2[3][3] * m3[3][1];
m1[3][2] = m2[3][0] * m3[0][2] + m2[3][1] * m3[1][2] + m2[3][2] *
m3[2][2] + m2[3][3] * m3[3][2];
m1[3][3] = m2[3][0] * m3[0][3] + m2[3][1] * m3[1][3] + m2[3][2] *
m3[2][3] + m2[3][3] * m3[3][3];
}

Urgh! This took me over 20  minutes to write out, and a further 34
minutes  of  debugging.   That's  54  minutes to  write  a  simple
function!   This included 23  invocations of  the compiler  and 18
invocations of  the test program.  I was disgusted with  myself! I
should have automated the writing of it with Haskell!

The  reason for this  was mostly  that I  kept pressing  the wrong
buttons  when writing those  many, many  array indices.   I became
blind  to the  numbers in  the  end, and  it became  one of  those
nightmare  debugging  sessions where  you  check your  printMatrix
function and  other stupid things.  I'd  like to say that  I was a
bit tired, but that's just an excuse.

Some points about the C version:

   1. It's only 21 lines of code.

   2. It  took me 54 minutes  to write and debug.   Therefore I can
  write eight of these in a standard working day. Hmm.

   3. It's written in C,  so everybody and their dog understands it
  completely. Honest.

   4. It's written  in C, so it can just be  pasted into nearly any
  other   language   (because   they're  all   just   augmented
  C). Therefore no one needs to understand it.

   5. It does not consume much space (in terms of memory).

   6. It's pretty fast (being written in C):

  Performing  100,000,000 matrix  multiples  takes on  average
  6.117s (with gcc -O2).

   7. Passing a NULL pointer anywhere will cause it to explode.

   8. If the pointers are not to arrays of 16 floats, it will trash
  memory  (being  as  it  destructively  updates  one  of  it's
  arguments).

   9. It will only operate on 4x4 float matrices.

10. How am I storing matrices again...? Column major?


So, after drinking  some coffee and having a  smoke, I started the
Haskell version:

[foldl (+) 0 $ zipWith (*) x y | x<- m1, y<- transpose m2]

Ah.

So... anyway, some points about the Haskell version:

   1  It's only  one line  of code  - that's  20 less  than  the C
  version.

   2.  It took 

Re: [Haskell-cafe] Haskell, C and Matrix Multiplication

2011-01-17 Thread Ketil Malde
Blake Rain  writes:

> Here is the one in C:
>
> void multiplyM4 (float m1[4][4], float m2[4][4], float m3[4][4]) {
> m1[0][0] = m2[0][0] * m3[0][0] + m2[0][1] * m3[1][0] + m2[0][2] * m3[2][0] + 
> m2[0][3] * m3[3][0];
> m1[0][1] = m2[0][0] * m3[0][1] + m2[0][1] * m3[1][1] + m2[0][2] * m3[2][1] + 
> m2[0][3] * m3[3][1];
(etc)
:

> Some points about the C version:
:
>   6. It's pretty fast (being written in C):
>
>  Performing  100,000,000 matrix  multiples  takes on  average
>  6.117s (with gcc -O2).
:

> Haskell version:
>
> [foldl (+) 0 $ zipWith (*) x y | x <- m1, y <- transpose m2]
:
>   5. It's very fast (being written in Haskell):
>
>  Performing  100,000,000  matrix  multiples takes  on  average
>  1.435s  (with ghc  -O2).

This is surprising: a C program doing an explicit list of arithmetic
operation is beaten by a factor of 4 or so by a polymorphic, lazy,
list-consuming Haskell program?  Are you sure? What am I missing? 

But, regardless of performance, I think this is a nice exposition of
some of Haskell's strengths.

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe