Re: [Haskell-cafe] why cannot i get the value of a IORef variable ?

2009-10-22 Thread Gregory Crosswhite
For clarity, one trick that uses unsafePerformIO which you may have  
seen posted on this list earlier today is the following way of  
creating a globally visible IORef:


import Data.IORef
import System.IO.Unsafe

*** counter = unsafePerformIO $ newIORef 0 ***

next = do
  modifyIORef counter (+1)
  readIORef counter

The key line has been starred;  this created a counter IORef that  
next could access without having to be explicitly given it on each  
call.


Personally, though, I never use this approach because it creates a  
race condition.  Because Haskell is lazy, counter will not be  
evaluated until the first time that it is accessed.  If two threads  
access it at the same time before it has been evaluated, then in GHC  
there are conditions under which they could both evaluate it and  
create a new IORef simultaneously, resulting in each thread  
effectively ending up with its own counter variable when you  
specifically wanted a globally shared counter variable.


This is why I personally use the ReaderT monad if I want to implicitly  
share a global IORef, rather than using this trick.


Cheers,
Greg


On Oct 21, 2009, at 10:50 PM, Colin Paul Adams wrote:



   zaxis thank you! In fact i really donot understand
   zaxis unsafePerformIO very much !

Then all you have to understand is - never use it!
--
Colin Adams
Preston Lancashire
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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


Re: [Haskell-cafe] Time Typeable Instances

2009-10-22 Thread Warren Harris

Hi John,

I just stumbled on this issue while trying to compile turbinado with  
the haskell platform / ghc 6.10.4. I got past it by manually editing  
back in the time definitions, but just wondering if there was an  
official resolution. Thanks,


Warren


On Oct 13, 2009, at 6:48 AM, John Goerzen wrote:


Hugo Gomes wrote:

The Glorious Glasgow Haskell Compilation System, version 6.10.4
with old-time-1.0.0.2 and time-1.1.2.4

This is a standard haskell platform on a windows xp. Cabal install
didn't work complaining about missing instances of typeable for posix
time and other datatypes, yet, after removing the macros (thus,  
adding

those instances), hdbc and convertible compiled and installed fine.

Removing the macros might be a bit overkill, probably finetuning  
them so

that they add only the necessary instances for typeable in ghc  610
might be a better solution.


I'm going to CC haskell-cafe on this because I am confused.

Hugo, can you confirm what version of convertible you have?

Back on May 21, I started a thread [1] on haskell-cafe complaining  
that
GHC 6.10.3 included a newer time that included instances of Typeable  
for

NominalDiffTime and UTCTime.  This broke my code, which had manually
defined instances for these types, as they were needed.  Things got
complicated, as only time's minor version number got incremented
(x.x.x.Y) [2].  Cabal can't test against that version number.

I wanted my code to work with old and new versions of GHC.  Since
testing against the version of time was impossible, I did the next  
best

thing: tested against the version of GHC.

#if __GLASGOW_HASKELL__ = 610
-- instances added in GHC 6.10.3
#else
instance Typeable NominalDiffTime where
   typeOf _ = mkTypeName NominalDiffTime

instance Typeable UTCTime where
   typeOf _ = mkTypeName UTCTime
#endif

Also, in the .cabal, there is a build-depends on time=1.1.2.4.

Now, that would break for GHC 6.10.1 and 6.10.2 users, but will work  
for

6.10.3 and above, or 6.8 and below.  Or so I thought.

Now I'm getting complaints from people using 6.10.4 saying that there
are now missing instances of Typeable with time 1.1.2.4.

1) Did the Typeable instances get dropped again from time?
2) What exactly should I do so this library compiles on GHC 6.8 and  
6.10.x?


I'm looking at the darcs repo for time and don't see the instances  
ever

getting dropped.

[1] http://osdir.com/ml/haskell-cafe@haskell.org/2009-05/msg00982.html
[2] http://osdir.com/ml/haskell-cafe@haskell.org/2009-05/msg00985.html


 later addendum 

so it appears that what's happening here is that GHC 6.10.3 extralibs
included time 1.1.3, but then haskell-platform standardized on  
1.1.2.4.
This is pretty annoying -- that haskell-platform would standardize  
on a
version older than what shipped with a GHC release -- but I guess I  
can

work around it by restricting my build-dep to be time  1.1.3 and
re-adding the instances.

Does this sound right?




On Tue, Oct 13, 2009 at 2:51 AM, John Goerzen jgoer...@complete.org
mailto:jgoer...@complete.org wrote:

   Hugo Gomes wrote:

Hi,

convertible and hdbc packages fail to compile in my standard

   instalation
of the haskell platform. I think this has to do with those if ghc  
=

610 macros on the typeable instances for some datatypes. I

   removed them

and now they work fine...






   What version of GHC and time do you have?

   -- John




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


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


Re[2]: [Haskell-cafe] Is there a null statement that does nothing?

2009-10-22 Thread Bulat Ziganshin
Hello michael,

Thursday, October 22, 2009, 4:59:43 AM, you wrote:

return () does the trick if another branch also returns ()

 Thanks guys,

 I understand what you're telling me, but have some nested IFs and
 just want to fall through on one of the ELSES but then I end up with
 two ELSES in a row and nothing between them. Oh, well, on to restructuring.

 Michael 

 --- On Wed, 10/21/09, Tim Wawrzynczak inforichl...@gmail.com wrote:

 From: Tim Wawrzynczak inforichl...@gmail.com
 Subject: Re: [Haskell-cafe] Is there a null statement that does nothing?
 To: michael rice nowg...@yahoo.com
 Cc: haskell-cafe@haskell.org
 Date: Wednesday, October 21, 2009, 8:49 PM

 Yes, an if statement must have both 'then' and 'else' branches.  As an 
 example, what if you had

 let a = if b == 2 then True  else False

 and you were missing an else branch?  What would 'a' get assigned to?
  
 The if statement returns a value so must have both branches.

 However, in a monadic constraint, there are the functions 'when'
 and 'unless.'  They allow conditional evaluation of expressions in a monadic 
 context.  For example,
  
 main = do
   line - getLine
   when (line == hello) putStrLn Hello back!

 Cheers,
  - Tim


 On Wed, Oct 21, 2009 at 7:43 PM, michael rice nowg...@yahoo.com wrote:
  
 It looks like both the THEN and the ELSE in an IF expression must
 each have an expression. What's a graceful way to do nothing in
 either or both slots, kind of like the Fortran CONTINUE statement. 

   --mr

  

 [mich...@localhost ~]$ ghci
 GHCi, version 6.10.3: http://www.haskell.org/ghc/  :? for help
 Loading package ghc-prim ... linking ... done.
 Loading package integer ... linking ... done. 
 Loading package base ... linking ... done.
Prelude if (1==1) then else

 interactive:1:15: parse error on input `else'
Prelude if (1==1) then True else
  
 interactive:1:24: parse error (possibly incorrect indentation)
Prelude if (1==1) then True else False
 True
Prelude 


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


  


   


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

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


[Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread zaxis

aaa - newIORef ([]::[(Int,Int)])
writeIORef aaa [(1,1),(2,2),(3,3)] 

then if i want to change aaa to [(1,1),(2,222),(3,3)] , what's the best way
? 
re-write aaa is not permitted.

Sincerely!
-- 
View this message in context: 
http://www.nabble.com/How-can-i-safely-change-the-value-of-specified-key---tp26005244p26005244.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] How can i safely change the value of specified key ?

2009-10-22 Thread minh thu
2009/10/22 zaxis z_a...@163.com:

 aaa - newIORef ([]::[(Int,Int)])
 writeIORef aaa [(1,1),(2,2),(3,3)]

 then if i want to change aaa to [(1,1),(2,222),(3,3)] , what's the best way
 ?
 re-write aaa is not permitted.

Why do you say that ? You can use again writeIORef of modifyIORef to
change aaa's content.

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


Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread Bulat Ziganshin
Hello zaxis,

Thursday, October 22, 2009, 11:28:14 AM, you wrote:

 aaa - newIORef ([]::[(Int,Int)])
 writeIORef aaa [(1,1),(2,2),(3,3)] 

 then if i want to change aaa to [(1,1),(2,222),(3,3)] , what's the best way
 ? 
 re-write aaa is not permitted.

it's the only way. in Haskell, you have *immutable* values. aaa is a
reference to immutable value. you can mutate reference so it will
point to another immutable value but you cannot change this value.

there are two ways to make aaa==[(1,1),(2,222),(3,3)]. first, you can
apply function to whole value:

value - readIORef aaa
writeIORef aaa (f value)

second, you may create list of IORefs, tuple of IORefs and so:

a - newIORef (1,1)
...
let aaa = [a,b,c]

now aaa is a immutable list of mutable IORefs. of course, you can
create IORef pointing to list of IORefs too



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

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


[Haskell-cafe] What's this pattern called?

2009-10-22 Thread Martijn van Steenbergen

Bonjour café,


data ExprF r
  =  Add  r  r
  |  Sub  r  r
  |  Mul  r  r
  |  Div  r  r
  |  Num  Int


This is a well-known pattern that for example allows nice notation of 
morphisms. But what is it called? I've heard fixed-point view, open 
datatypes and some others, but I'm curious where this pattern comes up 
in literature and what it is called there.


Thanks,

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


Re: [Haskell-cafe] Simple but interesting (for me) problem

2009-10-22 Thread minh thu
2009/10/21 Gregory Crosswhite gcr...@phys.washington.edu:
 And just because this has not been explicitly stated:  it's not just for
 aesthetic reasons that you couldn't do this with a pure function, but
 because it violates the semantics and gets you the wrong result.  So for
 example, if you modified Tim's code to be

 import Data.IORef
 import System.IO.Unsafe
 mkNext :: (Num a) = IO a
 mkNext = do
   ref - newIORef 0
   return . unsafePerformIO $
  do
modifyIORef ref (+1)
readIORef ref
 main :: IO ()
 main = do
   foo - mkNext
   print foo
   print foo
   print foo

 Then the output that you will see (with GHC at least) is
 1
 1
 1
 because the compiler assumes that it only needs to evaluate foo once, after
 which it can cache the result due to assumed referential transparency.
 - Greg

This is indeed wrong, but not how you think it is.

The code you pass to unsafePerformIO has type Num a = IO a, so the
value passed to return has type Num a. So foo has type Num a too and
its value is 1.

Exactly like in

mkNext = do
  ref - newIORef 0
  modifyIORef ref (+1)
  readIORef ref

which is a complicated way to write

mkNext = return 1

Now, it's clear that foo has value 1 and printing it three times
should output three 1. The whole point of having mkNext return an
action (that should be called next, and not foo, as it is much
clearer) in previous code was too be able to execute it multiple times
and having it return a new value.

In general, expecting

print bar
print bar
print bar

outputing three different things is wrong, as bar should be pure. If
bar is not pure, then it should be
a - bar
print a
b - bar
print b
c - bar
print c

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


Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread Bulat Ziganshin
Hello zaxis,

Thursday, October 22, 2009, 11:28:14 AM, you wrote:

 then if i want to change aaa to [(1,1),(2,222),(3,3)] , what's the best way

... well, anyway what you are doing isn't very haskellish. it may be
considered as advanced topic but basically, best way to compute
something in Haskell is to construct pure function


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

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


Re: [Haskell-cafe] What's this pattern called?

2009-10-22 Thread Sebastian Fischer

Hi Martijn,

On Oct 22, 2009, at 9:47 AM, Martijn van Steenbergen wrote:

I've heard fixed-point view, open datatypes and some others, but I'm  
curious where this pattern comes up in literature and what it is  
called there.


Tim Sheard and Emir Pasalic call this technique two-level types in  
their JFP'04 paper Two-Level Types and Parameterized Modules:


http://homepage.mac.com/pasalic/p2/papers/JfpPearl.pdf

Cheers,
Sebastian


--
Underestimating the novelty of the future is a time-honored tradition.
(D.G.)



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


Re: [Haskell-cafe] What's this pattern called?

2009-10-22 Thread Sean Leather
  I've heard fixed-point view, open datatypes and some others, but I'm
 curious where this pattern comes up in literature and what it is called
 there.


 Tim Sheard and Emir Pasalic call this technique two-level types in their
 JFP'04 paper Two-Level Types and Parameterized Modules:

http://homepage.mac.com/pasalic/p2/papers/JfpPearl.pdf


Apparently from reading Section 2 of that paper, they would call ExprF
non-recursive type the structure operator. I think, by itself, the type
ExprF doesn't mean much. It really matters how it's used to determine what
you call it.

Martijn already mentioned the fixed-point view, but that only makes since in
the context of something like Fix:

 data ExprF r  = Add r r | Sub r r | Mul r r | Div r r | Num Int
 newtype Fix f = In (f (Fix f))
 type Expr = Fix ExprF

I believe the F suffix that Martijn used typically means functor, and I
sometimes call types like this functors (with an optional but obvious
instance of Functor).

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


[Haskell-cafe] Is there in Haskell the eval function?

2009-10-22 Thread Waldemar Biernacki
Hello!

I have a very stupid question. Sorry for that!

Is there the eval function like in imperative languages? 

I'd like to write an application which has to be compiled to exec file. It is 
neccessary to performe some additional procedures which are unknown at the 
moment of the compilition.

Regards
wb.

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


Re: [Haskell-cafe] Is there in Haskell the eval function?

2009-10-22 Thread Martijn van Steenbergen

Waldemar Biernacki wrote:
Is there the eval function like in imperative languages? 


You mean like in interpreted languages?


I'd like to write an application which has to be compiled to exec file. It is 
neccessary to performe some additional procedures which are unknown at the 
moment of the compilition.


You could use the GHC libraries, or Hint which is built on top of GHC. 
I've used it before and it works pretty nicely.


http://hackage.haskell.org/package/hint

HTH,

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


[Haskell-cafe] NetSnmp problem.

2009-10-22 Thread Magicloud Magiclouds
Hi,
  I am trying to use NetSnmp to get some information from my switch.
And I met this problem.
  After initialize, I used snmpWalk to get some information, and
dealed with it. Everything is fine. Then I sleep for 5 mins. SnmpWalk
again. But this time, all asnValues were unsupported format, which
broke my dealing process.
  Why? I am sure my switch and OID, bluh bluh bluh are correct. They
worked in ruby script.
-- 
竹密岂妨流水过
山高哪阻野云飞
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread zaxis

value - readIORef aaa
writeIORef aaa (f value) 
then aaa will *point to* a new value. The original value will be Garbage
Collected, right ?
BTW, 
Is [(1,1),(2,2),(3,3)] been regarded as a hash ? If not, what is the best
way to change it to [(1,1),(2,),(3,3)] in function `f` ?


Bulat Ziganshin-2 wrote:
 
 Hello zaxis,
 
 Thursday, October 22, 2009, 11:28:14 AM, you wrote:
 
 aaa - newIORef ([]::[(Int,Int)])
 writeIORef aaa [(1,1),(2,2),(3,3)] 
 
 then if i want to change aaa to [(1,1),(2,222),(3,3)] , what's the best
 way
 ? 
 re-write aaa is not permitted.
 
 it's the only way. in Haskell, you have *immutable* values. aaa is a
 reference to immutable value. you can mutate reference so it will
 point to another immutable value but you cannot change this value.
 
 there are two ways to make aaa==[(1,1),(2,222),(3,3)]. first, you can
 apply function to whole value:
 
 value - readIORef aaa
 writeIORef aaa (f value)
 
 second, you may create list of IORefs, tuple of IORefs and so:
 
 a - newIORef (1,1)
 ...
 let aaa = [a,b,c]
 
 now aaa is a immutable list of mutable IORefs. of course, you can
 create IORef pointing to list of IORefs too
 
 
 
 -- 
 Best regards,
  Bulatmailto:bulat.zigans...@gmail.com
 
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 

-- 
View this message in context: 
http://www.nabble.com/How-can-i-safely-change-the-value-of-specified-key---tp26005244p26006471.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] How can i safely change the value of specified key ?

2009-10-22 Thread Ketil Malde
zaxis z_a...@163.com writes:

value - readIORef aaa
writeIORef aaa (f value) 

 then aaa will *point to* a new value. 

Exactly.  That's what IORefs are, references pointing to contents that
can be changed in the IO monad.

 The original value will be Garbage Collected, right ?

Yes, unless something else is holding on to it.

 Is [(1,1),(2,2),(3,3)] been regarded as a hash? 

It's a list of pairs.  You can treat it as an association list using
e.g. 'lookup' from Data.List, if that's what you mean.

 If not, what is the best way to change it to [(1,1),(2,),(3,3)] in
 function `f` ? 

Not sure if there's anything in Data.List, but you could do something
like: 

  f xs = (2,) : filter ((/=2) . fst) xs

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re[2]: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread Bulat Ziganshin
Hello zaxis,

Thursday, October 22, 2009, 1:03:21 PM, you wrote:

value - readIORef aaa
writeIORef aaa (f value) 
 then aaa will *point to* a new value. The original value will be Garbage
 Collected, right ?

yes, exactly

 BTW, 
 Is [(1,1),(2,2),(3,3)] been regarded as a hash ? If not, what is the best
 way to change it to [(1,1),(2,),(3,3)] in function `f` ?

hm, i'm frequently use lists of tuples as dictionaries, there is
Data.List.lookup function that perfroms lookup. but these are stored
naively so it's useful only for small dictionaries. if you need hash,
you can use Data.Hashtable module (it provides hash with imperative
access), or use Data.Map that has pure interface. again, for the first
time i strongly recommend to learn Data.Map. if you will learn Haskell
as imperative language, you will find it slow, awkward and useless. if
you will learn functional features and only then go to imperative
part, you may find it much more pleasant

just one example from my code:

  repeat_while (receiveP pipe) notTheEnd $ \(FileStart fi) - do
extract_file command fi $ \writer - do
  repeat_while (receiveP pipe) notDataEnd (\(DataChunk buf len) - do 
writer buf len; send_backP pipe (buf,len))

yes, it's imperative but it includes several lambdas per each line


 Bulat Ziganshin-2 wrote:
 
 Hello zaxis,
 
 Thursday, October 22, 2009, 11:28:14 AM, you wrote:
 
 aaa - newIORef ([]::[(Int,Int)])
 writeIORef aaa [(1,1),(2,2),(3,3)] 
 
 then if i want to change aaa to [(1,1),(2,222),(3,3)] , what's the best
 way
 ? 
 re-write aaa is not permitted.
 
 it's the only way. in Haskell, you have *immutable* values. aaa is a
 reference to immutable value. you can mutate reference so it will
 point to another immutable value but you cannot change this value.
 
 there are two ways to make aaa==[(1,1),(2,222),(3,3)]. first, you can
 apply function to whole value:
 
 value - readIORef aaa
 writeIORef aaa (f value)
 
 second, you may create list of IORefs, tuple of IORefs and so:
 
 a - newIORef (1,1)
 ...
 let aaa = [a,b,c]
 
 now aaa is a immutable list of mutable IORefs. of course, you can
 create IORef pointing to list of IORefs too
 
 
 
 -- 
 Best regards,
  Bulatmailto:bulat.zigans...@gmail.com
 
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 




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

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


Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread zaxis

f xs = (2,) : filter ((/=2) . fst) xs 
It works but not general as `f` still needs to change other value according
to the KEY.
Maybe Data.List will supply what i need.


Ketil Malde-5 wrote:
 
 zaxis z_a...@163.com writes:
 
value - readIORef aaa
writeIORef aaa (f value) 
 
 then aaa will *point to* a new value. 
 
 Exactly.  That's what IORefs are, references pointing to contents that
 can be changed in the IO monad.
 
 The original value will be Garbage Collected, right ?
 
 Yes, unless something else is holding on to it.
 
 Is [(1,1),(2,2),(3,3)] been regarded as a hash? 
 
 It's a list of pairs.  You can treat it as an association list using
 e.g. 'lookup' from Data.List, if that's what you mean.
 
 If not, what is the best way to change it to [(1,1),(2,),(3,3)] in
 function `f` ? 
 
 Not sure if there's anything in Data.List, but you could do something
 like: 
 
   f xs = (2,) : filter ((/=2) . fst) xs
 
 -k
 -- 
 If I haven't seen further, it is by standing in the footprints of giants
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 

-- 
View this message in context: 
http://www.nabble.com/How-can-i-safely-change-the-value-of-specified-key---tp26005244p26006947.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[2]: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread Bulat Ziganshin
Hello zaxis,

Thursday, October 22, 2009, 1:03:21 PM, you wrote:

 Is [(1,1),(2,2),(3,3)] been regarded as a hash ? If not, what is the best
 way to change it to [(1,1),(2,),(3,3)] in function `f` ?

f = map (\x@(a,b) - if a==2 then (a,) else x)

or

f xs = [(a, if a==2 then  else b) | (a,b) - xs]


but anyway, modifying one value inside complex datastructure is not FP
way. you should consider writing a function that maps your entire
input data to output one


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

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


Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread minh thu
2009/10/22 zaxis z_a...@163.com:

f xs = (2,) : filter ((/=2) . fst) xs
 It works but not general as `f` still needs to change other value according
 to the KEY.
 Maybe Data.List will supply what i need.

I don't think Data.List has what you want as-is. The above code is
generalized simply:
replace k v xs = (k,v) : filter ((/=v) . fst) xs

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


Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread zaxis

replace k v xs = (k,v) : filter ((/=v) . fst) xs
Great !  thanks you very much


minh thu wrote:
 
 2009/10/22 zaxis z_a...@163.com:

f xs = (2,) : filter ((/=2) . fst) xs
 It works but not general as `f` still needs to change other value
 according
 to the KEY.
 Maybe Data.List will supply what i need.
 
 I don't think Data.List has what you want as-is. The above code is
 generalized simply:
 replace k v xs = (k,v) : filter ((/=v) . fst) xs
 
 Cheers,
 Thu
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 

-- 
View this message in context: 
http://www.nabble.com/How-can-i-safely-change-the-value-of-specified-key---tp26005244p26007076.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] How can i safely change the value of specified key ?

2009-10-22 Thread minh thu
2009/10/22 zaxis z_a...@163.com:

replace k v xs = (k,v) : filter ((/=v) . fst) xs
 Great !  thanks you very much

I'm not sure why you're so much happy:

Assume some function defined as follow:

foo a b c = e x y z a b c
  where x = some constant
y = some other constant
z = some other constant

'e' means just that the complete body of the function foo involves x,
y, z, a, b and c. Generalizing foo to let the caller choose the values
of x, y, z is just

foo a b c x y z = e x y z a b c
  -- the where is unneeded

In fact, this is what functions are made for: generalizing a given
expression so that part of the expression can be given in arguements.

E.g. turning

e a b

into

f a = e a b

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


Re: [Haskell-cafe] pretty printing with comments

2009-10-22 Thread Niklas Broberg
 I'll see when I can get to fixing it, hopefully it won't be long.

Two fairly easy bugs to fix, darcs repo is updated. It's probably high
time I did another release, stay tuned.

I just can't believe I didn't have a single class declaration in my
test suite to catch the first one! Or that no one else triggered it
before (I guess no one has actually started using exactPrint in their
code yet).

Thanks again for reporting! :-)

Cheers,

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


Re: [Haskell-cafe] Is there in Haskell the eval function?

2009-10-22 Thread Waldemar Biernacki
Thank you Martijn!

 You mean like in interpreted languages?
Naturally! My mistake.

  I'd like to write an application which has to be compiled to exec file. It 
  is neccessary to performe some additional procedures which are unknown at 
  the moment of the compilition.
 
 You could use the GHC libraries, or Hint which is built on top of GHC. 
 I've used it before and it works pretty nicely.
 
 http://hackage.haskell.org/package/hint

At first sight it seems be what I'm looking for. I'll try.

Thank you for the help!

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


Re: [Haskell-cafe] What's this pattern called?

2009-10-22 Thread Daniel Schüssler
Hi,

On Thursday 22 October 2009 09:47:32 Martijn van Steenbergen wrote:
 Bonjour café,
 
  data ExprF r
=  Add  r  r
 
|  Sub  r  r
|  Mul  r  r
|  Div  r  r
|  Num  Int
 
 This is a well-known pattern that for example allows nice notation of
 morphisms. But what is it called? 
 ...


The multirec package calls this the pattern functor (more accurately, it 
generates a sum-of-products-like higher order functor whose fixed point is 
isomorphic to your family of types, and calls that the pattern functor).


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


Re: [Haskell-cafe] What's this pattern called?

2009-10-22 Thread George Pollard
Conor also calls these functors:

http://strictlypositive.org/slicing-jpgs/

The fixpoint construction builds recursive types (think trees) from
functors by identifying superstructures with substructures: each node
frames its children. 
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] What's this pattern called?

2009-10-22 Thread Jose Iborra

Obviously you are modelling the datatype

-- data Expr = Add Expr Expr | Sub Expr Expr | Mul Expr Expr | Div  
Expr Expr | Num Int


You already have ExprF, and now you need to throw in Fix

newtype Fix f = In (f(Fix f))

in order to be able to build Expr like terms.

type Expr' = Fix ExprF

add a b = In (Add a b)
sub a b = In (Sub a b)


I've heard people refer to this technique of modelling datatypes as  
taking the initial algebra of the associated endofunctor (in this case  
ExprF) [http://strictlypositive.org/indexed-containers.pdf]


This pattern is discussed in depth in Jeremy Gibbons work. I really  
recommend his Datatype-Generic Programming piece [http://www.comlab.ox.ac.uk/jeremy.gibbons/publications/dgp.pdf 
].
Someone else mentioned the multirec library. If you feel really  
adventurous you can look at the paper behind: Generic programming  
with fixed points for mutually recursive datatypes [http://people.cs.uu.nl/andres/Rec/MutualRec.pdf] or check out Andres presentation at ICFP [http://vimeo.com/6612724 
].


Just my 2c.

On 22/10/2009, at 09:47, Martijn van Steenbergen wrote:


Bonjour café,


data ExprF r
 =  Add  r  r
 |  Sub  r  r
 |  Mul  r  r
 |  Div  r  r
 |  Num  Int


This is a well-known pattern that for example allows nice notation  
of morphisms. But what is it called? I've heard fixed-point view,  
open datatypes and some others, but I'm curious where this pattern  
comes up in literature and what it is called there.


Thanks,

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


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


Re: [Haskell-cafe] ANN: mecha-0.0.0

2009-10-22 Thread John Van Enk
Is this on hackage yet, or should we just consult the mecha link on your
home page?

On Wed, Oct 21, 2009 at 11:42 PM, Tom Hawkins tomahawk...@gmail.com wrote:

 A few months ago, I started toying with a few alternative pump designs
 to power our hydraulic hybrids.  After not being able to secure a ProE
 license, I searched for a free solid modeler to sketch out a few
 ideas.  To my surprise, their are practically no open source 3D CAD
 packages available.  So I created Mecha, DSL for constructive solid
 modeling.

 Mecha's geometry is based on octrees, which makes it easy to perform
 set operations on solids, as well as volumetric calculations such as
 center-of-mass, moments of inertia, and of course, total volume.
 Drawbacks of octrees include consuming a lot of memory and the loss of
 some surface information.  To address the later, Mecha carries forward
 surface points and normals to the leaf nodes of the octree to assist
 in rendering, such that solids don't look like they're made from a
 bunch of LEGOS.

 Note this is a very early release.  The only thing Mecha can do at the
 moment is draw a pretty blue ball -- well it can also zoom and pan on
 a pretty blue ball (mouse wheel).  Establishing a primitive API and
 building a primitive library must be finished before Mecha an do
 anything useful.

 Any comments and suggestions are welcome.

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

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


Re: [Haskell-cafe] ANN: mecha-0.0.0

2009-10-22 Thread Deniz Dogan
Hackage link: http://hackage.haskell.org/package/mecha/

2009/10/22 John Van Enk vane...@gmail.com:
 Is this on hackage yet, or should we just consult the mecha link on your
 home page?

 On Wed, Oct 21, 2009 at 11:42 PM, Tom Hawkins tomahawk...@gmail.com wrote:

 A few months ago, I started toying with a few alternative pump designs
 to power our hydraulic hybrids.  After not being able to secure a ProE
 license, I searched for a free solid modeler to sketch out a few
 ideas.  To my surprise, their are practically no open source 3D CAD
 packages available.  So I created Mecha, DSL for constructive solid
 modeling.

 Mecha's geometry is based on octrees, which makes it easy to perform
 set operations on solids, as well as volumetric calculations such as
 center-of-mass, moments of inertia, and of course, total volume.
 Drawbacks of octrees include consuming a lot of memory and the loss of
 some surface information.  To address the later, Mecha carries forward
 surface points and normals to the leaf nodes of the octree to assist
 in rendering, such that solids don't look like they're made from a
 bunch of LEGOS.

 Note this is a very early release.  The only thing Mecha can do at the
 moment is draw a pretty blue ball -- well it can also zoom and pan on
 a pretty blue ball (mouse wheel).  Establishing a primitive API and
 building a primitive library must be finished before Mecha an do
 anything useful.

 Any comments and suggestions are welcome.

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


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


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


Re: [Haskell-cafe] Re: What *is* a DSL?

2009-10-22 Thread Robert Atkey
On Sat, 2009-10-10 at 20:12 +0200, Ben Franksen wrote:

 Since 'some' is defined recursively, this creates an infinite production for
 numbers that you can neither print nor otherwise analyse in finite time.

Yes, sorry, I should have been more careful there. One has to be careful
to handle EDSLs that have potentially infinite syntax properly.

 I can see at least two solutions: One is to parameterize everything over the
 type of terminals, too. 

 The second solution (which I followed) is to break the recursion by adding
 another nonterminal to the NT type:

A third solution is to add the Kleene star to the grammar DSL, so the
representation of productions becomes:

 data Production nt a
   =   Stopa
   |   TerminalChar   (Production nt a)
   | forall b. NonTerminal (nt b) (Production nt (b - a))
   | forall b. Star(Production nt b) (Production nt ([b] - a))

You also need to add the necessary parts for Alternative to the
Production type too, because they may be nested inside Star
constructors:

   |   Zero
   |   Choose (Production nt a) (Production nt a)

The type Production nt is now directly an Applicative and an Alternative
and also has the combinator:
 star :: Production nt a - Production nt [a]
 star p = Star p $ Stop id

The type of grammars is changed to (with the additional of the starting
nonterminal, as you point out):

 type Grammar nt = forall a. nt a - Production nt a

It is probably also possible to write a function that converts grammars
with “Star”s in to ones without by introducing new non-terminals in the
way you did.

Bob



-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

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


Re: [Haskell-cafe] Re: What *is* a DSL?

2009-10-22 Thread Robert Atkey
On Sun, 2009-10-11 at 21:54 +0200, Ben Franksen wrote:
 Ben Franksen wrote:
  Next thing I'll try is to transform such a grammar into an actual
  parser...
 
 Which I also managed to get working. However, this exposed yet another
 problem I am not sure how to solve.

Another option is to not use a recursive descent parser, and switch to a
parsing algorithm for any context-free such as CYK or Earley's
algorithm. A little test implementation of a well-typed version of the
CYK algorithm seems to work and seems to be as efficient as the normal
imperative one if enough memoisation is used. I'm trying to see if I can
get Earley's algorithm to work nicely in the well-typed setting.

Bob


-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

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


Re: [Haskell-cafe] What *is* a DSL?

2009-10-22 Thread Robert Atkey
On Tue, 2009-10-13 at 13:28 +0100, Nils Anders Danielsson wrote:
 On 2009-10-07 17:29, Robert Atkey wrote:
  A deep embedding of a parsing DSL (really a context-sensitive grammar
  DSL) would look something like the following. I think I saw something
  like this in the Agda2 code somewhere, but I stumbled across it when I
  was trying to work out what free applicative functors were.
 
 The Agda code you saw may have been due to Ulf Norell and me. There is a
 note about it on my web page:
 
   
 http://www.cs.nott.ac.uk/~nad/publications/danielsson-norell-parser-combinators.html

Yes, it might have been that, OTOH I'm sure I saw it in some Haskell
code. Maybe I was imagining it.

  Note that these grammars are strictly less powerful than the ones that
  can be expressed using Parsec because we only have a fixed range of
  possibilities for each rule, rather than allowing previously parsed
  input to determine what the parser will accept in the future.
 
 Previously parsed input /can/ determine what the parser will accept in
 the future (as pointed out by Peter Ljunglöf in his licentiate thesis).
 Consider the following grammar for the context-sensitive language
 {aⁿbⁿcⁿ| n ∈ ℕ}:

Yes, sorry, I was sloppy in what I said there. Do you know of a
characterisation of what languages having a possibly infinite amount of
nonterminals gives you. Is it all context-sensitive languages or a
subset?

  And a general definition for parsing single-digit numbers. This works
  for any set of non-terminals, so it is a reusable component that works
  for any grammar:
 
 Things become more complicated if the reusable component is defined
 using non-terminals which take rules (defined using an arbitrary
 non-terminal type) as arguments. Exercise: Define a reusable variant of
 the Kleene star, without using grammars of infinite depth.

I see that you have an answer in the paper you linked to above. Another
possible answer is to consider open sets of rules in a grammar:

 data OpenRuleSet inp exp =
forall hidden. OpenRuleSet (forall a. (exp :+: hidden) a - 
Rule (exp :+: hidden :+: inp) a)

 data (f :+: g) a = Left2 (f a) | Right2 (g a)

So OpenRuleSet inp exp, exports definitions of the nonterminals in
'exp', imports definitions of nonterminals in 'inp' (and has a
collection of hidden nonterminals).

It is then possible to combine them with a function of type:

 combineG :: (inp1 := exp1 :+: inp) -
 (inp2 := exp2 :+: inp) -
 OpenRuleSet inp1 exp1 -
 OpenRuleSet inp2 exp2 -
 OpenRuleSet inp (exp1 :+: exp2)

One can then give a reusable Kleene star by stating it as an open rule
set:

 star :: forall a nt. Rule nt a - OpenRuleSet nt (Equal [a])

where Equal is the usual equality GADT.

Obviously, this would be a bit clunky to use in practice, but maybe more
specialised versions combineG could be given.

Bob


-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

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


[Haskell-cafe] Time and space complexity of take k . sort

2009-10-22 Thread Paul Johnson
This question on StackOverflow asked about how to find the largest 100 
items in a very long list:


http://stackoverflow.com/questions/1602998/fastest-way-to-obtain-the-largest-x-numbers-from-a-very-large-unsorted-list/1603198#1603198

I replied that you could do it with something like this (but here taking 
the k smallest to strip out some irrelevant complications):


  takeLargest k = take k . sort

Because sort is lazily evaluated this only does enough sorting to find 
the first k elements.  I guess the complexity is something like 
O(n*k*log(k)).


But of equal practical interest is the space complexity.  The optimum 
algorithm is to take the first k items, sort them, and then iterate 
through the remaining items by adding each item to the sorted list and 
then throwing out the highest one.  That has space complexity O(k).  
What does the function above do?


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


Re: [Haskell-cafe] How can i safely change the value of specified key ?

2009-10-22 Thread Kyle Murphy
- Sorry for the late reply on this, I actually sent it last night before I
went to bed but accidentally sent it only to Bulat, re-sending it now for
the entire list -

As Bulat said, it sounds like you're not fully understanding functional
programming. You really should read one of the better tutorials or books on
Haskell like Real World Haskell (you can read it for free online, just
google for it), as it seems like you're trying to use a advanced feature of
the language (IORefs) that's designed to help solve some particularly
difficult problems in order to treat the language more like a procedural
language (which is a really bad way to approach it). This would be something
like if you decided to learn Java by only ever have one class that consisted
of nothing but a whole bunch of public static methods, it pretty much misses
the point of the language.

Try not to think of variables as places to store values, instead think of
them as functions that take zero or more arguments.

when you do something like
 let foo = [1,2,3]
don't think of foo as a variable that contains a list of numbers, instead
think of foo as a function that doesn't take any arguments and returns a
list of numbers (in this case a constant list). You should also concentrate
on ways data can be transformed particularly using higher order functions
like map and foldr.

For instance, if you want to convert a list like [1,2,3] into one like
[(1,1),(2,2),(3,3)] (that is, convert [Int] to [(Int, Int)]) you can use map
or zip like so:
 map (\x - (x,x)) [1,2,3] -- applies a function to each member, in this
case the lambda \x - (x,x) which just makes a tuple
 zip [1,2,3] [1,2,3] -- combines two lists into one by making each element
of the joined list a tuple of one element from each list

-R. Kyle Murphy
--
Curiosity was framed, Ignorance killed the cat.


On Thu, Oct 22, 2009 at 03:41, Bulat Ziganshin bulat.zigans...@gmail.comwrote:

 Hello zaxis,

 Thursday, October 22, 2009, 11:28:14 AM, you wrote:

  then if i want to change aaa to [(1,1),(2,222),(3,3)] , what's the best
 way

 ... well, anyway what you are doing isn't very haskellish. it may be
 considered as advanced topic but basically, best way to compute
 something in Haskell is to construct pure function


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

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

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


Re: [Haskell-cafe] NetSnmp problem.

2009-10-22 Thread John Dorsey
   I am trying to use NetSnmp to get some information from my switch.
 And I met this problem.
   After initialize, I used snmpWalk to get some information, and
 dealed with it. Everything is fine. Then I sleep for 5 mins. SnmpWalk
 again. But this time, all asnValues were unsupported format, which
 broke my dealing process.

Are you on a 64-bit machine?  I've made an update or two, but haven't pushed
them to Hackage yet... I'll do that today.  Please let me know if this fixes
your problem.

John

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


Re: [Haskell-cafe] Simple but interesting (for me) problem

2009-10-22 Thread Gregory Crosswhite
Thank you for the additional explanation, but it isn't clear that what  
you have added is inconsistent with my explanation.


The point I was trying to make is that in an impure/imperative world,  
you may assume that a function is called every time that you use it.   
However, in a pure world the assumption is that a function called with  
the same arguments will always return the same result (i.e.,  
referential transparency) so that you only need to run it's code once  
and then you can re-use that value henceforth.


In practice, of course, what happens under the hood (at least, with  
GHC) is that foo - mkNext constructs a thunk named foo which is  
evaluated the at the first print foo and from then on the thunk is  
in an evaluated state and so later references to it just use this  
value rather than re-evaluating it.  This is because, due to  
referential transparency, it is equivalent to think of foo both as a  
function whose value can be cached and as a constant value that we  
just don't know yet.


The problem with the foo that was defined is that its code will  
actually give you a different value each time that you run it,  
violating the semantics of the language since all functions are  
assumed to be pure.  The problem with violating this semantic is that  
the compiler uses it whenever it can to make things more efficient,  
which in this case means treating foo as a value that only needs to be  
evaluated once even though each time you run the code you actually get  
a different result.  Hence, the results are in a sense undefined since  
the compiler is allowed to run foo as many times as it wants expecting  
to get the same result each time;  for example if two threads  
evaluated foo at the same time then under pathological conditions the  
first thread might see 1 and the second thread 2.


So the moral of this story --- and perhaps the point that you were  
trying to make --- is that it is better to think of foo as a  
constant value that you just don't know yet (until you evaluate) it  
rather than as a function that you can call.


(Your nitpick that next would have been a better name than foo is  
well taken, though.)


Cheers,
Greg

On Oct 22, 2009, at 12:48 AM, minh thu wrote:


2009/10/21 Gregory Crosswhite gcr...@phys.washington.edu:
And just because this has not been explicitly stated:  it's not  
just for

aesthetic reasons that you couldn't do this with a pure function, but
because it violates the semantics and gets you the wrong result.   
So for

example, if you modified Tim's code to be

import Data.IORef
import System.IO.Unsafe
mkNext :: (Num a) = IO a
mkNext = do
 ref - newIORef 0
 return . unsafePerformIO $
do
  modifyIORef ref (+1)
  readIORef ref
main :: IO ()
main = do
 foo - mkNext
 print foo
 print foo
 print foo

Then the output that you will see (with GHC at least) is
1
1
1
because the compiler assumes that it only needs to evaluate foo  
once, after
which it can cache the result due to assumed referential  
transparency.

- Greg


This is indeed wrong, but not how you think it is.

The code you pass to unsafePerformIO has type Num a = IO a, so the
value passed to return has type Num a. So foo has type Num a too and
its value is 1.

Exactly like in

mkNext = do
 ref - newIORef 0
 modifyIORef ref (+1)
 readIORef ref

which is a complicated way to write

mkNext = return 1

Now, it's clear that foo has value 1 and printing it three times
should output three 1. The whole point of having mkNext return an
action (that should be called next, and not foo, as it is much
clearer) in previous code was too be able to execute it multiple times
and having it return a new value.

In general, expecting

print bar
print bar
print bar

outputing three different things is wrong, as bar should be pure. If
bar is not pure, then it should be
a - bar
print a
b - bar
print b
c - bar
print c

Cheers,
Thu


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


Re: [Haskell-cafe] Time and space complexity of take k . sort

2009-10-22 Thread Paul Johnson

On 22/10/09 15:31, Paul Johnson wrote:


  takeLargest k = take k . sort

Because sort is lazily evaluated this only does enough sorting to 
find the first k elements.  I guess the complexity is something like 
O(n*k*log(k)).



Correction: O(n*log(k))
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Is there in Haskell the eval function?

2009-10-22 Thread Joe Fredette
You may also want to look at Dyre. It does dynamic recompilation of  
source files. Depending on your application, hint may not be what you  
need. Eg, if you're trying to build something like lambdabot's  
interpreter, then Hint is probably on the right track, if you just  
want to use Haskell-as-configuration file (a la xmonad), then Dyre is  
probably a better (or at least easier, Hint can be evil...) choice



/Joe


On Oct 22, 2009, at 7:07 AM, Waldemar Biernacki wrote:


Thank you Martijn!


You mean like in interpreted languages?

Naturally! My mistake.

I'd like to write an application which has to be compiled to exec  
file. It is neccessary to performe some additional procedures  
which are unknown at the moment of the compilition.


You could use the GHC libraries, or Hint which is built on top of  
GHC.

I've used it before and it works pretty nicely.

http://hackage.haskell.org/package/hint


At first sight it seems be what I'm looking for. I'll try.

Thank you for the help!

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


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


[Haskell-cafe] Word128, Word256

2009-10-22 Thread Maurí­cio CA

Hi,

Do you think we could have the range of sizes for Int* and Word*
expanded to allow also 128 and 256 bits sizes?

My reason is that I have a long standing issue trying to bind to
C numerical libraries using complex numbers, as those are usually
structs passed by value. See this from GNU GSL:

  typedef struct
{
  double dat[2];
}
  gsl_complex;

I imagine I could do:

  type GslComplex = Word128 -- size would depend on architecture

and then write foreign declarations to have:

  get_real :: GslComplex - CDouble
  get_imag :: GslComplex - CDouble
  gsl_complex_sin :: GslComplex - GslComplex
  gsl_complex_cos :: GslComplex - GslComplex

Do you think this is a reasonable request?

Thanks for your attention,
Maurício

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


Re: [Haskell-cafe] why cannot i get the value of a IORef variable ?

2009-10-22 Thread Thomas DuBuisson
 For clarity, one trick that uses unsafePerformIO which you may have seen
 posted on this list earlier today is the following way of creating a
 globally visible IORef:

 import Data.IORef
 import System.IO.Unsafe

 *** counter = unsafePerformIO $ newIORef 0 ***

 next = do
  modifyIORef counter (+1)
  readIORef counter

This is still unsafe but it can evidently be slightly improved with
NOINLINE pragma:

{-# NOINLINE counter #-}
counter = unsafePerformIO $ newIORef 0

without said pragma the counter could be initialized numerous times,
as I understand it.  All this said, I hope people avoid
unsafePerformIO for mutable globals - reader monad, state monad, and
partial application should be sufficient tools.

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


Re: [Haskell-cafe] ANN: Data.Stream 0.4

2009-10-22 Thread Bas van Dijk
On Wed, Oct 21, 2009 at 9:44 PM, Wouter Swierstra w...@cs.nott.ac.uk wrote:
 The only change with the previous version has been to add irrefutable
 patterns to several function definitions. This is rather delicate design
 decision: too many irrefutable patterns could result in thunks not being
 evaluated; too few irrefutable patterns could cause your functions diverge.
 As a rule of thumb I've chosen only to use irrefutable patterns in functions
 that produce streams from streams. The operations that observe finite
 information (a prefix, the element at index i, etc.) do not have have
 irrefutable patterns and force evaluation to weak head normal form.

Hi Wouter,

I have two questions:

1) What's the difference between your:
tail ~(Cons _ xs) = xs
and the more simple:
tailStrict (Cons _ xs) = xs ?

I know they're desugared to:
tail ys = let Cons _ xs = ys in xs
and:
tailStrict ys = case ys of Cons _ xs - xs respectively.

But aren't they operationally the same:

tail undefined = undefined
and:
tailStrict undefined = undefined

2) Why don't you also use an irrefutable pattern in take? take is
now defined as:

take :: Int - Stream a  - [a]
take n (Cons x xs)
  | n == 0= []
  | n  0 =  x : (take (n - 1) xs)
  | otherwise = error Stream.take: negative argument.

so take 0 undefined = undefined while:

takeLazy :: Int - Stream a  - [a]
takeLazy n ~(Cons x xs)
  | n == 0= []
  | n  0 =  x : (takeLazy (n - 1) xs)
  | otherwise = error Stream.take: negative argument.

takeLazy 0 undefined = []

regards,

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


Re: [Haskell-cafe] why cannot i get the value of a IORef variable ?

2009-10-22 Thread David Menendez
On Thu, Oct 22, 2009 at 2:23 AM, Gregory Crosswhite
gcr...@phys.washington.edu wrote:
 For clarity, one trick that uses unsafePerformIO which you may have seen
 posted on this list earlier today is the following way of creating a
 globally visible IORef:

 import Data.IORef
 import System.IO.Unsafe

 *** counter = unsafePerformIO $ newIORef 0 ***

Danger! If the monomorphism restriction is disabled, this ends up
creating a value of type forall a. Num a = IORef a, which can be used
to break type safety.

More generally,

cell :: IORef a
cell = unsafePerformIO $ newIORef undefined

unsafeCoerce :: a - b
unsafeCoerce x = unsafePerformIO $ do
writeIORef cell x
readIORef cell

This way lies segmentation faults. That unsafe is there for a reason.

-- 
Dave Menendez d...@zednenem.com
http://www.eyrie.org/~zednenem/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Word128, Word256

2009-10-22 Thread John Meacham
jhc potentially has Word128, but it only has meaning when __int128_t is
defined for a given target. (gcc + x86_64 for instance). What would
something like Word256 mean? As in, the only reason to supply something
like Word128 in the compiler is so FFI calls involving __int128_t will
work, without a defined ABI type to map Word256 to, then a portable pure
haskell implementation seems just as good.

Your example doesn't seem to make sense, as the calling convention for
__int128_t (what Word128 would map to) and double x[2] are unlikely to
coincide. they are probably passed in different registers for instance.

Adding the ability for the FFI to pass complex types directly would be
useful. Something like 'CComplex CDouble' has been proposed before.. but
that again would be independent of a Word128 type.

John

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


Re: [Haskell-cafe] why cannot i get the value of a IORef variable ?

2009-10-22 Thread Gregory Crosswhite
Yes, I was once taught that Every time you use unsafePerformIO, God  
kills a kitten,  so every time I consider using it I first ask  
myself:  is this really worth an innocent kitten's life?


Cheers,
Greg

On Oct 22, 2009, at 11:32 AM, David Menendez wrote:


On Thu, Oct 22, 2009 at 2:23 AM, Gregory Crosswhite
gcr...@phys.washington.edu wrote:
For clarity, one trick that uses unsafePerformIO which you may  
have seen

posted on this list earlier today is the following way of creating a
globally visible IORef:

import Data.IORef
import System.IO.Unsafe

*** counter = unsafePerformIO $ newIORef 0 ***


Danger! If the monomorphism restriction is disabled, this ends up
creating a value of type forall a. Num a = IORef a, which can be used
to break type safety.

More generally,

cell :: IORef a
cell = unsafePerformIO $ newIORef undefined

unsafeCoerce :: a - b
unsafeCoerce x = unsafePerformIO $ do
   writeIORef cell x
   readIORef cell

This way lies segmentation faults. That unsafe is there for a  
reason.


--
Dave Menendez d...@zednenem.com
http://www.eyrie.org/~zednenem/


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


Re: [Haskell-cafe] Re: cvs.haskell.org down? haskell-mode abandoned?

2009-10-22 Thread Stefan Monnier
 In any case, I need it fixed before I can work on haskell-mode.
 Preferably by migrating haskell-mode over to cvs. :-)
 You mean migrating to DaRCS?  That would be appreciated, yes.
 Um, yes.
 I can get the revision history into darcs pretty easily, such as it
 is, assuming cvs.haskell.org comes back up.  Then I'll put the
 repository somewhere you can get at it.  That fine?

Yes, that would be wonderful.  Ideally, it will eventually make it into
something like darcs.haskell.org.

 who *is* the current maintainer?
 Last I heard, it was me.  But if someone wants to take over,
 he's welcome.
 You're saying you don't have time to maintain it?

Pretty much, yes.  At least, I don't think I've done a good job
maintaining it over the last couple years.

 Still, there needs to be a new release,

Yes, urgently.

 and I've got a lot of improved haskell-mode code (written by other
 people) that isn't currently in the repository.

Can you show me the patches?


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


Re: [Haskell-cafe] why cannot i get the value of a IORef variable ?

2009-10-22 Thread Tim Wawrzynczak
Well, I apologize for starting this whole thread which involves so many dead
kittens :(  I was just trying to help answer a question :)
I guess I assumed too much.. that someone would think to be careful when
using a function with the word 'unsafe' in it...

So, be warned, all Haskellers!  Be careful when using any function that
starts with the word 'unsafe'!  You may kill a kitten!  And I guess this
says something about using 'unsafe' functions...
http://upload.wikimedia.org/wikipedia/en/1/11/God-kills-kitten.jpg (NSFW)...

Cheers and sorry all,
Tim


On Thu, Oct 22, 2009 at 1:59 PM, Gregory Crosswhite 
gcr...@phys.washington.edu wrote:

 Yes, I was once taught that Every time you use unsafePerformIO, God kills
 a kitten,  so every time I consider using it I first ask myself:  is this
 really worth an innocent kitten's life?

 Cheers,
 Greg


 On Oct 22, 2009, at 11:32 AM, David Menendez wrote:

  On Thu, Oct 22, 2009 at 2:23 AM, Gregory Crosswhite
 gcr...@phys.washington.edu wrote:

 For clarity, one trick that uses unsafePerformIO which you may have
 seen
 posted on this list earlier today is the following way of creating a
 globally visible IORef:

 import Data.IORef
 import System.IO.Unsafe

 *** counter = unsafePerformIO $ newIORef 0 ***


 Danger! If the monomorphism restriction is disabled, this ends up
 creating a value of type forall a. Num a = IORef a, which can be used
 to break type safety.

 More generally,

 cell :: IORef a
 cell = unsafePerformIO $ newIORef undefined

 unsafeCoerce :: a - b
 unsafeCoerce x = unsafePerformIO $ do
   writeIORef cell x
   readIORef cell

 This way lies segmentation faults. That unsafe is there for a reason.

 --
 Dave Menendez d...@zednenem.com
 http://www.eyrie.org/~zednenem/ http://www.eyrie.org/%7Ezednenem/


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

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


Re: [Haskell-cafe] Re: cvs.haskell.org down? haskell-mode abandoned?

2009-10-22 Thread Johan Tibell
Hi,

I just wanted to say that I'd be really happy to see haskell-mode in
code.haskell.org. I think it will make it easier for people to hack on
it.

Thanks,

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


Re: [Haskell-cafe] Time and space complexity of take k . sort

2009-10-22 Thread Ketil Malde
Paul Johnson p...@cogito.org.uk writes:

  takeLargest k = take k . sort

 But of equal practical interest is the space complexity.  The optimum
 algorithm is to take the first k items, sort them, and then iterate
 through the remaining items by adding each item to the sorted list and
 then throwing out the highest one.  That has space complexity O(k).
 What does the function above do?

Well - 'sort' doesn't know the value of 'k', so it needs to retain all
elements, just in case 'k' might be 'n'.  So I don't see how you can use
space less than 'n' for a construct like the above.

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Time and space complexity of take k . sort

2009-10-22 Thread Dan Weston
Unless of course you use a GHC RULE to rewrite the RHS into the LHS, 
which should always be a valid transformation.


Ketil Malde wrote:

Paul Johnson p...@cogito.org.uk writes:


 takeLargest k = take k . sort



But of equal practical interest is the space complexity.  The optimum
algorithm is to take the first k items, sort them, and then iterate
through the remaining items by adding each item to the sorted list and
then throwing out the highest one.  That has space complexity O(k).
What does the function above do?


Well - 'sort' doesn't know the value of 'k', so it needs to retain all
elements, just in case 'k' might be 'n'.  So I don't see how you can use
space less than 'n' for a construct like the above.

-k


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


Re: [Haskell-cafe] why cannot i get the value of a IORef variable ?

2009-10-22 Thread Colin Paul Adams
 Gregory == Gregory Crosswhite gcr...@phys.washington.edu writes:

Gregory Yes, I was once taught that Every time you use
Gregory unsafePerformIO, God kills a kitten, so every time I
Gregory consider using it I first ask myself: is this really
Gregory worth an innocent kitten's life?

I've changed my mind.

Everyone go out and use unsafePerformIO all the time. That way we can
get rid of all those mudering kittens, and the dragonflies will live longer.
-- 
Colin Adams
Preston Lancashire
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Re: cvs.haskell.org down? haskell-mode abandoned?

2009-10-22 Thread Ben Franksen
Johan Tibell wrote:
 I just wanted to say that I'd be really happy to see haskell-mode in
 code.haskell.org. I think it will make it easier for people to hack on
 it.

Another option is http://patch-tag.com/

Cheers
Ben

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


Re: [Haskell-cafe] Re: cvs.haskell.org down? haskell-mode abandoned?

2009-10-22 Thread Svein Ove Aas
On Thu, Oct 22, 2009 at 9:02 PM, Stefan Monnier
monn...@iro.umontreal.ca wrote:
 Can you show me the patches?

Sure, once cvs.haskell.org comes back up. :P

It's mostly newer variants of haskell-indentation.el, with a little
homegrown code. Well, you can see what I'm currently running at
http://brage.info/~svein/haskell-mode/; the other stuff is all the
same as what's in CVS.

I think.


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


Re: [Haskell-cafe] why cannot i get the value of a IORef variable ?

2009-10-22 Thread Anton van Straaten

Colin Paul Adams wrote:

Gregory == Gregory Crosswhite gcr...@phys.washington.edu writes:


Gregory Yes, I was once taught that Every time you use
Gregory unsafePerformIO, God kills a kitten, so every time I
Gregory consider using it I first ask myself: is this really
Gregory worth an innocent kitten's life?

I've changed my mind.

Everyone go out and use unsafePerformIO all the time. That way we can
get rid of all those mudering kittens, and the dragonflies will live longer.


You're missing the bigger picture.  It's clear from the literature[*] 
that the IO monad, the type system, and possibly even Haskell itself, is 
powered by kittens.  If you use up all the kittens, Haskell will just 
stop working.  Terms won't even reach WHNF, they'll be stuck in KAF, 
Kittenless Abnormal Form.


On the plus side, this does make for a slogan with high market appeal:

   Haskell: Kittens inside

--
[*] http://arcanux.org/lambdacats.html
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] why cannot i get the value of a IORef variable ?

2009-10-22 Thread Derek Elkins
On Thu, Oct 22, 2009 at 1:32 PM, David Menendez d...@zednenem.com wrote:
 On Thu, Oct 22, 2009 at 2:23 AM, Gregory Crosswhite
 gcr...@phys.washington.edu wrote:
 For clarity, one trick that uses unsafePerformIO which you may have seen
 posted on this list earlier today is the following way of creating a
 globally visible IORef:

 import Data.IORef
 import System.IO.Unsafe

 *** counter = unsafePerformIO $ newIORef 0 ***

 Danger! If the monomorphism restriction is disabled, this ends up
 creating a value of type forall a. Num a = IORef a, which can be used
 to break type safety.

 More generally,

 cell :: IORef a
 cell = unsafePerformIO $ newIORef undefined

 unsafeCoerce :: a - b
 unsafeCoerce x = unsafePerformIO $ do
    writeIORef cell x
    readIORef cell

 This way lies segmentation faults. That unsafe is there for a reason.

This is exactly what happened in the original example.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] NetSnmp problem.

2009-10-22 Thread Magicloud Magiclouds
Hi,
  The box is just 32bit with linux. But anyway, I will try out the
update. Thanks.

On Thu, Oct 22, 2009 at 11:35 PM, John Dorsey hask...@colquitt.org wrote:
   I am trying to use NetSnmp to get some information from my switch.
 And I met this problem.
   After initialize, I used snmpWalk to get some information, and
 dealed with it. Everything is fine. Then I sleep for 5 mins. SnmpWalk
 again. But this time, all asnValues were unsupported format, which
 broke my dealing process.

 Are you on a 64-bit machine?  I've made an update or two, but haven't pushed
 them to Hackage yet... I'll do that today.  Please let me know if this fixes
 your problem.

 John





-- 
竹密岂妨流水过
山高哪阻野云飞
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] NetSnmp problem.

2009-10-22 Thread Magicloud Magiclouds
It works pretty well.

On Fri, Oct 23, 2009 at 7:24 AM, Magicloud Magiclouds
magicloud.magiclo...@gmail.com wrote:
 Hi,
  The box is just 32bit with linux. But anyway, I will try out the
 update. Thanks.

 On Thu, Oct 22, 2009 at 11:35 PM, John Dorsey hask...@colquitt.org wrote:
   I am trying to use NetSnmp to get some information from my switch.
 And I met this problem.
   After initialize, I used snmpWalk to get some information, and
 dealed with it. Everything is fine. Then I sleep for 5 mins. SnmpWalk
 again. But this time, all asnValues were unsupported format, which
 broke my dealing process.

 Are you on a 64-bit machine?  I've made an update or two, but haven't pushed
 them to Hackage yet... I'll do that today.  Please let me know if this fixes
 your problem.

 John





 --
 竹密岂妨流水过
 山高哪阻野云飞




-- 
竹密岂妨流水过
山高哪阻野云飞
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] What's this pattern called?

2009-10-22 Thread wren ng thornton

Martijn van Steenbergen wrote:

Bonjour café,


data ExprF r
  =  Add  r  r
  |  Sub  r  r
  |  Mul  r  r
  |  Div  r  r
  |  Num  Int


This is a well-known pattern that for example allows nice notation of 
morphisms. But what is it called? I've heard fixed-point view, open 
datatypes and some others, but I'm curious where this pattern comes up 
in literature and what it is called there.


This is an example of open recursion, which is when you take some 
recursive function/datatype and rewrite it without recursion by passing 
the function/type in as an argument to itself. It's the datatype 
equivalent of doing:


fibF _ 0 = 0
fibF _ 1 = 1
fibF f n = f(n-1) + f(n-2)

fib = fix fibF

Which can be useful for functions because we can use a different 
fixed-point operator, e.g. one that adds memoization abilities or other 
features in addition to the recursion.


As others've mentioned, the open-recursive version of a recursive data 
type happens to be a functor. Or rather, the recursive type happens to 
be isomorphic to the least fixed point of a generating functor[1][2] 
because the functor is also, in the terms of recursion theory, an 
initial algebra. Part of why this pattern is so nice comes from the 
fact that it's a functor (so we can use fmap to apply a function one ply 
down), but part of it also comes from the isomorphism of using an 
explicit fixed-point operator (which allows us to un-fix the type and do 
things like storing the accumulators of a fold directly in the normal 
constructors, rather than needing to come up with an ad-hoc isomorphic 
set of constructors[3]), and the fact that it's an initial algebra ties 
these two things together nicely.


This is also an example of Tim Sheard's two-level types, albeit a 
trivial one since the fixed-point operator doesn't add anything other 
than recursion. One of the particular ideas behind Sheard's two-level 
types is that we can split the original recursive type in a different 
place where one of the levels contains some constructors and the other 
level contains other constructors. This can be helpful when you have a 
family (informally speaking) of similar types, as for example with 
implementing unification. All types that can be unified share 
constructors for unification variables; but the constructors for the 
structural components of the type are left up to another level. Thus we 
can reuse the variable processing code for unifying different types, and 
also be modular about the type being unified.




[1] This should be somewhat obvious if you're familiar with the 
inductive phrasing of constructing the set of all values for some type. 
E.g. Basis: [] is a list. Induction: (:) takes a value and a list into 
a list. So we have some functor and we keep applying it over and over 
to generate the set of all values, building up from the base cases.


[2] Do note that in Haskell the least fixed point and the greatest fixed 
point coincide. Technically, whether the least or greatest fixed point 
is used depends on the construction (e.g. catamorphisms use least, 
anamorphisms use greatest). This is also related to the topic of 
codata which is the fixed point of a terminal coalgebra.


[3] Data.List.unfoldr is a prime example of an ad-hoc isomorphic set of 
constructors. Instead of the current type, we could instead use an 
implementation where:


newtype Fix   f   = Fix { unFix :: f (Fix f) }
dataListF a r = Nil | Cons a r
typeList  a   = Fix (ListF a)

unfoldr :: (b - ListF a b) - b - List a
unfoldr = ...

which is a bit more obviously correlated with anamorphisms in recursion 
theory.


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