Re: [Haskell-cafe] Dynamically altering sort order

2009-05-03 Thread Denis Bueno
On Fri, Apr 24, 2009 at 19:49, Edward Kmett  wrote:
> On Fri, Apr 24, 2009 at 5:11 PM, Denis Bueno  wrote:
>> Is there an Ord instance that can be dynamically changed in this way?
>>
>> My first idea is something like this:
>>
>>    data CompareRecord = CR{ rCompare :: Record -> Record -> Ordering,
>> unCR :: Record }
>>    instance Ord CompareRecord where
>>        compare (CR cmp x) (CR _ y) = cmp x y
>>
>> where the rCompare field would be a function that is based on the
>> flags passed to the command-line problem.  But this has an ugly
>> asymmetry.  Does anyone have any other ideas?
>
> You can make a safer 'CompareRecord' using 'reflection' from hackage:

This is what I ended up doing and it worked out great.  Thanks for the
suggestion.

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


[Haskell-cafe] Combining computations

2009-05-03 Thread michael rice
If you look at this stuff long enough it almost begins to make sense. Maybe. ;-)

I've been messing around with MonadPlus and I understand its usage with the 
Maybe and List monads. Since one use of Monads is combining computations, how 
can I combine a Maybe with a List?

let m1 = Nothing
let m2 = [1]
let m3 = m1 `mplus` m2  ==> [1]    --if the Maybe is Nothing, do nothing  

let m1 = Just 1 
let m2 = []
let m3 = m1 `mplus` m2  ==> [1]  --if the Maybe is not Nothing, add it to the 
list

Or am I misunderstanding combining computations?

Michael




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


Re: [Haskell-cafe] Re: gcd

2009-05-03 Thread Daniel Fischer
Am Sonntag 03 Mai 2009 00:17:22 schrieb Achim Schneider:
> Steve  wrote:
> > "It is useful to define gcd(0, 0) = 0 and lcm(0, 0) = 0 because then
> > the natural numbers become a complete distributive lattice with gcd
> > as meet and lcm as join operation. This extension of the definition
> > is also compatible with the generalization for commutative rings
> > given below."
>
> Ouch. Speak of mathematicians annoying programmers by claiming that 0
> isn't divisible by any of [1..],

Beg pardon? 0 is divisible by all of them. And while we're talking about rings, 
0 is also 
divisible by 0.


> and further implying that 0 is bigger
> than all of those,

'Tis, in the divisibility preorder :)

> not to mention justifying all that with long words.

Sorry for the long words, but having gcd 0 0 == lcm 0 0 == 0 is the sensible 
thing and 
having it differently in Haskell is a bad mistake (IMO).

>
> Damn them buggers.

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


Re: [Haskell-cafe] Re: gcd

2009-05-03 Thread Luke Palmer
On Sat, May 2, 2009 at 4:17 PM, Achim Schneider  wrote:

> Steve  wrote:
>
> > "It is useful to define gcd(0, 0) = 0 and lcm(0, 0) = 0 because then
> > the natural numbers become a complete distributive lattice with gcd
> > as meet and lcm as join operation. This extension of the definition
> > is also compatible with the generalization for commutative rings
> > given below."
> >
> Ouch. Speak of mathematicians annoying programmers by claiming that 0
> isn't divisible by any of [1..], and further implying that 0 is bigger
> than all of those, not to mention justifying all that with long words.
>
> Damn them buggers.


0 is divisible by everything.  It's "bigger" than all of them with respect
to divisibility, not size.

Which you may have known.  Your irony was too complex for me :-p

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


[Haskell-cafe] Combining computations

2009-05-03 Thread michael rice
I posted something similar about an hour ago but it seems to have gotten lost. 
Very strange.

I've read that Monads can combine computations. Can a Maybe monad be combined 
with a List monad such that

Nothing `mplus` [] ==> [] 
Just 1 `mplus` [] ==> [1]

If not, can someone supply a simple example of combining computations?

Michael




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


Re: [Haskell-cafe] using haskell for a project

2009-05-03 Thread Belka

Welcome to Haskell! =)

> But now I don't know how to dynamically add new spells (new spells can be
> created in my gameplay). Since I can't assign a new value to the `spells'
> variable (Data.Map.insert returns a new map), I just don't know where to
> go.

Do distinguish *pure function* concept and *action with side effects*
concept. 
The default in FP (Funct. Progr.) paradigm usualy is *pure function*, if
*action with side effects* is avoidable. Pure functions deal with immutable
memory "objects". So in your case, whenever you add a new spell, you get a
new character (since your chacter's "essence" is a spell list). That way
after calculating *addNewSpell* you will have 2 versions of character: a new
one (with spells list bigger), an the original one. Since old one usualy
drop out of the scope (while program evaluation progresses) it gets *garbage
collected* (note: garbage collection showed up in FP already in 1960s).

addNewSpell :: Character -> Spell -> Character
addNewSpell char_old_version spell_to_add = ... 

main = let char_new_version = addNewSpell char_old_version new_spell in
putStrLn $ show char_new_version
  where char_old_version = Charcter {...}
   new_spell = Spell {  }

Have luck, with the brain rewriting! =) 
Belka
-- 
View this message in context: 
http://www.nabble.com/using-haskell-for-a-project-tp23348425p23352598.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

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


Re: [Haskell-cafe] Re: gcd

2009-05-03 Thread Nathan Bloomfield
Having gcd(0,0) = 0 doesn't mean that 0 is not divisible by any other
natural number. On the contrary, any natural trivially divides 0 since n*0 =
0. Perhaps the disagreement is over what is meant by "greatest". The
"greatest" in gcd is not w.r.t. the canonical ordering on the naturals;
rather w.r.t. the partial order given by the divides relation. Similarly for
the "least" in lcm.

Suppose gcd(0,0) = a. Then a|0, and if b|0 then b|a. (That's what it means
to be the gcd.) So what is a? Since every natural number divides zero, a
must be divisible by every natural number. The only natural number with this
property is 0, which can be proved using the essential uniqueness of prime
factorizations and infinitude of primes.

So having gcd(0,0) = 0 isn't just useful, it's the correct thing to do.

I hope that didn't use too many long words. :)

-Nathan Bloomfield
Grad Assistant, University of Arkansas, Fayetteville

On Sat, May 2, 2009 at 5:17 PM, Achim Schneider  wrote:

> Steve  wrote:
>
> > "It is useful to define gcd(0, 0) = 0 and lcm(0, 0) = 0 because then
> > the natural numbers become a complete distributive lattice with gcd
> > as meet and lcm as join operation. This extension of the definition
> > is also compatible with the generalization for commutative rings
> > given below."
> >
> Ouch. Speak of mathematicians annoying programmers by claiming that 0
> isn't divisible by any of [1..], and further implying that 0 is bigger
> than all of those, not to mention justifying all that with long words.
>
> Damn them buggers.
>
> --
> (c) this sig last receiving data processing entity. Inspect headers
> for copyright history. All rights reserved. Copying, hiring, renting,
> performance and/or quoting of this signature prohibited.
>
>
> ___
> 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] Combining computations

2009-05-03 Thread Tony Morris
michael rice wrote:
> If you look at this stuff long enough it almost begins to make sense.
> Maybe. ;-)
>
> I've been messing around with MonadPlus and I understand its usage
> with the Maybe and List monads. Since one use of Monads is combining
> computations, how can I combine a Maybe with a List?
>
> let m1 = Nothing
> let m2 = [1]
> let m3 = m1 `mplus` m2  ==> [1]--if the Maybe is Nothing, do nothing 
>
> let m1 = Just 1
> let m2 = []
> let m3 = m1 `mplus` m2  ==> [1]  --if the Maybe is not Nothing, add it
> to the list
>
> Or am I misunderstanding combining computations?
>
> Michael
>
>
> 
>
> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>   

Hi Michael,
You'll want the Data.Maybe.listToMaybe and Data.Maybe.maybeToList functions.

-- 
Tony Morris
http://tmorris.net/


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


Re: [Haskell-cafe] using haskell for a project

2009-05-03 Thread j.waldmann


don't think in terms of modifying state,
think of it as computing new values from old values. I find that the payoff
of learning to think ,like this is massive, as it's usually much easier to
reason about.


Indeed. 

[warning: what follows is a rant with a purpose:]

The imperative/OO guys have learnt this as well, and the result comes under
different names, e.g., "a service component should be stateless", "stateless
session bean",
"refactoring: introduce state object". 

It's kind of funny how they re-invent functional programming 
(in several places, e.g. "composite pattern" = algebraic data type,
"visitor pattern" = fold, "iterator pattern" = lazy list). 

... after trying to avoid the truth for some 20 years or so. You're exactly
right,
the functional/declarative way is "much easier to reason about",
and this matches the observation that there was not much reasoning going on
during these decades

instead, just hacking, and then some testing. Well, they even finally
admitted one should
write the tests before coding. Yes, that's called specification before
implementation,
and that's what Dijkstra had been teaching for ages, e.g.,
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD13xx/EWD1305.html

Still, given their strange approach to programming, the imperative/OO guys
have rather brilliant tools
for developers. No surprise, without them they'd be lost immediately.
But we have http://leksah.org/ , and there's a GSoC project for eclipsefp .

[now, the punchline:]

(for extended rants on the subject, register for
http://www.iba-cg.de/hal4.html )


-- 
View this message in context: 
http://www.nabble.com/using-haskell-for-a-project-tp23348425p23353526.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

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


Re: [Haskell-cafe] Combining computations

2009-05-03 Thread Kalman Noel
michael rice schrieb:
> let m1 = Just 1 
> let m2 = []
> let m3 = m1 `mplus` m2  ==> [1]  --if the Maybe is not Nothing, add it to the 
> list
> 
> Or am I misunderstanding combining computations?

You just got the type of mplus wrong:

mplus :: (MonadPlus m) => m a -> m a -> m a

Note that it takes two values of the same type (m a), but you're giving
it values of different types.  That is, combining computations of
different types is not within the scope of MonadPlus.  In this case, it
makes sense to convert (Just 1) to [1] via Data.Maybe.maybeToList, thus:

m1 = Just 1
m2 = [2,3]
m3 = maybeToList m1 `mplus` m2   -- [1,2,3]

Note also that, in this example, Monoid (mappend) instead of MonadPlus
(mplus) would be sufficient.  Actually MonadPlus becomes useful only
when you are concerned about the extra properties that its instances are
expected to satisfy.

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


Re: [Haskell-cafe] Combining computations

2009-05-03 Thread Luke Palmer
mplus requires both arguments to be in the same monad (the same type,
even).   Fortunately, the empty list behaves like Nothing, and a singleton
list behaves like Just.  So convert the Maybe before composing, using:

maybeToList Nothing = []
maybeToList (Just x) = [x]

(The maybeToList function can be found in Data.Maybe)

Keep in mind that this will give you:

Just 1 `mplus` [2,3,4]  ==>  [1,2,3,4]

Which may not be what you want...

Luke

On Sat, May 2, 2009 at 9:26 PM, michael rice  wrote:

> I posted something similar about an hour ago but it seems to have gotten
> lost. Very strange.
>
> I've read that Monads can combine computations. Can a Maybe monad be
> combined with a List monad such that
>
> Nothing `mplus` [] ==> []
> Just 1 `mplus` [] ==> [1]
>
> If not, can someone supply a simple example of combining computations?
>
> 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] Combining computations

2009-05-03 Thread Luke Palmer
On Sun, May 3, 2009 at 4:41 AM, Luke Palmer  wrote:

> mplus requires both arguments to be in the same monad (the same type,
> even).   Fortunately, the empty list behaves like Nothing, and a singleton
> list behaves like Just.  So convert the Maybe before composing, using:
>
> maybeToList Nothing = []
> maybeToList (Just x) = [x]
>
> (The maybeToList function can be found in Data.Maybe)
>
> Keep in mind that this will give you:
>
> Just 1 `mplus` [2,3,4]  ==>  [1,2,3,4]


Silly me:

maybeToList (Just 1) `mplus` [2,3,4] ==> [1,2,3,4]



>
>
> Which may not be what you want...
>
> Luke
>
> On Sat, May 2, 2009 at 9:26 PM, michael rice  wrote:
>
>>  I posted something similar about an hour ago but it seems to have gotten
>> lost. Very strange.
>>
>> I've read that Monads can combine computations. Can a Maybe monad be
>> combined with a List monad such that
>>
>> Nothing `mplus` [] ==> []
>> Just 1 `mplus` [] ==> [1]
>>
>> If not, can someone supply a simple example of combining computations?
>>
>> 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] Decoupling OpenAL/ALUT packages from OpenGL

2009-05-03 Thread Sven Panne
Am Sonntag, 3. Mai 2009 00:56:00 schrieb Tillmann Vogt:
> Sven Panne schrieb:
> >* a tiny "ObjectName" package, consisting only of OpenGL's ObjectName
> > class (In "Data.ObjectName"? I'm not very sure about a good place in the
> > hierarchy here.)
>
> How about Data.GraphicsObjects ? [...]

Thanks for all the suggestions so far, a few remarks from my side (I just 
picked the last mail for the reply, no strong reason for this...):

"Data.GraphicsObjects" is a bit misleading, because OpenAL's Buffers and 
Sources are instances, too, and they have nothing to do with graphics. 
Instances of ObjectName are just opaque resources from an external API, which 
you have to allocate and deallocate explicitly.

> I think it would be nice to have data types and functions for dot
> produkt, scalar product, norms, ...
> together with HOpenGL types.

I fear that this might open a can of worms and could lead to even longer 
discussions than the ones about a collection framework. The design space for a 
vector math package is quite large, and I fear that e.g. a mathematician 
trying to implement some linear algebra package has vastly different 
requirements than somebody trying to implement the n-th incarnation of the 
Quake engine. Some points to consider:

   * Should the components of vectors etc. be strict? In OpenGL they are, and 
Data.Complex is similar in this respect. In my experience non-strict 
components lead to space leaks too easily, and I guess this is the rationale 
behind Complex, too. But non-strict components have some benefits, too, of 
course, especially if you do symbolic computation.

   * Should we abstract over the number of dimension of vectors, etc.? If yes, 
how strong can our compile-time type checking be?

   * If we really make this a more general vector math package, things like 
colors etc. should probably stay in the OpenGL package. But there are a few 
other packages needing color data types, too...

   * If the package is not general enough, it might be a bad idea to steal 
names/hierarchy locations which *are* general.

Nevertheless, I'd be happy to see some proposals for a sensible, compact 
vector math package. Probably we can fulfill most of the common use cases with 
something simple.

And one word about lumping the 3 packages together: Any function, module, and 
package should have *one* clearly defined task, this is crucial for every SW 
design. I would have a hard time explaining what this "super package" is all 
about, even if we throw only 2 of the 3 packages together. Personally I feel 
that this is a strong argument for 3 separate packages.

> (I know that glu has tesselation). [...]

But GLU is basically dead with OpenGL 3.x. :-)

Cheers,
   S.

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


Re: [Haskell-cafe] Combining computations

2009-05-03 Thread Felipe Lessa
I don't know if I understood your intentions, but let's go.  The
problem is that you're trying to combine different monads.  We
have

  mplus :: MonadPlus m => m a -> m a -> m a,

so you never leave 'm', but you want

  mplus' :: ??? => n a -> m a -> m a

where 'n' could be a different monad.  In some specific cases
where you know the internal structure of the monad, you can write
'mplus'', for example:

  mplus' :: MonadPlus m => Maybe a -> m a -> m a
  mplus' m l = maybeToMonad m `mplus` l

  maybeToMonad :: Monad m => Maybe a -> m a
  maybeToMonad = maybe (fail "Nothing") return

In general, however, this operation can't be done.  For example,
how would you write:

  mplus' :: IO a -> [a] -> [a]

?


HTH,

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


Re: [Haskell-cafe] Combining computations

2009-05-03 Thread Daniel Fischer
Am Sonntag 03 Mai 2009 05:26:22 schrieb michael rice:
> I posted something similar about an hour ago but it seems to have gotten
> lost. Very strange.
>
> I've read that Monads can combine computations. Can a Maybe monad be
> combined with a List monad such that
>
> Nothing `mplus` [] ==> []
> Just 1 `mplus` [] ==> [1]

Not directly, the type of mplus is

mplus :: MonadPlus m => m a -> m a -> m a

, so the monad has to be the same for both arguments. For [] and Maybe, you can 
use 
maybeToList and listToMaybe to convert one into the other:

Prelude Data.Maybe Control.Monad> maybeToList Nothing
[]
Prelude Data.Maybe Control.Monad> maybeToList (Just 1)
[1]
Prelude Data.Maybe Control.Monad> maybeToList Nothing `mplus` [1]
[1]
Prelude Data.Maybe Control.Monad> maybeToList (Just 1) `mplus` []
[1]
Prelude Data.Maybe Control.Monad> Nothing `mplus` listToMaybe [1]
Just 1
Prelude Data.Maybe Control.Monad> Nothing `mplus` listToMaybe [1,2,3]
Just 1

, for certain other combinations, you can also have a meaningful conversion 
from one monad 
to another (e.g. 
stateToStateT :: Monad m => State s a -> StateT s m a
stateToStateT comp = StateT (return . runState comp)
) and employ the technique to combine them, but it's of limited use.

A monad allows you to combine computations of 'similar' type (for some fuzzy 
meaning of 
similar), using (>>=), (>>) to combine them sequentially and perhaps mplus to 
combine them 
'in parallel'.

>
> If not, can someone supply a simple example of combining computations?
>
> Michael

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


Re: [Haskell-cafe] Combining computations

2009-05-03 Thread Tillmann Rendel

Hi,

normally, one uses monads to express and combine computations in the 
same monad. However, you can convert between some monads, e.g. from 
Maybe to List:


  import Data.Maybe (maybeToList)

  > let m1 = Nothing
  > let m2 = [1]
  > let m3 = maybeToList m1 `mplus` m2

  > let m1 = Just 1
  > let m2 = []
  > let m3 = maybeToList m1 `mplus` m2


In fact, you can convert from Maybe to any MonadPlus.

  maybeToMonadPlus Nothing = mzero
  maybeToMonadPlus (Just x) = return x

And you can convert from List to any MonadPlus:

  listToMonadPlus Nothing  = []
  listToMonadPlus (x : xs) = return x `mplus` listToMonadPlus xs

Now you should be able to do:

  m1 = maybeToMonadPlus (Just 1)
  m2 = listtoMonadPlus [2, 3]
  m3 = m1 `mplus` m2 :: Just Int -- Just 1
  m4 = m1 `mplus` m2 :: [Int]-- [1, 2, 3]

The reason this is possible is that Maybe and List do not support 
additional effects beyond what is common to all MonadPlus instances.



Another option is to never specify which monad your computations are in 
in the first place. Instead, only specify which computational effects 
the monad should support.


  m1 = mzero:: MonadPlus m => m a
  m2 = return 1 :: (Monad m, Num a) => m a
  m3 = m1 `mplus` m2 `mplus` Just 2 -- Just 1
  m4 = m1 `mplus` m2 `mplus` [2, 3] -- [1, 2, 3]

In this version, m1 and m2 are polymorphic computations, which can be 
used together with List computations, Maybe computations, or any other 
MonadPlus instances. m1 needs MonadPlus, while m2 is happy with any 
Monad instance. This fact is encoded in their type.


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


Re: [Haskell-cafe] Array Binary IO & molecular simulation

2009-05-03 Thread Grigory Sarnitskiy
To sum up here is the example that can write two arrays in one file and then 
read this two arrays back. To restore written data it just reads the file into 
bytestring, then splits the bytestring into equal parts. The parts are decoded. 
I suppose the method is suitable for decoding files with unboxed arrays of 
equal size.

import Data.Array.Unboxed
import Data.Binary
import qualified Data.ByteString.Lazy as BL
import IO

a = listArray ((1,1),(3,2)) [3,4,5,6,7,8] :: UArray (Int, Int) Float
b = listArray ((1,1),(3,2)) [9,10,11,12,13,14] :: UArray (Int, Int) Float

encodeFile2 f = BL.appendFile f . encode

encoder = do
encodeFile "Results.txt" a
encodeFile2 "Results.txt" b

decoder = do
contents <- BL.readFile "Results.txt"
print $ (show (decode (fst (BL.splitAt 118 contents)) :: UArray (Int, Int) 
Float))
print $ (show (decode (snd (BL.splitAt 118 contents)) :: UArray (Int, Int) 
Float))
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] ANN: Silkworm game

2009-05-03 Thread Sven Panne
Nice work! Two minor suggestions, apart from the the paths issue already 
discussed here:

   * Either include a license file in the source distribution or remove the 
corresponding line in the .cabal file. Cabal won't work if it is specified and 
missing.

   * List all your build dependencies directly, so users can simply do a 
"cabal install", pulling all missing dependencies automatically. No need for 
Makefiles or a long description in the README anymore...

Patch for those items attached.

As a side note, I get a very bad feeling when Hipmunk gets compiled on my 
x86_64 box:

chipmunk/cpCollision.c: In function ‘findVerts’:

chipmunk/cpCollision.c:174:0:
 warning: cast from pointer to integer of different size

chipmunk/cpCollision.c:180:0:
 warning: cast from pointer to integer of different size
chipmunk/cpCollision.c: In function ‘findPointsBehindSeg’:

chipmunk/cpCollision.c:233:0:
 warning: cast from pointer to integer of different size
chipmunk/cpCollision.c: In function ‘seg2poly’:

chipmunk/cpCollision.c:274:0:
 warning: cast from pointer to integer of different size

chipmunk/cpCollision.c:276:0:
 warning: cast from pointer to integer of different size
chipmunk/cpSpace.c: In function ‘queryFunc’:

chipmunk/cpSpace.c:411:0:
 warning: cast from pointer to integer of different size

chipmunk/cpSpace.c:411:0:
 warning: cast from pointer to integer of different size

This can't be correct, but I'll probably have to take a look at that. Or is it 
a know bug that Hipmunk ist not 64bit-clean?

Cheers,
   S.



--- Silkworm.cabal.orig	2009-04-14 00:33:14.0 +0200
+++ Silkworm.cabal	2009-05-03 13:48:05.0 +0200
@@ -2,7 +2,6 @@
 Version: 0.2
 Description: 2D game based on the Hipmunk physics engine bindings.
 License: LGPL
-License-file:LICENSE
 Author:  Duane Johnson
 Maintainer:  duane.john...@gmail.com
 Build-Type:  Simple
@@ -10,5 +9,5 @@
 
 Executable haq
   Main-is:   main.hs
-  Build-Depends: base
+  Build-Depends: base, array, containers, directory, random, pngload, Hipmunk, GLFW, OpenGL
 
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] using haskell for a project

2009-05-03 Thread Martijn van Steenbergen

Hi Nicolas,

Nicolas Martyanoff wrote:

So now I'd want to use it for a small project of mine, a simple
multiplayer roguelike based on telnet. I wrote a minimal server in C, and
it took me a few hours. Now I'm thinking about doing the same in Haskell,
and I'm in trouble.


I don't know if this is of any help at all but I've been working a bit 
on a MUD in Haskell. You can find the source code here:


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

You might be able to get some ideas from the source code. If you have 
any questions, feel free to ask.


There are no spells (yet), but there are players and rooms and you can 
walk around a few rooms.


Martijn.

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


Re: [Haskell-cafe] using haskell for a project

2009-05-03 Thread Eugene Kirpichov
To the original author: I must notice that this is not the most
convincing use of purity. I personally would prefer to have a
character's spell list be mutable because this corresponds better to
the nature of the problem (in the problem domain it is nonsense to
have two versions of a character and hope that one version will drop
out of the scope).
However, Haskell forces you to be pure, and that pays off a lot,
despite the existence of problems that are not modeled intuitively
this way.

In case you absolutely need mutable state, use the ST monad or
IORef's, but **you have to first become skilled with pure
computations, because that's the only way to not make a mess of your
treatment of the impure ones**.

And if anyone tries to seduce you with unsafePerformIO, resist!

2009/5/3 Belka :
>
> Welcome to Haskell! =)
>
>> But now I don't know how to dynamically add new spells (new spells can be
>> created in my gameplay). Since I can't assign a new value to the `spells'
>> variable (Data.Map.insert returns a new map), I just don't know where to
>> go.
>
> Do distinguish *pure function* concept and *action with side effects*
> concept.
> The default in FP (Funct. Progr.) paradigm usualy is *pure function*, if
> *action with side effects* is avoidable. Pure functions deal with immutable
> memory "objects". So in your case, whenever you add a new spell, you get a
> new character (since your chacter's "essence" is a spell list). That way
> after calculating *addNewSpell* you will have 2 versions of character: a new
> one (with spells list bigger), an the original one. Since old one usualy
> drop out of the scope (while program evaluation progresses) it gets *garbage
> collected* (note: garbage collection showed up in FP already in 1960s).
>
> addNewSpell :: Character -> Spell -> Character
> addNewSpell char_old_version spell_to_add = ...
>
> main = let char_new_version = addNewSpell char_old_version new_spell in
> putStrLn $ show char_new_version
>          where char_old_version = Charcter {...}
>                   new_spell = Spell {  }
>
> Have luck, with the brain rewriting! =)
> Belka
> --
> View this message in context: 
> http://www.nabble.com/using-haskell-for-a-project-tp23348425p23352598.html
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
>
> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



-- 
Eugene Kirpichov
Web IR developer, market.yandex.ru
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Combining computations

2009-05-03 Thread michael rice
Thanks for all the help, everyone.

I think this stuff is starting to come together.

Michael

--- On Sun, 5/3/09, Tillmann Rendel  wrote:

From: Tillmann Rendel 
Subject: Re: [Haskell-cafe] Combining computations
To: "michael rice" 
Cc: haskell-cafe@haskell.org
Date: Sunday, May 3, 2009, 7:33 AM

Hi,

normally, one uses monads to express and combine computations in the same 
monad. However, you can convert between some monads, e.g. from Maybe to List:

  import Data.Maybe (maybeToList)

  > let m1 = Nothing
  > let m2 = [1]
  > let m3 = maybeToList m1 `mplus` m2

  > let m1 = Just 1
  > let m2 = []
  > let m3 = maybeToList m1 `mplus` m2


In fact, you can convert from Maybe to any MonadPlus.

  maybeToMonadPlus Nothing = mzero
  maybeToMonadPlus (Just x) = return x

And you can convert from List to any MonadPlus:

  listToMonadPlus Nothing  = []
  listToMonadPlus (x : xs) = return x `mplus` listToMonadPlus xs

Now you should be able to do:

  m1 = maybeToMonadPlus (Just 1)
  m2 = listtoMonadPlus [2, 3]
  m3 = m1 `mplus` m2 :: Just Int -- Just 1
  m4 = m1 `mplus` m2 :: [Int]    -- [1, 2, 3]

The reason this is possible is that Maybe and List do not support additional 
effects beyond what is common to all MonadPlus instances.


Another option is to never specify which monad your computations are in in the 
first place. Instead, only specify which computational effects the monad should 
support.

  m1 = mzero    :: MonadPlus m => m a
  m2 = return 1 :: (Monad m, Num a) => m a
  m3 = m1 `mplus` m2 `mplus` Just 2 -- Just 1
  m4 = m1 `mplus` m2 `mplus` [2, 3] -- [1, 2, 3]

In this version, m1 and m2 are polymorphic computations, which can be used 
together with List computations, Maybe computations, or any other MonadPlus 
instances. m1 needs MonadPlus, while m2 is happy with any Monad instance. This 
fact is encoded in their type.

  Tillmann



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


Re: [Haskell-cafe] Array Binary IO & molecular simulation

2009-05-03 Thread Grigory Sarnitskiy
To sum up here is the example
that can write two arrays in one file and then read this two arrays
back. To restore written data it just reads the file into bytestring,
then splits the bytestring into equal parts. The parts are decoded. I
suppose the method is suitable for decoding files with unboxed arrays
of equal size.

import Data.Array.Unboxed
import Data.Binary
import qualified Data.ByteString.Lazy as BL
import IO

a = listArray ((1,1),(3,2)) [3,4,5,6,7,8] :: UArray (Int, Int) Float
b = listArray ((1,1),(3,2)) [9,10,11,12,13,14] :: UArray (Int, Int) Float

encodeFile2 f = BL.appendFile f . encode

encoder = do
    encodeFile "Results.txt" a
    encodeFile2 "Results.txt" b

decoder = do
    contents <- BL.readFile "Results.txt"
    print $ (show (decode (fst (BL.splitAt 118 contents)) :: UArray (Int, Int) Float))
    print $ (show (decode (snd (BL.splitAt 118 contents)) :: UArray (Int, Int) Float))
P.S. I've already sent this letter to mailist several ours ago, but it wasn't published :-/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] StateT IO Action on `onKeyPress`

2009-05-03 Thread Andy Stewart
Hi all,

I have a function named `keymapTest` need moand state WindowListT, and
WindowListT is `type WindowListT = StateT WindowList IO`.

when i add "(\event -> keymapTest winList event >> return False)" after
`onKeyPress` for handle key press event, i got GHC error:

Manatee.hs:57:58:
Couldn't match expected type `IO a'
   against inferred type `WindowListT Bool'
In the first argument of `(>>)', namely `keymapTest winList event'
In the expression: keymapTest winList event >> return False
In the second argument of `onKeyPress', namely
`(\ event -> keymapTest winList event >> return False)'

So function `onKeyPress` just accept *one* IO-action? 
Have a way to fix above problem?

Any help?

Thanks! 

  -- Andy

Below is source code of Manatee.hs file.

--> Manatee.hs start   
<--
module Main where

import Text.Printf
import Data.Monoid
import Data.List
import Data.Maybe
import Control.Monad
import Control.Monad.State
import Control.Applicative

import Data.IORef

import Graphics.UI.Gtk hiding (Window, windowNew, get)
import Graphics.UI.Gtk.SourceView
import Graphics.UI.Gtk.Abstract.Widget

import Manatee.Event
import Manatee.Buffer
import Manatee.WindowList
import Manatee.Pane
import Manatee.Statusbar
import Manatee.Utils
import Manatee.Window

import qualified Data.Set as Set
import qualified Graphics.UI.Gtk.Windows.Window as W
import qualified Graphics.UI.Gtk.Gdk.Events as E

main :: IO ()
main = do
  -- Init.
  initGUI

  -- Root frame.
  rootFrame <- W.windowNew
  rootFrame `onDestroy` mainQuit  -- quit main loop when root frame close

  -- Root frame status.
  windowFullscreen rootFrame   -- fullscreen

  -- Windows list.
  let windowsList = WindowList 0 Set.empty

  evalStateT (do
   -- Window 1
   window1 <- windowNewWithBuffer DTop "test"
   liftIO $ containerAdd rootFrame $ windowPaned window1

   (window2, window3) <- windowSplitVertically window1

   (window4, window5) <- windowSplitHorizontally window3 

   winList <- windowListGetList
   liftIO $ rootFrame `onKeyPress` (\event -> keymapTest winList 
event >> return False)

   -- Handle window content synchronous.
   windowHandleSynchronous
   ) windowsList

  -- Loop
  widgetShowAll rootFrame   -- display all widget
  mainGUI

keymapTest :: [Window] -> E.Event -> WindowListT Bool
keymapTest winList event = do
  window <- liftIO $ windowFindFocus winList
  case window of
Just x -> handleKeyPress x event
Nothing -> return False

handleKeyPress :: Window -> E.Event -> WindowListT Bool
handleKeyPress window ev = do
  liftIO $ 
 case eventTransform ev of
   Nothing -> return False
   Just e -> do
 let display = statusbarOutputSubitemSetText $ paneStatusbar $ 
windowPane $ window
 eventName = eventGetName e
 case eventName of
-- Window commands.
"M-t" -> display "windowSplitVertically" 
-- "M-t" -> windowSplitVertically window >> return False
_ -> display $ printf "%s undefined." eventName 
--> Manatee.hs end   <--

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


[Haskell-cafe] Re: gcd

2009-05-03 Thread Achim Schneider
Nathan Bloomfield  wrote:

> The "greatest" in gcd is not w.r.t. the canonical ordering on the
> naturals; rather w.r.t. the partial order given by the divides
> relation.
>
This, to defend myself, was not how it was explained in high school.

-- 
(c) this sig last receiving data processing entity. Inspect headers
for copyright history. All rights reserved. Copying, hiring, renting,
performance and/or quoting of this signature prohibited.


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


[Haskell-cafe] ST.Lazy vs ST.Strict

2009-05-03 Thread Tobias Olausson
Hello!
I have a program that is using ST.Strict, which works fine.
However, the program needs to be extended, and to do that,
lazy evaluation is needed. As a result of that, I have switched
to ST.Lazy to be able to do stuff like

foo y = do
x <- something
xs <- foo (y+1)
return (x:xs)

However, when running the program compiled with ST.Lazy, the
following is outputted:
   [to...@wobsi]$ ./runnerLazy looper.hex
   runnerLazy: <>

The very same program compiled with ST.Strict outputs:
   [to...@wobsi]$ ./runner looper.hex
   83298556

The code that is actually computing stuff is this:
loopSys :: Int -> CPU s Int
loopSys cc = do
instr <- fetch
if instr == 0xEA --NOP
   then return cc
   else do
 c <- execute instr
 loopSys $! (cc+c)

The CPU type looks as follows:
   type CPU s a = ReaderT (SysEnv s) (ST s) a

The program is run like
   runReaderT (loopSys 0)
which in turn is being runST'd and then printed

Does anyone know why the program just outputs <>
when compiled under ghc 6.10.2, and runs perfectly fine
under ghc 6.8.2? The program is compiled with --make and -O2


Tobias Olausson
tob...@gmail.com
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Vector-like data structure

2009-05-03 Thread Krzysztof Skrzętnicki
Hi

I'm looking for a data structure with following characteristics:
1. O(1) lookup
2. O(1) modification
3. amortized O(1) append
4. O(1) size query

This roughly characterizes C++ vector<> class. I'm ready to implement
it myself, but first I would like to ask if anyone knows package with
similar data structure.
If there are many, which one would you choose and why?

Best regards

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


[Haskell-cafe] Haskell vs Clean (speed comparison)

2009-05-03 Thread Daniel Carrera

Hi,

I think the mail server may have been acting up earlier. I sent this to 
Haskell-beginners, but it more properly belongs here.


I found something interesting. "General wisdom" is that Clean (or OCaml) 
is faster than Haskell. The claim is often followed by a link to the 
Debian shootout. But on closer inspection, I question this conclusion. 
The Debian shoot out actually has four sets of benchmarks:


1) Intel 32-bit one core.
2) Intel 32-bit quad-core.
3) Intel 64-bit one core.
4) Intel 64-bit quad-core.

It turns out that Clean is only faster for (1). For the others, Haskell 
is faster. Here I compare Haskell, Clean, OCaml, Lisp SBCL, C# Mono and 
Fortran because they are all in the same ball mark:


32-bit sing core [1]: Lisp, Fortran, Clean, Haskell, C# Mono.
32-bit quad-core [2]: Haskell, C# Mono, Lisp, Clean, Fortran.
64-bit sing core [3]: Fortran, OCaml, Haskell, Clean, C# Mono, Lisp.
64-bit quad-core [4]: Haskell, OCaml, Lisp, C# Mono, Fortran, Clean.

Notes:

* The order is "fast language first".
* There are no results for OCaml for 32-bit.
* "Lisp" is "List SCBL" whatever that is.

Tentative conclusions:

1) Haskell makes very good use of multiple cores. It smokes Clean.
2) For single core, they are neck and neck. Whether Clean is faster 
depends non the architecture.



What do you think?

Daniel.

P.S.
[1]http://shootout.alioth.debian.org/u32/benchmark.php?test=all&lang=all&d=data&ghc=on&ocaml=on&sbcl=on&csharp=on&ifc=on&clean=on&calc=calculate&box=1
[2]http://shootout.alioth.debian.org/u32q/benchmark.php?test=all&lang=all&d=data&ghc=on&ocaml=on&sbcl=on&csharp=on&ifc=on&clean=on&calc=calculate&box=1
[3]http://shootout.alioth.debian.org/u64/benchmark.php?test=all&lang=all&d=data&ghc=on&ocaml=on&sbcl=on&csharp=on&ifc=on&clean=on&calc=calculate&box=1
[4]http://shootout.alioth.debian.org/u64q/benchmark.php?test=all&lang=all&d=data&ghc=on&ocaml=on&sbcl=on&csharp=on&ifc=on&clean=on&calc=calculate&box=1

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


Re: [Haskell-cafe] Haskell vs Clean (speed comparison)

2009-05-03 Thread Gwern Branwen
On Sun, May 3, 2009 at 2:24 PM, Daniel Carrera
 wrote:
> Hi,
>
> I think the mail server may have been acting up earlier. I sent this to
> Haskell-beginners, but it more properly belongs here.
>
> I found something interesting. "General wisdom" is that Clean (or OCaml) is
> faster than Haskell. The claim is often followed by a link to the Debian
> shootout. But on closer inspection, I question this conclusion. The Debian
> shoot out actually has four sets of benchmarks:
>
> 1) Intel 32-bit one core.
> 2) Intel 32-bit quad-core.
> 3) Intel 64-bit one core.
> 4) Intel 64-bit quad-core.
>
> It turns out that Clean is only faster for (1). For the others, Haskell is
> faster. Here I compare Haskell, Clean, OCaml, Lisp SBCL, C# Mono and Fortran
> because they are all in the same ball mark:
>
> 32-bit sing core [1]: Lisp, Fortran, Clean, Haskell, C# Mono.
> 32-bit quad-core [2]: Haskell, C# Mono, Lisp, Clean, Fortran.
> 64-bit sing core [3]: Fortran, OCaml, Haskell, Clean, C# Mono, Lisp.
> 64-bit quad-core [4]: Haskell, OCaml, Lisp, C# Mono, Fortran, Clean.
>
> Notes:
>
> * The order is "fast language first".
> * There are no results for OCaml for 32-bit.
> * "Lisp" is "List SCBL" whatever that is.
>
> Tentative conclusions:
>
> 1) Haskell makes very good use of multiple cores. It smokes Clean.
> 2) For single core, they are neck and neck. Whether Clean is faster depends
> non the architecture.
>
>
> What do you think?
>
> Daniel.
>
> P.S.
> [1]http://shootout.alioth.debian.org/u32/benchmark.php?test=all&lang=all&d=data&ghc=on&ocaml=on&sbcl=on&csharp=on&ifc=on&clean=on&calc=calculate&box=1
> [2]http://shootout.alioth.debian.org/u32q/benchmark.php?test=all&lang=all&d=data&ghc=on&ocaml=on&sbcl=on&csharp=on&ifc=on&clean=on&calc=calculate&box=1
> [3]http://shootout.alioth.debian.org/u64/benchmark.php?test=all&lang=all&d=data&ghc=on&ocaml=on&sbcl=on&csharp=on&ifc=on&clean=on&calc=calculate&box=1
> [4]http://shootout.alioth.debian.org/u64q/benchmark.php?test=all&lang=all&d=data&ghc=on&ocaml=on&sbcl=on&csharp=on&ifc=on&clean=on&calc=calculate&box=1

Perhaps it's just that no one has parallelized the Clean programs?
Haskellers seem to care about the shootout programs much more than
Cleaners do.

eg. I randomly looked at Mandelbrot on [2].
clean: 
http://shootout.alioth.debian.org/u32q/benchmark.php?test=mandelbrot&lang=clean&id=2
haskell: 
http://shootout.alioth.debian.org/u32q/benchmark.php?test=mandelbrot&lang=ghc&id=2

I can't really read Clean, but it certainly looks as if it's making no
use of concurrency at all, while the Haskell one most certainly is.

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


Re: [Haskell-cafe] Re: gcd

2009-05-03 Thread Nathan Bloomfield
> This, to defend myself, was not how it was explained in high school.

No worries. I didn't realize this myself until college; most nonspecialist
teachers just don't know any better. Nor did, it appears, the original
authors of the Haskell Prelude. :)

BTW, this definition of gcd makes it possible to consider gcds in rings that
otherwise have no natural order- such as rings of polynomials in several
variables, group rings, et cetera.

Nathan Bloomfield

On Sun, May 3, 2009 at 11:16 AM, Achim Schneider  wrote:

> Nathan Bloomfield  wrote:
>
> > The "greatest" in gcd is not w.r.t. the canonical ordering on the
> > naturals; rather w.r.t. the partial order given by the divides
> > relation.
> >
> This, to defend myself, was not how it was explained in high school.
>
> --
> (c) this sig last receiving data processing entity. Inspect headers
> for copyright history. All rights reserved. Copying, hiring, renting,
> performance and/or quoting of this signature prohibited.
>
>
> ___
> 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] using haskell for a project

2009-05-03 Thread Bulat Ziganshin
Hello Nicolas,

Saturday, May 2, 2009, 9:17:55 PM, you wrote:

> But now I don't know how to dynamically add new spells (new spells can be
> created in my gameplay). Since I can't assign a new value to the `spells'
> variable (Data.Map.insert returns a new map), I just don't know where to
> go.

well, i've written mid-size program that combines pure haskell,
imperative haskell and c++ and that's my vision:

in C, you don't have the barrier between pure and imperative worlds
and as result you don't got habit to distinguish them. haskell forces
you to separate pure and imperative code: pure code is much simpler to
write but it cannot call imperative one (i.e. function can't call
procedures) so you should understand the difference and carefully
make your choice

you are *forced* to use imperative code when it responds to some
external events - user input, network input, so on. so when your spell
list mutates as a result of external events, you should do something
imperatively. although it may be as easy as tail-recursively calling
itself with purely modified value:

mainCycle spells = do
  x <- userInput
  case x of
AddSpell -> mainCycle (newSpell:spells)


further improving this idea, you may develop data structire contating
whole state info:

data GameState = GameState {spells: [Spell],
.}

and update appropriate parts of GameState on tail-recursive calls:

mainCycle gameState = do
  x <- userInput
  case x of
AddSpell -> mainCycle (gameState {spells = newSpell:spells gameState})


as you see, this approach allows you to live in imperative world but
use only pure computations to update state


but once your program will become larger, you may find that this
approach needs too much typing. in that case, you can program using
IORefs, MVars and other types of C-like global vars. or, you may use
them semi-globally - i.e. create this vars in outside procedure and
then pass to all interested routines:

main = do vspells <- newIORef []
  let stateVars = StateVars {spells=vspells, ...}
  mainCycle stateVars

you may even use implicit parameters to hide passing details from
program code but i don't have experience of using them


-- 
Best regards,
 Bulatmailto:bulat.zigans...@gmail.com

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


Re: [Haskell-cafe] Haskell vs Clean (speed comparison)

2009-05-03 Thread Daniel Carrera

Gwern Branwen wrote:

Perhaps it's just that no one has parallelized the Clean programs?
Haskellers seem to care about the shootout programs much more than
Cleaners do.


I'm not sure about the second comment. I haven't seen the Haskell site 
mention the shootout, whereas web pages about Clean often do. Perhaps 
what happens is that Haskell has a larger community, so there's more 
chance that someone will submit an optimized program.


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


[Haskell-cafe] Foldable for BNFC generated tree

2009-05-03 Thread Deniz Dogan
Hi

I have a bunch of data types which are used to represent a JavaScript
program. The data types and a lexer and a parser have all been
generated using BNFC.  So basically an entire JavaScript program is
represented as a tree using these data types.  Ideally I'd like to be
able to "fold" over this data structure, but I can't instantiate
Foldable for my data types, since the data types all have kind *, if
I'm not completely lost.

Here's an example data type:

data Statement =
   StmtFunDecl JIdent [JIdent] ExprOrBlock
 | StmtVarDecl [VarDecl]
 | StmtLetDecl [VarDecl]
 | StmtWhile Expr Statement
 | ...

So, basically I'd like some sort of folding functionality for these
data types, without having to hack the lexer/parser myself
(parameterising the data types), because as I said they're being
generated by BNFC. I noticed that you can make BNFC generate GADTs
instead of normal ADTs, which would allow me to instantiate Foldable,
but I'm not entirely sure that this is the best way to do this.

Any help is appreciated,
Deniz Dogan
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Haskell vs Clean (speed comparison)

2009-05-03 Thread Bulat Ziganshin
Hello Daniel,

Sunday, May 3, 2009, 10:24:52 PM, you wrote:

> 32-bit sing core [1]: Lisp, Fortran

:)  this test measures speed of some programs, not "languages".
results are depends mainly on bundled libraries and RTS. by no means
it demonstrates speed of compiler-generated code of carefully-written
program what is typically considered as "language speed". the reasons
are:

1) C++ people (and probably Fortran too) aren't so interested in
making fastest possible programs as Haskell community. it becomes
popular a few years ago, you can find that Haskell becomes several
faster at average since then, which doesn't reflect actual
improvements in GHC code generation (10-20%)

2) Most programs there depend on speed of libraries. Moreover, there
is limitation that it should be *bundled* libraries, so results
greatly depends on what currently included in one compiler or another

3) it's prohibited to write your own fast code if bundled library is
too slow (for example, because it's too general)


-- 
Best regards,
 Bulatmailto:bulat.zigans...@gmail.com

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


Re: [Haskell-cafe] Vector-like data structure

2009-05-03 Thread Bulat Ziganshin
Hello Krzysztof,

Sunday, May 3, 2009, 10:06:30 PM, you wrote:

> This roughly characterizes C++ vector<> class. I'm ready to implement

http://haskell.org/haskellwiki/Library/ArrayRef#Using_dynamic_.28resizable.29_arrays

although this (mine) package is probably incompatible with current ghc
versions :)


-- 
Best regards,
 Bulatmailto:bulat.zigans...@gmail.com

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


Re[2]: [Haskell-cafe] Haskell vs Clean (speed comparison)

2009-05-03 Thread Bulat Ziganshin
Hello Gwern,

Sunday, May 3, 2009, 10:29:37 PM, you wrote:

>> 32-bit quad-core [2]: Haskell, C# Mono, Lisp, Clean, Fortran.

> I can't really read Clean, but it certainly looks as if it's making no
> use of concurrency at all, while the Haskell one most certainly is.

probably other languages goes w/o built-in concurrency support and
this test doesn't allow to use external libs :)

-- 
Best regards,
 Bulatmailto:bulat.zigans...@gmail.com

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


Re: [Haskell-cafe] Re: gcd

2009-05-03 Thread João Ferreira
Something that perhaps could be added is that leaving  0 `gcd` 0  undefined
has two obvious annoying consequences: gcd is no longer idempotent (i.e. we
don't have a `gcd` a = a, for all a), and it is no longer associative ((a
`gcd` 0) `gcd` 0 is well-defined whilst a `gcd` (0 `gcd` 0) is not).

(We actually wrote something about this on a recent paper. If you're
interested, see  http://www.joaoff.com/publications/2009/euclid-alg )

Regards,
Joao

2009/5/3 Nathan Bloomfield 

> > This, to defend myself, was not how it was explained in high school.
>
> No worries. I didn't realize this myself until college; most nonspecialist
> teachers just don't know any better. Nor did, it appears, the original
> authors of the Haskell Prelude. :)
>
> BTW, this definition of gcd makes it possible to consider gcds in rings
> that otherwise have no natural order- such as rings of polynomials in
> several variables, group rings, et cetera.
>
> Nathan Bloomfield
>
>
> On Sun, May 3, 2009 at 11:16 AM, Achim Schneider  wrote:
>
>> Nathan Bloomfield  wrote:
>>
>> > The "greatest" in gcd is not w.r.t. the canonical ordering on the
>> > naturals; rather w.r.t. the partial order given by the divides
>> > relation.
>> >
>> This, to defend myself, was not how it was explained in high school.
>>
>> --
>> (c) this sig last receiving data processing entity. Inspect headers
>> for copyright history. All rights reserved. Copying, hiring, renting,
>> performance and/or quoting of this signature prohibited.
>>
>>
>> ___
>> 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[2]: [Haskell-cafe] Haskell vs Clean (speed comparison)

2009-05-03 Thread Bulat Ziganshin
Hello Daniel,

Sunday, May 3, 2009, 10:42:06 PM, you wrote:

> I'm not sure about the second comment. I haven't seen the Haskell site
> mention the shootout

just search cafe archives ;)


-- 
Best regards,
 Bulatmailto:bulat.zigans...@gmail.com

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


Re: [Haskell-cafe] Re: gcd

2009-05-03 Thread Daniel Fischer
Am Sonntag 03 Mai 2009 18:16:38 schrieb Achim Schneider:
> Nathan Bloomfield  wrote:
> > The "greatest" in gcd is not w.r.t. the canonical ordering on the
> > naturals; rather w.r.t. the partial order given by the divides
> > relation.

Nitpick: it's not a partial order, but a preorder (2 | (-2), (-2) | 2, 2 /= 
(-2)).

>
> This, to defend myself, was not how it was explained in high school.

Understandably. One wouldn't want to confuse the average pupil with too 
abstract concepts 
like arbitrary rings or preorders. Unfortunately that leads to teaching 
concepts of 
primes, greatest common divisors and least common multiples which don't agree 
with the 
modern mathematical concepts :-(

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


Re: [Haskell-cafe] Haskell vs Clean (speed comparison)

2009-05-03 Thread Daniel Carrera

Bulat Ziganshin wrote:

32-bit sing core [1]: Lisp, Fortran


:)  this test measures speed of some programs, not "languages".


I know. But since I know that you know that too, I opted for brevity.

"How can we benchmark a programming language?
We can't - we benchmark programming language implementations.

How can we benchmark language implementations?
We can't - we measure particular programs."


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


Re: [Haskell-cafe] Haskell vs Clean (speed comparison)

2009-05-03 Thread Thomas DuBuisson
>
> I haven't seen the Haskell site mention the shootout, whereas web pages
> about Clean often do.


Well, there certainly has been significant efforts on the shootout in the
Haskell community.  There's wiki pages about it [1] and it comes up on the
Haskell reddit and proggit frequently.

With regard to the original discussion on comparing Haskell shootout results
to Clean I suggest people think more about making Haskell use as little
memory as Clean does and not worrying so much about the Haskell vs. Clean
CPU time.


Thomas

[1] http://haskell.org/haskellwiki/Shootout
[2] http://www.reddit.com/r/haskell/search?q=shootout - and that doesn't
show comments, just some submitted links.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] ANN: Silkworm game

2009-05-03 Thread Duane Johnson
So here's the thing to get it to run on Mac OS X, I have to build  
a "SilkwormGame.app" directory, with a "Resources" directory inside,  
along with a lot of other rubbish, just so that GLFW can create a Mac  
OS window that accepts mouse and keyboard input.  This is the purpose  
of the Makefile--it uses a script borrowed from wxWindows to assemble  
all the parts.


Obviously, this rigamarole is not necessary in Unix.  But am I correct  
in assuming that there is no facility in Cabal that prepares a Mac OS  
"app" in this way?  Any suggestions so that a cabal install will work  
for both Unix and Mac users?


Here is what I get (using Don's Silkworm.cabal):

~/Documents/Duane/BYU Semesters/2009-Winter/Graphics/ 
Silkworm(master) $ cabal install

Resolving dependencies...
Configuring Silkworm-0.2...
Preprocessing executables for Silkworm-0.2...
Building Silkworm-0.2...
[ 1 of 16] Compiling Silkworm.Action  ( Silkworm/Action.hs, dist/ 
build/SilkwormGame/SilkwormGame-tmp/Silkworm/Action.o )

... ...
[16 of 16] Compiling Main ( main.hs, dist/build/ 
SilkwormGame/SilkwormGame-tmp/Main.o )

Linking dist/build/SilkwormGame/SilkwormGame ...
Installing executable(s) in /Users/duane/.cabal/bin

~/Silkworm(master) $ SilkwormGame
Working in unbundled mode.  You should build a .app wrapper for your  
Mac OS X applications.
SilkwormGame: ~/Silkworm/background.png: openBinaryFile: does not  
exist (No such file or directory)


Putting the binary files in the place it expects produces the same  
"working in unbundled mode" along with a window that will not capture  
ANY input from the mouse or keyboard.  In fact, it won't even rise to  
the top of the window stack--it remains behind the terminal window.


Thanks for you help,
-- Duane

On May 2, 2009, at 6:07 PM, Don Stewart wrote:


Yes, it is quite fun.

I think it should be using cabal's datadir from Paths_silkworm.hs to
install (and find) the resources.

Yell if you can't figure out how to do that. (xmonad has an example)

-- Don


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


Re: [Haskell-cafe] ANN: Silkworm game

2009-05-03 Thread Don Stewart
For the Mac, you might have to use mkbndl or one of the other native
package builders for the Mac.

-- Don

duane.johnson:
> So here's the thing to get it to run on Mac OS X, I have to build a 
> "SilkwormGame.app" directory, with a "Resources" directory inside, along 
> with a lot of other rubbish, just so that GLFW can create a Mac OS window 
> that accepts mouse and keyboard input.  This is the purpose of the 
> Makefile--it uses a script borrowed from wxWindows to assemble all the 
> parts.
>
> Obviously, this rigamarole is not necessary in Unix.  But am I correct  
> in assuming that there is no facility in Cabal that prepares a Mac OS  
> "app" in this way?  Any suggestions so that a cabal install will work  
> for both Unix and Mac users?
>
> Here is what I get (using Don's Silkworm.cabal):
>
>> ~/Documents/Duane/BYU Semesters/2009-Winter/Graphics/Silkworm(master) $ 
>> cabal install
>> Resolving dependencies...
>> Configuring Silkworm-0.2...
>> Preprocessing executables for Silkworm-0.2...
>> Building Silkworm-0.2...
>> [ 1 of 16] Compiling Silkworm.Action  ( Silkworm/Action.hs, dist/ 
>> build/SilkwormGame/SilkwormGame-tmp/Silkworm/Action.o )
>> ... ...
>> [16 of 16] Compiling Main ( main.hs, dist/build/ 
>> SilkwormGame/SilkwormGame-tmp/Main.o )
>> Linking dist/build/SilkwormGame/SilkwormGame ...
>> Installing executable(s) in /Users/duane/.cabal/bin
>>
>> ~/Silkworm(master) $ SilkwormGame
>> Working in unbundled mode.  You should build a .app wrapper for your  
>> Mac OS X applications.
>> SilkwormGame: ~/Silkworm/background.png: openBinaryFile: does not  
>> exist (No such file or directory)
>
> Putting the binary files in the place it expects produces the same  
> "working in unbundled mode" along with a window that will not capture  
> ANY input from the mouse or keyboard.  In fact, it won't even rise to  
> the top of the window stack--it remains behind the terminal window.
>
> Thanks for you help,
> -- Duane
>
> On May 2, 2009, at 6:07 PM, Don Stewart wrote:
>
>> Yes, it is quite fun.
>>
>> I think it should be using cabal's datadir from Paths_silkworm.hs to
>> install (and find) the resources.
>>
>> Yell if you can't figure out how to do that. (xmonad has an example)
>>
>> -- Don
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Vector-like data structure

2009-05-03 Thread Don Stewart
gtener:
> Hi
> 
> I'm looking for a data structure with following characteristics:
> 1. O(1) lookup
> 2. O(1) modification
> 3. amortized O(1) append
> 4. O(1) size query
> 
> This roughly characterizes C++ vector<> class. I'm ready to implement
> it myself, but first I would like to ask if anyone knows package with
> similar data structure.
> If there are many, which one would you choose and why?
> 

A number of the array packages behave like this.  The trie packages are
O(log(wordsize)), so another option (they tend to have better append
complexity as well).

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


Re: [Haskell-cafe] Combining computations

2009-05-03 Thread Claus Reinke

 mplus' :: MonadPlus m => Maybe a -> m a -> m a
 mplus' m l = maybeToMonad m `mplus` l

 maybeToMonad :: Monad m => Maybe a -> m a
 maybeToMonad = maybe (fail "Nothing") return

In general, however, this operation can't be done.  For example,
how would you write:

 mplus' :: IO a -> [a] -> [a]


Perhaps the question should be: is there an interesting structure
that would allow us to capture when this kind of merging Monads
is possible? We can convert every 'Maybe a' to a '[] a', but the 
other way round is partial or loses information, so lets focus on 
the first direction. Should there be a


   type family Up m1 m2
   type instance Up Maybe [] = []

so that one could define

   mplusUp :: m1 a -> m2 a -> (m1 `Up` m2) a 


? Well, we'd need the conversions, too, so perhaps

   {-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, TypeFamilies, 
TypeOperators #-}

   import Control.Monad

   class Up m1 m2 where
 type m1 :/\: m2 :: * -> *
 up :: m1 a -> m2 a -> ((m1 :/\: m2) a, (m1 :/\: m2) a)

   instance Up m m where
 type m :/\: m = m
 up ma1 ma2 = (ma1, ma2)

   instance Up Maybe [] where
 type Maybe :/\: [] = []
 up m1a m2a = (maybe [] (:[]) m1a, m2a)

   instance Up [] Maybe where
 type [] :/\: Maybe = []
 up m1a m2a = (m1a, maybe [] (:[]) m2a)

   mplusUp :: (m ~ (m1 :/\: m2), Up m1 m2, MonadPlus m) => m1 a -> m2 a -> m a
   m1a `mplusUp` m2a = mUp1a `mplus` mUp2a
 where (mUp1a,mUp2a) = up m1a m2a

Whether or not that is interesting, or whether it needs to be defined
differently to correspond to an interesting structure, I'll leave to the 
residential (co-)Categorians!-)


Claus


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


Re: [Haskell-cafe] gcd

2009-05-03 Thread Hans Aberg

On 2 May 2009, at 04:05, Steve wrote:


Why is gcd 0 0 undefined?


In math, one may define gcd(x, y) as a generator of the ideal  
generated by x and y in the ring of integers Z. The gcd(x, y) then  
always exists as the ring Z is a PID (principal ideal domain), i.e.,  
all ideals can be generated by a single element, which can be proven  
using Euclid's algorithm, also useful for computing the gcd in Z.


Anyway, the ideal generated by 0 and 0 is the zero ideal 0, which also  
is generated by the single generator 0. So gcd(0, 0) = 0 by this  
definition.


In Z, one may take the gcd >= 0, but that may not work in every PID.  
If k is a field, then the polynomial ring k[x] is a PID, but not the  
ring k[x_1, ..., x_n]. So that leads to Buchberger's Groebner Basis  
Algorithm.


  Hans


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


[Haskell-cafe] Research in functional programming

2009-05-03 Thread Louis Wasserman
Where might I find or submit a paper on functional data structures?
Examples I've found so far include ICFP  and
the JFP , but
Google hasn't found me anything else.

Louis Wasserman
wasserman.lo...@gmail.com
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Combining computations

2009-05-03 Thread Brandon S. Allbery KF8NH

On May 3, 2009, at 16:59 , Claus Reinke wrote:

Perhaps the question should be: is there an interesting structure
that would allow us to capture when this kind of merging Monads
is possible? We can convert every 'Maybe a' to a '[] a', but the  
other way round is partial or loses information, so lets focus on  
the first direction. Should there be a


It feels to me kinda like numeric upconversion.

--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allb...@kf8nh.com
system administrator [openafs,heimdal,too many hats] allb...@ece.cmu.edu
electrical and computer engineering, carnegie mellon universityKF8NH




PGP.sig
Description: This is a digitally signed message part
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Getting WriterT log lazily

2009-05-03 Thread Magnus Therning

I've been playing around with (WriterT [Int] IO), trying to get the log
out and map `print` over it... and do it lazily.  However, I'm not
really happy with what I have so far, since I've had to resort to
`unsafePerformIO`.  Any hints are welcome.

What I have so far is:

  foo = let
  _tell i = do
  a <- return $ unsafePerformIO $ sleep 1
  tell [a + 1 `seq` i]
  in do
  mapM_ _tell [1..10]

  main = do
  (_, ~res) <- runWriterT foo
  mapM_ print res

Without the `seq` the call to sleep will simply be skipped (is there an
easier way to force evaluation there?).  Without `unsafePerformIO` all
the sleeping is done up front, and all numbers are print at once at the
end.

The goal is of course to use code along the same shape to do something 
more useful, and then `unsafePerformIO` will really be unsafe...


/M

--
Magnus Therning(OpenPGP: 0xAB4DFBA4)
magnus@therning.org  Jabber: magnus@therning.org
http://therning.org/magnus identi.ca|twitter: magthe



signature.asc
Description: OpenPGP digital signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] traversing a tree using monad.cont

2009-05-03 Thread Ryan Ingram
Cont with success and failure isn't Cont; it's something else (albeit similar)

There's a great exposition of using something much like Cont to get
success and failure "for free" here:
http://www-ps.informatik.uni-kiel.de/~sebf/haskell/barefaced-pilferage-of-monadic-bind.lhs.html

  -- ryan

On Sat, May 2, 2009 at 2:13 AM, Anatoly Yakovenko  wrote:
>> Though I don't fully understand what you are doing (specifically what you
>> mean by "specific order"), but in a lazy language, traversals are usually
>> simply encoded as lists.  Just write a function which returns all the leaves
>> as a list, and filter over it.
>
> yea, i know, i am trying to learn how to use the Cont monad. or
> continuation in haskell.  The idea is that while i am processing some
> data i may hit a point whree some dependency isn't met and i want to
> take a different branch via continuation.  I expect that branch to
> furfill my dependency and when its done i want to continue down the
> original branch
>
>
>>> module TestCont where
>>> import Control.Monad.Cont
>>> import Control.Monad.Identity
>>> import Control.Monad.State.Lazy
>>>
>>> --our stupid tree
>>> data Tree a = Tree [Tree a]
>>>            | Leaf a
>>>
>>> --traverse all the branches
>>> search (Tree ts) next = do
>>>   mapM_ (\ ti -> (callCC (search ti))) ts
>>>   next $ ()
>>>
>>> search tt@(Leaf a) next = do
>>>   cur <- lift get
>>>   case ((cur + 1) == a) of
>>>      True -> do --the current leaf is what we want, update the state and 
>>> return
>
> this is where i succeed in my current branch, so i can just do my thing and 
> exit
>
>>>         lift $ put a
>>>         return $ ()
>>>      False -> do --the current leaf is not what we want, continue first, 
>>> then try again
>
> this is where i fail, so i want to take the "other" branch first
> expecting it to fulfill my dependency.
>
>>>         next ()
>>>         search tt (\ _ -> error "fail")
>>>
>>> t1 = Leaf 1
>>> t2 = Leaf 2
>>> t3 = Tree [t1,t2]
>>> t4 = Leaf 3
>>> t5::Tree Int = Tree [t4,t3]
>>>
>>> run =  runIdentity (runStateT ((runContT $ callCC (search t5)) return) 0)
>
>
> but i think next doesn't do exactly what i think it does
> ___
> 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] Getting WriterT log lazily

2009-05-03 Thread Ryan Ingram
How about this:

> type ActionLog v = Writer [IO v]

> myTell :: v -> ActionLog v ()
> myTell a = tell [sleep 1 >> return a]

> foo :: ActionLog Int ()
> foo = mapM_ myTell [1..10]

> main = sequence_ results where
>(_, vals) = runWriter foo
>results = map (>>= print) vals

  -- ryan

On Sun, May 3, 2009 at 2:17 PM, Magnus Therning  wrote:
> I've been playing around with (WriterT [Int] IO), trying to get the log
> out and map `print` over it... and do it lazily.  However, I'm not
> really happy with what I have so far, since I've had to resort to
> `unsafePerformIO`.  Any hints are welcome.
>
> What I have so far is:
>
>  foo = let
>  _tell i = do
>  a <- return $ unsafePerformIO $ sleep 1
>  tell [a + 1 `seq` i]
>  in do
>  mapM_ _tell [1..10]
>
>  main = do
>  (_, ~res) <- runWriterT foo
>  mapM_ print res
>
> Without the `seq` the call to sleep will simply be skipped (is there an
> easier way to force evaluation there?).  Without `unsafePerformIO` all
> the sleeping is done up front, and all numbers are print at once at the
> end.
>
> The goal is of course to use code along the same shape to do something more
> useful, and then `unsafePerformIO` will really be unsafe...
>
> /M
>
> --
> Magnus Therning(OpenPGP: 0xAB4DFBA4)
> magnus@therning.org  Jabber: magnus@therning.org
> http://therning.org/magnus identi.ca|twitter: magthe
>
>
> ___
> 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] Combining computations

2009-05-03 Thread Tillmann Rendel

Claus Reinke wrote:

 mplus' :: MonadPlus m => Maybe a -> m a -> m a
 mplus' m l = maybeToMonad m `mplus` l

 maybeToMonad :: Monad m => Maybe a -> m a
 maybeToMonad = maybe (fail "Nothing") return

In general, however, this operation can't be done.  For example,
how would you write:

 mplus' :: IO a -> [a] -> [a]


Perhaps the question should be: is there an interesting structure
that would allow us to capture when this kind of merging Monads
is possible? 


For me, it seems that Foldable is the other side of Alternative. A 
functor F supports Alternative if (F a) supports a monoidal structure 
for the construction of values, and it supports Foldable if (F a) 
supports a monoidal structure for the decomposition of values. That 
means that we can give a translation from every Foldable functor to 
every Alternative functor as follows:


  foldable2alternative = foldr (<|>) empty . fmap pure

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


Re: [Haskell-cafe] ST.Lazy vs ST.Strict

2009-05-03 Thread Ryan Ingram
So, I don't know what is causing your problem, but foo will not do
what you want even with lazy ST.

foo y = do
   x <- something
   xs <- foo (y+1)
   return (x:xs)

Desugaring:

foo y = something >>= \x -> foo (y+1) >>= \xs -> return (x:xs)
= something >>= \x -> something >>= \x2 -> foo (y+2) >>= \xs2 ->
return (x2:xs2) >>= \xs -> return (x:xs)
= something >>= \x -> something >>= \x2 -> foo (y+2) >>= \xs2 ->
return (x:x2:xs2)

You see that there is an infinite chain of "foo" calls; the lazy ST
still needs to thread the state through that chain; so in the case of
foo 0 >>= something_else, the state is _|_ for something_else and you
will fail if you use read/write/newSTRef after that point.  In fact,
I'm not sure that lazy ST is very useful :)

My guess is that you want one of
(1) mdo, when the effects in 'something' only matter once, or
(2) unsafeInterleaveST, if you just want to be able to traverse the
(x:xs) list lazily and the references it uses are dead after calling
foo.

  -- ryan

On Sun, May 3, 2009 at 10:27 AM, Tobias Olausson  wrote:
> Hello!
> I have a program that is using ST.Strict, which works fine.
> However, the program needs to be extended, and to do that,
> lazy evaluation is needed. As a result of that, I have switched
> to ST.Lazy to be able to do stuff like
>
> foo y = do
>    x <- something
>    xs <- foo (y+1)
>    return (x:xs)
>
> However, when running the program compiled with ST.Lazy, the
> following is outputted:
>   [to...@wobsi]$ ./runnerLazy looper.hex
>   runnerLazy: <>
>
> The very same program compiled with ST.Strict outputs:
>   [to...@wobsi]$ ./runner looper.hex
>   83298556
>
> The code that is actually computing stuff is this:
> loopSys :: Int -> CPU s Int
> loopSys cc = do
>    instr <- fetch
>    if instr == 0xEA --NOP
>       then return cc
>       else do
>         c <- execute instr
>         loopSys $! (cc+c)
>
> The CPU type looks as follows:
>   type CPU s a = ReaderT (SysEnv s) (ST s) a
>
> The program is run like
>   runReaderT (loopSys 0)
> which in turn is being runST'd and then printed
>
> Does anyone know why the program just outputs <>
> when compiled under ghc 6.10.2, and runs perfectly fine
> under ghc 6.8.2? The program is compiled with --make and -O2
>
>
> Tobias Olausson
> tob...@gmail.com
> ___
> 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] StateT IO Action on `onKeyPress`

2009-05-03 Thread Ryan Ingram
Hi Andy.

The GTK bindings use IO for their callbacks, not any custom monad like
your WindowListT.

I suggest, instead of StateT s IO a, you use ReaderT (IORef s) IO a:

putR :: s -> ReaderT (IORef s) IO ()
putR s = do
r <- ask
liftIO $ writeIORef r s

getR :: ReaderT (IORef s) IO s
getR = ask >>= liftIO . readIORef

Otherwise, there are techniques to use an IORef to hold onto the state
while calling into IO (which might make callbacks), and then read it
back out and put it in the state while running your action.  But it's
simpler to just switch to ReaderT

  -- ryan

On Sun, May 3, 2009 at 8:27 AM, Andy Stewart  wrote:
> Hi all,
>
> I have a function named `keymapTest` need moand state WindowListT, and
> WindowListT is `type WindowListT = StateT WindowList IO`.
>
> when i add "(\event -> keymapTest winList event >> return False)" after
> `onKeyPress` for handle key press event, i got GHC error:
>
> Manatee.hs:57:58:
>    Couldn't match expected type `IO a'
>           against inferred type `WindowListT Bool'
>    In the first argument of `(>>)', namely `keymapTest winList event'
>    In the expression: keymapTest winList event >> return False
>    In the second argument of `onKeyPress', namely
>        `(\ event -> keymapTest winList event >> return False)'
>
> So function `onKeyPress` just accept *one* IO-action?
> Have a way to fix above problem?
>
> Any help?
>
> Thanks!
>
>  -- Andy
>
> Below is source code of Manatee.hs file.
>
> --> Manatee.hs start   
> <--
> module Main where
>
> import Text.Printf
> import Data.Monoid
> import Data.List
> import Data.Maybe
> import Control.Monad
> import Control.Monad.State
> import Control.Applicative
>
> import Data.IORef
>
> import Graphics.UI.Gtk hiding (Window, windowNew, get)
> import Graphics.UI.Gtk.SourceView
> import Graphics.UI.Gtk.Abstract.Widget
>
> import Manatee.Event
> import Manatee.Buffer
> import Manatee.WindowList
> import Manatee.Pane
> import Manatee.Statusbar
> import Manatee.Utils
> import Manatee.Window
>
> import qualified Data.Set as Set
> import qualified Graphics.UI.Gtk.Windows.Window as W
> import qualified Graphics.UI.Gtk.Gdk.Events as E
>
> main :: IO ()
> main = do
>  -- Init.
>  initGUI
>
>  -- Root frame.
>  rootFrame <- W.windowNew
>  rootFrame `onDestroy` mainQuit  -- quit main loop when root frame close
>
>  -- Root frame status.
>  windowFullscreen rootFrame   -- fullscreen
>
>  -- Windows list.
>  let windowsList = WindowList 0 Set.empty
>
>  evalStateT (do
>               -- Window 1
>               window1 <- windowNewWithBuffer DTop "test"
>               liftIO $ containerAdd rootFrame $ windowPaned window1
>
>               (window2, window3) <- windowSplitVertically window1
>
>               (window4, window5) <- windowSplitHorizontally window3
>
>               winList <- windowListGetList
>               liftIO $ rootFrame `onKeyPress` (\event -> keymapTest winList 
> event >> return False)
>
>               -- Handle window content synchronous.
>               windowHandleSynchronous
>               ) windowsList
>
>  -- Loop
>  widgetShowAll rootFrame       -- display all widget
>  mainGUI
>
> keymapTest :: [Window] -> E.Event -> WindowListT Bool
> keymapTest winList event = do
>  window <- liftIO $ windowFindFocus winList
>  case window of
>    Just x -> handleKeyPress x event
>    Nothing -> return False
>
> handleKeyPress :: Window -> E.Event -> WindowListT Bool
> handleKeyPress window ev = do
>  liftIO $
>         case eventTransform ev of
>           Nothing -> return False
>           Just e -> do
>             let display = statusbarOutputSubitemSetText $ paneStatusbar $ 
> windowPane $ window
>                 eventName = eventGetName e
>             case eventName of
>                -- Window commands.
>                "M-t" -> display "windowSplitVertically"
>                -- "M-t" -> windowSplitVertically window >> return False
>                _ -> display $ printf "%s undefined." eventName
> --> Manatee.hs end   
> <--
>
> ___
> 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] converting IOException to Either in ErrorT

2009-05-03 Thread brian
I wrote this to make it a little nicer to catch IO exceptions and
convert them to ErrorT failure:

onExceptionThrowError
  :: (Error ce) =>
 IO a
  -> (String -> ce)
  -> ErrorT ce IO a
onExceptionThrowError a ce =
liftIO (try a) >>=
  either
(\(e :: IOException) -> throwError (ce (show e)))
return

So now I can do, e.g.,
writeFile fp s `onExceptionThrowError` SpecificErrorConstructor ...

It works, but seems bad. Please let me know if you see ways to improve
it. Thanks.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] ANN: Silkworm game

2009-05-03 Thread Daryoush Mehrtash
I noticed that Chipmunk also has a Ruby interface.  Do you have any pro/con
of implementing the game in Ruby vs Haskell?

Thanks,

Daryoush

On Sat, May 2, 2009 at 12:00 PM, Duane Johnson wrote:

> Reprinted from my blog post [1]:
>
> ===
>
> The semester is over, my final project was a success (at least in that I
> passed the class) and it’s time now to release the game I made for Graphics
> 455: Silkworm!
>
> This is my first full application in Haskell.  The process has been an
> enlarging experience–I’ve come to really enjoy the mental work that goes
> into thinking about a program in a functional way.  I highly recommend the
> challenge to other software engineers.
>
> Silkworm combines the Hipmunk binding to Chipmunk 2D Game Dynamics with
> OpenGL, and GLFW (an alternative to GLUT).
>
> It’s built to work on Mac OS X, but it uses cross-platform libraries so it
> should be fairly easy to port to other platforms.  The source code is here
> [2] and below are some screenshots [1]
>
> -- Duane Johnson
> (canadaduane)
>
> ===
>
> [1]
> http://blog.inquirylabs.com/2009/05/02/silkworm-game-written-in-haskell/
> [2]
> http://inquirylabs.com/downloads/Silkworm.tgz___
> 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: Getting WriterT log lazily

2009-05-03 Thread Ertugrul Soeylemez
Hello Magnus,

although your approach is a bit more pragmatic, I always prefer to use
concurrency to implement predictable logging.  This is a bit more code,
but works much nicer and a lot more predictable:

  {-# LANGUAGE ExistentialQuantification #-}

  module Main where

  import Control.Concurrent
  import Control.Monad


  data LoggerMsg
= forall a. Show a => LogLine a
| QuitLogger (IO ())


  main :: IO ()
  main = do
log <- newEmptyMVar
forkIO $ forever $ do
  msg <- takeMVar log
  case msg of
LogLine ln   -> print ln
QuitLogger c -> c >> myThreadId >>= killThread

forM_ [1..10] $ putMVar log . LogLine

waiter <- newEmptyMVar
putMVar log $ QuitLogger (putMVar waiter ())
takeMVar waiter

Whenever you put a LogLine message into the MVar, as soon as the putMVar
action returns, it is guaranteed that the last log line has been
processed.  If you don't need that guarantee, use Chan instead of MVar.


Greets,
Ertugrul.


Magnus Therning  wrote:

> I've been playing around with (WriterT [Int] IO), trying to get the
> log out and map `print` over it... and do it lazily.  However, I'm not
> really happy with what I have so far, since I've had to resort to
> `unsafePerformIO`.  Any hints are welcome.
>
> What I have so far is:
>
>foo = let
>_tell i = do
>a <- return $ unsafePerformIO $ sleep 1
>tell [a + 1 `seq` i]
>in do
>mapM_ _tell [1..10]
>
>main = do
>(_, ~res) <- runWriterT foo
>mapM_ print res
>
> Without the `seq` the call to sleep will simply be skipped (is there
> an easier way to force evaluation there?).  Without `unsafePerformIO`
> all the sleeping is done up front, and all numbers are print at once
> at the end.
>
> The goal is of course to use code along the same shape to do something
> more useful, and then `unsafePerformIO` will really be unsafe...


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://blog.ertes.de/


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


Re: [Haskell-cafe] ANN: Silkworm game

2009-05-03 Thread Duane Johnson
I'm not too much of an expert in Haskell, but I did notice that  
building the game required keeping track of a lot of state  
information, which was not very intuitive in Haskell (although the  
OpenGL state info is rather intuitive).  If I were to do it in Haskell  
again, I would try to learn more about FRP (Functional Reactive  
Programming) and see if that improves things in terms of state.


Ruby is my favorite imperative language, so I would certainly  
recommend it for game development.  It would likely be much easier in  
Ruby, but perhaps a little slower.  My experience with Ruby on Rails  
has been that it is always a little slower than I wish it were :)


-- Duane

On May 3, 2009, at 4:48 PM, Daryoush Mehrtash wrote:

I noticed that Chipmunk also has a Ruby interface.  Do you have any  
pro/con of implementing the game in Ruby vs Haskell?


Thanks,

Daryoush

On Sat, May 2, 2009 at 12:00 PM, Duane Johnson > wrote:

Reprinted from my blog post [1]:

===

The semester is over, my final project was a success (at least in  
that I passed the class) and it’s time now to release the game I  
made for Graphics 455: Silkworm!


This is my first full application in Haskell.  The process has been  
an enlarging experience–I’ve come to really enjoy the mental work  
that goes into thinking about a program in a functional way.  I  
highly recommend the challenge to other software engineers.


Silkworm combines the Hipmunk binding to Chipmunk 2D Game Dynamics  
with OpenGL, and GLFW (an alternative to GLUT).


It’s built to work on Mac OS X, but it uses cross-platform libraries  
so it should be fairly easy to port to other platforms.  The source  
code is here [2] and below are some screenshots [1]


-- Duane Johnson
(canadaduane)

===

[1] http://blog.inquirylabs.com/2009/05/02/silkworm-game-written-in-haskell/
[2] 
http://inquirylabs.com/downloads/Silkworm.tgz___
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] ANN: Silkworm game

2009-05-03 Thread Felipe Lessa
On Sun, May 03, 2009 at 02:07:23PM +0200, Sven Panne wrote:
> As a side note, I get a very bad feeling when Hipmunk gets compiled on my
> x86_64 box:
[...]
> This can't be correct, but I'll probably have to take a look at that. Or is it
> a know bug that Hipmunk ist not 64bit-clean?

My machine is actually a x86_64, and I don't like those warnings
as well :).  Yes, it is a know bug in Chipmunk, but maybe it got
corrected.  One small problem with Chipmunk is that it is a
moving target, so updating Hipmunk's Chipmunk library would
require changing the bindings as well (AFAIK at least the joints
code would have to be rewritten), and I don't know if there is a
changelog specifying the changes (i.e. "read the diff" :).

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


Re: [Haskell-cafe] ST.Lazy vs ST.Strict

2009-05-03 Thread Tobias Olausson
Would unsafeInterleaveST work just as unsafeInterleaveIO in the manner
that it returns immediately, and then is computed lazily?
The idea in the complete program is that one part representing
the CPU will produce a list lazily, which will then be consumed
lazily by another part of the program, which in turn will produce
a lazy list fed to the CPU.
I might add that there was a base case for foo. It turns out i omitted
that for "simplicity". Even stranger, if one adds the following

foo y = do
if something then return y
else do
   val <- foo (y+1)
   fail "this is a fail"

Will make the program fail with "this is a fail". Without the fail, that
does not return the computed y. How come?

//Tobias

2009/5/4 Ryan Ingram :
> So, I don't know what is causing your problem, but foo will not do
> what you want even with lazy ST.
>
> foo y = do
>   x <- something
>   xs <- foo (y+1)
>   return (x:xs)
>
> Desugaring:
>
> foo y = something >>= \x -> foo (y+1) >>= \xs -> return (x:xs)
> = something >>= \x -> something >>= \x2 -> foo (y+2) >>= \xs2 ->
> return (x2:xs2) >>= \xs -> return (x:xs)
> = something >>= \x -> something >>= \x2 -> foo (y+2) >>= \xs2 ->
> return (x:x2:xs2)
>
> You see that there is an infinite chain of "foo" calls; the lazy ST
> still needs to thread the state through that chain; so in the case of
> foo 0 >>= something_else, the state is _|_ for something_else and you
> will fail if you use read/write/newSTRef after that point.  In fact,
> I'm not sure that lazy ST is very useful :)
>
> My guess is that you want one of
> (1) mdo, when the effects in 'something' only matter once, or
> (2) unsafeInterleaveST, if you just want to be able to traverse the
> (x:xs) list lazily and the references it uses are dead after calling
> foo.
>
>  -- ryan
>
> On Sun, May 3, 2009 at 10:27 AM, Tobias Olausson  wrote:
>> Hello!
>> I have a program that is using ST.Strict, which works fine.
>> However, the program needs to be extended, and to do that,
>> lazy evaluation is needed. As a result of that, I have switched
>> to ST.Lazy to be able to do stuff like
>>
>> foo y = do
>>    x <- something
>>    xs <- foo (y+1)
>>    return (x:xs)
>>
>> However, when running the program compiled with ST.Lazy, the
>> following is outputted:
>>   [to...@wobsi]$ ./runnerLazy looper.hex
>>   runnerLazy: <>
>>
>> The very same program compiled with ST.Strict outputs:
>>   [to...@wobsi]$ ./runner looper.hex
>>   83298556
>>
>> The code that is actually computing stuff is this:
>> loopSys :: Int -> CPU s Int
>> loopSys cc = do
>>    instr <- fetch
>>    if instr == 0xEA --NOP
>>       then return cc
>>       else do
>>         c <- execute instr
>>         loopSys $! (cc+c)
>>
>> The CPU type looks as follows:
>>   type CPU s a = ReaderT (SysEnv s) (ST s) a
>>
>> The program is run like
>>   runReaderT (loopSys 0)
>> which in turn is being runST'd and then printed
>>
>> Does anyone know why the program just outputs <>
>> when compiled under ghc 6.10.2, and runs perfectly fine
>> under ghc 6.8.2? The program is compiled with --make and -O2
>>
>>
>> Tobias Olausson
>> tob...@gmail.com
>> ___
>> Haskell-Cafe mailing list
>> Haskell-Cafe@haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>



-- 
Tobias Olausson
tob...@gmail.com
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: StateT IO Action on `onKeyPress`

2009-05-03 Thread Andy Stewart
Ryan Ingram  writes:
Thank you very much!

I try to use your solution to fix my problem.

  -- Andy

> Hi Andy.
>
> The GTK bindings use IO for their callbacks, not any custom monad like
> your WindowListT.
>
> I suggest, instead of StateT s IO a, you use ReaderT (IORef s) IO a:
>
> putR :: s -> ReaderT (IORef s) IO ()
> putR s = do
> r <- ask
> liftIO $ writeIORef r s
>
> getR :: ReaderT (IORef s) IO s
> getR = ask >>= liftIO . readIORef
>
> Otherwise, there are techniques to use an IORef to hold onto the state
> while calling into IO (which might make callbacks), and then read it
> back out and put it in the state while running your action.  But it's
> simpler to just switch to ReaderT
>
>   -- ryan
>
> On Sun, May 3, 2009 at 8:27 AM, Andy Stewart  
> wrote:
>> Hi all,
>>
>> I have a function named `keymapTest` need moand state WindowListT, and
>> WindowListT is `type WindowListT = StateT WindowList IO`.
>>
>> when i add "(\event -> keymapTest winList event >> return False)" after
>> `onKeyPress` for handle key press event, i got GHC error:
>>
>> Manatee.hs:57:58:
>>    Couldn't match expected type `IO a'
>>           against inferred type `WindowListT Bool'
>>    In the first argument of `(>>)', namely `keymapTest winList event'
>>    In the expression: keymapTest winList event >> return False
>>    In the second argument of `onKeyPress', namely
>>        `(\ event -> keymapTest winList event >> return False)'
>>
>> So function `onKeyPress` just accept *one* IO-action?
>> Have a way to fix above problem?
>>
>> Any help?
>>
>> Thanks!
>>
>>  -- Andy
>>
>> Below is source code of Manatee.hs file.
>>
>> --> Manatee.hs start   
>> <--
>> module Main where
>>
>> import Text.Printf
>> import Data.Monoid
>> import Data.List
>> import Data.Maybe
>> import Control.Monad
>> import Control.Monad.State
>> import Control.Applicative
>>
>> import Data.IORef
>>
>> import Graphics.UI.Gtk hiding (Window, windowNew, get)
>> import Graphics.UI.Gtk.SourceView
>> import Graphics.UI.Gtk.Abstract.Widget
>>
>> import Manatee.Event
>> import Manatee.Buffer
>> import Manatee.WindowList
>> import Manatee.Pane
>> import Manatee.Statusbar
>> import Manatee.Utils
>> import Manatee.Window
>>
>> import qualified Data.Set as Set
>> import qualified Graphics.UI.Gtk.Windows.Window as W
>> import qualified Graphics.UI.Gtk.Gdk.Events as E
>>
>> main :: IO ()
>> main = do
>>  -- Init.
>>  initGUI
>>
>>  -- Root frame.
>>  rootFrame <- W.windowNew
>>  rootFrame `onDestroy` mainQuit  -- quit main loop when root frame close
>>
>>  -- Root frame status.
>>  windowFullscreen rootFrame   -- fullscreen
>>
>>  -- Windows list.
>>  let windowsList = WindowList 0 Set.empty
>>
>>  evalStateT (do
>>               -- Window 1
>>               window1 <- windowNewWithBuffer DTop "test"
>>               liftIO $ containerAdd rootFrame $ windowPaned window1
>>
>>               (window2, window3) <- windowSplitVertically window1
>>
>>               (window4, window5) <- windowSplitHorizontally window3
>>
>>               winList <- windowListGetList
>>               liftIO $ rootFrame `onKeyPress` (\event -> keymapTest winList 
>> event >> return False)
>>
>>               -- Handle window content synchronous.
>>               windowHandleSynchronous
>>               ) windowsList
>>
>>  -- Loop
>>  widgetShowAll rootFrame       -- display all widget
>>  mainGUI
>>
>> keymapTest :: [Window] -> E.Event -> WindowListT Bool
>> keymapTest winList event = do
>>  window <- liftIO $ windowFindFocus winList
>>  case window of
>>    Just x -> handleKeyPress x event
>>    Nothing -> return False
>>
>> handleKeyPress :: Window -> E.Event -> WindowListT Bool
>> handleKeyPress window ev = do
>>  liftIO $
>>         case eventTransform ev of
>>           Nothing -> return False
>>           Just e -> do
>>             let display = statusbarOutputSubitemSetText $ paneStatusbar $ 
>> windowPane $ window
>>                 eventName = eventGetName e
>>             case eventName of
>>                -- Window commands.
>>                "M-t" -> display "windowSplitVertically"
>>                -- "M-t" -> windowSplitVertically window >> return False
>>                _ -> display $ printf "%s undefined." eventName
>> --> Manatee.hs end   
>> <--
>>
>> ___
>> 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] ST.Lazy vs ST.Strict

2009-05-03 Thread David Menendez
On Sun, May 3, 2009 at 6:11 PM, Ryan Ingram  wrote:
> So, I don't know what is causing your problem, but foo will not do
> what you want even with lazy ST.

That depends on what he wants to do. As long as nothing subsequent to
the call to foo tries to read a reference, then foo is fine.

For example, this works fine:

bar r = do
x <- readSTRef r
writeSTRef $! x + 1
return x

> take 10 $ runST (newSTRef 0 >>= \r -> sequence (repeat (bar r)))
[0,1,2,3,4,5,6,7,8,9]

>> Does anyone know why the program just outputs <>
>> when compiled under ghc 6.10.2, and runs perfectly fine
>> under ghc 6.8.2? The program is compiled with --make and -O2

As I understand it, you get <> when the RTS detects a thunk
which depends on itself, which can happen if you're trying to do a
strict computation lazily.

I can't imagine why different versions of GHC would give different
results, though.

-- 
Dave Menendez 

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


Re: [Haskell-cafe] ST.Lazy vs ST.Strict

2009-05-03 Thread David Menendez
On Sun, May 3, 2009 at 7:54 PM, Tobias Olausson  wrote:
> Would unsafeInterleaveST work just as unsafeInterleaveIO in the manner
> that it returns immediately, and then is computed lazily?
> The idea in the complete program is that one part representing
> the CPU will produce a list lazily, which will then be consumed
> lazily by another part of the program, which in turn will produce
> a lazy list fed to the CPU.

Like co-routines? It might be possible to do this with non-strict ST,
but it feels like it would be tricky to get right.

How are you feeding the lists from the two computations together? Are
they computed in the same ST thread?

> I might add that there was a base case for foo. It turns out i omitted
> that for "simplicity". Even stranger, if one adds the following
>
> foo y = do
>    if something then return y
>    else do
>       val <- foo (y+1)
>       fail "this is a fail"
>
> Will make the program fail with "this is a fail". Without the fail, that
> does not return the computed y. How come?

In the non-strict ST monad, the recursive call to foo does not get
evaluated until forced by an evaluation of val. That particular
version would work identically if you replaced the recursive call to
foo with "undefined".

-- 
Dave Menendez 

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


Re: [Haskell-cafe] converting IOException to Either in ErrorT

2009-05-03 Thread David Menendez
On Sun, May 3, 2009 at 6:36 PM,   wrote:
> I wrote this to make it a little nicer to catch IO exceptions and
> convert them to ErrorT failure:
>
> onExceptionThrowError
>  :: (Error ce) =>
>     IO a
>  -> (String -> ce)
>  -> ErrorT ce IO a
> onExceptionThrowError a ce =
>    liftIO (try a) >>=
>      either
>        (\(e :: IOException) -> throwError (ce (show e)))
>        return
>
> So now I can do, e.g.,
> writeFile fp s `onExceptionThrowError` SpecificErrorConstructor ...
>
> It works, but seems bad. Please let me know if you see ways to improve
> it. Thanks.

What about this?

liftCatch :: (IOException -> e) -> IO a -> ErrorT e IO a
liftCatch f m = ErrorT $ liftM (either (Left . f) Right) (try m)

-- 
Dave Menendez 

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