Re: Re[Haskell-cafe] [2]: Threading and FFI

2010-02-20 Thread Dean Herington

At 2:53 PM -0800 2/18/10, Yves Parès wrote:

Ben Franksen wrote:

You can leave them unsafe if you are sure that

1) they do not call (back) any function in your program
2) they do not block (or not long enough that it bothers you)

Otherwise they are no less safe that the safe calls. If (1) is not
fulfilled bad things might (that is, probably will) happen. Thus, if you
are not sure, chose safe.


Okay. Since I know which functions call back to haskell code and which make
pauses, I know what need to be safe and what can be let safely unsafe (^^).


Think of it as which foreign functions need to 
be called *safe*ly (because they may call back or 
block) and which can be called *unsafe*ly 
(because they don't).





 Bound thread are ONLY needed if you (that is, some foreign functions
 you use) rely on thread-local storage.


Yes, but since the main thread (if I understood well) is bound, if I call to
C functions which rely on thread-local storage (like OpenGL functions,
according to GHC Control.Concurrent doc) in this thread I should have no
problem, shouldn't I?
And if I choose to call these functions from outside the main thread it'll
have to be from a thread launched with forkOS? (The C functions I call in my
real code actually use OpenGL internally)


Careful.  If you're calling foreign functions 
that rely on thread-local storage, you must call 
them using the same OS thread (to provide them 
with the correct thread state), which means using 
the same, *bound*, Haskell thread.  It wouldn't 
work to call them from both the main Haskell 
thread and another Haskell thread, even if the 
latter were a bound thread, as there would be two 
copies of thread-local storage at play.





 If runtime is threaded and FFI call is marked safe and Haskell thread is
 unbound, then calls to such a function will be executed from SOME extra OS
 thread that is managed completely behind the scenes.


Okay, that's what I didn't understand! Only the call to my safe function
will be done in an external bound thread, not the whole thread in which the
call is done.


Note that boundedness is a property of a 
Haskell thread, not an OS thread.  The phrase 
external bound thread is not sensible.



So, to sum up, my program will run this way (not necessarily in this order):
My main is launched in a bound thread.


Yes.


My casual haskell I/O operations (read and print from and to the terminal)
are launched in an unbound thread.


This phrasing also seems a bit confused.  You may 
perform Haskell I/O operations in any Haskell 
thread, and they will be carried out in some OS 
thread (which one doesn't matter).



Whenever my main thread reaches the call to my safe C function which
'sleeps', it launches it in another bound thread.


No.  Because the main thread is a bound thread, 
it carries out all of its foreign calls using the 
same OS thread.



Am I right or is it no that easy to foresee the behaviour of my program?


The existence of both Haskell and OS threads can 
be confusing.  The key is that, to make a foreign 
call, a Haskell thread needs an OS thread as a 
vehicle.  Normally (that is, when any OS thread 
will do) the Haskell thread may (and should, for 
better performance) be unbound.  When the 
OS-defined per-thread state is important, the 
Haskell thread must be bound so that it always 
uses the same OS thread (but it's up to you to 
use the same Haskell thread!).


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


[Haskell-cafe] Re: Threading and FFI

2010-02-20 Thread Maciej Piechotka
Sorry I start spin-off of thread but all over the haskell code is idiom:

throwErrorIfMinus1_ $ someCode args

However this code is dependent on errno, which is thread dependent. Is
GHC making sure that errno set from C call is propagated back into
correct thread?

Are there any requirements regarding safeness/unsafeness of such
propagation (and if there are shouldn't they be added to various places
where it is simply said that safe call simply does not call back into
Haskell).

Regards



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


[Haskell-cafe] Computing sums

2010-02-20 Thread Andrew Coppin

The other day, I found myself writing the following code:

data Property x y = forall s. Property s (x - s - s) (s - y)

step :: x - Property x y - Property x y
step x (Property s f g) = Property (f x s) f g

read :: Property x y - y
read (Property s _ g) = g s

pure :: (x - y) - Property x y
pure f = Property undefined const f

fold :: (x - y - y) - y - Property x y
fold f y = Property y f id

fold1 :: (x - x - x) - Property x x
fold1 f = Property Nothing (\x mx - maybe (Just x) (Just . f) mx) fromJust

(==) :: Property x y - Property y z - Property x z
p0 == p1 = Property (p0,p1) (\x (q0,q1) - let w0 = step x q0 in (w0, 
step (read w0) q1)) (\(_,q1) - (read q1))


(==) :: Property x y - Property x y' - Property x (y,y')
p0 == p1 = Property (p0,p1) (\x (q0,q1) - (step x q0, step x q1)) 
(\(q0,q1) - (read q0, read q1))


sum = fold (+) 0
count = pure (const 1) == sum
minimum = fold1 min
maximum = fold1 max

Have I just invented arrows?

(And if so, why is the above code nowhere to be seen in the libraries?)

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


Re: [Haskell-cafe] Computing sums

2010-02-20 Thread Luke Palmer
On Sat, Feb 20, 2010 at 3:30 AM, Andrew Coppin
andrewcop...@btinternet.com wrote:
 Have I just invented arrows?

No... you have a data type which is *an* Arrow (probably/almost).  The
pure implementation bugs me because of its use of undefined.  Might
still be okay though.  I would be more comfortable if it could not
output until it has *some* input; i.e.

data Property' a b = forall s. Property' s (a - s - (b,s))

Anyway, for yours: try to implement (.) :: Property b c - Property a
b - Property a c, and first :: Property a b - Property (a,c) (b,c).
Then you will have an arrow.

This is a Causal Commutative Arrow, even, similar to the kinds of
things that are done in Yampa.  It might be more recognizable as the
non-recursive version of:

  newtype Property a b = Property b (a - Property a b)

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


[Haskell-cafe] A small oversight

2010-02-20 Thread Andrew Coppin
I just discovered the highly useful function Data.Function.on. I vaguely 
recall a few people muttering a couple of years back that this would be 
a useful thing to have, but I had no idea it was in the standard 
libraries now.


Anyway, while using it, I discovered a small omission from the Haskell 
libraries: We have min, max, minimum and maximum. We also have minimumBy 
and maximumBy. But there's no sign of minBy or maxBy. You can do, for 
example,


 (min `on` postcode) customer1 customer2

but that gives you the lowest postcode, not the customer to which this 
postcode belongs. By contrast,


 minimumBy (compare `on` postcode) [customer1, customer2]

gives you the corresponding customer. But it seems silly to have to 
actually construct a list just for this. So... is there any danger of 
getting minBy and maxBy added? (I don't actually know off the top of my 
head where min and max are defined - the Prelude?)




Also, constructions like

 sortBy (compare `on` foo)

must surely be very common. Perhaps it would be an idea to define a 
family of functions like


 sortOn :: (Ord y) = (x - y) - [x] - [x]
 sortOn foo = sortBy (compare `on` foo)

Just an idea.



Finally, take a look at this:

 newtype SwapOrd x = SwapOrd (unswap_ord :: x) deriving Eq

 instance Ord x = Ord (SwapOrd x) where
   compare x y = swap_ord $ compare x y

 swap_ord :: Ordering - Ordering
 swap_ord o = case o of
   EQ - EQ
   GT - LT
   LT - GT

Just in case you wanted to sort things in reverse order. I think having 
swap_ord would be nice if nothing else...


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


Re: [Haskell-cafe] A small oversight

2010-02-20 Thread Holger Siegel
Am Samstag, den 20.02.2010, 10:47 + schrieb Andrew Coppin:
 I just discovered the highly useful function Data.Function.on. I vaguely 
 recall a few people muttering a couple of years back that this would be 
 a useful thing to have, but I had no idea it was in the standard 
 libraries now.
 
 Anyway, while using it, I discovered a small omission from the Haskell 
 libraries: We have min, max, minimum and maximum. We also have minimumBy 
 and maximumBy. But there's no sign of minBy or maxBy. You can do, for 
 example,
 
   (min `on` postcode) customer1 customer2
 
 but that gives you the lowest postcode, not the customer to which this 
 postcode belongs. By contrast,
 
   minimumBy (compare `on` postcode) [customer1, customer2]
 
 gives you the corresponding customer. But it seems silly to have to 
 actually construct a list just for this. So... is there any danger of 
 getting minBy and maxBy added? (I don't actually know off the top of my 
 head where min and max are defined - the Prelude?)

minBy and maxBy are already defined in Data.List - but they are local to
minimumBy and maximumBy.


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


Re: [Haskell-cafe] A small oversight

2010-02-20 Thread Matthias Görgens
 Also, constructions like

  sortBy (compare `on` foo)

 must surely be very common.

Just as a data point: I use constructions like the above very often.
(Perhaps someone more enlightened than me can point out the connection
to arrows?)

Data.Function.on is surprisingly useful in some other contexts, too.

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


Re: [Haskell-cafe] Computing sums

2010-02-20 Thread Andrew Coppin

Luke Palmer wrote:

On Sat, Feb 20, 2010 at 3:30 AM, Andrew Coppin
andrewcop...@btinternet.com wrote:
  

Have I just invented arrows?



No... you have a data type which is *an* Arrow (probably/almost).


Well, OK, that's kind of what I meant. ;-)


The
pure implementation bugs me because of its use of undefined.  Might
still be okay though.  I would be more comfortable if it could not
output until it has *some* input;


Yes, this irritates me also.


i.e.

data Property' a b = forall s. Property' s (a - s - (b,s))
  


Hmm. I hadn't thought of that. I will work though the consequences and 
see what happens...



Anyway, for yours: try to implement (.) :: Property b c - Property a
b - Property a c, and first :: Property a b - Property (a,c) (b,c).
Then you will have an arrow.
  


(.) = flip (==)

first p = p == pure id


This is a Causal Commutative Arrow, even, similar to the kinds of
things that are done in Yampa.  It might be more recognizable as the
non-recursive version of:

  newtype Property a b = Property b (a - Property a b)
  


...OK, I'm lost...

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


Re: [Haskell-cafe] What happened in Ohloh?

2010-02-20 Thread Paul Johnson

On 19/02/10 22:31, Don Stewart wrote:

paul:
   

I'd like to use this kind of graph at work as evidence that Haskell is
on a growth trajectory.
 

You might be more interested in data from Hackage:

 http://www.galois.com/blog/2009/03/23/one-million-haskell-downloads/

runched when we passed the 1M downloads mark a year ago (closer to 2M
downloads now).

We've also got just shy of 2000 packages on Hackage, up from 1100 a year
ago (~3 new packages a day)
   
Thanks Don.  I've already used this data in presentations.  I don't want 
to use the Hackage upload graph you posted because a) its got more to do 
with the growth of Hackage than the growth of Haskell, and b) it levels 
off.  I need something with more visual punch.


This is always a problem, related to the Why is Haskell so little used 
in Industry question.  Decision makers use a simple chain of reasoning: 
I've never heard of it = academic language = can't hire programmers = 
unsupportable software.


Maybe I should write a guide to Haskell advocacy in the workplace.  
Would there be any interest?

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


Re: [Haskell-cafe] Computing sums

2010-02-20 Thread Andrew Coppin



Anyway, for yours: try to implement (.) :: Property b c - Property a
b - Property a c, and first :: Property a b - Property (a,c) (b,c).
Then you will have an arrow.  


(.) = flip (==)

first p = p == pure id


No, not quite.

 \p - p == pure id :: Property a b - Property a (b, a)

What you want is

 first p = (pure fst == p) == (pure snd)

which has the required type.

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


Re: [Haskell-cafe] Computing sums

2010-02-20 Thread Stephen Tetley
Hi Andrew

Spot the difference

data Property x y = forall s. Property s (x - s - s) (s - y)
data Fold b c = forall a. F (a - b - a) a (a - c)


The later is from:
http://squing.blogspot.com/2008/11/beautiful-folding.html

Max Rabkin's is is closer to the original argument ordering of foldl.

Best wishes

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


Re: Re[Haskell-cafe] [2]: Threading and FFI

2010-02-20 Thread Yves Parès

Dean Herington wrote:
Careful.  If you're calling foreign functions 
that rely on thread-local storage, you must call 
them using the same OS thread (to provide them
with the correct thread state), which means using 
the same, *bound*, Haskell thread.  It wouldn't 
work to call them from both the main Haskell 
thread and another Haskell thread, even if the 
latter were a bound thread, as there would be two 
copies of thread-local storage at play.

That's alright. I'm used to putting all the related calls in the same thread
as long as it is possible. It'd seem unlogical to me -- conceptually
speaking -- to spread OpenGL calls between several threads.

I understand better, now. Actually, I think I was getting worked up for
nothing ^^.
Just one last remark: when I moved all my OpenGL calls from the main thread
to an unbound thread. I thought it'd not work -- because I assumed I would
have to launch it in a bound thread -- and however it went right...

-
Yves Parès

Live long and prosper
-- 
View this message in context: 
http://old.nabble.com/Threading-and-FFI-tp27611528p27665592.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

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


Re: [Haskell-cafe] Computing sums

2010-02-20 Thread Andrew Coppin

Stephen Tetley wrote:

Hi Andrew

Spot the difference

data Property x y = forall s. Property s (x - s - s) (s - y)
data Fold b c = forall a. F (a - b - a) a (a - c)


The later is from:
http://squing.blogspot.com/2008/11/beautiful-folding.html

Max Rabkin's is is closer to the original argument ordering of foldl.
  


Hmm. So somebody else has come up with the exact same solution to the 
same problem.


My problem isn't exactly the same though. I'm interested in computing a 
property of a list, and recomputing it as elements are appended to the 
list. My Property structure provides an efficient way to do this, and 
to do so composibly. (E.g., if I want the maximum of the mean or 
something. Maximum and mean are both folds.)


PS. Epic, epic comment spam.

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


Re: [Haskell-cafe] Computing sums

2010-02-20 Thread Max Rabkin
On Sat, Feb 20, 2010 at 9:10 PM, Andrew Coppin
andrewcop...@btinternet.com wrote:
 PS. Epic, epic comment spam.

Yeah, sorry. Every now and again I decide I should deal with it. Then
I rediscover that it takes about four clicks to delete each comment.
Basically, I leave my blog alone until I have something (hopefully)
interesting to write.

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


Re: [Haskell-cafe] A small oversight

2010-02-20 Thread Ben Millwood
I can't answer your question (about getting minBy into the libraries)
but I thought I'd point out some tricks:

On Sat, Feb 20, 2010 at 10:47 AM, Andrew Coppin
andrewcop...@btinternet.com wrote:
 Also, constructions like

  sortBy (compare `on` foo)

 must surely be very common.

Common enough that Data.Ord introduces comparing:

comparing :: (Ord a) = (b - a) - b - b - Ordering
comparing = (compare `on`)

see 
http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-Ord.html#v%3Acomparing
But it would still be useful to have sortOn et al to capture the
common technique when your sorting property is potentially expensive
(sortOn length, for example):

sortOn f = map fst . sortBy (comparing snd) . map (\x - (x, f x))

a technique which I believe is called a Schwarzian transform.

 Finally, take a look at this:

  newtype SwapOrd x = SwapOrd (unswap_ord :: x) deriving Eq

  instance Ord x = Ord (SwapOrd x) where
   compare x y = swap_ord $ compare x y

  swap_ord :: Ordering - Ordering
  swap_ord o = case o of
   EQ - EQ
   GT - LT
   LT - GT

 Just in case you wanted to sort things in reverse order. I think having
 swap_ord would be nice if nothing else...


swap_ord (compare x y) = compare y x, so usually flip compare fills
this requirement :)
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] A small oversight

2010-02-20 Thread Andrew Coppin

Ben Millwood wrote:

I can't answer your question (about getting minBy into the libraries)
but I thought I'd point out some tricks:

On Sat, Feb 20, 2010 at 10:47 AM, Andrew Coppin
andrewcop...@btinternet.com wrote:
  

Also, constructions like

 sortBy (compare `on` foo)

must surely be very common.



Common enough that Data.Ord introduces comparing:

comparing :: (Ord a) = (b - a) - b - b - Ordering
comparing = (compare `on`)
  


Heh. I didn't even notice that Data.Ord existed...


But it would still be useful to have sortOn et al to capture the
common technique when your sorting property is potentially expensive
(sortOn length, for example):

sortOn f = map fst . sortBy (comparing snd) . map (\x - (x, f x))

a technique which I believe is called a Schwarzian transform.
  


Yes, that looks quite useful...


swap_ord (compare x y) = compare y x, so usually flip compare fills
this requirement :)
  


*facepalm*

Damnit, why didn't *I* think of that??

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


[Haskell-cafe] Re: darcs 2.4 release candidate 2

2010-02-20 Thread Petr Rockai
Hi,

Ben Franksen benjamin.frank...@bessy.de writes:
 This rc release is still notably slower on some operations than older
 releases. My test case is a large project named RTEMS (a real-time OS),
 that we wish to import into darcs (at work) to better track our own
 additions and modifications.

 To repeat, download two adjacent releases, e.g.

 wget http://www.rtems.org/ftp/pub/rtems/4.9.0/rtems-4.8.1.tar.bz2
 wget http://www.rtems.org/ftp/pub/rtems/4.9.0/rtems-4.9.0.tar.bz2

 unpack, initialize darcs and record in the 4.8.1 tree, then copy _darcs to
 the 4.9.0 version and try to record -l or whatsnew -l.
[snip]

I am not sure about 2.4, but if this is an important usecase for you, I
might have some good news. I have cobbled together a simple replacement
for the current summary code in darcs whatsnew (right now it does not
handle moves, but that should be relatively easy to add that without
much performance impact).

The code in this form never looks *into* files, it just looks at their
hashes (which are cached in the index), and for the look-for-adds case,
for their presence in the unrecorded state (i.e. if they'd be there
without look-for-adds). In your rtems example, I get following: with
totally cold cache and no index (rm -f _darcs/index ; echo 3 
/proc/sys/vm/drop_caches) about 12 seconds, hot cache  up-to-date
index, less than one second. The reported list looks vaguely correct.

Unfortunately, this is not going to help you for record -lam new
release -- which will still need to construct the whole huge patch in
memory (and fail). I will eventually look into that problem as well, but
this is likely going to be much harder.

 I think this regression should be fixed before 2.4 is released.

Either way, I am not sure how serious this is wrt 2.4. I guess we could
manage a rewrite of the summary code, but that would probably require
another full beta cycle. I would be willing to list this under known
issues, advising people that really need this to stick with 2.3.1 for
now and upgrade to 2.5 which will hopefully improve matters in this
respect (also for rec -lam).

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


Re: [Haskell-cafe] A small oversight

2010-02-20 Thread Daniel Fischer
Am Samstag 20 Februar 2010 13:39:24 schrieb Ben Millwood:
 a technique which I believe is called a Schwarzian transform.

Schwartzian transform, with a tz. After Randal L. Schwartz.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] ANN: HaskellTorrent v0.0

2010-02-20 Thread Jesper Louis Andersen
Hello,

I am happy to announce HaskellTorrent v0.0, a concurrent bittorrent client.

The current release is a rather early preview release: it can download torrents
and seed them, but currently there are many optimizations and tweaks needed
before it is ready for day-to-day use. In particular, we are not yet fully
compliant with the bittorrent specification and need proper clients to connect
to in order to operate. Basically, you install the cabal package and then run

HaskellTorrent foo.torrent

and it will then download the contents of the torrent to the current directory.

We would like to hear about any kind of problems you might encounter. Send them
to jesper.louis.ander...@gmail.com for now. We have only tested the software on
GHC 6.10.4 and 6.12.1, mostly on Unix platforms. It is known to work on
Debian/Unstable with GHC610 and GHC612.

If you want to hack code, get in touch with me. There are something
for any level of skill to do.

***

HaskellTorrent is using the CML library to get concurrency. It easily runs some
200-300 processes when it operates and its process model is somewhat loosely
inspired by the model of Erlang.

Looking forward, the next goal is to optimize concurrency, memory
usage and CPU usage in the client. It currently hogs resources due to
naive data structure choices and some expensive correctness
assertions.

***

Links of interest:

http://hackage.haskell.org/package/HaskellTorrent
http://github.com/jlouis/haskell-torrent

***

This release was brought to you by:

Alex Mason:
A lot of Wireprotocol and BCode serialization work through the use
of the cereal package.

Astro:
GHC 6.12 support.
Support for multi-file torrents.

In addition, patches were contributed by:
  Jesper Louis Andersen
  John Gunderman
  Thomas Christensen

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


[Haskell-cafe] Restricted categories

2010-02-20 Thread Sjoerd Visscher
Hi all,

I want restricted categories, just like the rmonad package provides restricted 
monads. The ultimate goal is to have a product category: 
http://en.wikipedia.org/wiki/Product_category

 {-# LANGUAGE TypeOperators, TypeFamilies, MultiParamTypeClasses, 
 FlexibleInstances, FlexibleContexts, ScopedTypeVariables, 
 UndecidableInstances #-}

 import Prelude hiding ((.), id, fst, snd)
 import qualified Prelude

First lets create a clone of Data.Suitable from the rmonad package, but this 
time for types with two arguments:

 class Suitable2 m a b where
data Constraints m a b
constraints :: m a b - Constraints m a b

 withResConstraints :: forall m a b. Suitable2 m a b = (Constraints m a b - 
 m a b) - m a b
 withResConstraints f = f (constraints undefined :: Constraints m a b)

 withConstraintsOf :: Suitable2 m a b = m a b - (Constraints m a b - k) - k
 withConstraintsOf v f = f (constraints v)

Now we can define a restricted category:

 class RCategory (~) where
   id :: Suitable2 (~) a a = a ~ a
   (.) :: (Suitable2 (~) b c, Suitable2 (~) a b, Suitable2 (~) a c) = b ~ 
 c - a ~ b - a ~ c

(-) (or Hask) is an instance of this class:

 instance Suitable2 (-) a b where
   data Constraints (-) a b = HaskConstraints
   constraints _ = HaskConstraints

 instance RCategory (-) where
   id = Prelude.id
   (.) = (Prelude..)

Now on to the product category. Objects of the product category are types that 
are an instance of IsProduct:

 class IsProduct p where
   type Fst p :: *
   type Snd p :: *
   fst :: p - Fst p
   snd :: p - Snd p

For example:

 instance IsProduct (a, b) where
   type Fst (a, b) = a
   type Snd (a, b) = b
   fst = Prelude.fst
   snd = Prelude.snd

Arrows from the product of category c1 and category c2 are a pair of arrows, 
one from c1 and one from c2:

 data (c1 :***: c2) a b = c1 (Fst a) (Fst b) :***: c2 (Snd a) (Snd b)

The instance for Suitable2 restricts objects to IsProduct, and requires the Fst 
and Snd parts of the objects to be suitable in c1 resp. c2:

 instance (IsProduct a, IsProduct b, Suitable2 c1 (Fst a) (Fst b), Suitable2 
 c2 (Snd a) (Snd b)) = Suitable2 (c1 :***: c2) a b where
   data Constraints (c1 :***: c2) a b = (IsProduct a, IsProduct b, Suitable2 
 c1 (Fst a) (Fst b), Suitable2 c2 (Snd a) (Snd b)) = ProdConstraints
   constraints _ = ProdConstraints

Finally the RCategory instance:

 instance (RCategory c1, RCategory c2) = RCategory (c1 :***: c2) where
   id = withResConstraints $ \ProdConstraints - id :***: id
   -- f@(f1 :***: f2) . g@(g1 :***: g2) = 
   --   withResConstraints $ \ProdConstraints - 
   --   withConstraintsOf f $ \ProdConstraints - 
   --   withConstraintsOf g $ \ProdConstraints - 
   --   (f1 . g1) :***: (f2 . g2)

Here I am running into problems. I get this error:

Could not deduce (Suitable2 c2 (Snd a) (Snd b),
  Suitable2 c1 (Fst a) (Fst b))
  from the context (IsProduct b,
IsProduct c,
Suitable2 c1 (Fst b) (Fst c),
Suitable2 c2 (Snd b) (Snd c))
  arising from a use of `withConstraintsOf'
In the first argument of `($)', namely `withConstraintsOf g'

I don't understand this, as I thought the constraints the error is complaining 
about is just what withConstraintsOf g should provide.
I guess there's something about the Suitable trick that I don't understand, or 
possibly the type families Fst and Snd are biting me.

Who can help me out? Thanks.

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


Re: [Haskell-cafe] Category Theory woes

2010-02-20 Thread Nick Rudnick

A place in the hall of fame and thank you for mentioning clopen... ;-)

Just wanting to present open/closed as and example of improvable maths 
terminology, I oversaw this even more evident defect in it and even 
copied it into my improvement proposal, bordered/unbordered:


It is questionable style to name two properties, if they can occur 
combined, as an antagonistic pair...!


Acccordingly, it is more elegant to draw such terms from independent 
domains.


This subject seems to drive me crazy... I actually pondered on 
improvement, and came to:


«faceless» in replacement of «open»

Rough explanation: The «limit» of a closed set can by the limit of 
another closed set that may even share only this limit -- a faceless set 
has -- under the given perspective -- no such part to «face» to beyond. 
Any comments?


But the big question is now: What (non antagonistic) name can be found 
for the other property??


Any ideas...??

Cheers,

   Nick



Ergonomic terminology comes not for free, giving a quick answer here 
would be «maths style» with replacing


Michael Matsko wrote:

Nick,

Actually, clopen is a set that is both closed and open.  Not one 
that is neither.  Except in the case of half-open intervals, I can't 
remember talking much in topology about sets with a partial boundary.





Alexander Solla wrote:


Clopen means a set is both closed and open, not that it's partially 
bordered.



Daniel Fischer wrote:


And we'd be very wrong. There are sets which are simultaneously open and 
closed. It is bad enough with the terminology as is, throwing in the 
boundary (which is an even more difficult concept than open/closed) would 
only make things worse.
  


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


Re: [Haskell-cafe] Category Theory woes

2010-02-20 Thread Nick Rudnick

Richard O'Keefe wrote:


On Feb 19, 2010, at 2:48 PM, Nick Rudnick wrote:
Please tell me the aspect you feel uneasy with, and please give me 
your opinion, whether (in case of accepting this) you would rather 
choose to consider Human as referrer and Int as referee of the 
opposite -- for I think this is a deep question.

I've read enough philosophy to be wary of treating reference
as a simple concept.  And linguistically, referees are people
you find telling rugby players naughty naughty.  Don't you
mean referrer and referent?
Yes, thanks. I am not a native English speaker, and in my mother tongue, 
a referent is somebody who refers, so I missed the guess... Such 
statements are exactly what I was looking for... So, as a reference is 
directed, it is possible to distinguish


referrer ::= the one which does refer to s.th.

referent ::= one which is referred to by s.th.

Of course a basic point about language is that the association
between sounds and meanings is (for the most part) arbitrary.
I would rather like to say it is not strictly determined, as an 
evolutionary tendence towards, say ergonomy, cannot be overlooked, can it?



Why should the terminology of mathematics be any different?

;-) Realizing an evolutionary tendence towards ergonony, is my subject...

Why is a small dark floating cloud, indicating rain, called
a water-dog?  Water, yes, but dog?  Why are the brackets at
each end of a fire-place called fire-dogs?  Why are unusually
attractive women called foxes (the females of that species
being vixens, and both sexes smelly)?  
:-)) The shape of the genitals, which might come into associative 
imagination of the hopeful observer?? (The same with cats, bears, etc.) 
[... desperately afraid of getting kicked out of this mailing list ;-))]


Thanks for this beautiful example and, honestly, again I ask again 
whether we may regard this as «just noise»: In contrary, aren't such 
usages not paradigmatical examples of memes, which as products of 
memetic evolution, should be studied for their motivational value?


Let me guess: Our cerebral language system is highly coupled with our 
intentional system, so that it helps learning to have motivating 
«animation» enclosed... Isn't this in use in contemporary learning 
environments...?


The problem I see is that common maths claims an exception in claiming 
that, in it's domain, namings are no more than noise -- possible 
motivated by an extreme rejection of anything between «strictly formally 
determined» and «noise». This standpoint again does not realize the 
developments in foundations of mathematics of at least the century ago 
-- put roughly, this comes close to Hilbert's programme...


To my mind, any of the breakthroughs of the last decades -- like 
incompleteness, strange attractors, algorithmic information theory, 
CCCs, and not the least computing science itself with metaprogramming, 
soft computing, its linear types/modes and monads (!) -- have to do with 
constructs which emancipate such claims of ex ante predetermination. 
Isn't category theory pretty much a part of all this?



What's the logic in
doggedness being a term of praise but bitchiness of opprobrium?

Sexism...??


We can hope for mathematical terms to be used consistently,
but asking for them to be transparent is probably too much to
hope for.  (We can and should use intention-revealing names
in a program, but doing it across the totality of all programs
is something never achieved and probably never achievable.)
We have jokers: Evolutionary media, like markdown or even stylesheet may 
allow us to switch and translate in a moment, and many more useful 
gimmicks... Online collaboration platforms...


And we can stay pragmatical: If we can reach a (broad, to my 
estimate...) public, which originally would have to say «the book has 
really left me dumbfounded» (so the originator of this thread) and offer 
them an entertaining intuitive way -- why not even in a 
self-configurable way? -- category theory could be introduced to 
contemporary culture.


Personally, I can't accept statements like (in another posting) «You 
need a lot of training in abstraction to learn very abstract concepts. 
Joe Sixpack's common sense isn't prepared for that.»


Instead, I think that there is good evidence to believe that there are 
lots of isomorphisms to be found between every day's life and 
terminology and concepts category theory -- *not* to be confused with 
its *applications to maths*...


And, to close in your figurative style:

Which woman gets hurt by a change of clothes?

Cheers,

Nick




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


[Haskell-cafe] Re: Heterogeneous Data Structures - Nested Pairs and functional references

2010-02-20 Thread Heinrich Apfelmus
Alexander Solla wrote:

 So, the type (View view) -- ignoring class instances -- is basically
 isomorphic to this (slightly simpler) type:
 
 data View = EmptyView | TextView String | ConcatView View View |
 NestViews View View View | ...
 instance Monoid View where ...
 
 Now, consider the problem of generic programming on the simpler
 type:   you quantify over the data constructors generically, and in
 doing so you gain traversals for the type.[1]  You gain the same
 things by turning View into (View view) --  a functor, a foldable
 functor, and so on.  When it comes time to render a format for a View
 (for example, a bit of Html from Text.XHtml.Strict), I use some higher
 order functions I'm already familiar with.  Something like
 
 renderXHtml :: (View view) - Html
 renderXHtml (ConcatViews l r) = fold $ renderXHtml (ConcatViews l r)
 renderXHtml (NestViews l m r) = fold $ renderXHtml (NestViews l m r)
 renderXHtml (TextView string) = stringToHtml string
 renderXHtml (PageView v_title, v_heading, v_header, v_footer, v_contents) =
 (the_title  (renderXHtml v_title)) +++ -- (We assume
 v_title is a TextView String)
 (body  ( renderXHtml v_header )  +++
  (render_page_contents v_contents v_heading) +++
  (renderXHtml v_footer) )
  where render_page_contents contents heading = undefined

But isn't the line

renderXHtml (ConcatView l r) = fold $ renderXHtml (ConcatViews l r)

a type error? I'm assuming

Data.Foldable.fold :: (Foldable m, Monoid t) = m t - t

being applied to the result type of  renderXHtml  which is  Html  and
not of the form  m t .


Your intention reminds me of the use of type variables to get
functor-like behavior for free, like in

data RGB' a = RGB a a a   -- auxiliary type constructor
type RGB = RGB' Int   -- what we're interested in

instance Functor RGB' where
fmap f (RGB x y z) = RGB (f x) (f y) (f z)

mapRGB :: (Int - Int) - RGB - RGB
mapRGB = fmap

but I don't quite see what you're doing with the free monad here, Alexander?


Regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com

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


Re: [Haskell-cafe] Restricted categories

2010-02-20 Thread Alexander Solla


On Feb 20, 2010, at 10:29 AM, Sjoerd Visscher wrote:

I don't understand this, as I thought the constraints the error is  
complaining about is just what withConstraintsOf g should provide.
I guess there's something about the Suitable trick that I don't  
understand, or possibly the type families Fst and Snd are biting me.


Who can help me out? Thanks.


You specifically ask withConstraintsOf to accept only Suitable2's when  
you say


withConstraintsOf :: Suitable2 m a b = m a b - (Constraints m a b - 
 k) - k


But you aren't saying that the argument of withConstraintsOf IS a  
Suitable2, when you say:



instance (RCategory c1, RCategory c2) = RCategory (c1 :***: c2) where
id = withResConstraints $ \ProdConstraints - id :***: id
-- f@(f1 :***: f2) . g@(g1 :***: g2) =
--   withResConstraints $ \ProdConstraints -
--   withConstraintsOf f $ \ProdConstraints -
--   withConstraintsOf g $ \ProdConstraints -
--   (f1 . g1) :***: (f2 . g2)



You need to make a type class instance for Suitable2 for whatever type  
\ProdConstraints - ... represents.


For comparison, try:

 data Unordered = A | B | C

 A = B
   No instance for (Ord Unordered)
 arising from a use of `=' at interactive:1:0-5
   Possible fix: add an instance declaration for (Ord Unordered)
   In the expression: A = B
   In the definition of `it': it = A = B

and now:

 data Ordered = A | B | C deriving (Show, Eq, Ord) -- (easier than  
writing an instance for Eq, Ord)

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


Re: [Haskell-cafe] Pointfree composition for higher arity

2010-02-20 Thread Matt Hellige
On Fri, Feb 19, 2010 at 10:42 PM, wren ng thornton w...@freegeek.org wrote:
 Sean Leather wrote:

 The second option approaches the ideal pointfreeness (or pointlessness if
 you prefer), but I'd like to go farther:

 (...) :: (c - d) - (a - b - c) - a - b - d

 (...) f g x y = f (g x y)
 infixr 9 ...

 I go with infixl 8 personally. It seems to play better with some of the
 other composition combinators.

 In a somewhat different vein than Oleg's proposed general composition, I've
 particularly enjoyed Matt Hellige's pointless fun combinators[0]. I have a
 version which also adds a strict application combinator in my desiderata
 package[1] so we can say things like:

    foo $:: bar ~ baz !~ bif

 which translates to:

    \a b - bif (foo (bar a) (baz $! b))

 These combinators are especially good when you don't just have a linear
 chain of functions.


Thanks! I'm glad to know that people have found this approach useful.
In cases where it works, I find it somewhat cleaner than families of
combinators with (what I find to be) rather obscure names, or much
worse, impenetrable sections of (.). We can write the original example
in this style:
  fun = someFun someDefault $:: id ~ id ~ runFun
but unfortunately, while it's both pointfree and fairly clear, it
isn't really an improvement over the pointful version, IMHO.

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


[Haskell-cafe] ANNOUNCE: ioctl 0.0.1

2010-02-20 Thread Maciej Piechotka
A package for type-safe I/O control. Currently only ioctl is supported.

Currently simply a extract from my tuntap fork

TODO:
- Return the integer as well as structure (will break the API)
- Port for Windows Network.Socket.IOCtl (as soon as I manage to setup
some sane environment on this platform)
- Wrapping around DeviceIoControl

Example (in hsc):
data NotRead = NotRead
instance NotRead Int where
ioctlReq _ = #const FIONREAD

notRead s = ioctlsocket' s NotRead

Regards



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


Re: [Haskell-cafe] Re: Heterogeneous Data Structures - Nested Pairs and functional references

2010-02-20 Thread Alexander Solla


On Feb 20, 2010, at 10:25 AM, Heinrich Apfelmus wrote:


But isn't the line

   renderXHtml (ConcatView l r) = fold $ renderXHtml (ConcatViews l r)

a type error? I'm assuming

   Data.Foldable.fold :: (Foldable m, Monoid t) = m t - t

being applied to the result type of  renderXHtml  which is  Html  and
not of the form  m t .


Yup, that's a type error.  I mean to fold the View (in this case a  
ConcatView) into a monoid.  I think I meant


 foldMap renderXHtml (ConcatViews l r)


Your intention reminds me of the use of type variables to get
functor-like behavior for free, like in

   data RGB' a = RGB a a a   -- auxiliary type constructor
   type RGB = RGB' Int   -- what we're interested in

but I don't quite see what you're doing with the free monad here,  
Alexander?


As you noticed, I am seeking that functorial behavior in order to gain  
some genericity.  bind and return do encode some logic about the  
nature of monadic adjunction, which I am relying on theoretically.  I  
could have used a Functor instance just as easily, but I would have  
lost my intention of defining co-equalizers implicitly.  (http://en.wikipedia.org/wiki/Beck%27s_monadicity_theorem 
)


Also, it was easier to write a monad instance than an Applicative  
instance, at least on my first try. ;-)

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


Re: [Haskell-cafe] Pointfree composition for higher arity

2010-02-20 Thread wren ng thornton

Matt Hellige wrote:

Thanks! I'm glad to know that people have found this approach useful.
In cases where it works, I find it somewhat cleaner than families of
combinators with (what I find to be) rather obscure names, or much
worse, impenetrable sections of (.). We can write the original example
in this style:
  fun = someFun someDefault $:: id ~ id ~ runFun
but unfortunately, while it's both pointfree and fairly clear, it
isn't really an improvement over the pointful version, IMHO.


For something this simple it's not too helpful. But, one of the places 
it really shines is when dealing with newtypes in order to clean up the 
wrapping/unwrapping so they don't obscure the code.


--
Live well,
~wren
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Restricted categories

2010-02-20 Thread Nick Rudnick

Alexander Solla wrote:
You specifically ask withConstraintsOf to accept only Suitable2's when 
you say
withConstraintsOf :: Suitable2 m a b = m a b - (Constraints m a b 
- k) - k


But you aren't saying that the argument of withConstraintsOf IS a 
Suitable2, when you say:

instance (RCategory c1, RCategory c2) = RCategory (c1 :***: c2) where
id = withResConstraints $ \ProdConstraints - id :***: id
-- f@(f1 :***: f2) . g@(g1 :***: g2) =
-- withResConstraints $ \ProdConstraints -
-- withConstraintsOf f $ \ProdConstraints -
-- withConstraintsOf g $ \ProdConstraints -
-- (f1 . g1) :***: (f2 . g2) 
As I understand, Sjoerd expects this to be done at the definition of (.) 
in the type class RCategory, so that an instance method can relay on the 
constraints collected by it:

class RCategory (~) where
  id :: Suitable2 (~) a a = a ~ a
  (.) :: (Suitable2 (~) b c, Suitable2 (~) a b, Suitable2 (~) a c) = b ~ c - a 
~ b - a ~ c
  
A simple example: 


class Show el= ExceptionNote el where
comment:: Show exception= exception- el- String

instance ExceptionNote Int where
comment exception refId = show refId ++ :  ++ show exception

Here you don't need to constrain ?exception? to be of ?Show? at the 
instance declaration. So it does not appear wrong for Sjoerd to expect f 
and g to already be of Suitable2...


This is exciting stuff, I am really a little astonished about the giant 
leap Haskell has made since my efforts to translate the examples of 
Rydeheart  Burstall, which actually was my intro to categories, from ML 
to Haskell. This looks very elegant... Maybe it's time for a second 
edition of the unique approach of Rydeheart  Burstall on basis of 
Haskell? Wow, really cool stuff... :-)


Cheers,

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


Re: [Haskell-cafe] A small oversight

2010-02-20 Thread Leon Smith
On Sat, Feb 20, 2010 at 5:47 AM, Andrew Coppin
andrewcop...@btinternet.com wrote:

  sortOn :: (Ord y) = (x - y) - [x] - [x]
  sortOn foo = sortBy (compare `on` foo)


Incidentally,  this function is provided as Data.List.Ordered.sortOn'
in the data-ordlist package...


On Sat, Feb 20, 2010 at 7:39 AM, Ben Millwood hask...@benmachine.co.uk wrote:
 But it would still be useful to have sortOn et al to capture the
 common technique when your sorting property is potentially expensive
 (sortOn length, for example):

 sortOn f = map fst . sortBy (comparing snd) . map (\x - (x, f x))

 a technique which I believe is called a Schwar[t]zian transform.

An older name for this technique is decorate-sort-undecorate.
Data-ordlist also provides this as Data.List.Ordered.sortOn

http://hackage.haskell.org/package/data-ordlist

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