[Haskell-cafe] ANNOUNCE: Haskell XML Toolbox Version 8.0.0

2008-04-28 Thread Uwe Schmidt
Haskell XML Toolbox 8.0.0

I would like to announce a new version of the Haskell XML Toolbox.

The main change is the separation of the old filter interface
into a separate package hxt-filter. 
This makes the API simpler and more readable.
The filter API will not be developed further. But all the
functionality ist still available. The filter package hxt-filter
must be installed on top of hxt.

The darcs repos for HXT are at
http://darcs.fh-wedel.de/hxt
http://darcs.fh-wedel.de/hxt-filter

Furthermore there is a new wiki page
http://www.haskell.org/haskellwiki/HXT/Conversion_of_Haskell_data_from/to_XML
describing the serialization and deserialization of Haskell values to/from XML 
data.

More information and download:
http://www.fh-wedel.de/~si/HXmlToolbox/index.html

There's also a copy on Hackage.

Please email comments, bugs, etc. to [EMAIL PROTECTED] or [EMAIL PROTECTED]

Uwe

--

University of Applied Sciences, Wedel, Germany
http://www.fh-wedel.de/~si/index.html

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


Re: [Haskell-cafe] approximating pi

2008-04-28 Thread jerzy . karczmarczuk
Benjamin L. Russell: 

Assuming the square had 100 pixels per side, on the average, approximately 
how many random pixels should be plotted in the square before obtaining a 
reasonably good estimate of pi?


Nothing to do with Haskell... 


What do you mean by reasonable? This Monte-Carlo procedure is very
inefficient anyway. The relative error falls as 1/sqrt(N) where N is the
number of samples, so, several hundred thousands of samples may give you
just three significant digits.
And, at any rate, this has nothing to do with pixels, what, introduce
one more source of errors through the truncation of real randoms? 

Jerzy Karczmarczuk 


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


[Haskell-cafe] Haskell as a general purpose programming language...

2008-04-28 Thread Martin Grabmueller
Hi all,

just a thought:  is it just me, or is Haskell really really really the
coolest general-purpose programming language?

If I had the time, I would love to do a re-implementation of Harpy [1]
using category-extras [2].

Martin

[1] http://hackage.haskell.org/cgi-bin/hackage-scripts/package/harpy-0.4
[2] 
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/category-extras-0.2




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


Re: [Haskell-cafe] Haskell as a general purpose programming language...

2008-04-28 Thread Bulat Ziganshin
Hello Martin,

Monday, April 28, 2008, 3:51:11 PM, you wrote:

 just a thought:  is it just me, or is Haskell really really really the
 coolest general-purpose programming language?

the only Haskell drawback is that it forces you to hate all other
programming languages (c) people :)

-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

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


[Haskell-cafe] FFI Query: How to free a CString allocated in Haskell

2008-04-28 Thread Verma Anurag-VNF673
Hi,
I am calling a Haskell function from my C code, wherein I export a
Haskell function xyz that takes as an argument string and returns a
string:

Foreign export ccall xyz::CString - IO CString 

xyz = do
 -- extract the input string and act on it--
   -- at the end I return a string like this
  newCString str

Now once I call this function from C code, I am freeing the allocated
memory using free function. I want to confirm that this is the right
thing to do.

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


Re: [Haskell-cafe] FFI Query: How to free a CString allocated in Haskell

2008-04-28 Thread Bulat Ziganshin
Hello Verma,

Monday, April 28, 2008, 4:11:51 PM, you wrote:

   newCString str

 Now once I call this function from C code, I am freeing the allocated
 memory using free function. I want to confirm that this is the right
 thing to do.

yes, i've traced this function down to mallocArray - mallocBytes -
malloc() calls


-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

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


Re: [Haskell-cafe] question about GHC and Unicode

2008-04-28 Thread John Goerzen
On Sun April 27 2008 2:02:25 pm Don Stewart wrote:
 zefria:
 In GHC there's a GHC.Unicode library, but for a string such as 
  *AIOO, a GHC compiled program prints it as a string of unknown
  characters, and in the interpreter, the string evaluates to a string of
  escape sequences instead of displaying properly.
 
 Is there a way to get/activate unicode support in GHC?

 GHC supports unicode internally, and String and Char are all unicode.

 To do unicode IO however, you need to use the utf8-string package:

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

 Just import and use IO functions from System.IO.UTF8, and that's it!

That's a wonderful interface, but unfortunately it appears to assume that 
your Unicode I/O is always UTF-8, and never UTF-16.  I happen to deal with 
more UTF-16 data than UTF-8 over here at the moment.

(Did I mention UTF-7 or UTF-EBCDIC?  horrors...)

There is a Haskell binding for iconv for those that need UTF-16, but it 
doesn't appear to have as convenient an interface.

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


Re: [Haskell-cafe] [help] Program immplementing an alarm

2008-04-28 Thread Yitzchak Gale
Antonio Regidor García wrote:
  I'm trying to implement an alarm in Haskell and wrote the following code: 
 http://hpaste.org/7201
  But it doesn't work as expected.

Hi,

The getLine function ties up stdin. So the system function isn't
able to proceed until getLine completes, even when it is in
another thread.

Try using System.Process.runInteractiveProcess instead of system.
Don't forget to call waitForProcess on the process handle
when you are finished, or you will be left with a zombie.

Hope this helps,
Yitz
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] maintainer of StorableVector

2008-04-28 Thread Henning Thielemann


I sent some patches to the maintainer of StorableVector, Spencer Janssen
  http://code.haskell.org/~sjanssen/storablevector
 but did not get some feedback and the patches are still not applied. Any 
idea what might went wrong?

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


Re: [Haskell-cafe] approximating pi

2008-04-28 Thread David Roundy
On Mon, Apr 28, 2008 at 09:47:44AM +0200, [EMAIL PROTECTED] wrote:
 Benjamin L. Russell: 
 
 Assuming the square had 100 pixels per side, on the average, approximately 
 how many random pixels should be plotted in the square before obtaining a 
 reasonably good estimate of pi?
 
 Nothing to do with Haskell... 
 
 What do you mean by reasonable? This Monte-Carlo procedure is very
 inefficient anyway. The relative error falls as 1/sqrt(N) where N is the
 number of samples, so, several hundred thousands of samples may give you
 just three significant digits.
 And, at any rate, this has nothing to do with pixels, what, introduce
 one more source of errors through the truncation of real randoms? 

Indeed, Monte-Carlo is generally very inefficient, but it does have the
advantage of often allowing one to write very simple code that is thus
easily shown to be correct, and (as long as the random number generator is
good!!!) to get rigorously-correct error bars, which is something that more
efficient methods often struggle on.  For simple tasks like computing pi or
integrating smooth functions, this doesn't matter much, but it's quite a
big deal when considering methods such as Quantum Monte Carlo (although,
alas the fermion problem means that for most QMC calculations one *doesn't*
get a rigorous error bar on the calculation... it's just better than almost
any other method, and for bosons you *can* get a rigorous error bar).
-- 
David Roundy
Department of Physics
Oregon State University
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Caching the Result of a Transaction?

2008-04-28 Thread ChrisK
The garbage collector never gets to collect either the action used to populate 
the cached value, or the private TMVar used to hold the cached value.


A better type for TIVal is given below.  It is a newtype of a TVal.  The 
contents are either a delayed computation or the previously forced value.


Thew newTIVal(IO) functions immediately specify the delayed action.

The newEmptyTIVal(IO) functions create a private TMVar that allows the delayed 
action to be specified once later.  Note the use of tryPutTMVar to return a Bool 
instead of failing, in the event that the user tries to store more that one action.


When force is called, the previous action (and any private TMVar) are forgotten. 
 The garbage collector might then be free to collect them.


--
Chris


-- By Chris Kuklewicz (April 2008), public domain
module TIVal(TIVal,newTIVal,newTIValIO,force,cached) where

import Control.Applicative(Applicative(..))
import Control.Concurrent.STM(STM,TVar,newTVar,newTVarIO,readTVar,writeTVar
 
,TMVar,newEmptyTMVar,newEmptyTMVarIO,tryPutTMVar,readTMVar)
import Control.Monad(Monad(..),join,liftM2)
import System.IO.Unsafe(unsafePerformIO)

newtype TIVal a = TIVal (TVar (Either (STM a) a))

-- the non-empty versions take a computation to delay

newTIVal :: STM a - STM (TIVal a)
newTIVal = fmap TIVal . newTVar . Left

newTIValIO :: STM a - IO (TIVal a)
newTIValIO = fmap TIVal . newTVarIO . Left

-- The empty versions stage things with a TMVar, note the use of join
-- Plain values 'a' can be stored with (return a)

newEmptyTIVal :: STM ( TIVal a, STM a - STM Bool)
newEmptyTIVal = do
  private - newEmptyTMVar
  tv - newTVar (Left (join $ readTMVar private))
  return (TIVal tv, tryPutTMVar private)

newEmptyTIValIO :: IO ( TIVal a, STM a - STM Bool )
newEmptyTIValIO = do
  private - newEmptyTMVarIO
  tv - newTVarIO (Left (join $ readTMVar private))
  return (TIVal tv, tryPutTMVar private)

-- force will clearly let go of the computation (and any private TMVar)

force :: TIVal a - STM a
force (TIVal tv) = do
  v - readTVar tv
  case v of
Right a - return a
Left wait - do a - wait
writeTVar tv (Right a)
return a

-- Conal's cached function. This is actually safe.

cached :: STM a - TIVal a
cached = unsafePerformIO . newTIValIO

-- The instances

instance Functor TIVal where
  f `fmap` tiv = cached (f `fmap` force tiv)

instance Applicative TIVal where
  pure x  = cached (pure x)
  ivf * ivx = cached (force ivf * force ivx)

instance Monad TIVal where
  return x  = cached (return x)
  tiv = k = cached (force tiv = force . k)

instance Applicative STM where
  pure x = return x
  ivf * ivx = liftM2 ($) ivf ivx 


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


Re: [Haskell-cafe] maintainer of StorableVector

2008-04-28 Thread Don Stewart
lemming:
 
 I sent some patches to the maintainer of StorableVector, Spencer Janssen
   http://code.haskell.org/~sjanssen/storablevector
  but did not get some feedback and the patches are still not applied. Any 
 idea what might went wrong?

I think its just unmaintained. 

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


[Haskell-cafe] CPU simulation library?

2008-04-28 Thread Justin Bailey
I want to simulate various process/thread scheduling algorithms for a
small project. I'm trying to compare the performance versus power
consumption of several algorithms. If anyone knows of some Haskell
code along those lines I'd love to hear about it. Thanks in advance!

Justin

p.s. I can find non-Haskell code on my own - I'm hoping there is
something in my favorite language, but any particularly good
suggestions are also welcome.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Simplest possible Fasta shootout entry. How do I zap the ugly line? Suggest any other improvements.

2008-04-28 Thread Don Stewart
r.kelsall:
 (Extracting these questions from my previous thread for clarity.)
 
 Below is my simplest possible program to solve the Fasta shootout
 benchmark.
 
 http://shootout.alioth.debian.org/gp4/benchmark.php?test=fastalang=all
 http://haskell.org/haskellwiki/Shootout/Fasta
 
 I can see one remaining flaw - the line marked 'Ugly'. What's the best
 way to get rid of this line?
 
 Any other suggestions for simplifying or improving the program would
 also be interesting.
 
 This code is about three or four times slower that the current fastest
 GHC entry for the Fasta benchmark. I'll elaborate it for speed when
 I've produced the best version regardless of speed.

This is quite nice, and you can probably match the current entry
by switching to lazy ByteString IO, as the current entry does. 

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


Re: [Haskell-cafe] Simplest possible Fasta shootout entry. How do I zap the ugly line? Suggest any other improvements.

2008-04-28 Thread Richard Kelsall

Don Stewart wrote:

r.kelsall:

(Extracting these questions from my previous thread for clarity.)

Below is my simplest possible program to solve the Fasta shootout
benchmark.

http://shootout.alioth.debian.org/gp4/benchmark.php?test=fastalang=all
http://haskell.org/haskellwiki/Shootout/Fasta

I can see one remaining flaw - the line marked 'Ugly'. What's the best
way to get rid of this line?

Any other suggestions for simplifying or improving the program would
also be interesting.

This code is about three or four times slower that the current fastest
GHC entry for the Fasta benchmark. I'll elaborate it for speed when
I've produced the best version regardless of speed.


This is quite nice, and you can probably match the current entry
by switching to lazy ByteString IO, as the current entry does. 


-- Don



Thanks Don :) I'll try that. The thing I really like about this version
is that it localizes the 'breaking the lines at 60 characters' part of
the program to just one function. I would never have thought to do this
in a language other than Haskell and looking through most of the other
language submissions for Fasta I can't see any that abstract this
feature. I seem to be able to think more clearly in Haskell.


Richard.


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


Re: [Haskell-cafe] Trouble compiling collections-0.3 (from Hackage)

2008-04-28 Thread Adrian Hey

ccing Haskell Cafe in case anyone else is interested in my answer..

Bryan O'Sullivan wrote:

Adrian Hey wrote:


I don't think anyone is interested in working on this or maintaining
it, so it's probably best not to use it for new stuff.


If nobody has stepped up yet, I'll take it over.  It would be a shame to
see it bitrot.


I think the main problem is that the collections package contained a
number of different sub-projects that should really be separated out
(and indeed have been to some extent), as they were progressing at
rather different rates and it was never clear when would be a good time
to do a new release of collections as a whole, so it was never done.

There's the abstract collections class API's which were really what
Jean-Philippe Bernardy was working on. I'm not sure if he's still
interested in that so you'd better ask him. I think he's more interested
in Yi these days :-)

Then there's the AVL tree lib which has been put in hackage as a
separate package already. I kinda washed my hands of this too, but I
guess I'd better change my mind about that as if I don't maintain it
I don't think any one else will.

Then there's the relatively trivial AVL based Data.Set/Map clones which
aren't in hackage yet but I guess could be put there without too much
work.

Then there's all the generalised trie stuff which has stalled for the
time being, at least until type families are available. You'll probably
see some more action on this in the summer as it's the subject of Jamie
Brandons GSoC project.

Regards
--
Adrian Hey

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


[Haskell-cafe] Re: Caching the Result of a Transaction?

2008-04-28 Thread Conal Elliott
Hi Chris,

Thanks a bunch for the new angle.

Question  comments:

* I like the simplicity of using a single TVar whose state reflects the
not-computed/computed state of the IVal.

* I also like the public interface of taking an STM argument (newTIVal(IO))
over returning a sink (newEmptyTIVal(IO)), which came from some non-STM
thinking.  In fact, maybe 'cached' is a better public interface yet.  I'm
going to try it out, renaming cached to ival.  (Oh yeah, I'm shortening
TIVal to IVal.)

* Why tryPutTMVar in place of putTMVar?  Perhaps to encourage checking that
var hasn't been written?

* A perhaps prettier version of force:

force (TIVal tv) = readTVar tv = either compute return
 where
   compute wait = do a - wait
 writeTVar tv (Right a)
 return a

* The Applicative STM instance can be simplified:

instance Applicative STM where { pure = return; (*) = ap }

Cheers,  - Conal

On Mon, Apr 28, 2008 at 7:40 AM, ChrisK [EMAIL PROTECTED]
wrote:

 The garbage collector never gets to collect either the action used to
 populate the cached value, or the private TMVar used to hold the cached
 value.

 A better type for TIVal is given below.  It is a newtype of a TVal.  The
 contents are either a delayed computation or the previously forced value.

 Thew newTIVal(IO) functions immediately specify the delayed action.

 The newEmptyTIVal(IO) functions create a private TMVar that allows the
 delayed action to be specified once later.  Note the use of tryPutTMVar to
 return a Bool instead of failing, in the event that the user tries to store
 more that one action.

 When force is called, the previous action (and any private TMVar) are
 forgotten.  The garbage collector might then be free to collect them.

 --
 Chris

  -- By Chris Kuklewicz (April 2008), public domain
  module TIVal(TIVal,newTIVal,newTIValIO,force,cached) where
 
  import Control.Applicative(Applicative(..))
  import
  Control.Concurrent.STM(STM,TVar,newTVar,newTVarIO,readTVar,writeTVar
 
  ,TMVar,newEmptyTMVar,newEmptyTMVarIO,tryPutTMVar,readTMVar)
  import Control.Monad(Monad(..),join,liftM2)
  import System.IO.Unsafe(unsafePerformIO)
 
  newtype TIVal a = TIVal (TVar (Either (STM a) a))
 
  -- the non-empty versions take a computation to delay
 
  newTIVal :: STM a - STM (TIVal a)
  newTIVal = fmap TIVal . newTVar . Left
 
  newTIValIO :: STM a - IO (TIVal a)
  newTIValIO = fmap TIVal . newTVarIO . Left
 
  -- The empty versions stage things with a TMVar, note the use of join
  -- Plain values 'a' can be stored with (return a)
 
  newEmptyTIVal :: STM ( TIVal a, STM a - STM Bool)
  newEmptyTIVal = do
   private - newEmptyTMVar
   tv - newTVar (Left (join $ readTMVar private))
   return (TIVal tv, tryPutTMVar private)
 
  newEmptyTIValIO :: IO ( TIVal a, STM a - STM Bool )
  newEmptyTIValIO = do
   private - newEmptyTMVarIO
   tv - newTVarIO (Left (join $ readTMVar private))
   return (TIVal tv, tryPutTMVar private)
 
  -- force will clearly let go of the computation (and any private TMVar)
 
  force :: TIVal a - STM a
  force (TIVal tv) = do
   v - readTVar tv
   case v of
 Right a - return a
 Left wait - do a - wait
 writeTVar tv (Right a)
 return a
 
  -- Conal's cached function. This is actually safe.
 
  cached :: STM a - TIVal a
  cached = unsafePerformIO . newTIValIO
 
  -- The instances
 
  instance Functor TIVal where
   f `fmap` tiv = cached (f `fmap` force tiv)
 
  instance Applicative TIVal where
   pure x  = cached (pure x)
   ivf * ivx = cached (force ivf * force ivx)
 
  instance Monad TIVal where
   return x  = cached (return x)
   tiv = k = cached (force tiv = force . k)
 
  instance Applicative STM where
   pure x = return x
   ivf * ivx = liftM2 ($) ivf ivx
 

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


[Haskell-cafe] Confused by instances

2008-04-28 Thread Fraser Wilson
Hello, Haskellers,

I feel like I'm missing something obvious, but here's some example code:

 module Instance where

 data Value = Value Integer

 class ValueClass a where
 fromValue :: Value - a

 instance ValueClass Bool where
 fromValue (Value n) = n /= 0

 instance ValueClass String where
 fromValue (Value n) = show n

 instance (Num a) = ValueClass a where
 fromValue (Value n) = fromInteger n


The Bool instance compiles fine.  The String instance fails, and that would
be because String is a synonym for [Char], and we can't create instances for
those.  There's a slight hack in the Prelude which allows you to define your
own list show function, and that's exactly what gets done by Show Char,
right?  I'm sorry about calling it a hack but, well, it is.  :)


What I'm really confused by is the response to instance (Num a) =
ValueClass a -- what I am trying to say is if a is an instance of Num, then
can be an instance of ValueClass too, and here's how.  But ghc says:

Instance.hs:14:0:
Illegal instance declaration for `ValueClass a'
(All instance types must be of the form (T a1 ... an)
 where a1 ... an are distinct type *variables*
 Use -XFlexibleInstances if you want to disable this.)
In the instance declaration for `ValueClass a'

I don't understand why what I'm doing above is different from, for example,
instance Show a = Show [a], or Monad m = MonadState s (StateT s m) ... I
imagine that it's related to the fact that the difference between those two
and what I have is that they have either the same class on each side of the
=, or new type variables on the right.  But I'm having trouble abstracting
from that to the general rule.

Any help would be greatly appreciated.  In context, I'm attempting to wrap
up different types inside a data type, and rather than extracting the value
with getBoolean, getInteger, getString etc, I thought I would let type
classes work for me, and just say fromValue instead.  Is this silly?  You
can tell me, I won't be hurt.

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


Re: [Haskell-cafe] Confused by instances

2008-04-28 Thread Brandon S. Allbery KF8NH


On Apr 28, 2008, at 16:22 , Fraser Wilson wrote:


 instance (Num a) = ValueClass a where
 fromValue (Value n) = fromInteger n

What I'm really confused by is the response to instance (Num a) =  
ValueClass a -- what I am trying to say is if a is an instance of  
Num, then can be an instance of ValueClass too, and here's how.


The format is instance [context =] classname instance.
Your classname is ValueClass.
Your instance is a.  a is not of the form (T a1 ... an).

(How to fix it?  Not sure, and am trying to get myself out of here  
and on the road :)


--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [EMAIL PROTECTED]
system administrator [openafs,heimdal,too many hats] [EMAIL PROTECTED]
electrical and computer engineering, carnegie mellon universityKF8NH


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


Re: [Haskell-cafe] Confused by instances

2008-04-28 Thread Fraser Wilson
On Mon, Apr 28, 2008 at 10:50 PM, Brandon S. Allbery KF8NH 
[EMAIL PROTECTED] wrote:



 The format is instance [context =] classname instance.
 Your classname is ValueClass.
 Your instance is a.  a is not of the form (T a1 ... an).


But neither is
 instance (Show a) = Show [a] ...

*sound of penny dropping *

Ah.  Wood, trees, all that.

Thanks!

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


[Haskell-cafe] pls help about subtree

2008-04-28 Thread cetin tozkoparan
Assume a tree is a subtree of the other if all elements of the first
tree is included in the second with the exact structure; all
parent-child relations are preserved with their order.

data
Tree = Empty | Leaf Int | Node (Int,Tree,Tree)
subtree::
Tree - Tree - Bool

How can i start to write this code with this data structure? 
Example;


tree1 tree2 
  3
 / \
5   6
   / \
  7   4
13
 /\
3  12
   / \ / \ 
5   6   9   6
 / \ / \
7   4   11  7
   /   / \
  2   3   4


  

Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Confused by instances

2008-04-28 Thread Luke Palmer
2008/4/28 Fraser Wilson [EMAIL PROTECTED]:
 On Mon, Apr 28, 2008 at 10:50 PM, Brandon S. Allbery KF8NH
 [EMAIL PROTECTED] wrote:
  The format is instance [context =] classname instance.
  Your classname is ValueClass.
  Your instance is a.  a is not of the form (T a1 ... an).

 But neither is
  instance (Show a) = Show [a] ...

Yes it is, it's just a weird looking T, (namely []).  This works just as well:

instance (Show a) = Show ([] a) ...

(Unless that's not H98, but I think it is)

Instances have to have concrete constructors at their heads is for
technical reasons, namely you can't in general do type inference with
unrestricted instances.  It is a pattern-matching algorithm as you'd
expect, but it goes *backwards*, from right to left; i.e it sees the
pattern [a] and generates a new constraint Show a. This is contrary to
the intuition that it has a big set of instances, and when it sees,
say, Show Int it adds Show [Int].   The direction of the arrow can be
misleading :-)

To answer your other question, no, there is no list show hack.  What
is being complained about is that you're using a type synonym as an
instance.  If you just expand the synonym everything works fine.  This
is not specific to lists, nor show.  You're just not allowed to use
synonyms in instances (and as the compiler suggests, this restriction
can be lifted with {-# LANGUAGE FlexibleInstances #-}).

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


Re: [Haskell-cafe] Confused by instances

2008-04-28 Thread Luke Palmer
2008/4/28 Fraser Wilson [EMAIL PROTECTED]:
 what I am trying to say is if a is an instance of Num, then
 can be an instance of ValueClass too, and here's how.

Oh, didn't answer this one.  This is almost canned response, questions
like this get asked weekly on this list.

Short answer: you can't.

Longer answer: you can, but you have to wrap it in a newtype, which is
irritating.

newtype NumValue a = NumV a
instance (Num a) = ValueClass (NumV a) where
fromValue (Value n) = NumV (fromInteger n)

Essentially you have to tell the compiler when you use this
instance.  So you still get all the power, but with less convenience
(than the impossible thing you want).

Abridged longest answer:  you can, and you don't need a newtype, but
only if you're The Devil.  Here's an explanation, but *please do not
do this*!  It's unpredictable, poor style, a bad habit, nonmodular,
etc. etc. etc.

First enable undecidable instances:

{-# LANGUAGE UndecidableInstances #-}

With this pragma you are forfeiting your right to a terminating
compiler.  The compiler may instance stack overflow or run forever
for no discernible reason.

Now you are allowed exactly one instance of the form you desire:

instance (Num a) = ValueClass a where
fromValue (Value n) = fromInteger n

If you're lucky, you might be able to define some well-formed
instances in addition and have everything behave.  It *will* break if
you add another such instance, for example:

instance (Read a) = ValueClass a where ...

Because when the compiler sees fromValue, it will try to match it
against the head of an instance.  Both the Num and the Read forms
match every type, so it will *pick one arbitrarily*, without
backtracking.  So if you wanted the Read one and it picked the Num
one, you are permanently out of luck and you basically have to scrap
everything.

So, yeah, there's a little excursion into the dirty corners of the
typeclass system.  If you don't want to get spontaneously eaten by a
bear, use a newtype as above :-).

And now it's time to go make/edit a wiki page on the subject.

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


Re: [Haskell-cafe] Confused by instances

2008-04-28 Thread Fraser Wilson
On Mon, Apr 28, 2008 at 11:33 PM, Luke Palmer [EMAIL PROTECTED] wrote:


 To answer your other question, no, there is no list show hack.


Perhaps hack was a strong word.  I'm not referring to type synonyms, but to
the fact that Prelude's show class happens to have a special show function
for lists, which happens to be handy when writing an instance for Show
Char.  I find the coupling here (between Show and a particular instance of
Show, namely Show Char) to be disturbing -- Show should not (if you ask me)
contain special machinery for specific instances.  Not that I'll lose sleep
over it, and of course the benefit (having pretty strings) far outweighs the
philosophical cost.

The showList function has always struck me as a bit of a wart though.

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


Re: [Haskell-cafe] Confused by instances

2008-04-28 Thread Luke Palmer
On Mon, Apr 28, 2008 at 3:47 PM, Fraser Wilson [EMAIL PROTECTED] wrote:
 On Mon, Apr 28, 2008 at 11:33 PM, Luke Palmer [EMAIL PROTECTED] wrote:
 
  To answer your other question, no, there is no list show hack.

 Perhaps hack was a strong word.  I'm not referring to type synonyms, but to
 the fact that Prelude's show class happens to have a special show function
 for lists, which happens to be handy when writing an instance for Show Char.

Oh, showList, that hack!  Right, I had forgotten about that :-)

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


Re: [Haskell-cafe] pls help about subtree

2008-04-28 Thread Luke Palmer
2008/4/28 cetin tozkoparan [EMAIL PROTECTED]:
  Assume a tree is a subtree of the other if all elements of the first tree
 is included in the second with the exact structure; all parent-child
 relations are preserved with their order.

 data Tree = Empty | Leaf Int | Node (Int,Tree,Tree)

Bit of a nitpick: Haskell folks would usually leave off the tuple on
the Node constructor, since constructors tuple for you already.  So
Tree would become:

data Tree = Empty | Leaf Int | Node Int Tree Tree

But it's (almost) equivalent either way.

 subtree:: Tree - Tree - Bool

 How can i start to write this code with this data structure?

The way most tree algorithms are done is by recursion.  Maybe it would
help if you saw the problem in a more explicit form:

A tree t is a subtree of a tree u if:
 t = u, u is a Node and t is a subtree of the left child, or u is
a Node and t is a subtree of the right child.

That definition should roughly translate into an inefficient
implementation.  There's probably a more efficient one somewhere, but
it's somewhere other than the top of my head.

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


Re: [Haskell-cafe] Confused by instances

2008-04-28 Thread Fraser Wilson
On Mon, Apr 28, 2008 at 11:46 PM, Luke Palmer [EMAIL PROTECTED] wrote:

 2008/4/28 Fraser Wilson [EMAIL PROTECTED]:
  what I am trying to say is if a is an instance of Num, then
  can be an instance of ValueClass too, and here's how.

 Oh, didn't answer this one.  This is almost canned response, questions
 like this get asked weekly on this list.


Thanks for the detailed response.  In the end, I've just selected the types
I'm really interested in, and added instances for those.  It works a treat.


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


[Haskell-cafe] Something like optimistic evaluation

2008-04-28 Thread Daniil Elovkov

Hello

Somewhat on the topic of optimistic evaluation, I've just thought of 
another way to evaluate thunks.


When the program is about to block on some IO, what if we start a thread 
to evaluate (any) unevaluated thunks. We'll have additional system 
thread, but the blocked one will not actually consume any processor time.


This would take place only when the program is compiled as threaded and 
run with -N k, k1.


The RTS knows at least about some operations that will block, those 
which IO operations are implemented with. for example. It could merely 
start a process of evaluating any (or something more clever) outstanding 
thunks right before going into one of those operations and stop it when 
it's back.


Of course, it's not like optimistic evaluation because we don't avoid 
creating thunks. But in a sense it's similar. It could also be compared 
with incremental garbage collection :)


Has something like that been done, discussed?

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


Re: [Haskell-cafe] Something like optimistic evaluation

2008-04-28 Thread Jake Mcarthur

On Apr 28, 2008, at 5:09 PM, Daniil Elovkov wrote:

Somewhat on the topic of optimistic evaluation, I've just thought of  
another way to evaluate thunks.


When the program is about to block on some IO, what if we start a  
thread to evaluate (any) unevaluated thunks. We'll have additional  
system thread, but the blocked one will not actually consume any  
processor time.


Seems like this would be bad in cases where the programmer is trying  
to exploit laziness, such as using infinite lists, no?


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


Re: [Haskell-cafe] Confused by instances

2008-04-28 Thread Brandon S. Allbery KF8NH


On Apr 28, 2008, at 17:47 , Fraser Wilson wrote:
Perhaps hack was a strong word.  I'm not referring to type  
synonyms, but to the fact that Prelude's show class happens to have  
a special show function for lists, which happens to be handy when  
writing an instance for Show Char.  I find the coupling here  
(between Show and a particular instance of Show, namely Show Char)  
to be disturbing -- Show should not (if you ask me) contain special  
machinery for specific instances.  Not that I'll lose sleep over  
it, and of course the benefit (having pretty strings) far outweighs  
the philosophical cost.



This is what comes of overloading lists as strings.  (And what goes  
along with it, namely slowness.  See Data.ByteString for a solution  
that solves both problems but sadly is unlikely to replace String)


--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [EMAIL PROTECTED]
system administrator [openafs,heimdal,too many hats] [EMAIL PROTECTED]
electrical and computer engineering, carnegie mellon universityKF8NH


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


Re: [Haskell-cafe] Glome.hs-0.3 (bounding objects, heterogeneous lists)

2008-04-28 Thread Jim Snow

Andrew Coppin wrote:
Well, for example, Haskell doesn't have hetrogenous lists - which 
are trivial in any OOP language. That's quite awkward to get around. 
Also, both spheres and cylinders have a radius property, but then 
you end up with name clashes. Again, a non-issue in OOP languages. 
[I gather there's some new GHC language extension to partially solve 
this one - but only when types are statically known. In the general 
case, you'd have to write a radius class and define instances... 
yuck!]


It's funny you mention that, those are actually problems I ran into, 
but (having learned my lesson the hard way in Ocaml), I decided not 
to try and force the language to do things my way, but instead try to 
do things in a way that Haskell makes easy.
For instance, I started out by making each primitive a separate type 
(Sphere, Triangle, etc...), and then made a type class that defines a 
ray intersection method (and a few other odds and ends), but then I 
realized that I can't do something as simple as store a list of 
primitives (or if there is in fact a way, I'm not aware of it).

Instead, I made a single large sum type with all my primitives:
so that now I can easily make a list of primitives.  (In fact, that's 
what a Group is.)  I also gave up on using named fields, since coming 
up with a separate name for each one gets ugly:  instead of radius, 
you have sphere_radius, cylinder_radius, cone_radius disc_radius, etc...


All of which works, but now it's a pain to add new primitives. And 
*all* supported primitives must be defined in a single monolithic 
module. And you can't reuse spheres as bounding volumes without either 
reimplementing them or loosing type safety.
The part about spheres and bounding spheres is actually not so much of a 
problem:  I implemented a general Bound primitive that can bound any 
primitive with any other.  In general, you would use a simple object 
like a sphere or box as the left argument and a complex object as the 
right argument.


data Solid =  ...
   | Bound Solid Solid
 ...

and then to intersect the Bound object, you first do a shadow test on 
the left object before testing the right object:


rayint :: Solid - Ray - Flt - Texture - Rayint
rayint (Bound sa sb) r d t =
let (Ray orig _) = r
in if inside sa orig || shadow sa r d
   then rayint sb r d t
   else RayMiss

Some kinds of primitives aren't likely to work well for bounding since 
they don't have a well-defined inside and outside (like triangles and 
discs), but I'd rather provide maximum flexibility and assume that users 
know how to use it sensibly.


http://www.haskell.org/haskellwiki/Glome_tutorial#Bounding_Objects

As for the maintenance issues, that is still a problem.  It would be 
nice to split all the individual primitives into their own modules.


Sebastian Sylvan wrote:


On 4/27/08, Jim Snow [EMAIL PROTECTED] wrote:
  

 For instance, I started out by making each primitive a separate type
 (Sphere, Triangle, etc...), and then made a type class that defines a
 ray intersection method (and a few other odds and ends), but then I
 realized that I can't do something as simple as store a list of
 primitives (or if there is in fact a way, I'm not aware of it).



You can, by using a wrapper type which wraps up any instance of the
Intersect class:

data Intersectable = forall a. Intersect a = MkIntersectable a

For convenience you probably want to instantiate this wrapper in the
class itself:

instance Intersect Intersectable where
  rayIntersection (MkIntersectable x) ray = rayIntersection x ray
  boundingVolume (MkIntersectable x) = boundingVolume x
  -- etc...

Now you can stick Intersectables in lists etc.

  

I think that sounds like what I ought to be doing.

-jim


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


Re: [Haskell-cafe] Re: Caching the Result of a Transaction?

2008-04-28 Thread Ryan Ingram
The problem I have with all of these STM-based solutions to this
problem is that they don't actually cache until the action fully
executes successfully.

For example, if you have a :: TIVal a, and f :: a - TIVal b, and you execute
   force (a = f)

and the action returned by f executes retry for whatever reason, then
the caching done in a gets undone.  Ideally I want to be able to
provide some proof that the result of a is pure and have it committed
immediately when it finishes.

Every attempt I've had so far to solve this problem ends up being some
type of the form
   newtype X a = IO (STM (Either a (X a)))
which has its own problems.

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


[Haskell-cafe] HaXml and the XHTML 1.0 Strict DTD

2008-04-28 Thread Peter Gammie

Is anyone using HaXml to validate XHTML Strict?

The old 1.13.2 version has some bugs in how it handles attributes that  
stop me from using it. It handled the DTD parsing fine.


The most-recent darcs version relies on a newer ByteString than I  
have, so it is not easy for me to test it.


A recent (this year) darcs version failed to parse the DTD, yielding  
this error:


validate: In a sequence:
  in content spec of ELEMENT decl: head
  When looking for a non-empty sequence with separators:
In a sequence:
Expected % but found |
  in file xhtml1  at line 252 col 50
when looking for a content particle

  when looking for a content particle

This is the context:

!-- Document Head  
===--


!ENTITY % head.misc (script|style|meta|link|object)*

!-- content model is %head.misc; combined with a single
 title and an optional base element in any order --

!ELEMENT head (%head.misc;,
 ((title, %head.misc;, (base, %head.misc;)?) |
  (base, %head.misc;, (title, %head.misc;

I appreciate any advice on this.

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


Re: [Haskell-cafe] FFI Query: How to free a CString allocated in Haskell

2008-04-28 Thread Peter Gammie

On 28/04/2008, at 7:23 PM, Bulat Ziganshin wrote:


Hello Verma,

Monday, April 28, 2008, 4:11:51 PM, you wrote:


 newCString str



Now once I call this function from C code, I am freeing the allocated
memory using free function. I want to confirm that this is the right
thing to do.


yes, i've traced this function down to mallocArray - mallocBytes -
malloc() calls


Depending on how you feel about it, Bulat's response either entirely  
answers your question or is neither here nor there.


If you want to write portable code, you should check with the FFI  
spec. If you just want to write code that works with GHC as it  
presently is, you can strace it or whatever.


As Bulat has already given you the pragmatic answer, allow me to grind  
my axe a little. In the FFI spec on p33, we find:



newCString :: String - IO CString
newCStringLen :: String - IO CStringLen

Allocate a memory area for a Haskell string and marshal the string  
into its C representation.
There are two variants of the routine, one for each supported string  
representation. The
memory area allocated by these routines may be deallocated using  
MarshalAlloc.free.


OK, so back on p19 we find:


free :: Ptr a - IO ()

Free a block of memory that was allocated with malloc, mallocBytes,  
realloc, reallocBytes,
or any of the allocation functions from MarshalArray (see Section  
5.9).


and we can draw the inference (reading between the lines) that this  
function need not have a relation to C's malloc. (Roughly, the  
intention is to allow a Haskell implementation to use whatever memory  
management it likes.)


So no, using C's free is not the right thing to do. It will probably  
work in the real world, but you should try to use Haskell's free if  
possible.


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


Re: [Haskell-cafe] Re: Caching the Result of a Transaction?

2008-04-28 Thread Jake Mcarthur

On Apr 28, 2008, at 10:01 PM, Ryan Ingram wrote:


[...] if you have a :: TIVal a, and f :: a - TIVal b, and you execute
  force (a = f)

and the action returned by f executes retry for whatever reason, then
the caching done in a gets undone.


Dangit, you're right. You just rained on the parade! Hmm...

Perhaps we could use the data structure Chris proposed and mix it with  
my first approach involving forkIO. Another thread can perform the  
caching in a separate transaction _or_ the main thread can perform the  
caching itself in case the extra thread doesn't get a chance first.  
This would ensure that it manages to get cached _sometime_ even when  
the main transaction itself retries, but it loses some of the elegance  
we had gained by not forking a new thread and still doesn't  
_guarantee_ that the computations are run only once.


This is not ideal. :(

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


Re: [Haskell-cafe] Re: Caching the Result of a Transaction?

2008-04-28 Thread Conal Elliott
Thanks, Ryan, for the reminder and explanation of this problem.  - Conal

On Mon, Apr 28, 2008 at 8:01 PM, Ryan Ingram [EMAIL PROTECTED] wrote:

 The problem I have with all of these STM-based solutions to this
 problem is that they don't actually cache until the action fully
 executes successfully.

 For example, if you have a :: TIVal a, and f :: a - TIVal b, and you
 execute
   force (a = f)

 and the action returned by f executes retry for whatever reason, then
 the caching done in a gets undone.  Ideally I want to be able to
 provide some proof that the result of a is pure and have it committed
 immediately when it finishes.

 Every attempt I've had so far to solve this problem ends up being some
 type of the form
   newtype X a = IO (STM (Either a (X a)))
 which has its own problems.

  -- ryan

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


RE: [Haskell-cafe] FFI Query: How to free a CString allocated in Haskell

2008-04-28 Thread Verma Anurag-VNF673
Peter,
A naïve question I have now after reading your mail. How do I call 
MarshallAlloc.free from my C code because that's where I need to free it? 

Anurag

 So no, using C's free is not the right thing to do. It will probably  
 work in the real world, but you should try to use Haskell's free if  
 possible.

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


Re: [Haskell-cafe] FFI Query: How to free a CString allocated in Haskell

2008-04-28 Thread Brandon S. Allbery KF8NH


On Apr 29, 2008, at 1:45 , Verma Anurag-VNF673 wrote:
A naïve question I have now after reading your mail. How do I call  
MarshallAlloc.free from my C code because that's where I need to  
free it?


Provide a Haskell wrapper function to deallocate / free it, as a  
parallel to the function that allocates and returns it to the C code.


--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [EMAIL PROTECTED]
system administrator [openafs,heimdal,too many hats] [EMAIL PROTECTED]
electrical and computer engineering, carnegie mellon universityKF8NH


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