Re: [Haskell-cafe] same function's type accepted in top level, but rejected in where clause

2013-07-08 Thread Chaddaï Fouché
On Fri, Jul 5, 2013 at 11:03 PM, Ömer Sinan Ağacan omeraga...@gmail.comwrote:

 There's an implicit quantifier in type of `f`, like this: `f :: forall
 a. a - ListF a a`. When I add `ScopedTypeVariables` and `forall a.
 ...` in top level definition, it's like all `a`s in scope of top level
 definition are same, except when explicitly defined as `forall a.
 ...`.

 Is my intuition correct?


Yes it is ! :)
ScopedTypeVariables is a very nice and non problematic extension, it may
even be made part of some future Haskell standard.
-- 
Jedaï
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] ln, e

2013-01-05 Thread Chaddaï Fouché
log and (exp 1) are the natural logarithm and e.
--
Jedaï

On Sun, Jan 6, 2013 at 2:03 AM, Christopher Howard
christopher.how...@frigidcode.com wrote:
 Hi. Are natural log and Euler's constant defined somewhere in base, or a
 convenience math module somewhere? I'm having trouble finding them with
 hayoo or system documentation.

 --
 frigidcode.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] Substituting values

2012-12-26 Thread Chaddaï Fouché
Since recently, the notion of prisms from the lens library can achieve
that : to modify a value only in certain conditions but you have to
write the prism so it's not that convenient, though at least you'll
have an uniform API.
See 
http://hackage.haskell.org/packages/archive/lens/3.7.0.2/doc/html/Control-Lens-Prism.html
, especially the nat example.

--
Jedaï

On Fri, Dec 21, 2012 at 6:46 PM, Radical radi...@google.com wrote:
 Sometimes I'll need something like:

   if value == Foo then Bar else value

 Or some syntactic variation thereof:

   case value of { Foo - Bar; _ - value }

 Is there a better/shorter way to do it? I'm surprised that it's more
 complicated to substitute a value on its own than e.g. in a list, using
 filter. Or perhaps I'm missing the right abstraction?

 Thanks,

 Alvaro




 ___
 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] Segment Tree based Set

2012-10-29 Thread Chaddaï Fouché
On Mon, Oct 29, 2012 at 9:43 AM, Tony Morris tonymor...@gmail.com wrote:
 It is not a Set, but a Map. Of course, I could use it to implement the
 function I need with something like: type SSet a = STree [()] a, but
 then I'd have to unnecessarily go beyond Haskell98.


Couldn't you just use :

 instance Measured (Interval a) Bool where
   measure _ = True

Then the normal functions of SegmentTree would do what you wish for,
no ? You don't need much beyond Haskell 98 (MPTC is used everywhere
already).

-- 
Jedaï

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


Re: [Haskell-cafe] A clarification about what happens under the hood with foldMap

2012-10-23 Thread Chaddaï Fouché
Le 23 oct. 2012 09:54, Alfredo Di Napoli alfredo.dinap...@gmail.com a
écrit :

 What this code does is straighforward. I was struck from the following
sentences in LYAH:

 Notice that we didn't have to provide the function that takes a value
and returns a monoid value.
 We receive that function as a parameter to foldMap and all we have to
decide is where to apply
 that function and how to join up the resulting monoids from it.


 This is obvious, so in case of (+) f = Sum, so f 3 = Sum 3 which is a
Monoid.
 What I was wondering about is how Haskell knows that it has to pass, for
example, Sum in case of (+) and Product in case of (*)?
 In other term, I'm missing the logical piece of the puzzle which maps the
associative binary function passed to fold up to our foldMap declaration.

Haskell doesn't know it has to pass anything ! As the doc said, this is a
parameter to foldmap : so you would use treeSum = foldmap Sum for
example. And the binary function associed would be determined by the monoid
instance. Here that would be (+) because that's what mappend is for Sum (or
rather for Sum Int).

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


Re: [Haskell-cafe] A clarification about what happens under the hood with foldMap

2012-10-23 Thread Chaddaï Fouché
On Tue, Oct 23, 2012 at 10:36 AM, Alfredo Di Napoli
alfredo.dinap...@gmail.com wrote:
 I'm sure I'm missing a point, but the minimum definition for a Foldable
 instance is given in terms of foldMap, so I get the cake for free, foldr
 included, right?
 In the example I have defined my treeSum as:

 treeSum = Data.Foldable.foldr (+) 0

 So the only thing Haskell knows it that I want to fold over a Foldable for
 which foldMap (and therefore foldr) is defined, and specifically I want to
 fold using (+) as function.
 But foldMap is defined in terms of f, which in this case is Sum, because I
 want to sum things. It it were (*) f would have been Product, right?

 So what I'm missing is the relation between foldMap and foldr, aka How
 Haskell infer from (+) that I want f = Sum and not something different?

 I hope to have been clearer, don't know if I'm missing something crucial,
 though :)

Sorry I misunderstood your first post. The answer is that Haskell
doesn't have oracular powers so it doesn't know to choose Sum if you
pass (+) and Product if you pass (*), it especially doesn't have one
kind of monoid for each possible operation you can pass to foldr... So
it proceeds otherwise. It's pretty obvious that foldr can be
implemented with foldMap, if only by using the list monoid then using
the list foldr but the actual default implementation does it more
efficiently and more directly instead, you should probably look at the
source ( 
http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.6.0.0/src/Data-Foldable.html
) then investigate the endomorphism monoid.

-- 
Jedaï

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


Re: [Haskell-cafe] Interleaving type variables in a tree, has this a name ?

2012-10-13 Thread Chaddaï Fouché
On Sat, Oct 13, 2012 at 11:25 PM, Eric Dedieu papa.e...@free.fr wrote:
 Hi Haskell Helpers,

 For a hobby projet to learn haskell, I'm trying to use this kind of 
 interleaved
 tree structure (simplified):

 data ANode a b = ANode a [BNode a b] [BNode a b]
 data BNode a b = BNode b [ANode a b] [ANode a b]

Wouldn't the following work as well and be simpler to handle :

 data Node a b = Node a [Node b a] [Node b a]

-- 
Jedaï

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


Re: [Haskell-cafe] Ordering of BigFloats in numbers-3000.0.0.0

2012-10-09 Thread Chaddaï Fouché
On Sun, Oct 7, 2012 at 8:00 PM, Michael Orlitzky mich...@orlitzky.com wrote:
 I'm trying to use,

   http://hackage.haskell.org/package/numbers-3000.0.0.0

 to get better precision for free out of some numerical code. I ran
 into an issue pretty quickly, though. In Data.Number.BigFloat, we have,

   data BigFloat e = BF (Fixed e) Integer
 deriving (Eq, Ord)

 and the derived Ord is obviously incorrect:

   Prelude Data.Number.BigFloat let x = 0.1 :: BigFloat Prec50
   Prelude Data.Number.BigFloat let y = 0.02 :: BigFloat Prec50
   Prelude Data.Number.BigFloat x  y
   True


That's pretty strange since the derived Ord should be the same as
Fixed Ord, which itself is just a newtype over Rational (and 1%10 
2%100 == False), did you try to convert those BigFloat back to
Rational to see if they're still correct ? That may be worse than a
misbehaving Ord instance.

-- 
Jedaï

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


Re: [Haskell-cafe] Creating Repa arrays from unboxed vectors

2012-10-04 Thread Chaddaï Fouché
On Thu, Oct 4, 2012 at 1:58 PM, Janek S. fremenz...@poczta.onet.pl wrote:
 Thanks!

 This makes it look like you've got two versions of vector installed,
 This is true, I have vector-0.9.1 and vector-0.10, but
 with Repa built against the version that _isn't_ 0.9.1.

No, no, Repa is build against 0.9.1 since vector-0.9.1 appears in the
_expected_ type, that is the type expected by the environment of the
expression, here that's fromUnboxed which comes from the Repa library,
so if it's waiting for a Vector from vector-0.9.1 it means Repa is
built against this version (which is not the latest on your computer
thus the problem).

-- 
Jedaï

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


Re: [Haskell-cafe] foldl vs. foldr

2012-09-18 Thread Chaddaï Fouché

 18.09.2012, 16:32, Jan Stolarek jan.stola...@p.lodz.pl:
  Hi list,
 
  I have yet another question about folds. Reading here and there I
 encountered statements that
  foldr is more important than foldl, e.g. in this post on the list:
  http://www.haskell.org/pipermail/haskell-cafe/2012-May/101338.html
  I want to know are such statements correct and, if so, why? I am aware
 that foldl' can in some
  circumstances operate in constant space, while foldr can operate on
 infinite lists if the folding
  function is lazy in the second parameter. Is there more to this subject?


Basically the difference is that foldr is really the natural catamorphism
for the list type, that is for a type like :

data MyType = Zero | One A | Two B C | Recurse MyType

the natural catamorphism is a function that takes four arguments by which
it will replace the four constructor so as to deconstruct a value of MyType
:

myTypeCata :: r - (A - r) - (B - C - r) - (r - r)   -(MyType -
r)
myTypeCata z o t re Zero = z
myTypeCata z o t re (One a) = o a
myTypeCata z o t re (Two b c) = t b c
myTypeCata z o t re (Recurse myType) = re (myTypeCata z o t re myType)

So foldr is the natural catamorphism for the list type and foldl is not.

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


Re: [Haskell-cafe] how to check thunk

2012-07-02 Thread Chaddaï Fouché
On Mon, Jul 2, 2012 at 7:54 AM, Kazu Yamamoto k...@iij.ad.jp wrote:
 Hello,

 vacuum allow that and much more though I don't know if it still works
 correctly on GHC 7.4. Anyway your isThunk is

 isThunk a = fmap GHC.Vacuum.ClosureType.isThunk GHC.Vacuum.closureType

 Great. I confirmed that this works with GHC 7.4.

Good news !

 # I removed the a parameter.

Yes, I wrote an horrible hybrid of two idea and the result don't
work... I suppose you got something like that :

 isThunk :: a - IO Bool
 isThunk = fmap VC.isThunk . closureType

-- 
Jedaï

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


Re: [Haskell-cafe] package to expand TH macros?

2012-07-01 Thread Chaddaï Fouché
On Sun, Jul 1, 2012 at 8:11 PM, Brandon Allbery allber...@gmail.com wrote:
 On Sun, Jul 1, 2012 at 1:56 PM, Eric devnull1...@yahoo.com wrote:

 I seem to remember finding a package a few days ago that would take
 Haskell source with TH, then run and expand the TH macros in-place to
 produce equivalent, TH-free Haskell source.


 It is kinda hard to find for some reason...  you're looking for ZeroTH,
 http://hackage.haskell.org/package/zeroth


It should probably at least have the Template Haskell category...

-- 
Jedaï

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


Re: [Haskell-cafe] how to check thunk

2012-07-01 Thread Chaddaï Fouché
On Mon, Jul 2, 2012 at 5:29 AM, Kazu Yamamoto k...@iij.ad.jp wrote:
 Hello,

 Are there any ways to see if a value is a thunk or memorized?
 I would like to have a function like:
   isThunk :: a - IO Bool


vacuum allow that and much more though I don't know if it still works
correctly on GHC 7.4. Anyway your isThunk is

 isThunk a = fmap GHC.Vacuum.ClosureType.isThunk GHC.Vacuum.closureType

(Feel free to arrange your imports to make that a bit more readable ;)

http://hackage.haskell.org/package/vacuum
-- 
Jedaï

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


Re: [Haskell-cafe] Understanding GC time

2012-03-10 Thread Chaddaï Fouché
On Sat, Mar 10, 2012 at 4:21 PM, Thiago Negri evoh...@gmail.com wrote:
 c:\tmp\hspar +RTS -s -N1
 par +RTS -s -N1
 2000
     803,186,152 bytes allocated in the heap
     859,916,960 bytes copied during GC
     233,465,740 bytes maximum residency (10 sample(s))
      30,065,860 bytes maximum slop
             483 MB total memory in use (0 MB lost due to fragmentation)

  Generation 0:  1523 collections,     0 parallel,  0.80s,  0.75s elapsed
  Generation 1:    10 collections,     0 parallel,  0.83s,  0.99s elapsed

  Parallel GC work balance: nan (0 / 0, ideal 1)

 c:\tmp\hspar +RTS -s -N2
 par +RTS -s -N2
 2000
   1,606,279,644 bytes allocated in the heap
          74,924 bytes copied during GC
          28,340 bytes maximum residency (1 sample(s))
          29,004 bytes maximum slop
               2 MB total memory in use (0 MB lost due to fragmentation)

  Generation 0:  1566 collections,  1565 parallel,  0.00s,  0.01s elapsed
  Generation 1:     1 collections,     1 parallel,  0.00s,  0.00s elapsed

  Parallel GC work balance: 1.78 (15495 / 8703, ideal 2)

An important part of what happened is explained by this :
-N1
 483 MB total memory in use (0 MB lost due to fragmentation)

-N2
   2 MB total memory in use (0 MB lost due to fragmentation)

Thing is, in the first version, the list had to be present in memory
completely because you had two traversals and so the head was retained
during the first traversal so that the second traversal could work on
the same list. In the version where both traversals were done in
parallel, the list was produced and consumed in constant memory, since
both folds could progress simultaneously. So the memory use was much
simpler and smaller, which must explain in part why the collections
were so much faster (apparently there was still 0.01s elapsed for the
generation 0 collections).

-- 
Jedaï

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


Re: [Haskell-cafe] named pipe interface

2012-01-13 Thread Chaddaï Fouché
On Thu, Jan 12, 2012 at 7:53 PM, Serge D. Mechveliani mech...@botik.ru wrote:
 People,

 (I wonder: is this for  beginn...@haskell.org ?)

I don't think so.


 I need to organize a  string interface  for a Haskell function
 Main.axiom  and a C program
                            fifoFromA.c

 via a pair of  named pipes  (in Linux, UNIX).
 The pipes are created before running, by the commands    mkfifo toA
                                                         mkfifo fromA

 Main.axiom  outputs a  string  to  toA  and inputs the respond string
                                            from  fromA  as the result.
 fifoFromA  inputs a string from  toA,
           converts it to the string  resStr,  outputs resStr to  fromA.

 Now that seems interesting, but just to be clear : did you choose
this solution (and why won't you use the FFI instead) or is this just
to see how to work it out ?

-- 
Jedaï

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


Re: [Haskell-cafe] lists of arbitrary depth

2010-07-14 Thread Chaddaï Fouché
On Tue, Jul 13, 2010 at 11:28 AM, Shlomi Vaknin shlomivak...@gmail.com wrote:
 Thank you Bob,
 your example clarified how actually using such data type would appear in
 haskell. I naively thought it would be as simple as defining a regular list,
 but i see it is slightly more strict than that. I appreciate your help!
 Vadali

Well it _is_ as simple as defining a regular list, which would be
(fictionally since (:) and [] are reserved identifiers) :

 data [] a = [] | a : [a]

Which is the same as :

 data List a = Empty | Cons a (List a)

You can then handle lists with pattern matching :

 map f [] = []
 map f (x:xs) = f x : map f xs

Or for our List type :

 map f Empty = Empty
 map f (Cons x xs) = Cons (f x) (map f xs)


His definition of a tree :

 data Tree a = Leaf | Branch a [Tree a]

follows the same idea and is as easy to handle with pattern matching :

 treeMap f Leaf = Leaf
 treeMap f (Branch x xs) = Branch (f x) (map (treeMap f) xs)


As you see, an user defined type is manipulated with the same
mechanism as a primitive type, this uniformity is part of the power
of Haskell in that it encourages users to create their types and
allows seamless integration of external library types with primitive
types.

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


Re: [Haskell-cafe] Wire GUI

2010-06-03 Thread Chaddaï Fouché
On Wed, Jun 2, 2010 at 5:29 PM, Andrew Coppin
andrewcop...@btinternet.com wrote:
 Thanks to the people who replied about this.

 I would also like to thank my ISP for classifying the entire lot as spam and
 not showing it to me. *sigh*

 Blobs sounds interesting, but seems to require wxHaskell rather than Gtk2hs.
 I may be able to use some of the ideas from it though. I haven't had time to
 watch the YouTube video with sound yet.

I note that Sifflet allows you to connect a function to its arguments
and use gtk2hs. It then allows you to move the function and args
around and even rearrange them automatically, so it seems relevant to
your need.

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


Re: [Haskell-cafe] Strange random choice algorithm

2010-01-31 Thread Chaddaï Fouché
On Sat, Jan 30, 2010 at 9:38 PM, Daniel Fischer
daniel.is.fisc...@web.de wrote:
 Also, is there a more direct way of printing an array?

 Sure,

 printing immutable arrays:

 print arr ~ array (lo,hi) [(lo,arr!lo), ... , (hi,arr!hi)]
 print (assocs arr) ~ [(lo,arr!lo), ... , (hi,arr!hi)]
 print (elems arr) ~ [(arr!lo), ... , (arr!hi)]

Those are all fine.


 printing IO[U]Arrays:

 do immArr - freeze arr
   print (immArr :: [U]Array ix el)

 do ass - getAssocs arr
   print ass

 (getAssocs arr = print)

 do els - getElems arr
   print els


On the other hand, all those suggestions have a severe efficiency
problem due to the IO Monad strictness, for instance (getAssocs arr
= print) will start by creating the whole list of associations
before printing any of it.

More efficient and still better than the initial code would be :

mapM_ (readArray arr = print) [1..9]

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


Re: [Haskell-cafe] Having a look at XMonad window manager

2010-01-19 Thread Chaddaï Fouché
On Mon, Jan 18, 2010 at 11:53 PM, John Millikin jmilli...@gmail.com wrote:
 I've been quite happy with Ubuntu's xmonad package, though I run it
 within a GNOME session.

 Have you tried the instructions on the XMonad wiki for inter-operating
 with GNOME? http://www.haskell.org/haskellwiki/Xmonad/Using_xmonad_in_Gnome

I myself had such a setup (Xmonad in Gnome on Ubuntu) for more than a
year and it really was quite good. XMonad works quite well with
KDE/Gnome or other major desktops.

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


Re: [Haskell-cafe] pointfree-trouble

2009-12-22 Thread Chaddaï Fouché
On Tue, Dec 22, 2009 at 3:09 PM, slemi 0sle...@gmail.com wrote:
 this works fine, but if i leave the 'a' in the last function's definition
 like this:
 reMatr = Matr . (flip (.) unMatr)

The correct point free version would be :

 reMatr = (Matr .) . (. unMatr)

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


Re: [Haskell-cafe] How can i set the seed random number generator ?

2009-12-22 Thread Chaddaï Fouché
On Tue, Dec 22, 2009 at 1:16 PM, Scott Turner 1hask...@pkturner.org wrote:
 In haskell, i just use the following function to get the random number. It
 seems i donot need to set the seed of random number generator manually?

 rollDice ::  Int - IO Int
 rollDice n = randomRIO(1,n)

 That's correct.   randomRIO uses the global random number generator which is
 automatically initialized with a different seed each time your program starts
 up.

but you can always change it with setStdGen, and you can always use
the non-IO part of System.Random (or even another random library
altogether, particularly recommended if you need performances as
System.Random is more than slow).

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


Re: [Haskell-cafe] Haskell-Newbie and Char-Function

2009-12-06 Thread Chaddaï Fouché
On Sat, Dec 5, 2009 at 10:02 PM, ??? ?? m...@rkit.pp.ru wrote:
 fct a n = (snd $ break (==a) ['a'..'z']) !! n

Not bad but you forgot that it might need to wrap around, besides
break isn't really the best function to use here since we don't need
the first part of the pair :

 shift n ch = dropWhile (/=ch) (cycle ['a'..'z']) !! n

Still I think the ord and mod version will be faster.

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


Re: [Haskell-cafe] Haskell-Newbie and Char-Function

2009-12-05 Thread Chaddaï Fouché
On Sat, Dec 5, 2009 at 4:48 PM, Jochem Berndsen joc...@functor.nl wrote:
 MeAdAstra wrote:
 Hi guys,
 I only started learning Haskell some days ago. Maybe one of you can give me
 a hint on how to implement a function that needs a character in the range
 (a,b,...z) and an integer number k and returns the k-next neighbor of the
 character? For example, fct a 5 would result in output f.

 You might want to use the functions ord and chr from Data.Char, and the
 mod function from the Prelude.

Right, and by the way I would suggest you reverse the parameter order
of your function so that it takes the shift first, then you can write
:

 shift :: Int - Char - Char
 shift n c ...

 rot13 :: String - String
 rot13 = map (shift 13)

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


Re: [Haskell-cafe] Area from [(x,y)] using foldl

2009-11-09 Thread Chaddaï Fouché
On Sun, Nov 8, 2009 at 10:30 PM, michael rice nowg...@yahoo.com wrote:

 This doesn't.

 area :: [(Double,Double)] - Double
 area p = abs $ (/2) $ area' (last p):p

  where area' [] = 0
area' ((x0,y0),(x,y):ps) = ((x0-x)*(y0+y)) + area'
 (x,y):ps


This function is almost correct except you got your priorities wrong :
application priority is always stronger than any operator's so area' (last
p):p is read as (area' (last p)) : p... Besides your second pattern is
also wrong, the correct code is :

area :: [(Double,Double)] - Double
area p = abs $ (/2) $ area' (last p : p)
 where area' ((x0,y0):(x,y):ps) = ((x0-x)*(y0+y)) + area' (x,y):ps
  area' _ = 0

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


Re: [Haskell-cafe] Area from [(x,y)] using foldl

2009-11-08 Thread Chaddaï Fouché
On Sun, Nov 8, 2009 at 9:04 PM, michael rice nowg...@yahoo.com wrote:

 Of course! Back to the drawing board.


If I understand the problem correctly, I'm not convinced that foldl is the
right approach (nevermind that foldl is almost never what you want, foldl'
and foldr being the correct choice almost always). My proposition would be
the following :

 area ps = abs . (/2) . sum $ zipWith (\(x,y) (x',y') - (x - x') * (y +
y')) ps (tail $ cycle ps)

I think it express the algorithm more clearly.

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


Re: [Haskell-cafe] help with Haskell performance

2009-11-07 Thread Chaddaï Fouché
2009/11/7 Eugene Kirpichov ekirpic...@gmail.com:
 Ah, you're right. Then we need a foldl' insertWith with a strict plus.

We only need a foldl' insertWith' : (+) is already strict for all
the numeric types in the Prelude.

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


Re: [Haskell-cafe] AND/OR Perceptron

2009-10-31 Thread Chaddaï Fouché
On Sat, Oct 31, 2009 at 8:09 PM, Ryan Ingram ryani.s...@gmail.com wrote:

 where is just syntactic sugar for let...in

That's not perfectly true : where and let...in don't attach to the
same syntactic construction, where attaches to a definition and can
works for several guard clauses whereas let ... in expression is an
expression in itself.

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


Re: [Haskell-cafe] How come pattern match does not recognize this code style?

2009-10-26 Thread Chaddaï Fouché
On Mon, Oct 26, 2009 at 6:08 AM, Magicloud Magiclouds
magicloud.magiclo...@gmail.com wrote:
 Ah, I see.
 The reason I have to use my style is the same as others: the list is
 too long

You don't have to put everything on the same line, you just have to
indent the rest of the pattern a bit more than the first line :

 case bala of
  [ bala
, bala
, bala ] - bala bala bala
  _ - bala

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


Re: [Haskell-cafe] Need feedback on my Haskell code

2009-07-31 Thread Chaddaï Fouché
On Fri, Jul 31, 2009 at 2:12 PM, CK Kashyapck_kash...@yahoo.com wrote:
 I personally find
 map maySwitch (unfoldr go (x1,y1,0)) and map maySwitch $ unfoldr go
 (x1,y1,0) more intuitive.

 I can read it as map the maySwitch function over the list generated from the
 unfolding.

 Is there any difference in the evaluation steps between the composition
 version and the non-composition version?

There may be small differences in the result after optimisation but
you shouldn't worry about that right now (if you really want to
optimise this function, the first thing to do is to change your data
structure for points : a strict pair of int (data Point = P !Int !Int)
could easily go more than 5 times faster, I tried).

I tend to always use this map maySwitch . unfoldr go $ (x1,y1,0)
style since I like to see things in terms of functions compositions
and it is easier to refactor this kind of code with copy and paste
(not really relevant here but for longer chains it's interesting).

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


Re: [Haskell-cafe] Need feedback on my Haskell code

2009-07-28 Thread Chaddaï Fouché
On Tue, Jul 28, 2009 at 3:04 PM, CK Kashyapck_kash...@yahoo.com wrote:
 Hi Everyone,
 I managed to write up the line drawing function using the following links -
 http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html
 http://rosettacode.org/wiki/Bresenham%27s_line_algorithm#Haskell


I tried to simplify your function a little bit :

line :: Point - Point - [Point]
line pa@(xa,ya) pb@(xb,yb) = map maySwitch . unfoldr go $ (x1,y1,0)
  where
steep = abs (yb - ya)  abs (xb - xa)
maySwitch = if steep then (\(x,y) - (y,x)) else id
[(x1,y1),(x2,y2)] = sort [maySwitch pa, maySwitch pb]
deltax = x2 - x1
deltay = abs (y2 - y1)
ystep = if y1  y2 then 1 else -1
go (xTemp, yTemp, error)
| xTemp  x2 = Nothing
| otherwise  = Just ((xTemp, yTemp), (xTemp + 1, newY, newError))
where
  tempError = error + deltay
  (newY, newError) = if (2*tempError) = deltax
 then (yTemp+ystep,tempError-deltax)
 else (yTemp,tempError)

I think it will be a bit better, tell me what you think ?

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


Re: [Haskell-cafe] Are GADTs what I need?

2009-07-13 Thread Chaddaï Fouché
On Mon, Jul 13, 2009 at 12:41 PM, Kev
Mahoneymaill...@kevinmahoney.co.uk wrote:
 So far, I've learnt you can do this:

 data Value where
 VInt :: Integer - Value
 ...
 VWrapper :: a - Value

 which can let you encode arbitrary 'dynamic' types into Value. I was
 hoping to be able to pattern match to get the value out again e.g.

As such this type is pretty useless, since you don't know anything
about a, you can't do anything with it... Which is why you add
typeclass constraints, so you can use this value. Data.Dynamic adds a
Typeable constraint, which allows you to do safe coercing, so you can
have a typecase, if not like you tried to do it.

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


Re: [Haskell-cafe] Using Parsec with ByteString ?

2009-06-19 Thread Chaddaï Fouché
On Fri, Jun 19, 2009 at 1:51 PM, Fernandquarantedeu...@yahoo.fr wrote:
 but the parser one needs to write must parse ByteStrings instead of Strings
 (that is, something like having a Parsec Bytestring () type, unless I'm
 completely misunderstanding the situation). My problem is that I do not
 manage
 to write primitive parsing combinators (like string, satisfy, letter, etc.)
 to
 define my language's tokens.

Why would you want to do that ? After all, those combinators are
already available in Text.Parsec.Char : they work on any Stream
instance whose token type is Char, which happens to be the case for
ByteString.

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


Re: [Haskell-cafe] Re: curious about sum

2009-06-18 Thread Chaddaï Fouché
On Thu, Jun 18, 2009 at 6:38 PM, Alberto G. Coronaagocor...@gmail.com wrote:
 My question is: Why the process does not grow also in the lazy case and
 instead produces a stack overflow inmediately?

This question is answered in detail on the Wiki
http://www.haskell.org/haskellwiki/Foldr_Foldl_Foldl%27
As you thought, it is not the evaluation of foldl that overflow the
stack, since foldl is terminally recursive it won't ever overflow the
stack. On the other hand when the thunk created by foldl is finally
evaluated, if the argument function is strict in its first argument it
will overflow the stack if the list was too long.

It is important to note that the size of the list itself or even the
thunk doesn't guarantee a stack overflow : both of those structure are
in the heap and if the argument function can produce output before
evaluating its first argument, there will be no stack overflow,
whatever the size of the thunk.

As evidenced by this expression :
ghci take 10 . foldl (flip (:)) [] $ [1..100]
[100,99,98,97,96,95,94,93,92,91]

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


Re: [Haskell-cafe] force evaluation beginners question

2009-06-15 Thread Chaddaï Fouché
On Mon, Jun 15, 2009 at 6:46 PM, Nico Rollenro...@web.de wrote:
 Hi there

 I'm trying to compile a code snipped that i've go from a tutorial
 which uses the function force.
 But when I'm trying to compile it ghc reports an error that it
 couldn't find the defenition of force.
 my ghc verion is 6.10.3
 Did they moved force out of Prelude in one of the latest updates?


As far as I know there never was a force function in the Prelude of
Haskell98, this is probably a function specific to your tutorial, we
could help more if we had the code snippet or at least the tutorial
name.

By the way, while it's absolutely not inappropriate to post this
question here, there is a haskell-beginner mailing list more suited to
this kind of thing (you could also get very fast and friendly help
from the #haskell channel on irc.freenode.net).

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


Re: [Haskell-cafe] A small puzzle: inTwain as function of foldr

2009-06-04 Thread Chaddaï Fouché
On Thu, Jun 4, 2009 at 4:22 PM, Martijn van Steenbergen
mart...@van.steenbergen.nl wrote:
 Bonjour café,

 A small puzzle:

 Consider the function inTwain that splits a list of even length evenly into
 two sublists:

 inTwain Hello world!
 (Hello ,world!)

 Is it possible to implement inTwain such that the recursion is done by one
 of the standard list folds?

I don't think it is without a length before at least. On the other
hand if your specification is just splits a list of even length
evenly into two sublists, you can contrive something with a simple
foldr :

inTwain = foldr (\x ~(xs,ys) - (ys, x:xs)) ([],[])

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


Re: [Haskell-cafe] How to use Data.ByteString ?

2009-05-19 Thread Chaddaï Fouché
On Tue, May 19, 2009 at 8:46 AM, Brandon S. Allbery KF8NH
allb...@ece.cmu.edu wrote:
 On May 19, 2009, at 01:42 , Jason Dagit wrote:

 I've often seen this bit of scary code in VB:
 Dim i as Integer = 5
 If i = 5 Then
  ' Do something, because 5 = 5
 End If

 Sure, that works in Perl too.

That's because in numeric context Perl convert strings into numbers,
so even 5 == 5 coffees would be true... But 5 eq 5 coffees
wouldn't since eq force a string context and 5 is converted to 5
which is not string-equal to 5 coffees.
Perl is all about context and is coherent in its weirdness, whereas VB
is pretty screwed IIRC.

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


Re: [Haskell-cafe] Re: ANN: diagrams 0.2

2009-02-06 Thread Chaddaï Fouché
On Wed, Feb 4, 2009 at 4:56 PM, Gwern Branwen gwe...@gmail.com wrote:

 Now, to implement it, I would probably say to myself, well, we'll
 create a temporary file, we'll write some basic imports into it, then
 we'll write the user's expression into it as the definition of a
 function 'foo', and main will be defined as 'main = renderFile foo'.
 Then we use 'runhaskell' on the temporary file to create the picture,
 delete the temp file, and bob's your uncle.

 Except of course there's nothing to prevent DoS attacks or other
 exploits in the arbitrary code. So do we accept this and say that this
 is a plugin one uses at one's own risk?

Hackage contains some packages for that sandboxing, like mueval which
is now used by lambdabot on #haskell I believe.

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


Re: Re[2]: [Haskell-cafe] Climbing up the shootout...

2008-09-22 Thread Chaddaï Fouché
2008/9/23 Bulat Ziganshin [EMAIL PROTECTED]:
 http://haskell.org/haskellwiki/Wc seems to do fine; you'll notice
 it drops lines after the first version.

 actually it counts lines using built-in function. you may find that
 naive C is 6x fatser than naive Haskell and difference is so small
 only because C version is bound by memory speed

So what ? Strings represented as lists of character are slow ? What an
incredible discovery !

The ByteString versions are fast, ByteString was invented especially
for IO intensive situation, it's a library you can use pretty naively
and mostly get excellent performances, what exactly isn't Haskelly
enough for you in this solution ? The guts of ByteString aren't
idiomatic Haskell ? And ? The guts of the JVM aren't written in Java
either... At least ByteString was built over GHC which means it can be
done.

Besides a part of the performances of ByteString comes from stream
fusion and that's specifically something that is hard to achieve
outside of Haskell...


What exactly is your point ?
- Haskell is slow, we can't make it faster ? That's obviously false as
demonstrated by the progress in the latest years.
- Haskell is slower than C ? Well that's probably true, because C can
touch the hardware directly and can always optimize every single
aspects of a computation... On the other hand that kind of uber
optimization has a cost that I am unwilling to pay, I would rather
write high level Haskell and pay a little hit on execution time.
- We shouldn't center ourselves around performance to promote Haskell
(or should we even promote Haskell) ? Well there you may have a point,
but maybe it would be better to just make it and avoid denying other
peoples efforts to make Haskell faster ?

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


Re: Re[2]: [Haskell-cafe] Climbing up the shootout...

2008-09-22 Thread Chaddaï Fouché
2008/9/23 Bulat Ziganshin [EMAIL PROTECTED]:
 Hello Don,

 Tuesday, September 23, 2008, 4:22:19 AM, you wrote:

 bulat.ziganshin:
 when gcc developers will start to add to C libraries functions
 performing shootout benchmarks we will continue this discussion :D

 atoi(3).

 it isn't the same as readInt which was added specifically for this
 test. it doesn't support arbitrary-size streams and doesn't return
 rest of stream

Yeah, readInt has a more useful type than atoi() in most circumstances
so obviously it's a default which somehow disqualify this function...
(I wasn't aware that this function was only useful in the shoutout
context, I should probably scrap it from my other programs now)

I think we should write all the entries in Haskell98 and disable any
optimisation in GHC too, that would gives us a much fairer vision of
Haskell current performances.

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


Re: [Haskell-cafe] How to check if two Haskell files are the same?

2008-09-17 Thread Chaddaï Fouché
2008/9/16 Mauricio [EMAIL PROTECTED]:
 Hi,

 I would like to write a Haskell pretty-printer,
 using standard libraries for that. How can I
 check if the original and the pretty-printed
 versions are the same? For instance, is there
 a file generated by GHC at the compilation
 pipe that is always guaranteed to have the
 same MD5 hash when it comes from equivalent
 source?

There is not, though I have a suggestion :
Am I correct in assuming that you mean equivalent source in the
sense that only the formatting (and eventually {;} as a layout format
consequence) differs ?

Then the sequence of tokens from the source ought to do the trick as
long as you delete location information (map unLoc) and transform
ITvocurly (virtual braces for layout induced blocks) into ITocurly
(real braces for no-layout blocks) (and same for ITvccurly) (it's just
another map). If only the formatting differs, those two should be
identical.

Now the current GHC don't give you direct access to the Token stream
but the next release should contain the functions I wrote to support
this (for HaRe).

In fact you could do this with the AST but it would be more
complicated to do the necessary extractions and comparisons...

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


Re: [Haskell-cafe] Haskell Weekly News: Issue 85 - September 13, 2008

2008-09-14 Thread Chaddaï Fouché
2008/9/14 Rafael Almeida [EMAIL PROTECTED]:

 One thing have always bugged me: how do you prove that you have
 correctly proven something? I mean, when I write a code I'm formaly
 stating what I want to happen and bugs happen. If I try to prove some
 part of the code I write more formal text (which generally isn't even
 checked by a compiler); how can I be sure that my proof doesn't
 contain bugs? Why would it make my program less error-prone? Is there
 any article that tries to compare heavy testing with FM?

Well, that's where formal prover enter the picture : when you prove
something with Coq, you can be reasonably sure that your proof is
correct. And FM brings absolute certitude a propriety is verified by
your program whereas testing however heavy can only check this
property on a finite set of inputs (using randomly generated input
help alleviate the risk of blind spot but is still limited).

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


Re: [Haskell-cafe] Haskell as a scripting language.

2008-09-11 Thread Chaddaï Fouché
2008/9/10 David F. Place [EMAIL PROTECTED]:
 Hi, All.

 I needed to make a batch of edits to some input files after a big change
 in my program.  Normally, one would choose some scripting language, but
 I can't bear to program in them.   The nasty thing about using Haskell
 is that giving regexes as string constants sometime requires two levels
 of quoting.  For instance. (mkRegex ) matches \\.


Since some times in the HEAD and in the future 6.10 release, you can
use quasiquoting to get nice regex syntax as in Perl or Ruby.
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/regexqq

(quasiquoting basically allows you to embed a DSL with significantly
different syntax in your Haskell and still get typechecking and other
advantages at compile-time (though I imagine the errors won't be very
nice).

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


Re: [Haskell-cafe] monadic map on a Data.IntMap

2008-09-08 Thread Chaddaï Fouché
2008/9/8 minh thu [EMAIL PROTECTED]:
 Hi,

 Is there something like a fmapM_ ?
 In particular, I'd like to mapM_ a Data.IntMap instead of a List


The Traversable class (from Data.Traversable) includes a mapM function.
Unfortunately Data.IntMap don't provide an instance for IntMap (though
Data.Map does for Map).

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


Re: [Haskell-cafe] whatever happened to sendFile?

2008-08-13 Thread Chaddaï Fouché
2008/8/13 Jason Dusek [EMAIL PROTECTED]:
  I found an old lib for it:

http://www.haskell.org/ghc/docs/6.0/html/unix/System.Sendfile.html

  Hoogle turns up nothing, though.


That don't sound very useful... Maybe when we only had String it was
much more performant for big transfert, but now we can recode this in
one short line of ByteString code and get the same performance as C.

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


Re: [Haskell-cafe] whatever happened to sendFile?

2008-08-13 Thread Chaddaï Fouché
2008/8/13 Brandon S. Allbery KF8NH [EMAIL PROTECTED]:

 I should clarify:  what sendfile() is supposed to optimize isn't writing
 large strings, or even the user-kernel roundtrips; it's an optimization to
 the kernel network stack (network buffer management, to be specific).  Web
 servers use it to serve static content (e.g. icons, images, stylesheets)
 because it significantly reduces system load.


Ok, so it could still be useful in a restricted area (but then it
should be easy to write a FFI wrapper for it anyway).

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


Re: [Haskell-cafe] a really dumb posting question ;^(

2008-08-01 Thread Chaddaï Fouché
2008/7/31 Galchin, Vasili [EMAIL PROTECTED]:
 Hello,

 What do I do to do a followup haskell cafe posting? E.g. I want to put a
 posting on the category theory thread!

You respond to it. Sometimes it is not sufficient
(haskell-cafe@haskell.org isn't in the to or cc field), then you
probably have a respond to all action in your mailer, you should use
it. The best way is to use respond to list if you have it but
respond to all should work properly.

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


Re: [Haskell-cafe] Libevent FFI problems

2008-07-25 Thread Chaddaï Fouché
2008/7/25 Levi Greenspan [EMAIL PROTECTED]:
 Regarding your remark, that the RTS multiplexes IO already I am usure
 how this works out in practice. The reason I write this wrapper is
 that I want a network server which can handle thousands of
 connections. And to not require a permanent thread for each I would
 like to use something like select in C or Selector in Java. I haven't
 found something like this in Haskell, hence the libevent wrapper. If
 you have any information how to write something like this without this
 wrapper I would be more than happy.

The current model for concurrency in Haskell is m lightweight
user-threads working on n kernel threads, thus the threads are very
cheap in Haskell and take advantage of a multicore. I think with the
current model, the right choice for a web server in Haskell is a
multi-threaded model. As a point of comparison, I remember that blog
post which noted that its FastCGI was able to withstand more than 3000
hits by second (on a quite normal setting).

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


Re: [Haskell-cafe] Newbie: Replacing substring?

2008-07-23 Thread Chaddaï Fouché
2008/7/22 Ronald Guida [EMAIL PROTECTED]:
 2008/7/22 Dmitri O.Kondratiev [EMAIL PROTECTED]:
 On the side: The more I use Haskell - the more I like it ! It helps me think
 about the problem I solve much more clearly then when I use imperative
 language.

 If I want to replace a substring in a string, then I would search my
 string left to right, looking for any occurrence of the substring.  If
 I find such an occurrence, I would replace it and continue searching
 from immediately after the replacement.  This algorithm can be
 directly expressed in Haskell.  More efficient algorithms do exist.

Your idea but expressed in a more elegant fashion (maybe...) :
replace :: (Eq a) = [a] - [a] - [a] - [a]
replace _ _ [] = []
replace old new xs@(y:ys) =
  case stripPrefix old xs of
Nothing - y : replace old new ys
Just ys' - new ++ replace old new ys'

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


Re: [Haskell-cafe] Re: Optimizing 'sequence'

2008-07-22 Thread Chaddaï Fouché
2008/7/22 Luke Palmer [EMAIL PROTECTED]:
 A little formal reasoning reveals that sequence1 = sequence2 exactly
 when (=) is strict in its left argument.  There are four common
 monads which are _not_: Identity, Reader, Writer, State (and RWS by
 extension).

Still if that makes that much of a difference, maybe we could envision
putting a sequence' in the library ?

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


Re: [Haskell-cafe] Trouble with non-exhaustive patterns

2008-07-21 Thread Chaddaï Fouché
2008/7/21 Fernando Rodriguez [EMAIL PROTECTED]:
 Suddenly, the function stoped working with a rather cryptic (for a newbie at
 least) error message:

 *Temp final [4,5]

 interactive:1:9:
   No instance for (Num [a])
 arising from the literal `5' at interactive:1:9
   Possible fix: add an instance declaration for (Num [a])
   In the expr*Temp ession: 5
   In the first argument of `final', namely `[4, 5]'
   In the expression: final [4, 5]

 What have I done so wrong?

As Janis said final [] = [] conflicts with the signature you want for
final BUT the definition of final is still typeable, it just don't
have the type you think it have... and combined with the way Haskell
handle number literals you get this confusing error.

final's type is [[a]] - [a] because final [] = [] imply that the
return type should be a list and thus (final [a] = a) imply that the
input list elements should themselves be lists (because a should be a
list since it is returned in this case)...

So final wants a list of list. All is well until now and if you try
for example :
 final [True, False]
you'll get a slightly better error message (maybe hard for newbies but
very understandable with a little bit of experience) :
Couldn't match expected type `[a]' against inferred type `Bool'
In the expression: True
In the first argument of `final', namely `[True, False]'
In the expression: final [True, False]

Now your error message is particularly nasty because all integer
literals like 5 are treated in the following fashion in Haskell :
they're implicitly translated to fromInteger 5, fromInteger being a
function of the Num typeclass that for an instance Num a take an
Integer and translate it to the a type. This translation is normally
pretty obvious with some bound checking (for Int type) and simple
translation (to Double or Float) but you could in theory have some
pretty crazy instances of Num, [a] for example.
For example here Haskell complains that there are no Num instances for
the [a] type since that's what he want to translate the 5 into

I hope you understood this explanation, even if it's a little bit
crazy and blury for now, just remember, most of the time if you have
an error like :
No instance for (Num X)
   arising from the literal `5' at interactive:1:9
you can more or less translate it to :
Couldn't match expected type `X' against inferred type `Integer'

(This is only true for the Num typeclass, since this is the only case
of polymorphic literals you'll meet for now).

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


Re: [Haskell-cafe] Re: Help using Network.Curl

2008-07-21 Thread Chaddaï Fouché
2008/7/19 Jim Burton [EMAIL PROTECTED]:
 opts = [CurlEncoding text/xml
   , CurlHttpHeaders [X-EBAY-API-COMPATIBILITY-LEVEL=++compatLevel
 , X-EBAY-API-DEV-NAME=++devName
 , X-EBAY-API-APP-NAME=++appName
 , X-EBAY-API-CERT-NAME=++certName
 , X-EBAY-API-CALL-NAME=GeteBayOfficialTime
 , X-EBAY-API-SITEID=0]]

Isn't it : rather than = ? Just saying... (Don't know enough to be
sure I'm thinking about the right thing)

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


Re: [Haskell-cafe] ansi2html - one program, several issues

2008-07-19 Thread Chaddaï Fouché
2008/7/19 Krzysztof Skrzętnicki [EMAIL PROTECTED]:
 Hi all

 1) Profiling shows that very simple functions are source of great memory and
 time consumption. However, if I turn them off and simply print their input
 arguments instead, the overall time and memory consumption doesn't change.
 But now another function is acting badly. My guess: somehow the cost of
 Parsec code is shifted into whatever function is using it's output. Let's
 see:

Are you using Parsec to parse the whole file ? Then your problem is
there : Parsec needs to read and process the whole file before it can
give us any output since it thinks it could have to give us an error
instead and it can't be sure of that before he has read the whole
thing...
In your case, your problem is such that you would prefer to treat the
file as a stream, isn't it ?
There are some parser library that can give output lazily (look at
polyparse flavour), another option would be to only use Parsec where
you need it and just read and print the ordinary text for example.

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


Re: [Haskell-cafe] ansi2html - one program, several issues

2008-07-19 Thread Chaddaï Fouché
2008/7/19 Krzysztof Skrzętnicki [EMAIL PROTECTED]:
 I forgot to mention that the memory consumption is several times higher than
 file size. On 8,3 Mb file:
 532 MB total memory in use (4 MB lost due to fragmentation).

 Having that 8 Mb in memory is not the problem. 532 Mb is another story. In
 general, the program consumes roughly 64 times more memory than file size
 and it scales linearly.

You should be using ByteString, though this problem would be
alleviated if you were consuming the file as a stream.

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


Re: Re[2]: [Haskell-cafe] ansi2html - one program, several issues

2008-07-19 Thread Chaddaï Fouché
2008/7/20 Krzysztof Skrzętnicki [EMAIL PROTECTED]:
 On Sun, Jul 20, 2008 at 12:34 AM, Bulat Ziganshin
 [EMAIL PROTECTED] wrote:

 Hello Krzysztof,

 Sunday, July 20, 2008, 1:55:45 AM, you wrote:
  532 MB total memory in use (4 MB lost due to fragmentation).

 i think that Parsec library should hold entire file in memory only when
 you use 'try' for whole file. otherwise it should omit data as
 proceeded


 That's exactly what I thought. But even if I remove the only 'try' I use the
 memory consumption remains unchanged:

It's true, but in your case your output is almost the raw input data,
which means that even without a noxious try, you still have the
whole file in memory. Well hopefully not with your latest code, which
I would really like to see.

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


Re: [Haskell-cafe] Profiling nested case

2008-07-18 Thread Chaddaï Fouché
2008/7/12 Mitar [EMAIL PROTECTED]:
 So that I can easily change the type everywhere. But it would be much
 nicer to write:

 data Quaternion a = Q !a !a !a !a deriving (Eq,Show)

 Only the performance of Num instance functions of Quaternion is then
 quite worse.

You can probably use a specialization pragma to get around that.

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


Re: [Haskell-cafe] Newbie: Appending arrays?

2008-07-11 Thread Chaddaï Fouché
2008/7/11 Dmitri O.Kondratiev [EMAIL PROTECTED]:
 I don't quite understand how Data.Array.Diff work.
 I tried this:

 let arr = listArray (1,3) [1..3] :: DiffArray Int Int

 then:
 replaceDiffArray arr [(1, 777)]
 array (1,3) [(1,1),(2,777),(3,3)]
 Why when replacing first element the second one changes?

replaceDiffArray is low-level, nobody should use it, use the normal
IArray interface instead.
(To answer your question, replaceDiffArray works with low level index,
not the Ix class, all array are indexed by 0, 1, .. for it, so 1 is
the index of the second element of the array)

 and also trying to add 4-th element results in:
 Prelude Data.Array.Diff replaceDiffArray arr [(4, 444)]
 array (1,3) [(1,1),(2,2),(3,3)]

 It looks like replaceDiffArray can not be used to add new element to the end
 of array?

No, the size of a DiffArray can't be changed : DiffArray are just an
IArray instance with better performances for update than the classic
IArray instance (which copy the entire content on every update...).

There are some libraries that allows you to change the size of your
array, be aware though that this operation is very costly (in every
language).
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/ArrayRef

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


Re: [Haskell-cafe] Newbie: Appending arrays?

2008-07-11 Thread Chaddaï Fouché
2008/7/11 Dmitri O.Kondratiev [EMAIL PROTECTED]:
 How does Data.Sequence
 http://www.haskell.org/ghc/docs/latest/html/libraries/containers/Data-Sequence.html
 compares with ArrayRef for appending and accessing arrays efficiently ?

It doesn't since Data.Sequence doesn't use arrays, it uses a custom
data structure that allows to do plenty of operations with pretty good
asymptotic complexity. Be aware though that the constants aren't that
good and that depending on your usage, lists, array or another
specialized data structure could be much more efficient.

By comparisons, extending an array is very likely to be much more
expensive from time to time than adding some elements to your
Data.Sequence. On the other hand the random access would be much
faster in an array and even the amortized cost of extending the array
may not be much worse than the amortized cost on the Data.Sequence in
some cases.

What exactly are you trying to do ?

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


Re: [Haskell-cafe] Help with generalizing function

2008-06-25 Thread Chaddaï Fouché
2008/6/25 leledumbo [EMAIL PROTECTED]:

 Hi, I'm back. I have some troubles understanding your code:

  ( 1:1:? is never reached. )

Why would 1:1 be reached when we search for lists with a sum of 1 ??

Your derivation is incorrect, when you're reading a list comprehension
you must see it as imbricated loops, let's see a correct derivation :

findAllAns 2 1 == [ x:xs | x - [0..1], xs - findAllAns 1 (1 - x) ]
  == concat [ [ 0 : xs | xs - findAllAns 1 1 ], [ 1 : xs | xs -
findAllAns 1 0 ] ]

findAllAns 1 1 == [ concat [ [ [ 0 : xs | xs - findAllAns 0 1 ], [ 1
: xs | xs - findAllAns 0 0 ] ]
 == concat [ [], [[1]] ] == [ [ 1 ] ]

In reality, all list comprehensions are translated to ordinary
functions under the hood, the translation is the following :

[ x:xs | x - [0..s], xs - findAllAns (n - 1) (s - x) ] == concatMap
(x - concatMap (\xs - x : xs) (findAllAns (n - 1) (s - x)) [0..s]

which is then easier to derive.

 I don't understand why if the last element is [[]] then it's included in the
 result and not if it's [].

I think with the concatMap above you'll understand better what's going on :

concatMap (\xs - 0 : xs) (findAllAns 0 1) returns the empty list
because findAllAns 0 1 is an empty list while concatMap (\xs - 1 :
xs) (findAllAns 0 0) returns [[1]] because findAllAns 0 0 is a list
that contains the empty list.

But you don't have to think so hard to find the good response, just
think of the edge cases logically : faire une somme de 0 avec 0
éléments c'est possible (parce que 0 est l'élément neutre de
l'addition), tandis que faire une somme différente de 0 avec 0
éléments est impossible.
(The sum of 0 elements is always 0, and the product of 0 elements is always 1)

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


Re: [Haskell-cafe] message passing style like in Haskell?

2008-06-19 Thread Chaddaï Fouché
2008/6/19 jinjing [EMAIL PROTECTED]:
 encode xs = xs.group.map token where token x = (x.length, x.head)

Working in this direction is a question of taste, but the choice of
the dot for the operator is a pretty bad idea...

On the other hand, my favourite would be :

encode = map (length  head) . group

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


Re: [Haskell-cafe] Memory profiling

2008-06-16 Thread Chaddaï Fouché
2008/6/16 Pieter Laeremans [EMAIL PROTECTED]:
 Hi,

 Which tools do you recommand for memory profiling   haskell programs
 on a *nix system.
 I'm using haskell to develop a CGI program/script.

 The application has to be deployed on shared hosting infrastructure.
 Since I would like to be a good citizen ,
 I would need to meassure the maximum amount of memory allocated to the
 program during execution.
 The best I can do now is look at top but that 's not satisfactory.

Are you aware that by calling the program with the correct RTS options
you can have memory profiling ?
http://www.haskell.org/ghc/docs/latest/html/users_guide/runtime-control.html
(see option -s for simple statistics)
http://www.haskell.org/ghc/docs/latest/html/users_guide/prof-heap.html

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


Re: [Haskell-cafe] Re: [Haskell] ANN: random-access-list-0.1

2008-06-12 Thread Chaddaï Fouché
2008/6/12 Stephan Friedrichs [EMAIL PROTECTED]:
 For index, don't use Monad, use Maybe (I think that's what the recent
 [EMAIL PROTECTED] discussion concluded, in the context of switching
 Data.Map back to Maybe).

 I was just copying the idea from Data.Map and it's usually a good thing to
 have functions as general as possible, or why is it not?


Mainly because it's too easy to use them in a Monad that does not have
a meaningful fail(), like IO, many beginners do this error and are
surprised when their program is less robust that it should be. On the
other hand it's pretty easy to get an error out of a Maybe (for
example you can use fromMaybe with error() to get a meaningful error
output).

 Also, Data.List has genericLength etc, to

 At the moment, I'm using the Int type for size and indexing only for one
 reason: I haven't found a proper way to generalize it. I'd really like to
 use the Ix class, but it doesn't provide enough functionality, it only works
 on fixed-size intervals (i. e. for arrays, which don't change their size,
 but a list does). Maybe someone has an idea of how to realize lists with a
 variable starting index and size?

Given that this structure isn't lazy enough, I really don't see a
problem with using Int (any random access list with a size that needs
an Integer would blow the memory anyway...).

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


Re: [Haskell-cafe] Re: Damnit, we need a CPAN.

2008-05-30 Thread Chaddaï Fouché
2008/5/30 Achim Schneider [EMAIL PROTECTED]:
 I already was pleasantly surprised when discovering cabal-install, I
 think it deserves some more prominence, or even integration into cabal
 itself, to make everyone aware of the fact that there's such a thing as
 automatic installation and tempt people to make it work.

I completely agree, cabal-install is very nice, and should be more widely known.

 CPAN, of course, makes its life a lot easier by not caring about
 outside dependencies at all: You can install GTK bindings without
 having GTK installed... which of course does not work with Haskell, as
 things must be linked properly.

Not true : GTK bindings have a C part which needs to be properly
linked as well, you can't install them without GTK on your computer.
On the other hand, CPAN and the make-like tools in Perl are much more
mature and have more functionality than Cabal now (and they're a bit
of a mess too...) and there is often a external dependancies
downloader and installer integrated in packages.
Another truly important difference between CPAN and cabal-install is
the importance of the testing suite, all CPAN tools are geared so that
by default nothing is installed if it doesn't pass its test and
there's always a good number of tests. (So even when external
dependancies don't prevent the module from being build, it won't
install without forcing it)

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


Re: [Haskell-cafe] Re: Aren't type system extensions fun? [Further analysis]

2008-05-27 Thread Chaddaï Fouché
2008/5/27 Andrew Coppin [EMAIL PROTECTED]:
 Gleb Alexeyev wrote:

 foo :: (forall a . a - a) - (Bool, String)
 foo g = (g True, g bzzt)

 So, after an entire day of boggling my mind over this, I have brought it
 down to one simple example:

  (id 'J', id True)   -- Works perfectly.

  \f - (f 'J', f True)   -- Fails miserably.

 Both expressions are obviously perfectly type-safe, and yet the type checker
 stubbornly rejects the second example. Clearly this is a flaw in the type
 checker.

No, this is a flaw in the type inferencer, but simply adding the
Rank-N type to our type system makes type inference undecidable.

I would argue against the obviously perfectly type-safe too since
most type system don't even allow for the second case.

 So what is the type of that second expression? You would think that

  (x - x) - (Char, Bool)

 as a valid type for it. But alas, this is not correct. The CALLER is allowed
 to choose ANY type for x - and most of possible choices wouldn't be
 type-safe. So that's no good at all!

None of the possible choice would be type-safe. This type means :
whatever the type a, function of type (a - a) to a pair (Char, Bool)

But the type a is unique while in the body of your function, a would
need to be two different types.

 Interestingly, if you throw in the undocumented forall keyword, everything
 magically works:

  (forall x. x - x) - (Char, Bool)

 Obviously, the syntax is fairly untidy. And the program is now not valid
 Haskell according to the written standard. And best of all, the compiler
 seems unable to deduce this type automatically either.

As I said, the compiler can't deduce this type automatically because
it would make type inference undecidable.

 At this point, I still haven't worked out exactly why this hack works.
 Indeed, I've spent all day puzzling over this, to the point that my head
 hurts! I have gone through several theories, all of which turned out to be
 self-contradictory. So far, the only really plausible theory I have been
 able to come up with is this:

Rank-N type aren't a hack, they're perfectly understood and fit
nicely into the type system, their only problem is type inference.

 yada yada, complicated theory... cut

What you don't seem to understand is that (x - x) - (Char, Bool)
and (forall x. x - x) - (Char, Bool) are two very different
beasts, they aren't the same type at all !

Taking a real world situation to illustrate the difference :
Imagine you have different kind of automatic washer, some can wash
only wool while others can wash only cotton and finally some can wash
every kind of fabric...
You want to do business in the washing field but you don't have a
washer so you publish an announce :
If your announce say whatever the type of fabric x, give me a machine
that can wash x and all you clothes and I'll give you back all your
clothes washed, the customer won't be happy when you will give him
back his wool clothes that the cotton washing machine he gave you torn
to shreds...
What you really want to say is give me a machine that can wash any
type of fabric and your clothes and I'll give you back your clothes
washed.

The first announce correspond to (x - x) - (Char, Bool), the
second to (forall x. x - x) - (Char, Bool)

 - Why are top-level variables and function arguments treated differently by
 the type system?

They aren't (except if you're speaking about the monomorphism
restriction and that's another subject entirely).

 - Why do I have to manually tell the type checker something that should be
 self-evident?

Because it isn't in all cases to a machine, keep in mind you're an
human and we don't have real IA just yet.

 - Why is this virtually never a problem in real Haskell programs?

Because we seldom needs Rank-2 polymorphism.

 - Is there ever a situation where the unhacked type system behaviour is
 actually desirably?

Almost everytime. Taking a simple example : map
If the type of map was (forall x. x - x) - [a] - [a], you could
only pass it id as argument, if it was (forall x y. x - y) - [a]
- [b], you couldn't find a function to pass to it... (No function
has the type a - b)

 - Why can't the type system automatically detect where polymorphism is
 required?

It can and does, it can't detect Rank-N (N  1) polymorphism because
it would be undecidable but it is fine with Rank-1 polymorphism (which
is what most people call polymorphism).

 - Does the existence of these anomolies indicate that Haskell's entire type
 system is fundamentally flawed and needs to be radically altered - or
 completely redesigned?

Maybe this should suggest to you that it is rather your understanding
of the question that is flawed... Haskell's type system is a fine
meshed mechanism that is soundly grounded in logic and mathematics, it
is rather unlikely that nobody would have noted the problem if it was
so important...

-- 
Jedaï
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org

Re: [Haskell-cafe] GHC API GSoc project (was: ANN: Haddock version 2.1.0)

2008-05-14 Thread Chaddaï Fouché
2008/5/15 Claus Reinke [EMAIL PROTECTED]:
  Feel free to CC me or the ticket with things like that.  I'll be
  IMHO, trying to support a semantics- and comment-preserving
  roundtrip in (pretty . parse) would be a good way to start (David
  says he's going to look at the extracting comments/pragmas part,
  but they still need to be put back in during pretty printing). It sounds
  simple, and if it is, it will enable a lot more usage of the GHC API;
  and if it turns out not to be simple, you'll have a first sign of what
  you're up against, and can adjust your expectations!-)

  I also hope you are in touch with Chaddaï - the port of HaRe
  to the GHC API did not make it as a GSoC project, but I
  understand he is going to do some work in this direction
  anyway.

  - http://hackage.haskell.org/trac/ghc/ticket/1886
(GHC API should preserve and provide access to comments)

Well not in touch until now, I was waiting a little bit to see in
which direction this project is going to evolve. There's plenty of
interesting improvement that one could bring to GHC API. For my
project of course, I'm very interested in the preserving comment part.
I intended to make it independantly, like Haddock do now, but if the
GHC API was to get native support for it, it would be great (for all
kind of others applications too).

I'll keep in touch.

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


Re: [Haskell-cafe] Stack vs Heap allocation

2008-05-11 Thread Chaddaï Fouché
2008/5/10 Edsko de Vries [EMAIL PROTECTED]:
 The key reason why nested additions take stack space, is because (+) on
 Integers is *strict* in both arguments.  If it were somehow non-strict
 instead, then the unevaluated parts of the number would be heap-allocated
 rather than stack-allocated.

 I don't understand. Could you elaborate?


The key problem here is not that a huge thunk is built : it is
allocated on the heap.
But when it is forced, the fact that (+) is strict means that all the
calls of (+) that had been created must go on the stack...

So in the example of fold (+) 0 [long list], the problem is not in
the evaluation of foldl which only build a big thunk (with a tail
recursion), the problem intervene when you must evaluate the big thunk
since then you need to stack all the (+)... If (+) was lazy it
wouldn't be a problem since you would only need to call one (+) to get
a value (which will go in the heap).

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


Re: [Haskell-cafe] Re: dropping hyphens and \n in words

2008-05-11 Thread Chaddaï Fouché
2008/5/11 Achim Schneider [EMAIL PROTECTED]:
  Excuse my bluntness, but I utterly fail to make sense of this.
  Reformulating your understanding of it would surely be beneficial.

He has a routine that gives him a list of words classified by line,
and he want the hyphens to be accounted for.
So that :
Hello mis-
ter world !
gives [(1,[Hello,mister]),(2,[world,!])]

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


Re: Re[2]: a faster, accumulating mapM (was Re: [Haskell-cafe] mapM vs mapM_ performance)

2008-04-25 Thread Chaddaï Fouché
2008/4/25, Niklas Broberg [EMAIL PROTECTED]:
 Wow. A 10x slowdown for a very commonly used function that in 99.8% of
  all use cases has no need for the extra laziness at all. No wonder
  some people say Haskell is a toy language...


A toy language that is still much faster than many currently popular
languages so... Is Ruby/Python/... a toy too ?

Still these numbers seems odd, there's probably something that don't
optimize very well here.

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


Re: [Haskell-cafe] translating from fundeps into type families

2008-04-08 Thread Chaddaï Fouché
2008/4/8, Manuel M T Chakravarty [EMAIL PROTECTED]:
  You need to write the instance as

   instance (b ~ TheFoo a, Foo a) = Bar (Either a b) where
 bar (Left a) = foo' a
 bar (Right b) = foo' (foo b :: a)


If you do that, the program compile, but res still raise a panic in GHC6.8.2 .

Before you sent your solution, I also tried to do

class (TheFoo a ~ b) = Foo a b where ...

instance (Foo a b) = Bar (Either a b) where ...

But it didn't compile, it seems to me both versions should have the
same behaviour ?

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


Re: [Haskell-cafe] [GSoC] Porting HaRe to use the GHC API

2008-04-03 Thread Chaddaï Fouché
2008/4/1, Claus Reinke [EMAIL PROTECTED]:
  as for the project, the are actually two APIs to consider,
  GHC's and HaRe's, and the main stumbling points are those
  things that are not in those APIs (explicitly or at all):

 snip

Many thanks for the valuable insights.

I intend to work in close collaboration with the Kent team, indeed
I'll do an internship over the summer with Simon Thompson ! Don't
worry on this point.
Of course I'll also try to work with the GHC developers on the subject
of the GHC API. My objective is to make it work on the 6.8 API but if
it appears that certain functions would be useful in the future API
I'll be sure to report this to the GHC team. If I can, I would also
like to be present during the discussion between the two Simon.

Another notion I was interested in was to be able to reproduce a
sequence of multi-module refactorings even in the absence of the
initial module. It would allow to present a kind of interface diff
for a distribution which would allow the developer of a module which
depends on the distribution to update the call to this library. Of
course it wouldn't be perfect and wouldn't convey the semantic
modifications of the interface but it would help for all the renaming
and usual modifications of external interfaces.
To refactor modules that use CPP, I can see some possibilities :
transforming the macro into placeholders (undefined) and inversing the
transformation after the refactoring, trying every combination of the
variables for the #ifdef/ifndef, forgetting those that don't compile
and confronting the refactorisation of the others to see if they
merge... That would need some work and would probably be very
inefficient as it is

But all of that should wait after we get HaRe working with the GHC API. :-)

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


Re: [Haskell-cafe] [Newbie] Problem with Data.Map (or something else ?)

2008-04-01 Thread Chaddaï Fouché
2008/4/1, Bruno Carnazzi [EMAIL PROTECTED]:
 Because I don't know anything about arrays in Haskell. Thank you for
  pointing this, I have to read some more Haskell manuals :)

A good place to learn about Haskell's array (which come in many
flavours) is this wiki page :
http://www.haskell.org/haskellwiki/Modern_array_libraries

  
 import Data.Array
 import Data.List
 import Data.Ord

 syrs n = a
 where a = listArray (1,n) $ 0:[ syr n x | x - [2..n]]
   syr x = if x' = n then 1 + a ! x' else 1 + syr x'
   where x' = if even x then x `div` 2 else 3 * x + 1

 main = print $ maximumBy (comparing snd) $ assocs $ syrs 100
  


 The logic and the complexity in this algorithm is comparable to mine
  but the performance difference is huge, which is not very intuitive in
  my mind (There is no 1+1+1+1+1... problem with array ?)

Array or Map isn't really the problem here (my algorithm with a Map
instead only take 6s to find the solution) as I thought at first.
The main problem in your code I think is that because of Map.map, you
create multiple copies of your smaller Maps in memory and union force
them to materialize, while the fact that you don't evaluate the value
means the GC won't collect them. Anyway, your algorithm by itself is
pretty slow I think, since for every step to a number which is not
already recorded you must add 1 to all the numbers you passed on the
way.

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


Re: [Haskell-cafe] lexicographic order

2008-03-31 Thread Chaddaï Fouché
2008/3/31, Simeon Mattes [EMAIL PROTECTED]:
 why I should take as right

 (a,b) = (a',b') iff (a  a' or (a == a' and b = b'))

 and not

 (a,b) = (a',b') iff (a = a' or (a == a' and b = b'))


 The latter seems more logical, doesn't it?

No, it doesn't, since in the latter (1,2) = (1,1) because 1 = 1

 Though I can't understand why both
 (Branch l r) = (Branch l' r')  =  l  l' || l == l'  r = r'
 (Branch l r) = (Branch l' r')  =  l = l' || l == l'  r = r'
 give the same results

They don't, the second is a mistake, the first is the right one.

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


Re: [Haskell-cafe] [Newbie] Problem with Data.Map (or something else ?)

2008-03-31 Thread Chaddaï Fouché
2008/3/31, Bruno Carnazzi [EMAIL PROTECTED]:
Dears Haskellers,

  As an Haskell newbie, I'm learning Haskell by trying to resolve Euler
  Project problems (http://projecteuler.net/ ). I'm hanging on problem
  14 (Collatz problem).

  I've written the following program... Which does not end in a reasonable 
 time :(
  My algorithm seems ok to me but I see that memory consumption is gigantic...
  Is this a memory problem with Data.Map ? Or an infinite loop ? (Where ?)
  In a more general way, how can I troubleshoot these kind of problem ?

Others have pointed potential source of memory leaks, but I must say
that using Data.Map for the cache in the first place appear to me as a
very bad idea... Data.Map by nature take much more place than
necessary. You have an integer index, why not use an array instead ?

 import Data.Array
 import Data.List
 import Data.Ord

 syrs n = a
 where a = listArray (1,n) $ 0:[ syr n x | x - [2..n]]
   syr n x = if x' = n then a ! x' else 1 + syr n x'
   where x' = if even x then x `div` 2 else 3 * x + 1

 main = print $ maximumBy (comparing snd) $ assocs $ syrs 100

This solution takes 2 seconds (on my machine) to resolve the problem.

On the other hand, now that I have read your solution, I see that
using Map was the least of the problem... All those Map.map, while
retaining the original Map... Your solution is too clever (twisted)
for its own good, I suggest you aim for simplicity next time.

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


Re: [Haskell-cafe] lexicographic order

2008-03-30 Thread Chaddaï Fouché
2008/3/30, Bulat Ziganshin [EMAIL PROTECTED]:
  although the last alternative,
(Branch l r) = (Branch l' r')  =  l == l'  r = r' || l = l'
 seems suspicious to me. isn't it the same as
(Branch l r) = (Branch l' r')  =  l = l'

Yes, it should be :
(Branch l r) = (Branch l' r')  =  l  l' || l == l'  r = r'

Lexical order for a tuple (a,b) is :
(a,b) = (a',b') iff (a  a' or (a == a' and b = b'))
The same idea can be applied to list (where Nil is strictly less than
anything else) or other datatypes.

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


Re: [Haskell-cafe] Re: (flawed?) benchmark : sort

2008-03-10 Thread Chaddaï Fouché
2008/3/11, David Menendez [EMAIL PROTECTED]:
 I think Adrian is just arguing that a == b should imply f a == f b,
  for all definable f, in which case it doesn't *matter* which of two
  equal elements you choose, because there's no semantic difference.

  (Actually, it's probably not desirable to require it for *all*
  definable functions, since an implementation might define e.g. an
  unsafe function that does pointer comparisons. We'd probably also
  exclude functions using a private, internal interface that exposes
  implementation details.)

  I like that property, and it bugs me when I have to use a datatype
  whose Eq instance doesn't have it (either because (==) throws away
  information or because the type exposes non-semantic information).

I completely agree that this propriety should be true for all Eq
instance exported by a public module. I don't care if it is not the
case in a isolated code, but libraries shouldn't break expected
invariant (or at least be very cautious and warn the user). Even Eq
Double respects this propriety as far as I know.

Ord case is less evident, but assuming a propriety like (compare x y =
EQ = x == y) seems like a reasonable guess. Doing it in a library
(with a warning) doesn't seems all that bad to me.

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


Re: [Haskell-cafe] (flawed?) benchmark : sort

2008-03-04 Thread Chaddaï Fouché
2008/3/4, Krzysztof Skrzętnicki [EMAIL PROTECTED]:
 Hi

 I was playing with various versions of sorting algorithms. I know it's very
 easy to create flawed benchmark and I don't claim those are good ones.
 However, it really seems strange to me, that sort - library function - is
 actually the worse measured function. I can hardly belive it, and I'd rather
 say I have made a mistake preparing it.

 The overall winner seems to be qsort_iv - which is nothing less but old sort
 replaced by mergesort now.

 Any clues?

Part of what you may be missing :
-- cut here --
module Main where

import Control.Parallel.Strategies
import Control.Arrow
import System.CPUTime
import System.IO
import System.Environment
import System.Random
import Data.List( partition, sort )

data Tree a =
Node (Tree a) a (Tree a)
| Leaf


qsort_i []  = []
qsort_i (x:xs) = qsort_i (filter ( x) xs) ++ [x] ++ qsort_i (filter (= x) xs)

qsort_ii [] = []
qsort_ii (x:xs) = let (ls,gt) = partition ( x) xs in qsort_ii ls ++
[x] ++ qsort_ii gt

qsort_iii xs = qsort_iii' [] xs
qsort_iii' acc [] = acc
qsort_iii' acc (x:xs) =
let (ls,gt) = partition ( x) xs in
let acc' = (x:(qsort_iii' acc gt)) in qsort_iii' acc' ls

qsort_v [] = []
qsort_v (x:xs) = let (xlt, xgt ) = foldl (\ (lt,gt) el - case compare x el of
GT - (el:lt, gt)
_  - (lt,
el:gt) ) ([],[]) xs
 in qsort_v xlt ++ [x] ++ qsort_v xgt

-- zmodyfikowany i
qsort_vi [] = []
qsort_vi (x:xs) = qsort_vi (filter (\y- compare x y == GT) xs) ++ [x]
++ qsort_vi (filter (\y- compare x y /= GT) xs)


-- zmodyfikowany iii
qsort_vii xs = qsort_vii' [] xs
qsort_vii' acc [] = acc
qsort_vii' acc (x:xs) =
let (ls,gt) = partition (\y- compare x y == GT) xs in
let acc' = (x:(qsort_vii' acc gt)) in qsort_vii' acc' ls



-- qsort is stable and does not concatenate.
qsort_iv xs = qsort_iv' (compare) xs []

qsort_iv' _   [] r = r
qsort_iv' _   [x]r = x:r
qsort_iv' cmp (x:xs) r = qpart cmp x xs [] [] r

-- qpart partitions and sorts the sublists
qpart cmp x [] rlt rge r =
-- rlt and rge are in reverse order and must be sorted with an
-- anti-stable sorting
rqsort_iv' cmp rlt (x:rqsort_iv' cmp rge r)
qpart cmp x (y:ys) rlt rge r =
case cmp x y of
GT - qpart cmp x ys (y:rlt) rge r
_  - qpart cmp x ys rlt (y:rge) r

-- rqsort is as qsort but anti-stable, i.e. reverses equal elements
rqsort_iv' _   [] r = r
rqsort_iv' _   [x]r = x:r
rqsort_iv' cmp (x:xs) r = rqpart cmp x xs [] [] r

rqpart cmp x [] rle rgt r =
qsort_iv' cmp rle (x:qsort_iv' cmp rgt r)
rqpart cmp x (y:ys) rle rgt r =
case cmp y x of
GT - rqpart cmp x ys rle (y:rgt) r
_  - rqpart cmp x ys (y:rle) rgt r


-- code by Orcus

-- Zadanie 9 - merge sort
mergeSort :: Ord a = [a] - [a]
mergeSort []= []
mergeSort [x]   = [x]
mergeSort xs= let(l, r) = splitAt (length xs `quot` 2) xs
  in mergeSortP (mergeSort l) (mergeSort r)

-- funkcja pomocnicza scalajÄ…ca dwie listy uporzÄ…dkowane w jednÄ…
mergeSortP :: Ord a = [a] - [a] - [a]
mergeSortP xs []= xs
mergeSortP [] ys= ys
mergeSortP (x:xs) (y:ys)
| x = y = x:(mergeSortP xs (y:ys))
| otherwise = y:(mergeSortP (x:xs) ys)

-- Zadanie 10 - tree sort
treeSort :: Ord a = [a] - [a]
-- pointless po raz drugi
treeSort = (treeSortInorder . treeSortToTree)

treeSortToTree :: Ord a = [a] - Tree a
treeSortToTree []   = Leaf
treeSortToTree (x:xs)   = let (xlt, xgt) = foldl (\ (lt,gt) el - case
compare x el of
GT - (el:lt, gt)
_  - (lt,
el:gt) ) ([],[]) xs
  in Node (treeSortToTree xlt) x (treeSortToTree xgt)

treeSortInorder :: Ord a = Tree a - [a]
treeSortInorder Leaf= []
treeSortInorder (Node l x r)= (treeSortInorder l) ++ [x] ++
(treeSortInorder r)

-- end code by Orcus

-- begin benchmark making code

makeBenchs benchs xs = do
  let (funcNames, funcs) = unzip benchs
  tBegin - getCPUTime
  timers - mapM (\f- print (f xs)  getCPUTime) funcs
  let times = zipWith (-) timers (tBegin:timers)
  sortedResults = sort $ zip times funcNames
  minT = fromIntegral $ fst $ head sortedResults
  scaled = map (((/minT) . fromIntegral) *** id) sortedResults
  hPutStr stderr $ unlines $ map show scaled

onRandom eltCnt = do
  gen - getStdGen
  let xs = take eltCnt (randomRs (1::Int, bigNum) gen) `using` rnf
  xs `seq` return xs

onSorted eltCnt = do
  gen - getStdGen
  let xs = take eltCnt (randomRs (1::Int, bigNum) gen) `using` rnf
  sxs = sort xs `using` rnf
  xs `seq` sxs `seq` return sxs

bigNum = 100 :: Int

-- end of benchmark making code

main = makeBenchs [(i,qsort_i),
   (ii,qsort_ii),
   (iii,qsort_iii),
   (iv,qsort_iv),
   

Re: [Haskell-cafe] Doubting Haskell

2008-03-04 Thread Chaddaï Fouché
2008/3/4, Alan Carter [EMAIL PROTECTED]:
  I've written up some reflections on my newbie experience together with
  both versions, which might be helpful to people interested in
  popularizing Haskell, at:

  http://the-programmers-stone.com/2008/03/04/a-first-haskell-experience/

This is truly interesting, any learning experience is enlightening, we
truly do need to lower this barrier of admittance of which you speak.

On another subject, there are still point in your code that could be
clearer or done with less  cruft :

maxOfHistogram stats = snd (foldl (\(cA, vA) (cB, vB) - if (vA  vB)
then (cA, vA)
else (cB, vB))
  (0, 0)
  stats)

can become :

maxofHistogram stats = foldl' max 0 (map snd stats)

(foldl' max 0 could be replaced by maximum but there wouldn't be a
default 0 anymore)

more importantly, you can replace this kind of code :
  vA - varCreate []
  vB - varCreate []
  -- ...
  vL - varCreate []
  vM - varCreate []
  vN - varCreate []
  vO - varCreate []

by :
  [vA, vB, vC, vD, vE, vF, vG, vH, vI, vJ, vK, vL, vM, vN, vO] -
replicateM 15 (varCreate [])

(true also for the dA - textEntry statusFrame [text := 0,
alignment := AlignRight] sequence)

I'm not sure that functions like getdTotal couldn't be improved, I
wonder if a small Map for the elements of d wouldn't make the code
much better and offer other opportunities for abstractions. As it is,
enumeration like :

 [[label Total Entries,   widget (getdTotal d)]
 ,[label Valid Entries,   widget (getdValid d)]
 -- ...
 ,[label MDMA,widget (getdMdma d)]
 ,[label Caffeine,widget (getdCaffeine d)]]

could be slightly reduced by :
let bindLabelAndWidget (lbl,getter) = [label lbl, widget (getter d)]
in map bindLabelAndWidget [(Total Entries, getdTotal), (Valid
Entries, getdValid)
  ,(...)]

And little thing like :
mapM_ (\f - do repaint f) knownFrames
becoming :
mapM_ repaint knownFrames


I also do concur that a flag or a warning to signal mixed tabulations
and space would be a _very_ good idea !

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


Re: [Haskell-cafe] (flawed?) benchmark : sort

2008-03-04 Thread Chaddaï Fouché
2008/3/4, Krzysztof Skrzętnicki [EMAIL PROTECTED]:
 Thanks for improved code. My point was to measure which programming patterns
 are faster than the others so I can learn which ones I should use. However,
 the thing that is really bad is the fact, that even oneliner qsort_i is
 faster than library sort. Which is very different from what I've expected.
 My intuition is only best and fastest code goes to library, to the point
 that people can learn from it. It seems I was mislead.

I think you did not correctly got the point of my and Neil Mitchell's
message : you benchmarked those function on a completely random
sequences so qsort was at his best, but in the real world, most
sequences would have bias, and it is not infrequent at all to sort a
partially sorted (or reverse sorted) list... In this case the
performance of all your qsort are abysmal... Which is the reason the
old sort was replaced by the actual mergesort in the library. Try my
code (with 1 elements for example), you'll see that sort is the
best on a sorted list, and that qsort is at least 60 times slower (on
1, in fact it is degenerating in O(n^2)).

In the real world, the library maintainers decided it was ok to pay a
slight overhead in the case where the list to sort is really randomly
distributed since mergesort won so hugely over qsort in the pretty
frequent case (in programs) of lists which present regularities.

There is no sort which is ideal in all situations, but we can try to
get a sort that works well in all situations, and don't trash in
situations not so infrequent.

(On the other hand, don't expect libraries functions to always be the
best to use in your particular situation, they tend to be all-purpose
as we just saw and the maintainers prefer simple generic
implementations rather than complicated ones who could be slightly (or
even significantly) faster in some case)

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


Re: [Haskell-cafe] Generating a random list

2008-03-01 Thread Chaddaï Fouché
2008/3/1, Milos Hasan [EMAIL PROTECTED]:
 OK, thanks, this is an important point. So maybe I should have done this?

  main = print $ foldl1' (+) $! take 100 randFloats

  My intuition tells me that the $! (and `seq`) just reduces one level (to
  WHNF?). If so, is there a way to force complete evaluation (so that
  nothing is reducible anymore)?

In fact with this code you won't have any problem since the foldl1'
will consume strictly the elements as soon as take produce them,
avoiding any accumulation of thunk by randoms.

Now if you were to put a sort in there (supposedly to do something
else than a simple sum...), you could have a need for a function that
reduce the list and its elements :

 forceList [] = ()
 forceList (x:xs) = x `seq` forceList xs

  main = print $ foldl1' (+) $ (\xs - forceList xs `seq`  sort xs) $ take 
 100 randFloats

In Ghci it don't work (probably because the tail call in forceList
isn't optimised) but compiled it will work fine.

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


Re: [Haskell-cafe] haskellwiki and Project Euler

2008-02-24 Thread Chaddaï Fouché
2008/2/24, Rodrigo Queiro [EMAIL PROTECTED]:
 The only time I have found the solutions page useful is when I was working
 on problem 100, which I'd been thinking about on and off for several months.
 Eventually, I gave up and looked at the solution there, and was absolutely
 none the wiser as to how it was solved! I thought about it more over the
 next few months, and eventually just copied and ran that program, put it
 into PE, and looked at the forum, and finally understood how I should have
 solved the problem.

 Without the solutions page, I would probably never have been able to solve
 the problem, and would know even less about Diophantine Equations than I
 currently do. However, the only value was the actual numerical solution,
 since when I have solved a problem myself and want to see if my answer could
 be improved, I just look in the forum where I can see a range of methods of
 solution instead of just one.

 That said, I vote to keep the solutions (providing they are written by the
 page editor) since IMO they do no harm.

I agree with this (I personally contributed some small solutions and
tried to at least comment them a minimum), but I noticed a smartass
replaced valid solutions by references to the sequences dictionary...
As the goal of PE is to solve the question by program (at least in my
understanding), I feel this qualify as vandalism... especially as the
program replaced whatever their value were in Haskell whereas the
sequence dictionary has no relation to Haskell.

Otherwise I don't see where the fact that this page exists is in any
way harmful or unsporting, I never looked it up before solving a
question by myself but have found it valuable to check my solution
against.

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


Re: [Haskell-cafe] haskellwiki and Project Euler

2008-02-24 Thread Chaddaï Fouché
2008/2/24, Cale Gibbard [EMAIL PROTECTED]:
 I encourage you to put your solutions back up, that would be good.
  Referencing OEIS is a bit of a cheesy way to do things. (Though if
  it's going to be done, one could at least make use of the excellent
  Math.OEIS library :)

Indeed !!
But I don't think any of my solutions was replaced, I just noticed this.

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


Re: [Haskell-cafe] A beginners question

2008-02-23 Thread Chaddaï Fouché
2008/2/23, Harri Kiiskinen [EMAIL PROTECTED]:
 Dear All,

  banging my head against Haskell, but liking the feeling of hurting
  brains. Just a simple question:

  If

  fmap (^4) [1,2,3] = \i - shows i  

  gives

  1 16 81 

In the List Monad, (=) is defined as concatMap, so this code can be
translated by :
 concatMap (\i - shows i  ) (fmap (^4) [1,2,3])

shows is applied to each elements of the list, then the strings are concatened.

Whereas in
 let xs = fmap (^4) [1,2,3] in shows xs  
shows is applied to the whole list.

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


Re: [Haskell-cafe] Embedded Functions in Algebraic Data Types?

2008-02-10 Thread Chaddaï Fouché
2008/2/10, Michael Feathers [EMAIL PROTECTED]:
 On a lark, I loaded this into Hugs this morning, and it didn't complain:


 data Thing = Thing (Integer - Integer)



 But, I've never seen that sort of construct in an example.  Do people
 ever embed functions in ADTs?

Yes, anyway you can embed function in Maybe a or Either a b for
example, since they're polymorphic. Embedding functions in ADT
purposefully happens quite often too, just look at the State Monad,
the functionnal references proposal and so on...
Recently I embedded functions into a denotational semantic ADT for a
tiny DSL, there's plenty of use cases.

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


Re: [Haskell-cafe] nub vs. find + (:) Is this abysmal code?

2008-02-10 Thread Chaddaï Fouché
2008/2/10, Michael Feathers [EMAIL PROTECTED]:

 How bad is this:

 addProduct :: [Product] - Product - [Product]
 addProduct inventory product = nub (product : inventory)


This is pretty terrible, if the list is consumed afterward (which we
assume it will be) we should have something like a O(n^3)
complexity... Since nub is O(n^2).


 compared to this:

 addProduct :: [Product] - Product - [Product]
 addProduct inventory p
 | isNothing (find (==p) inventory)= p : inventory
 | otherwise= inventory


This is much better, though probably better writed :
 addProduct :: [Product] - Product - [Product]
 addProduct inventory p
 | elem p inventory = p : inventory
 | otherwise   = inventory

and probably even better with a Set instead of a List...

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


Re: [Haskell-cafe] Mutable arrays

2008-02-08 Thread Chaddaï Fouché
Après avoir un peu manipulé la solution de John pour qu'elle fasse la même
chose que la mienne, je peux affirmer qu'elle est légèrement moins rapide
(c'est infime et normal vu que ses leftFold passent plus d'informations),
mais que les deux solutions ont au moins cet avantage d'être rapides (2s sur
10M de Double) et en mémoire parfaitement constante :


import Data.Array.MArray
import Control.Monad
import Data.Array.IO
import System

maxArr a = liftM (foldl1' max) (getElems a)

foldlA :: (MArray a e m, Ix i) = (r - e - r) - r - a i e - m r
foldlA f a arr = getBounds arr =
 foldM (\a-a `seq` liftM $ f a) a
   . map (readArray arr) . range

foldl1A :: (MArray a e m, Ix i) = (e - e - e) - a i e - m e
foldl1A f arr = flip (foldlA f) arr = readArray arr . fst = getBounds
arr

foldMA :: (MArray a e m, Ix i) = (r - e - m r) - r - a i e - m r
foldMA f a arr = getBounds arr =
 foldM (\a-a `seq` (= f a)) a
   . map (readArray arr) . range

modifyArray :: (MArray a e m, Ix i) = (e - e) - a i e - m ()
modifyArray f arr = mapM_ (modifyElement f arr) . range = getBounds arr

modifyElement :: (MArray a e m, Ix i) = (e - e) - a i e - i - m ()
modifyElement f arr i = writeArray arr i . f = readArray arr i

main = do
  [n] - getArgs
  a - (newListArray (0, 10 ^ read n) [1..] :: IO (IOUArray Int Double))
  maxE - foldl1A max a
  modifyArray (* (1/maxE)) a
  print = readArray a (10 ^ read n)


En tout cas il serait agréable d'avoir quelques unes de ces fonctions dans
les librairies standards, vu qu'elles sont généralement utiles et très
efficace. Je n'utilise pas les langages fonctionnels pour avoir à écrire des
boucles explicites sur mes structures de données !! ;-)
(et comme on l'a vu, pour un débutant, l'écriture d'une généralisation
efficace de ces boucles n'est pas si triviale qu'il y paraît).

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


Re: [Haskell-cafe] Mutable arrays

2008-02-08 Thread Chaddaï Fouché
Sorry for the french, I was a little bit confused...

On 08/02/08, Chaddaï Fouché [EMAIL PROTECTED] wrote :
After I changed John's code so that it worked on the same dataset as mine, I
could benchmark both of them :
My solution is a bit faster (but that's a very tiny difference and to be
expected since John's fold are more general than mine), but in any case,
both solution are reasonably fast (2s on a 10M Double array (unboxed)) and
don't eat any more memory than necessary :

Anyway it would be nice to have some version of those function in the
standard library (MArray) since they are pretty useful and efficient. I
don't use functional language in order to have to code explicit loops on my
data structures !! ;-)
(And as we saw, to write an efficient generalisation of those loops isn't as
easy as it might seems)

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


Re: [Haskell-cafe] Mutable arrays

2008-02-07 Thread Chaddaï Fouché
2008/2/7, Jeff φ [EMAIL PROTECTED]:
 I played with your foldl1MArray' last night.  I noticed it can be reduced to
 . . .

 foldl1MArray' :: (MArray a e m, Ix i) = (e - e - e) - a i e - m e
 foldl1MArray' f a = do
  (l,u) - getBounds a
 foldl1' (liftM2 f) (map (readArray a) (range (l,u)))

 Unfortunately, my new version consumes a lot of stack and heap space.  Why
 is this so inefficient?  Is there a simple change that will make it
 efficient?

This code don't compute the results incrementally, it can't because
foldl1' is not aware of the monad, it only construct a huge action in
m which is then run. foldM advantage is that it can run incrementally.

Which is not to say my code was the best possible (far from it),
already the following would have been better :

 foldlA f a arr = getBounds arr =
  foldM (\a-a `seq` liftM $ f a) a
. map (readArray arr) . range

 foldl1A f arr = getBounds arr = readArray arr . fst =
 flip (foldlA f) arr

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


Re: [Haskell-cafe] Mutable arrays

2008-02-06 Thread Chaddaï Fouché
2008/2/6, Jeff φ [EMAIL PROTECTED]:
 I have solved both of these problems in Clean using a lazy list without
 resorting to unsafe operations.  So, it seems to me that uniqueness types
 are more general than monads.

 Are you aware that your code in Clean has some issues, like the lst
not being so lazy if you operate on ary2 before you operate on lst (it
is completely put in memory in this case) ? You've effectively created
a big uncertainty on the space taken by your function. Is this ok with
you ? The monadic fold (like I and some others proposed) guarantee you
a constant amount of memory consumed and is perfectly safe too, is the
Clean solution really so much better ?

Jonathan Cast :
 I'm confused --- does uToList_2D return the head of the list before or after 
 it finishes reading
 the array?  If before, then I don't see how the type system prevents me from 
 modifying the
 array before I finish examining the list, as you claim.  If after, then the 
 list isn't truly lazy.

What happens here is that the array can't be modified without
evaluating ary2 and ary2 can't be evaluated without completely
evaluating the list before, so you effectively get the list lazily as
long as you don't touch ary2 before touching the list, and you can't
damage the list by modifying the array (because in this case, lst
would be completely evaluated in the first place). You can do the same
in Haskell in fact, but you'll need to discipline yourself to evaluate
the witness before modifying the array.


So uniqueness here seems to have an advantage over monads, still, the
monads look much cleaner (sic) than Clean code with all those unique
value passing around...

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


Re: [Haskell-cafe] Mutable arrays

2008-02-02 Thread Chaddaï Fouché
2008/2/2, Rodrigo Queiro [EMAIL PROTECTED]:
 Sorry, I was lazy. New maximum':
 maximum' = foldl1' max

Sorry but none of those propositions change the heart of the problem :
the list of elements is totally produced before she can be consumed
due to the strict monadic (IO or ST) nature of getElems. Thus you get
an extraordinary waste of memory as well as resources...

To address this I propose this function :
foldl1MArray' :: (MArray a e m, Ix i) = (e - e - e) - a i e - m e
foldl1MArray' f a = do
  (l,u) - getBounds a
  firstElem - readArray a l
  foldM (\a mb - a `seq` mb = return . f a)
firstElem (map (readArray a) (range (l,u)))

With this, we can rewrite the original program using the excellent
modifyArray from Rodrigo :
normalizeArray :: (MArray a e m, Ix i, Fractional e, Ord e) = a i e - m ()
normalizeArray arr = do
max_elem - foldl1MArray' max arr
modifyArray (* (1/max_elem)) arr

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


Re: [Haskell-cafe] Field updates in a state monad

2008-01-10 Thread Chaddaï Fouché
2008/1/10, Lutz Donnerhacke [EMAIL PROTECTED]:
 * Michael Roth wrote:
  Exists there a way to write this cleaner without writing countless
  set_xyz helper functions?

 The syntactic sugar for record modifications is simply that: sugar.
 You might write your own modifier functions:
   set_bla x y = x { bla = y }

That seems to be exactly what Michael search to avoid. And I don't see
any way to do that with the haskell records. Some extension of records
may have first-class label though.

A pretty interesting alternative could be HList, it has first-class
label and a bunch of other good stuff. I never used it though, so I
don't know how it affects the performances, if someone could give his
experience with it ?

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


Re: [Haskell-cafe] How to convert number types.

2007-12-31 Thread Chaddaï Fouché
2007/12/31, L.Guo [EMAIL PROTECTED]:
 Hi MailList Haskell-Cafe:

 I am a new haskeller. And was farmilar with C.

 When tring to do some calculate, like this:

 input = 5 :: Int
 factor = 1.20 :: Float
 output = factor ** (toFloat input)

 I found that I do not know any function could do just what 'toFloat'
 should do.

 What should I do then ?

'fromIntegral' do 'toFloat' and others convertions.
But in your case, using (^) instead of (**) would probably be the good choice.

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


Re: [Haskell-cafe] ReRe: Basic question concerning data constructors

2007-12-30 Thread Chaddaï Fouché
2007/12/30, Joost Behrends [EMAIL PROTECTED]:
  . Now, let's say we had tried defining ClockTime with parameters as
  you suggested.
 
ClockTime' :: Integer - Integer - *
 
  Do you see the problem? In order to use the ClockTime type
  constructor, we would have to use Integer values.

 Cannot see any problem here - do we NOT want ClockTime to be initialized by 
 two
 Integers ? Or is this the main reason for introducing TOD - to be able to
  change it without having to make any changes to code using ClockTime ?
 To repeat myself - am i right understanding, that this needs a differently 
 named
 data constuctor ?

We want a value of type ClockTime to be initialized by two Integers,
but the type of this value will be ClockTime, not (ClockTime 2 3), its
_type_ won't depend on its _content_, ok ?

With dependent types, you can have type constructors (not data
constructors) with an non-type parameter, but Haskell hasn't dependent
typing. To give you an example of the potential usefulness of
dependent type, you can have a list type which is parametrized by the
length of the list.
data List :: * - Integer - * where
  Empty :: List a 0
  Cons :: a - List a n - List a (n+1)
you then can type head() so that you can't apply it on an empty list :
head :: List a n - List a (n-1) | n  0
head (Cons x xs) = xs
and the compiler will enforce this restriction at compile time (as
well as it can).

Dependent typing is interesting but not really practical yet. You can
simulate it more or less with Haskell and GHC extensions, but Haskell
isn't dependently typed.

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


Re: [Haskell-cafe] Re: Wikipedia on first-class object

2007-12-30 Thread Chaddaï Fouché
2007/12/30, Cristian Baboi [EMAIL PROTECTED]:
 A simple question:

 Can you write the value of x to a file where x = (1:x) ?

Yes, but you'll have to write it yourself, because Haskell can't
decide by itself that this value is infinite and try to print it with
a recursive definition, if it could do that, it could decide the
halting problem and that's not possible.

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


Re: [Haskell-cafe] Re: Wikipedia on first-class object

2007-12-30 Thread Chaddaï Fouché
2007/12/30, Chaddaï Fouché [EMAIL PROTECTED]:
 2007/12/30, Cristian Baboi [EMAIL PROTECTED]:
  A simple question:
 
  Can you write the value of x to a file where x = (1:x) ?

 Yes, but you'll have to write it yourself, because Haskell can't
 decide by itself that this value is infinite and try to print it with
 a recursive definition, if it could do that, it could decide the
 halting problem and that's not possible.

Sorry, I'll try to be more precise since Cristian is pretty sticky
with details when he can use them in its sense...

In the particular case of x = (1:x), it can be detected that the
structure is circular in memory and so infinite, but in the general
case (for example if you replace (1:x) by (1:f x)) it can't be decided
if the value is finite or infinite. You can't do a printer that do
what you want in Haskell itself but you could probably do it with a VM
or with compiler support, but it would be extremely tricky, also it
would _always_ use the running form, even if this form isn't the
optimal for storage (for example a filter of a huge list that results
in an empty list would take enormously more place than with a normal
printer).

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


Re: [Haskell-cafe] what does @ mean?.....

2007-12-28 Thread Chaddaï Fouché
2007/12/28, Alfonso Acosta [EMAIL PROTECTED]:
 @ works as an aliasing primitive for the arguments of a function

 f x@(Just y) = ...

 using x in the body of f is equivalent to use Just y. Perhaps in
 this case is not really useful, but in some other cases it saves the
 effort and space of retyping really long expressions. And what is even
 more important, in case an error is made when choosing the pattern,
 you only have to correct it in one place.


And in plenty of case it will greatly boost your speed because you
won't be reconstructing the objects against which you matched every
time you want to return them unchanged.

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


Re: [Haskell-cafe] what does @ mean?.....

2007-12-28 Thread Chaddaï Fouché
2007/12/28, Nicholls, Mark [EMAIL PROTECTED]:
 So in the example given...

 mulNat a b
  | a = b = mulNat' a b b
  | otherwise = mulNat' b a a
  where
   mulNat' x@(S a) y orig
   | x == one = y
   | otherwise = mulNat' a (addNat orig y) orig

 Is equivalent to

 mulNat a b
  | a = b = mulNat' a b b
  | otherwise = mulNat' b a a
  where
   mulNat' (S a) y orig
   | (S a) == one = y
   | otherwise = mulNat' a (addNat orig y) orig

 ?

Yes, but in the second version, it has to reconstruct (S a) before
comparing it to one where in the first it could do the comparison
directly. In this cas there may be some optimisation involved that
negate this difference but in many case it can do a real performance
difference.
The as-pattern (@ means as) is both practical and performant in most cases.

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


Re: [Haskell-cafe] Re: Comments on reading two ints off Bytestring

2007-12-24 Thread Chaddaï Fouché
2007/12/24, Paulo J. Matos [EMAIL PROTECTED]:
 On Dec 24, 2007 11:55 AM, Paulo J. Matos [EMAIL PROTECTED] wrote:
  On Dec 23, 2007 12:44 PM, Isaac Dupree [EMAIL PROTECTED] wrote:
  
   -- this should work too
   parseHeader3 :: BS.ByteString - Maybe (Int, Int)
   --note accurate type signature, which helps us use Maybe failure-monad,
   --although losing your separate error messages
 
  Oh gee, I just noticed that my type sig is in fact not correct. How
  come GHC doesn't complain?
 

Your type is correct.

   parseHeader3 bs = do
  (x, rest) - BS.readInt $ BS.dropWhile (not . isDigit) bs
  (y, _) - BS.readInt $ BS.dropWhile (not . isDigit) rest
  return (x, y)
 
  What happens then if the first BS.readInt return Nothing???
 

 Ok, got it, I'm not returning a maybe. That's it then.
 Still, the first question remains... what happens to (x, rest) if
 BS.readInt returns Nothing.


Your function return a Maybe (Int,Int), (x,y) is of type (Int,Int).
If readInt return a nothing, the whole funtion will return a Nothing
per (=) instance definition.

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


Re: [Haskell-cafe] Re: Printing and Referential transparency excuse

2007-12-24 Thread Chaddaï Fouché
2007/12/24, Cristian Baboi [EMAIL PROTECTED]:
 On Mon, 24 Dec 2007 11:27:11 +0200, apfelmus [EMAIL PROTECTED]
 wrote:

  Cristian Baboi wrote:

   How can I define a function to do the inverse operation ?
g :: String - ( a - b )
   This time I cannot see how referential transparency will deny it.
  What's the excuse now ?

It seems to me that referential transparency as well as the
executable don't embed it's source aren't simple excuses, though you
don't seem to understand that Haskell isn't an interpreted language...

Similarly, the languages that offer an eval() as it is mostly called
are always interpreted, most other languages push this capability (if
they have it at all) on libraries, in Haskell you can try to use
hs-plugins for example.

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


Re: [Haskell-cafe] string literals and haskell'

2007-10-23 Thread Chaddaï Fouché
2007/10/23, Justin Bailey [EMAIL PROTECTED]:

 My two cents - I haven't found another language that handles heredocs as
 nicely as Ruby does.


Perl Heredocs do the same things and predates Ruby's (at least they do
all you described and a bit more).

But what would be really nice is a way to write regex without \\
everywhere, as of now this is the single thing that makes regex in
Haskell less pleasant than in a language like Perl (which has built-in
support for them, but just to be able to ignore all escape codes would
be really useful).

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


Re: [Haskell-cafe] Puzzled

2007-10-06 Thread Chaddaï Fouché
2007/10/6, Peter Verswyvelen [EMAIL PROTECTED]:
  But great to know about the new strictness on vars! I really should get GHC
 6.8 RC1 for Windows...

Just in case you misunderstood : this functionality was already there
in GHC 6.4, it's just the new syntax to active it that is available
only in recent versions of GHC : this new syntax will hopefully be
adopted by others Haskell compilers and thus become a
compiler-agnostic way of specifying extensions to Haskell98 (so that
others compiler than GHC can tell you why they won't compile this
wonderful code that use all the latest extensions that have been
included into your release candidate of GHC, much better than the old
way where they just told you the syntax was wrong, isn't it ?).

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


Re: [Haskell-cafe] Re: PROPOSAL: New efficient Unicode string library.

2007-09-27 Thread Chaddaï Fouché
2007/9/27, Duncan Coutts [EMAIL PROTECTED]:
  Infrequent, but they exist, which means you can't seek x/2 bytes ahead
  to seek x characters ahead.  All such seeking must be linear for both
  UTF-16 *and* UTF-8.

 And in [Char] for all these years, yet I don't hear people complaining. Most
 string processing is linear and does not need random access to characters.

Well, if you never heard anyone complaining about [Char] and never had
any problem with it's slowness, you're probably not in a field where
the efficiency of a Unicode library is really a concern, that's for
sure. (I know that the _main_ problem with [Char] wasn't random
access, but you must admit [Char] isn't really a good example to speak
about efficiency problems)

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


  1   2   >