[Haskell-cafe] GSoC accepted projects

2008-04-22 Thread Malcolm Wallace
Haskell.org is pleased to announce that we received 7 funded student
slots from Google for this year's Summer of Code.  Of the 30 proposals
we received, the mentoring group chose the following 7:

  * GHC API Improvements
by Thomas Schilling, mentored by Simon Marlow

  * Dynamically Loaded Plugins for the Glasgow Haskell Compiler
by Maximilian Conroy Bolingbroke, mentored by Sean Seefried

  * Haskell API Search as an interface to Hackage
by Neil Mitchell, mentored by Niklas Broberg

  * Language.C, a standalone parser/pretty printer library for C99
by Benedikt Huber, mentored by Iavor S. Diatchki

  * Cabal 'make-like' dependency framework
by Andrea Vezzosi, mentored by Duncan Coutts

  * #1560 Efficient maps using generalised tries
by Jamie Brandon, mentored by Adrian Charles Hey

  * Data parallel physics engine
by Roman Cheplyaka, mentored by Manuel M. T. Chakravarty

Slightly fuller details at http://code.google.com/soc/2008/haskell/about.html
Congratulations to these students!  Before coding begins officially at
the end of May, there is a community bonding period, where the
students and community are encouraged to interact and further refine the
work that will be done.

To those students that did not get funding: do not be disheartened.
Almost all of the proposals were good, and we had some difficult choices
to make.  What's more, since this is Open Source, you can continue to
work on your projects anyway!  Many of them will be useful to the
community.  In fact, Google have said that if any GSoC applicant manages
(in the judgement of the mentors) to complete their project by the end
of the summer, despite not receiving a funded place, they will send you
a goodie-bag of Google swag (including a standard Google T-shirt, not
the special GSoC one).

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


Re: [Haskell-cafe] Hackage being too strict?

2008-04-22 Thread Hans van Thiel
[snip]
  The current a priori check, which said there were no fatal errors, while the
  a posteriori check failed, is misleading. Wouldn't it be better to warn 
  potential 
  uploaders that this first check is not complete?
 
 I'm not sure I see what you're getting at. We can't do a full build
 check synchronously at upload time.
Yes, I thought something like that would probably be the reason. What I
meant is that not everybody will realize this immediately, and will
think everything is OK because the message was that there are no fatal
errors (even though there's another message on the page that uploaders
should test their builds first...)
 
  I'd also like the warning about the -O2 optimization flag to go
  away...I'd guess this is a leftover from an earlier time in GHC.
 
 It's not a leftover, it was added recently. The intention is that user
 gets to set the optimisation level (cabal configure -O or -O2 or -O0).
 It's not really great when packages override that choice. From
 experience packaging haskell libraries for gentoo we found that most
 packages that do set -O2 do so out of habit without any particular
 justification and it significantly increases compile times and memory
 use. 
Compile time was negligible in my case, but I didn't think about memory
usage. That does not seem an issue either right now, but it's useful to
know, for the future...
 It's not banned though as there are some cases where it's perfectly
 justified.
Oh, all right. So it's recommended to leave that as a user option when
configuring the installation.
 
  More importantly, if Cabal is sort of an rpm for Haskell programs, 
  shouldn't 
  the uploader use version numbers of the libraries in the Cabal file? For 
  example, 
  I believe in my case at least gtk2hs 0.9.12 is needed, while Ubuntu seems 
  to have 
  only 0.9.11 (until a few months ago, at least). So, if an uploader knows 
  such 
  dependencies, how to express them?
 
 build-depends: gtk = 0.9.12   0.10
Thanks, that's clear now. 

Regards,

Hans

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


[Haskell-cafe] Re: [Haskell] ANNOUNCE: Generic Haskell 1.80 (Emerald)

2008-04-22 Thread Thomas van Noort

Hello,

I took the liberty to move this discussion to the Haskell-Cafe mailing list.


Adrian Hey wrote:

Thomas van Noort wrote:

   Pleasant programming,


Hello,

This looks like good stuff. But having done all this work it seems
a pity not to go the extra mm and cabalise this and make it buildable
on all platforms (at least ghc supported platforms).


You are right, cabalizing the Makefiles implies that Generic Haskell can 
be build on any platform that is supported by GHC. However, this 
requires a tremendous amount of effort since the Makefiles of Generic 
Haskell are enormous.




The reason I'm interested is this may be useful for the GSoC project
I'm mentoring..

 http://code.google.com/soc/2008/haskell/about.html

But as things are, I'd be unlikely to consider introducing a dependency
on this. Even if it built out of the box with cygwin (which it
doesn't BTW) I don't really think many windows users will be keen to
install cygwin and learn how to use it just so they can build GH.


As you already noticed, there is no Windows binary available for the 
Emerald release. However, there is one for the Coral release, available 
from:


http://www.generic-haskell.org

Although this is an old release of Generic Haskell, this release already 
supports generic types, which is what you need for your project 
probably. In the user's guide, there is a small example available which 
defines a generic type to represent tries.


The latest release of Generic Haskell supports generic views for generic 
types. This allows you to define a generic type that generates efficient 
balanced tries, as you can read in my Master's thesis :) This requires 
the use of the balanced view on the type level. Unfortunately, this 
generic view is not implemented in the Generic Haskell compiler...




Thanks
--
Adrian Hey


Regards,
Thomas

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


Re: [Haskell-cafe] functional update

2008-04-22 Thread Henning Thielemann


On Mon, 21 Apr 2008, Ryan Ingram wrote:


I recommend this blog entry:
http://twan.home.fmf.nl/blog/haskell/overloading-functional-references.details

along with a few additional combinators for imperative update:

data FRef s a = FRef
  { frGet :: s - a
  , frSet :: a - s - s
  }


http://darcs.haskell.org/record-access/src/Data/Accessor.hs
http://darcs.haskell.org/record-access/src/Data/Accessor/Example.hs

I should upload this to Hackage, I know ...
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Anyone mind proofing a short monad transformers explanation?

2008-04-22 Thread J.N. Oliveira

Ryan Ingram wrote:

On 12/17/07, *Jack Kelly* [EMAIL PROTECTED] 
mailto:[EMAIL PROTECTED] wrote:


 liftIO $ hPutStrLn h You lose
 liftIO $ hFlush h

IO is once again special. For other inner monads, the lift function
does the same thing. Note also that IO has no transformer and must
therefore always be the innermost monad.

 
Actually, this isn't true.  In the case of ReaderT Config IO (), 
liftIO is the same as lift.


Where can I find proofs that the monad transformers found in the Haskell 
libraries indeed produce monads as results?
There should be a general construction and proof (eg. based on adjoint 
functor compositionality) but couldn't find it yet.
Looking at Monads and Effects (Benton et al,  LNCS 2395), for 
instance, I don't see any foundations discussed beyond section 6.

Is this somewhere else?

jno

(www.di.uminho.pt/~jno)
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] ANNOUNCE: Galois web libraries for Haskell released

2008-04-22 Thread John Goerzen
On Mon April 21 2008 1:11:19 pm Don Stewart wrote:
 Galois, Inc. is pleased to announce the open source release of a suite of
 web programming libraries for Haskell!

Lots of cool stuff here!  A few questions:

   * xml
 A simple, lightweight XML parser/generator.

 http://hackage.haskell.org/cgi-bin/hackage-scripts/package/xml

Can you describe how this compares to HaXml?  Were there deficiencies in 
HaXml?

   * sqlite
 Haskell binding to sqlite3 http://sqlite.org/, a light, fast
 database.

 http://hackage.haskell.org/cgi-bin/hackage-scripts/package/sqlite

Similar questions here regarding HDBC.  Did HDBC (and HDBC-sqlite3) not 
address some need?

   * feed
 Interfacing with RSS (v 0.9x, 2.x, 1.0) and Atom feeds

 http://hackage.haskell.org/cgi-bin/hackage-scripts/package/feed

Sweet.  Might have to refactor hpodder to use this.


   * mime
 Haskell support for working with MIME types.

 http://hackage.haskell.org/cgi-bin/hackage-scripts/package/mime

FWIW, I have some similar but slightly different functions in MissingH.

http://software.complete.org/static/missingh/doc//MissingH/Data-MIME-Types.html

hsemail and WASH both also have some stuff in this area.  Probably not as 
nice as yours though.

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


Re: [Haskell-cafe] ANNOUNCE: Galois web libraries for Haskell released

2008-04-22 Thread Aaron Tomb


On Apr 22, 2008, at 6:20 AM, John Goerzen wrote:

 * xml
   A simple, lightweight XML parser/generator.

   http://hackage.haskell.org/cgi-bin/hackage-scripts/package/xml


Can you describe how this compares to HaXml?  Were there  
deficiencies in

HaXml?


The main difference is that it's simpler. It's perhaps 10-20% the size  
of HaXml. For a program that just has to do simple XML processing,  
it's sometimes undesirable to have to link in a library that does a  
lot more than you need.


Another difference is that this package is BSD licensed, while HaXml  
is under the LGPL.


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


Re: [Haskell-cafe] I hate Haskell's typeclasses

2008-04-22 Thread Ryan Ingram
On Mon, Apr 21, 2008 at 10:58 PM, Jonathan Cast
[EMAIL PROTECTED] wrote:
  I must have failed to communicate well.  To me, the point of giving a class
 a name is that then you can write a program that is parametric over the
 elements of that class.  Knowing that I can implement monads in Ruby doesn't
 impress me nearly as much as knowing that I can implement mapM does.
 Haskell has me addicted to code reuse (mapM) the way the rest of the
 programming world is addicted to design patterns (monads).  What I mean by
 `encoding Num and Monad' is that you can do something like this:

  sum = foldr (+) 0
  sequence = foldr (liftM2 (:)) (return [])

  I don't know of any language that is dynamically typed and also can encode
 `return' or `0' in a way that lets those examples work.  Statically typed
 languages where it works are rare, even.  Haskell gives up a fair amount of
 control by making these things implicit, which is what I think you're
 running up against --- but I think it gets something useful and non-trivial
 to acheive in return.

I think ruby generally solves this problem via duck-typing; instead of
the cast happening in the (implicit) fromInteger call in sum above,
instead the cast happens in the function with more information via a
call to otherclass#to_whatever_i_am.  You can do something like this:

class Frob
   attr_reader :val
   def initialize(i)
   @val = i
   end
   def to_frob
  self
   end
   def +(rhs)
  rhsF = rhs.to_frob
  Frob.new(rhsF.val + @val)
   end
end

class Integer
   def to_frob
   Frob.new(self)
   end
end

class Array
   def sum
   foldl(0) {|acc,x| acc + x}
   end
   def foldl(z)
  each {|x| z = yield(z,x)}
  z
   end
end

irb(main):055:0 [1,2,3].sum
= 6
irb(main):057:0 [1,2,3].map {|x| Frob.new(x)}.sum
= #Frob:0x2b65cf0 @val=6

  I'll agree with this point.  I've complained, concretely, about the lack of
 instances for (some) standard types before.  (STM is actually a rather bad
 offender here; it's lacking MonadPlus, as well, despite the specific
 observation in the paper that it has the right signature for that class.)

Actually:

GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
Loading package base ... linking ... done.
Prelude :m Control.Monad Control.Monad.STM
Prelude Control.Monad.STM Control.Monad :i STM
...
instance MonadPlus STM -- Defined in Control.Monad.STM

  When can we discharge a MaybeInstance context?

On any concrete type.  Like Typeable should be :)
The compiler then determines whether that type is an instance of the
class and provides the appropriate dictionary if applicable.

  Having the | Show a test suddenly trip from False to True because some
 other module imported Text.Show.Functions sounds like an odd change to me.
 At any rate, it scares me enough to make me oppose the idea.

I see the worry now.  I think this is more of a problem with orphan
instances; are orphan instances considered to be a valuable enough
feature to avoid potentially more powerful constructs?

Maybe there is a better solution to the I have class C from library X
and type T from library Y and I want to make them play nice together
problem than orphan instances.

  class Forceable alpha where
   seq :: alpha - beta - beta

  Instances derived automatically by the compiler, when possible, for every
 type (like Typeable should be).  We can omit functions if desired (I don't
 remember why I thought this was a good idea).  When you say

  f :: alpha - beta

  or

  f :: C alpha = alpha - beta

  The compiler adds implicit Forceable constraints on alpha and beta.  But,
 if you say

  f :: !Forceable alpha = alpha - beta

  The compiler leaves the Forceable alpha constraint off.  Then you can say

  build :: (forall c. !Forceable c = (a - c - c) - c - c) - [a]

  And the foldr/build law is still sound.

This seems like a really interesting idea.

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


Re: [Haskell-cafe] ANNOUNCE: Galois web libraries for Haskell released

2008-04-22 Thread Don Stewart
jgoerzen:
 On Mon April 21 2008 1:11:19 pm Don Stewart wrote:
  Galois, Inc. is pleased to announce the open source release of a suite of
  web programming libraries for Haskell!
 
 Lots of cool stuff here!  A few questions:
 
* xml
  A simple, lightweight XML parser/generator.
 
  http://hackage.haskell.org/cgi-bin/hackage-scripts/package/xml
 
 Can you describe how this compares to HaXml?  Were there deficiencies in 
 HaXml?

Much smaller, fewer dependencies. I think of it as the tagsoup
of xml parsers.

* sqlite
  Haskell binding to sqlite3 http://sqlite.org/, a light, fast
  database.
 
  http://hackage.haskell.org/cgi-bin/hackage-scripts/package/sqlite
 
 Similar questions here regarding HDBC.  Did HDBC (and HDBC-sqlite3) not 
 address some need?

Yes, we needed full, low-level access to sqlite for some unusual use cases.
For high level stuff, HDBC and Takusen are nicer.

* feed
  Interfacing with RSS (v 0.9x, 2.x, 1.0) and Atom feeds
 
  http://hackage.haskell.org/cgi-bin/hackage-scripts/package/feed
 
 Sweet.  Might have to refactor hpodder to use this.
 
 
* mime
  Haskell support for working with MIME types.
 
  http://hackage.haskell.org/cgi-bin/hackage-scripts/package/mime
 
 FWIW, I have some similar but slightly different functions in MissingH.
 
 http://software.complete.org/static/missingh/doc//MissingH/Data-MIME-Types.html
 
 hsemail and WASH both also have some stuff in this area.  Probably not as 
 nice as yours though.

We should probably bundle up a bunch of the small mime libs into 
a single package at some point.

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


[Haskell-cafe] mapM vs mapM_ performance

2008-04-22 Thread Ben
Hello Haskellers,

I'm running ghc 6.8.2 on vista 64.  Consider the following program,
which is compiled with -02 -prof -auto-all:

module Main where

import System.IO (openFile, IOMode(..), hPutStr)

testlst = let ls = [(i, [(j, (fromIntegral j)::Float) | j -
[1..5]::[Int]]) | i - [1..50]::[Int]]
  in ls

main2 = do
  h - openFile bardump WriteMode
  mapM_ ((hPutStr h) . show) testlst


main = do
  h - openFile bardump2 WriteMode
  mapM ((hPutStr h) . show) testlst
  return ()

main and main2 are different in only that mapM_ versus mapM_ are used.
 But the mapM version runs about 20x slower!  I'm running with +RTS -p
-hc -RTS and I see that the amount of memory allocated is about the
same, and I think the resident memory is about the same too.  But the
mapM_ version runs in about 8.7 seconds, and the mapM version takes
167 seconds.

You may ask, why use mapM if you're discarding the values?
Unfortunately in my real app I need the values, which are more
interesting than IO ().

?

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


Re: [Haskell-cafe] ANNOUNCE: Galois web libraries for Haskell released

2008-04-22 Thread John Goerzen
On Tue April 22 2008 12:20:34 pm Don Stewart wrote:

 Yes, we needed full, low-level access to sqlite for some unusual use
 cases. For high level stuff, HDBC and Takusen are nicer.

Can you elaborate on these use cases?  I would like to either add support for 
them to HDBC-sqlite3, or perhaps make HDBC-sqlite3 a wrapper around your 
library.

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


Re: [Haskell-cafe] ANNOUNCE: Galois web libraries for Haskell released

2008-04-22 Thread Don Stewart
jgoerzen:
 On Tue April 22 2008 12:20:34 pm Don Stewart wrote:
 
  Yes, we needed full, low-level access to sqlite for some unusual use
  cases. For high level stuff, HDBC and Takusen are nicer.
 
 Can you elaborate on these use cases?  I would like to either add support for 
 them to HDBC-sqlite3, or perhaps make HDBC-sqlite3 a wrapper around your 
 library.

Strange hardware. So we needed to be able to monkey around somewhat.

It might make sense to wrap our sqlite3 binding with HDBC-sqlite3
though, so you don't need to maintain your own sqlite binding.

-- Don

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


[Haskell-cafe] Re: Some clarity please!

2008-04-22 Thread Michael Karcher
In gmane.comp.lang.haskell.prime Aaron Denney [EMAIL PROTECTED] wrote:
  Prelude :i Ord
  class (Eq a) = Ord a where
compare :: a - a - Ordering
() :: a - a - Bool
(=) :: a - a - Bool
() :: a - a - Bool
(=) :: a - a - Bool
max :: a - a - a
min :: a - a - a
 
  ..while all functions could be easily derived from 'compare'.  Or from
  the Eq instance's (==) and (), say.
 
  What is the reason for this?  Efficiency?  (Which couldn't be handled
  equally well by RULES?)  Otherwise, it looks like an invitation for
  writing inconsistent instances.
 
 My impression (which may not be entirely accurate) is not primarily for
 efficiency (though that is one reason), but for ease of implementation.
[...]
 max and min seem to have neither justification going for them, although
 I suppose it's technically possible to write compare in terms of them
 and (==).

I am quite late to join this thread, but as I just read the thread
about Conal's AddBounds where he had a very valid point for
implementing min/max without resorting to = or compare:

min [] ys = []
min xs [] = []
min (x:xs) (y:ys)
 | cmp == LT = (x:xs)
 | cmp == GT = (y:ys)
 | cmp == EQ = x:min xs ys
where cmp = compare x y

This is a properly lazy implementation for min (the one in GHC's
prelude is not), as it is able to calculate (take 5 $ min [1,2..]
[1,2..]). This is not possible if min has to wait for compare or = to
compare the full lists before returning the head.

Regards,
  Michael Karcher

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


Re: [Haskell] Re: [Haskell-cafe] ANNOUNCE: Galois web libraries for Haskell released

2008-04-22 Thread Sterling Clover
* feed
  Interfacing with RSS (v 0.9x, 2.x, 1.0) and Atom feeds
 
  http://hackage.haskell.org/cgi-bin/hackage-scripts/package/feed


Curious how well this handles malformed feeds -- i.e. is it strict in its
parsing, or casual and tagsoup-like?

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


[Haskell-cafe] Processing XML with HXT

2008-04-22 Thread rodrigo.bonifacio
Hi all,

I´m just starting with HXT. My question is, how can I expose a use case from 
the main function below (the XmlPickler for UseCase has been already defined):

main :: IO ()
main = do
  runX ( xunpickleDocument xpUseCase [ (a_validate,v_0) ],  uc.xml )
  return ()

For example, if I just want to show the use case contents, how can I call 
show for a retrived use case.

Thanks in advance.

Rodrigo.

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


[Haskell-cafe] Re: Re: semi-closed handles

2008-04-22 Thread Ben Franksen
Ryan Ingram wrote:

 On 4/15/08, ChrisK [EMAIL PROTECTED] wrote:
 A small idiomatic nitpick:  When I see (length s) gets computed and
 thrown
 away I wince at the wasted effort.  I would prefer (finiteSpine s):
 
 On every piece of hardware I've seen, the actual calculation done by
 length is basically free.  Compared to the cache misses you'll get
 from traversing the list, or especially the disk access from reading
 the file, it's vanishingly small.
 
 It's also directly from the prelude and it's usually pretty clear to a
 newbie what it's doing, as compared to foldr (const id) () s which
 is on the path to functional languages make no sense land.  I
 consider myself moderately experienced, and assuming that it
 typechecks I know what it has to mean, but I can't just look at it and
 know what it does like I can with length.
 
 If there was a standard seqList or deepSeq, I'd use that instead.

I am almost sure that there is something in Control.Parallel.Strategies that
you can use (but too lazy to look it up now...)

Cheers
Ben

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


[Haskell-cafe] Storable class?

2008-04-22 Thread Galchin, Vasili
Hello,

 Examples of instances of the Storable class please.

Thank you,

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


Re: [Haskell-cafe] Storable class?

2008-04-22 Thread Evan Laforge
This uses hsc2hs.  As far as I can tell, alignment is unused.  I've
never had an error from using 'undefined' there.

 #include c_interface.h

 instance Storable Color where
 sizeOf _ = #size Color
 alignment _ = undefined
 peek = peek_color
 poke = poke_color

 peek_color colorp = do
 r - (#peek Color, r) colorp :: IO CUChar
 g - (#peek Color, g) colorp :: IO CUChar
 b - (#peek Color, b) colorp :: IO CUChar
 a - (#peek Color, a) colorp :: IO CUChar
 return $ Color (d r) (d g) (d b) (d a)
 where d uchar = fromIntegral uchar / 255.0

 poke_color colorp (Color r g b a) = do
 (#poke Color, r) colorp (c r)
 (#poke Color, g) colorp (c g)
 (#poke Color, b) colorp (c b)
 (#poke Color, a) colorp (c a)
 where c double = Util.c_uchar (floor (double*255))
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: ANNOUNCE: Galois web libraries for Haskell released

2008-04-22 Thread Gour
 Don == Don Stewart [EMAIL PROTECTED] writes:

Don It might make sense to wrap our sqlite3 binding with HDBC-sqlite3
Don though, so you don't need to maintain your own sqlite binding.

How about support for user-defined functions in sqlite3?


Sincerely,
Gour


-- 

Gour  | Zagreb, Croatia  | GPG key: C6E7162D



pgp3c8JJ4VlQ4.pgp
Description: PGP signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Storable class?

2008-04-22 Thread Sebastian Sylvan
On Tue, Apr 22, 2008 at 9:29 PM, Evan Laforge [EMAIL PROTECTED] wrote:

 This uses hsc2hs.  As far as I can tell, alignment is unused.  I've
 never had an error from using 'undefined' there.


Some architectures require all sorts of wacky alignments. E.g. floats may
need to be 4 byte aligned, so it's very important that you don't poke a
single byte, and then a float, you need to pad with 3 empty bytes first
(assuming the whole structure starts at a 4 byte alignment).



-- 
Sebastian Sylvan
+44(0)7857-300802
UIN: 44640862
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] ANNOUNCE: Galois web libraries for Haskell released

2008-04-22 Thread Andrew Coppin

Don Stewart wrote:

  * utf8-string
A UTF8 layer for IO and Strings. The utf8-string
package provides operations for encoding UTF8
strings to Word8 lists and back, and for reading and
writing UTF8 without truncation.


http://hackage.haskell.org/cgi-bin/hackage-scripts/package/utf8-string
  


When I read this, my first thought was but does it support 
ByteStrings? Apparently that's a yes... yay!



About:

Galois http://galois.com researches, designs and develops high
assurance technologies for security-critical systems, networks and
applications. We use Haskell as a primary development tool for producing
robust components for a diverse range of clients.
  


You know, this has been puzzling me for ages. Everybody round here seems 
to know who or what Galois is, except me. So now I know!



Web-based technologies are increasingly important in this area, and we
believe Haskell has a key role to play in the production of reliable,
secure web software. The culture of correctness Haskell encourages is
ideally suited to web programming, where issues of security,
authentication, privacy and protection of resources abound.  In
particular, Haskell's type system makes possible strong static
guarantees about access to resources, critical to building reliable web
applications. We hope that the release of this suite of libraries to the
community will push further the adoption of Haskell in the domain of web
programming.


Well, all of that is true of course... but given that most people 
percieve I/O as being hard in Haskell, it seems like an interesting 
target. But hey, here's to success!


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


Re: [Haskell-cafe] Storable class?

2008-04-22 Thread Evan Laforge
 On Tue, Apr 22, 2008 at 9:29 PM, Evan Laforge [EMAIL PROTECTED] wrote:
  This uses hsc2hs.  As far as I can tell, alignment is unused.  I've
  never had an error from using 'undefined' there.

 Some architectures require all sorts of wacky alignments. E.g. floats may
 need to be 4 byte aligned, so it's very important that you don't poke a
 single byte, and then a float, you need to pad with 3 empty bytes first
 (assuming the whole structure starts at a 4 byte alignment).

Does that mean that if ghc is on a sufficiently wacky architecture it
will evaluate alignment and throw undefined?  And if that's the case
it would seem that you can't write alignment without introducing
platform dependencies.

I've also heard that even on x86, if you don't have things aligned
properly, structure access will be slow.  But I'd assume since I'm
poking into a C structure, either the C compiler will have taken care
of alignment or its definition in C will have to include some manual
padding, and so the haskell side doesn't have to worry about it at
all.

The FFI doc doesn't really talk about the alignment method at all, so
I don't really understand how to write one or how it's used.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Stronger STM primitives needed? Or am I just doing it wrong?

2008-04-22 Thread Ryan Ingram
How can I implement the following operation efficiently in STM?  Given
a TVar now,

waitFor t0 = do
t - readTVar now
if (t  t0) then retry else return ()

This naive implementation has the problem that the transaction gets
restarted every time now gets updated, even if the new value is
still less than t0.

One primitive that would be strong enough is this:
retryUntil :: TVar a - (a - Bool) - STM ()

although it would still do some computation every time now changed.

The thought I originally had was to register the waitFor time with
some helper which kept track of the current time and fired off a
notice when it was ready.  But the problem with that is that retry
undoes all the work of setting that up; the goal is still to block,
but I want a stronger blocking primitive.

Does anyone have any ideas?

  -- ryan

Here's the background:  I'm following along with Conal's excellent FRP
paper at http://conal.net/papers/simply-reactive/

In his implementation of futures, Conal uses a race primitive which
spawns two threads each computing a value that (if successful) is
guaranteed to agree.  I was (and still am) sure that an implementation
using STM could avoid needing to race, with careful use of orElse 
retry.

Here's the datatype I'm using for Future:

 -- semantic type for future
 -- t = type of Time, an instance of Bounded.  a = value type
 type F t a = (t, a)
 force :: Future t a - F t a

 data Future t a = Fut
 { waitFor :: t - STM ()
 , value :: STM (t, a)
 }


 firstFuture :: Future t a - Future t a - Future t a
 -- with semantics:
 -- force (firstFuture f1 f2) = (min t1 t2, if t1 = t2 then v1 else v2)
 --   where
 -- (t1, v1) = force f1
 -- (t2, v2) = force f2

Each future lives in some universe; imagine you have the following:
] type Universe t -- abstract
] P :: Bounded t = Universe t -- universe of pure values
] R :: Universe Time -- universe of the real world
] univ :: Future t a - Universe t
] now :: Universe t - STM t
] -- now P = return maxBound
] -- now R = current time

The main thrust of this is that each universe has its own idea of what
time it is; but when combining futures, we get a new universe which
tracks the later of the times in the two universes.  The problem is
combining two futures:

Lets say we have:
anyKey :: Future Time () -- fires when the user first presses a key
thousand :: Num t = Future t () -- fires at tick 1000
thousand = exactly 1000 ()

and we are evaluating
  force (firstFuture anyKey thousand)

Now, clock is going to fire at tick 1000; we know this because it
lives in the pure universe and its value is always available.  So
firstFuture can get the following:

   x1 - maybeSTM (value anyKey)
   -- x1 = Nothing
   x2 - maybeSTM (value clock)
   -- x2 = Just (1000, ())
   ...
 -- synchronize P with R at tick 1000
 waitFor anyKey 1000 -- if nothing has changed before tick 1000
 return (1000, ())

-- converts a possibly-retry-calling STM into one that never fails
maybeSTM m = fmap Just m `orElse` return Nothing

So if we use the naive implementation for waitFor in terms of now,
the whole transaction will get re-evaluated every tick.  I only want
it to get re-evaluated if the user presses a key (changing some TVar
evaluated in value anyKey), or tick 1000 passes, whichever comes
first.  Is there a way to do this?  Is my choice of waitFor as the
basic universe synchronization operation too weak?
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Storable class?

2008-04-22 Thread Sebastian Sylvan
On Tue, Apr 22, 2008 at 10:48 PM, Evan Laforge [EMAIL PROTECTED] wrote:

  On Tue, Apr 22, 2008 at 9:29 PM, Evan Laforge [EMAIL PROTECTED] wrote:
   This uses hsc2hs.  As far as I can tell, alignment is unused.  I've
   never had an error from using 'undefined' there.
 
  Some architectures require all sorts of wacky alignments. E.g. floats
 may
  need to be 4 byte aligned, so it's very important that you don't poke a
  single byte, and then a float, you need to pad with 3 empty bytes first
  (assuming the whole structure starts at a 4 byte alignment).

 Does that mean that if ghc is on a sufficiently wacky architecture it
 will evaluate alignment and throw undefined?  And if that's the case
 it would seem that you can't write alignment without introducing
 platform dependencies.

 I've also heard that even on x86, if you don't have things aligned
 properly, structure access will be slow.  But I'd assume since I'm
 poking into a C structure, either the C compiler will have taken care
 of alignment or its definition in C will have to include some manual
 padding, and so the haskell side doesn't have to worry about it at
 all.

 The FFI doc doesn't really talk about the alignment method at all, so
 I don't really understand how to write one or how it's used.


Well I know that on the Xbox 360 (PowerPC with some extensions basically)
alignment problems will sometimes cause slowness, and sometimes a crash
depending on the data type. I can't remember which ones are which though,
and usually the compiler takes care of it, but in cases such as this when
you're manually poking bytes into a buffer, you do need to be very careful
and make sure that anything actually using these values will either have the
data in the correct alignment right away, or will copy it into an aligned
variable before doing anything with it.

-- 
Sebastian Sylvan
+44(0)7857-300802
UIN: 44640862
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re[2]: [Haskell-cafe] Storable class?

2008-04-22 Thread Bulat Ziganshin
Hello Evan,

Wednesday, April 23, 2008, 1:48:30 AM, you wrote:

 The FFI doc doesn't really talk about the alignment method at all, so
 I don't really understand how to write one or how it's used.

write: easy. just specify how much data shoulkd be aligned. for
primitive datatypes this usually equals to datasize, for complex
structures this should be the same as maximum alignment of elements
involved:

instance Storable Float
  alignment = 4

instance Storable (a,b)
  alignment = maximum [alignment a, alignment b]

use: just align memory blocks allocated to store this datatype. usual
alignment technique is:

alloc a = (allocBytes (sizeOf a + alignment a - 1) + (alignment a - 1)) .. 
(alignment a - 1)

well, many standard allocators such as C malloc, actually provide you
blocks with a maximum alignment required to store any (primitive)
type, so you don't need to worry about it


-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

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


Re: [Haskell-cafe] Stronger STM primitives needed? Or am I just doing it wrong?

2008-04-22 Thread Matthew Brecknell
Ryan Ingram said:
 How can I implement the following operation efficiently in STM?  Given
 a TVar now,
 
 waitFor t0 = do
 t - readTVar now
 if (t  t0) then retry else return ()
 
 This naive implementation has the problem that the transaction gets
 restarted every time now gets updated, even if the new value is
 still less than t0.

I'm not familiar with FRP, so this may be off the mark. Are you familiar
with Control.Concurrent.STM.TVar.registerDelay? I realise your concept
of time differs from that of registerDelay, but I suspect you'll need to
use a similar approach.

 One primitive that would be strong enough is this:
 retryUntil :: TVar a - (a - Bool) - STM ()
 
 although it would still do some computation every time now changed.

I don't think a primitive retryUntil would be able to do any better
than the obvious implementation (which looks a lot like your waitFor).

 The thought I originally had was to register the waitFor time with
 some helper which kept track of the current time and fired off a
 notice when it was ready.  But the problem with that is that retry
 undoes all the work of setting that up; the goal is still to block,
 but I want a stronger blocking primitive.

That's essentially what registerDelay does. The key is that registering
a timer must occur in the IO monad, outside the transaction that waits
for it.

I vaguely recall that there is at least one hackage library which is
somewhat more sophisticated than registerDelay, so you might also want
to look there.

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


[Haskell-cafe] Re: Stronger STM primitives needed? Or am I just doing it wrong?

2008-04-22 Thread Chris Smith
On Tue, 22 Apr 2008 14:48:54 -0700, Ryan Ingram wrote:
 waitFor t0 = do
 t - readTVar now
 if (t  t0) then retry else return ()
 
 This naive implementation has the problem that the transaction gets
 restarted every time now gets updated, even if the new value is still
 less than t0.
 
 One primitive that would be strong enough is this: retryUntil :: TVar a
 - (a - Bool) - STM ()

Hmm.  This makes me suspicious.  A change to a variable may change the 
transaction such that it never even calls your retryUntil the next time 
around.  Is it really safe to not retry the transaction?

-- 
Chris Smith

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


Re: [Haskell-cafe] Re: Stronger STM primitives needed? Or am I just doing it wrong?

2008-04-22 Thread Ryan Ingram
On 4/22/08, Chris Smith [EMAIL PROTECTED] wrote:
  One primitive that would be strong enough is this: retryUntil :: TVar a
  - (a - Bool) - STM ()

 Hmm.  This makes me suspicious.  A change to a variable may change the
 transaction such that it never even calls your retryUntil the next time
 around.  Is it really safe to not retry the transaction?

Of course not; the semantics would be that the transaction log,
instead of saying I read from v would say I read from v and failed
because v didn't satisfy this predicate.

Changes to any other variable in the log would have the same effect as
always: restarting the transaction.  This is actually required in my
desired use case; I want to block until now becomes = t, or a
different TVar gets filled with a non-Nothing value.

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


Re: [Haskell-cafe] Re: Stronger STM primitives needed? Or am I just doing it wrong?

2008-04-22 Thread Matthew Brecknell
Ryan Ingram said:
 
 retryUntil :: TVar a - (a - Bool) - STM ()
 
 [...]

 the semantics would be that the transaction log,
 instead of saying I read from v would say I read from v and failed
 because v didn't satisfy this predicate.
 
 Changes to any other variable in the log would have the same effect as
 always: restarting the transaction.  This is actually required in my
 desired use case; I want to block until now becomes = t, or a
 different TVar gets filled with a non-Nothing value.

I see now what you mean, and I can see that it might be a nice little
addition. By giving the STM runtime additional information about what
conditions would allow the transaction to progress, it might be able to
save some false retries. Note that the only work that is saved is the
work done to get to the retryUntil.

However, strictly speaking, the STM runtime doesn't need this additional
information. You could view a composite STM transaction as an inverted
tree, with individual reads at the leaves, merging through monadic
bindings towards the root, which is the final result (which includes the
transaction's atomic updates). A transaction waiting on a retry is just
one of these trees in suspended animation, waiting for a new value to be
written to one of the leaves. When such a value is written to one of the
leaves, only its downstream nodes need re-evaluation.

Implementing STM that way may not be worth the effort, though, whereas
retryUntil might be.

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


Re: [Haskell-cafe] Processing XML with HXT

2008-04-22 Thread Albert Y. C. Lai

rodrigo.bonifacio wrote:

I´m just starting with HXT. My question is, how can I expose a use case from 
the main function below (the XmlPickler for UseCase has been already defined):

main :: IO ()
main = do
  runX ( xunpickleDocument xpUseCase [ (a_validate,v_0) ],  uc.xml )
  return ()

For example, if I just want to show the use case contents, how can I call 
show for a retrived use case.


y - runX (xunpickleDocument xpUseCase [ (a_validate,v_0) ] uc.xml)
print y

Remark: y :: [UseCase]

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


Re: [Haskell-cafe] I hate Haskell's typeclasses

2008-04-22 Thread Derek Elkins
On Mon, 2008-04-21 at 22:58 -0700, Jonathan Cast wrote:

 class Forceable alpha where
seq :: alpha - beta - beta
 
 Instances derived automatically by the compiler, when possible, for  
 every type (like Typeable should be).  We can omit functions if  
 desired (I don't remember why I thought this was a good idea).  When  
 you say
 
 f :: alpha - beta
 
 or
 
 f :: C alpha = alpha - beta
 
 The compiler adds implicit Forceable constraints on alpha and beta.   
 But, if you say
 
 f :: !Forceable alpha = alpha - beta
 
 The compiler leaves the Forceable alpha constraint off.  Then you can  
 say
 
 build :: (forall c. !Forceable c = (a - c - c) - c - c) - [a]
 
 And the foldr/build law is still sound.

Why do you want types that lie, plus some crazy ad-hoc special case?
Why not just let f :: a - b mean what you write as f :: !Forceable a =
a - b exactly as it would if seq were moved (back) into a class?  Then
the free theorems would hold for the types as stated.

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


Re: Re[2]: [Haskell-cafe] Storable class?

2008-04-22 Thread Galchin, Vasili
Hello,

   Below is a snippet of my code  please look for the poke call
with . This causes a hang when I run a test case. If I
comment out the poke call then test case runs.

Vasili

-- |Correspond to some of the int flags from C's fcntl.h.
data MQFlags =
 MQFlags {
append1:: Bool, -- ^ O_APPEND
exclusive1 :: Bool, -- ^ O_EXCL
noctty1:: Bool, -- ^ O_NOCTTY
nonBlock1  :: Bool, -- ^ O_NONBLOCK
trunc1 :: Bool  -- ^ O_TRUNC
 }


-- |Default values for the 'MQFlags' type. False for each of
-- append, exclusive, noctty, nonBlock, and trunc.
--   TBD WNH MQdefaultFileFlags :: MQFlags
defaultMQFlags =
 MQFlags {
append1= False,
exclusive1 = False,
noctty1= False,
nonBlock1  = False,
trunc1 = False
  }

data MQAttributes =
 MQAttributes {
flags  :: MQFlags,
maxMsgNum  :: Int,
maxMsgSize :: Int,
curNumMsgs :: Int
 }

instance Storable MQAttributes where
 sizeOf (MQAttributes flags maxMsgNum maxMsgSize curNumMsgs) = 4

instance Storable MQFlags where
 sizeOf (MQFlags append1 exclusive1 noctty1 nonBlock1 trunc1) = 1



-- |Open and optionally create this message queue.  See 'System.Posix.Files'
-- for information on how to use the 'FileMode' type.
mqOpen :: String
   - MQOpenMode
   - Maybe FileMode -- ^Just x = creates the file with the given
modes, Nothing = the file must exist.
   - MQFlags
   - MQAttributes
   - IO Fd
mqOpen name how maybe_mode (MQFlags append1 exclusive1 noctty1
nonBlock1 truncate1) (MQAttributes flags
maxMsgNum maxMsgSize curNumMsgs) = do
   withCString name $ \ p_name - do
   putStrLn (name -  ++ name)
   allocaBytes (#const sizeof(struct mq_attr)) $ \ p_attrs - do
(#poke struct mq_attr, mq_flags)p_attrs  flags



On Tue, Apr 22, 2008 at 5:05 PM, Bulat Ziganshin [EMAIL PROTECTED]
wrote:

 Hello Evan,

 Wednesday, April 23, 2008, 1:48:30 AM, you wrote:

  The FFI doc doesn't really talk about the alignment method at all, so
  I don't really understand how to write one or how it's used.

 write: easy. just specify how much data shoulkd be aligned. for
 primitive datatypes this usually equals to datasize, for complex
 structures this should be the same as maximum alignment of elements
 involved:

 instance Storable Float
  alignment = 4

 instance Storable (a,b)
  alignment = maximum [alignment a, alignment b]

 use: just align memory blocks allocated to store this datatype. usual
 alignment technique is:

 alloc a = (allocBytes (sizeOf a + alignment a - 1) + (alignment a - 1))
 .. (alignment a - 1)

 well, many standard allocators such as C malloc, actually provide you
 blocks with a maximum alignment required to store any (primitive)
 type, so you don't need to worry about it


 --
 Best regards,
  Bulatmailto:[EMAIL PROTECTED]


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


Re: [Haskell-cafe] Re: Stronger STM primitives needed? Or am I just doing it wrong?

2008-04-22 Thread Ryan Ingram
Actually, I think I came up with a solution on the way home from work
today.  Instead of

 data Future t a = Fut
 { waitFor :: t - STM ()
 , value :: STM (t, a)
 }

I will use

 data Future t a = Fut
 { waitFor :: t - IO (STM ())
 , value :: IO (STM (t, a))
 }

The goal is to be able to wait on multiple things at a time, but that
doesn't mean all the setup has to happen in STM.  Now I can setup some
TVars in IO and then hand-off to STM for the niceness of orElse.

   -- ryan

On Tue, Apr 22, 2008 at 6:46 PM, Matthew Brecknell
[EMAIL PROTECTED] wrote:
 Ryan Ingram said:
  
   retryUntil :: TVar a - (a - Bool) - STM ()
  
   [...]

 
   the semantics would be that the transaction log,
   instead of saying I read from v would say I read from v and failed
   because v didn't satisfy this predicate.
  
   Changes to any other variable in the log would have the same effect as
   always: restarting the transaction.  This is actually required in my
   desired use case; I want to block until now becomes = t, or a
   different TVar gets filled with a non-Nothing value.

  I see now what you mean, and I can see that it might be a nice little
  addition. By giving the STM runtime additional information about what
  conditions would allow the transaction to progress, it might be able to
  save some false retries. Note that the only work that is saved is the
  work done to get to the retryUntil.

  However, strictly speaking, the STM runtime doesn't need this additional
  information. You could view a composite STM transaction as an inverted
  tree, with individual reads at the leaves, merging through monadic
  bindings towards the root, which is the final result (which includes the
  transaction's atomic updates). A transaction waiting on a retry is just
  one of these trees in suspended animation, waiting for a new value to be
  written to one of the leaves. When such a value is written to one of the
  leaves, only its downstream nodes need re-evaluation.

  Implementing STM that way may not be worth the effort, though, whereas
  retryUntil might be.



  ___
  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] Laziness and Either

2008-04-22 Thread Derek Elkins
On Mon, 2008-04-21 at 21:26 +0100, Magnus Therning wrote:
 John Goerzen wrote:
  Back when I was working on the logic for the bin-packing solver that I 
  added 
  to MissingH (for use with datapacker), I had a design decision to make: do 
  I 
  raise runtime errors with the input using error, or do I use an Either type 
  to return errors?
  
  Initially, for simplicity, I just used error.  But when I did a simple 
  refactoring to use Either, it occurred to me that this switch likely had a 
  negative impact on laziness.
  
  In this particular algorithm, we cannot tell for sure that we have no 
  errors 
  until we have consumed all items of the input list.  This is unlike, say, a 
  safe version of head where you can tell whether you have an error just by 
  whether you have an empty list or not.
  
  In the case of using error, we can happily process the data assuming 
  everything will be fine, and raise an error if and when it is encountered.  
  By using Either, however, any pattern match on the Left/Right result is 
  going to force the entire input to be evaluated so that we can know whether 
  or not it had any error.
  
  Is this analysis sensible?  If so, are there better solutions?
 
 This reminds me of a problem I had in dataenc[1], when trying to decode
 large files I ran out of memory.  It was simple to track that down to my
 decision to use Maybe for the return type.  Originally I only had one
 function for decoding:
 
   decode :: String - Maybe [Word8]
   decode = sequence . decode'
 
 In order to allow lazy decoding I ended up exporting decode' as well:
 
   decode' :: String - [Maybe Word8]
 
 Unfortunately that pushes handling of decoding errors to the layer
 above.  Not ideal maybe, but it was the easiest solution.
 
 Duncan mentioned that iconv took a similar route to provide laziness[2].
 
 I'm curious to see whether Malcom's paper can offer some further insight.
 
 /M
 
 [1]: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/dataenc
 [2]: http://www.haskell.org/pipermail/haskell-cafe/2007-December/036282.html

Indeed, years ago this is exactly what I suggested as a possible
solution.  See
http://web.archive.org/web/20061011010615/http://www.haskell.org/hawiki/TyingTheKnot
 near the bottom.

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


[Haskell-cafe] Re: Some clarity please!

2008-04-22 Thread John Meacham
On Tue, Apr 22, 2008 at 05:28:27PM +, Michael Karcher wrote:
 I am quite late to join this thread, but as I just read the thread
 about Conal's AddBounds where he had a very valid point for
 implementing min/max without resorting to = or compare:
 
 min [] ys = []
 min xs [] = []
 min (x:xs) (y:ys)
  | cmp == LT = (x:xs)
  | cmp == GT = (y:ys)
  | cmp == EQ = x:min xs ys
 where cmp = compare x y
 
 This is a properly lazy implementation for min (the one in GHC's
 prelude is not), as it is able to calculate (take 5 $ min [1,2..]
 [1,2..]). This is not possible if min has to wait for compare or = to
 compare the full lists before returning the head.

In addition, you need special min and max functions to implement IEEE
floating point properly. Of course, floating point is odd in general,
but we should be correct when we can.

John

-- 
John Meacham - ⑆repetae.net⑆john⑈
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] I hate Haskell's typeclasses

2008-04-22 Thread Jonathan Cast

On 22 Apr 2008, at 8:03 PM, Derek Elkins wrote:

On Mon, 2008-04-21 at 22:58 -0700, Jonathan Cast wrote:


class Forceable alpha where
   seq :: alpha - beta - beta

Instances derived automatically by the compiler, when possible, for
every type (like Typeable should be).  We can omit functions if
desired (I don't remember why I thought this was a good idea).  When
you say

f :: alpha - beta

or

f :: C alpha = alpha - beta

The compiler adds implicit Forceable constraints on alpha and beta.
But, if you say

f :: !Forceable alpha = alpha - beta

The compiler leaves the Forceable alpha constraint off.  Then you can
say

build :: (forall c. !Forceable c = (a - c - c) - c - c) - [a]

And the foldr/build law is still sound.


Why do you want types that lie, plus some crazy ad-hoc special case?
Why not just let f :: a - b mean what you write as f :: !Forceable  
a =
a - b exactly as it would if seq were moved (back) into a class?   
Then

the free theorems would hold for the types as stated.


See /Being Lazy with Class/, on why seq was made polymorphic in the  
first place.  I don't /want/ this design, but the most common case is  
that you don't care, for a type variable alpha, whether seq can be  
used on alpha or not.  When you do care, the most common case is that  
you want seq, and adding seq where it wasn't previously legal  
requires a chain of modifications reminiscent of const poisoning.   
I'm willing to consider seq :: a - b - b a practical necessity, and  
compromise with it, as long as I can still declare properly  
parametric types on command.


jcc

PS ‘Lie’ is a bit strong.  A lie is a statement crafted to have a  
meaning not in the belief set of the speaker.  The meaning of a  
Haskell type judgement is given by the language.  So


square :: Num alpha = alpha - alpha
square x = x ** 2

contains a lie.  But I don't think seq :: alpha - beta - beta 
does.___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] I hate Haskell's typeclasses

2008-04-22 Thread Jonathan Cast

On 22 Apr 2008, at 9:53 AM, Ryan Ingram wrote:

On Mon, Apr 21, 2008 at 10:58 PM, Jonathan Cast
[EMAIL PROTECTED] wrote:
 I must have failed to communicate well.  To me, the point of  
giving a class
a name is that then you can write a program that is parametric  
over the
elements of that class.  Knowing that I can implement monads in  
Ruby doesn't

impress me nearly as much as knowing that I can implement mapM does.
Haskell has me addicted to code reuse (mapM) the way the rest of the
programming world is addicted to design patterns (monads).  What I  
mean by

`encoding Num and Monad' is that you can do something like this:

 sum = foldr (+) 0
 sequence = foldr (liftM2 (:)) (return [])

 I don't know of any language that is dynamically typed and also  
can encode
`return' or `0' in a way that lets those examples work.   
Statically typed
languages where it works are rare, even.  Haskell gives up a fair  
amount of

control by making these things implicit, which is what I think you're
running up against --- but I think it gets something useful and  
non-trivial

to acheive in return.


I think ruby generally solves this problem via duck-typing; instead of
the cast happening in the (implicit) fromInteger call in sum above,
instead the cast happens in the function with more information via a
call to otherclass#to_whatever_i_am.  You can do something like this:

class Frob
   attr_reader :val
   def initialize(i)
   @val = i
   end
   def to_frob
  self
   end
   def +(rhs)
  rhsF = rhs.to_frob
  Frob.new(rhsF.val + @val)
   end
end

class Integer
   def to_frob
   Frob.new(self)
   end
end

class Array
   def sum
   foldl(0) {|acc,x| acc + x}
   end
   def foldl(z)
  each {|x| z = yield(z,x)}
  z
   end
end

irb(main):055:0 [1,2,3].sum
= 6
irb(main):057:0 [1,2,3].map {|x| Frob.new(x)}.sum
= #Frob:0x2b65cf0 @val=6


How do I extend Num, in this case?  Assign into the Integer namespace  
(maybe the wrong terminology, I don't know Ruby and don't want to)?   
I've considered designs like this, but they feel like a hack compared  
to type classes.


 I'll agree with this point.  I've complained, concretely, about  
the lack of
instances for (some) standard types before.  (STM is actually a  
rather bad

offender here; it's lacking MonadPlus, as well, despite the specific
observation in the paper that it has the right signature for that  
class.)


Actually:

GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
Loading package base ... linking ... done.
Prelude :m Control.Monad Control.Monad.STM
Prelude Control.Monad.STM Control.Monad :i STM
...
instance MonadPlus STM -- Defined in Control.Monad.STM


OK.  I was going off the documentation (which is spotty for STM anyway).


 When can we discharge a MaybeInstance context?


On any concrete type.  Like Typeable should be :)
The compiler then determines whether that type is an instance of the
class and provides the appropriate dictionary if applicable.


This works if orphan instances are outlawed.  That invalidates  
Haskell 98 programs, of course, including ones both of us have  
written.  Forbidding those would be an interesting language design  
choice, of course, and would make most of what you've asked for  
sensible.  Hmm.


 Having the | Show a test suddenly trip from False to True because  
some
other module imported Text.Show.Functions sounds like an odd  
change to me.

At any rate, it scares me enough to make me oppose the idea.


I see the worry now.  I think this is more of a problem with orphan
instances; are orphan instances considered to be a valuable enough
feature to avoid potentially more powerful constructs?

Maybe there is a better solution to the I have class C from library X
and type T from library Y and I want to make them play nice together
problem than orphan instances.


I can't think of one, for the general case, but I guarantee I'll be  
worrying about both questions until I think I know the answer...


snip

jcc

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