[Haskell-cafe] Re: Problematic irrefutable pattern matching of existentials

2006-10-02 Thread Ross Paterson
On Sun, Oct 01, 2006 at 08:05:15PM -0700, [EMAIL PROTECTED] wrote:
 I have come to realize that irrefutable pattern matching of
 existentials may indeed be problematic. Let us consider the following
 existential data type
 
  data FE = forall a. Typeable a = Foo a
| forall a. Typeable a = Bar a
 
 The following tests type and run (the latter raising the expected
 exception).
 
  test1 = case (Foo ()) of Foo x - show $ typeOf x
  test2 = case (Bar True) of Foo x - show $ typeOf x
 
 Let us suppose that irrefutable pattern matching of existentials is
 allowed. What would be the value of the expression
 
   case (Bar True) of ~(Foo x) - show $ typeOf x
 then?
 
 Hopefully not Bool. One might say that the expression ought to bottom
 out. However, what reason one may give for that outcome? The only
 binding in the above expression is of 'x', whose value is never
 demanded. Indeed, show $ typeOf (undefined :: ()) yields the
 non-bottom value (). 

There is also the implicit dictionary argument of typeOf, and this is
demanded, so the value should be _|_.

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


[Haskell-cafe] Re: [Haskell] Replacing and improving pattern guards with PMC syntax

2006-10-02 Thread Brandon Moore

[EMAIL PROTECTED] wrote:

...


So far I never considered it important to devise a concrete syntax for PMC,
but triggered by the current pattern guards thread on haskell-prime,
I now try to give a PMC syntax that blends well with Haskell.


I think with some alterations your syntax would blend even better. In
particular, it would be really nice to have a backwards-compatible 
syntax. I have designed a slightly different syntax with that goal in 
mind, which is described in the rest of this message.



One of my gripes with Haskell pattern matching is
that the anonymous variant, namely case expressions,
has an application to an argument built in,
and therefore does not by itself produce a function.

However, lambda-abstraction already allows single-alternativepattern matching, 
so I propose to relax this to multi-alternative
pattern matching:

length = \
  [] - 0
  x : xs - 1 + length xs

I'm with you here. Extending lambda to introduce a layout group of
alternatives makes sense even without the rest of the changes to
matching. This isn't completely backwards-compatible, but it's pretty close.

While we're at it, why not have the identifier of a top level definition
introduce a layout group of alternatives, rather than requiring the name
to be repeated all the time.
How about

take
0 _ = []
_ [] = []
n (x:xs) = x : take (pred n) ns


(Lists of alternatives correspond to the ``matchings'' of PMC,
 and concatenation of those lists (using ``;'' or layout) corresponds
 to matching alternative in PMC.
 ``\ m'' then corresponds to ``{| m |}'' in PMC.
)

For multi-argument pattern matching,
I propose to allow nested matchings:

zip = \
  x : xs - y : ys - (x,y) : zip xs ys
  _  - _  - []

This deviates from the current syntax of multi-argument lambda, and
introduces overloading on -. Perhaps - could be reserved for match
and lift, and whitespace could separate several alternatives:

zip = \
(x:xs) (y:ys) - (x,y) : zip xs ys
_ _ - []

There is some tension in the current between argument lists which allow
several arguments and require parentheses around constructor
applications to delimit patterns, and case expression which match only
one item, but without grouping.

If we are trying to unify things, perhaps case could be extended to
support matching several values together, perhaps separating expressions
with commas in the head and using the syntax of a lambda for the cases.

Perhaps '=' in function declarations vs. - for other matching could
also be simplified somehow. I don't have any ideas here.


take' = \
  0 -   _  - []
  n - { [] - []
   ; x : xs - x : take (pred n) xs
   }

I'm not sure how to handle a case like this. Introducing layout at
whitespace seems a bit excessive, but requiring explicit separators
looks a bit heavy on punctuation next to the current grammar, where only
records (and perhaps
Maybe the '\' could be reiterated to introduce the layout group:

take' = \
0 _ - []
n \[] - []
(x:x) - x : take (pred n) xs



Pattern guards now can be seen as a special case of
alternatives-level argument application
(``argument supply'' $\righttriangle$ in PMC).
I find it useful to write the argument first, as in PMC.

I see two options of writing argument supply in Haskell:
either using new keywords, giving rise to the syntax production

alt - match exp with alts

Maybe reuse case, unless a new keyword is needed to avoid confusion.

or using a keyword infix operator:

alt - exp | alts

(Using this kind of argument supply instead of pattern guards
 also has the advantage that multiple alternatives become possible.)


I'm not using - to separate patterns, but some leading symbol is
required to separate the last pattern from the expression.

I like '|' from the current syntax, selected with hopes of incorporating
and extending the current guard syntax.

I'm pretty sure incorporating the existing guard syntax will require
that the grammar keep track whether the last bit of the matching was a
pattern or a guard (just like currently | switches from patterns
separated by whitespace to a list of guards separated by commas).

Pattern guards can be translated to matching plus argument supply, so
argument supply needs to be part of the primitive matching syntax. I
like the suggestion
alt | exp | alts
It would be unambiguous but require unbounded lookahead to allow a case
to begin with argument supply:
foo :: [Int] - [Int]
foo = \lookup env v | (Just r) [] - [r]

Given argument supply, we can sugar in the existing guard syntax by allowing
alt | guard, guards, exp | alts
and
alt | guards - expr

The sequence -\ will transition from guards back to matching more
arguments. It might be nice to abbreviate that to '\'.

Guards can be eliminated with the translations

alt | exp, guards ...
==
alt | exp | True |guards

alt | exp - expr
==
alt | exp | True - expr

alt | pat - exp, guards
==
alt | exp | {pat | guards ... }

alt | pat - exp - expr
==
alt | exp | pat - expr



I start with Simon Peyton 

Re: [Haskell-cafe] How can I redirect stdout?

2006-10-02 Thread Krasimir Angelov

Hi Matthew,

On Windows stdout/stderr/stdin exists only when your application is
using the Console OS subsystem. All GUI applications doesn't have
console window and they don't have stdout/stderr/stdin. When you are
building DLLs then the subsystem is determined from the type of the
application that is loading your DLL. In your particular case the DLL
is loaded from Matlab which is definitely a GUI application. When you
are trying to load the DLL from other C application then probably it
is Console application and all print statements simply works. The
solution that you can use is to replace all your print statements with
Debug.Trace.trace or Debug.Trace.putTraceMsg. When you are using these
functions from Console application then the output is redirected to
stderr but when they are used from GUI application then the output
goes to the debugger. You can see the output in the Debug pane in the
Visual Studio IDE while you are running MatLab from the debugger.

Cheers,
 Krasimir

On 9/29/06, Matthew Bromberg [EMAIL PROTECTED] wrote:

I'm reposting this in it's own new thread since I think it involves a
general issue beyond exporting Haskell DLLs.

I am having some problems with GHCs stdout when a Haskell program is
called from a windows program.



As I noted earlier I am calling some Haskell code from C as a bridge to
being able to
ultimately call Haskell from Matlab 6.5.

The Haskell code is compiled into a .DLL file on a windows machine.
Matlab calls some C code which then calls the Haskell code.

As soon as it gets into the Haskell code I get this run time error in
ghcDLL.dll

stdout: hPutChars: invalid argument (Bad File Descriptor)

The Haskell code seems to work correctly if called from a stand-alone C
program. Moreover, if I scrub all print statements from the Haskell
code, it appears to run correctly from Matlab.

Matlab does not properly redirect stdout so any printf calls from the C
code simply fail to print.
However Haskell's behavior is to halt after generating a runtime error.

The C code used to generate the Haskell .dll is of course mingw gcc, but
the C code used to create my Matlab mex file is Microsoft VC++ 6.0.  I
tried to redirect stdout using freopen() in C, but that never seems to
work with Microsoft.  At any rate it certainly doesn't effect Haskells
use of stdout.  I think in general for windows programs there is no
stdout defined, whereas it's always defined for console programs.

My question is, is there some way I can redirect stdout from inside
Haskell so that all output is sent to a file?  Is there an equivalent of
freopen() in Haskell that works?
___
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] non lazy io

2006-10-02 Thread Bulat Ziganshin
Hello Matt,

Friday, September 29, 2006, 10:31:10 AM, you wrote:

 I would like a non-lazy alternative to

 readFile

 is there such a thing in the libraries?

i recommend you to use readFile from FPS library. it's strict (unles
you use Lazy module) and then you can either use returned ByteString
directly with a plenty of supported operations, or convert it to
String using unpack. it will be faster than solution by Neil


-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

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


Re: [Haskell-cafe] How would you replace a field in a CSV file?

2006-10-02 Thread Bulat Ziganshin
Hello tpledger,

Monday, October 2, 2006, 3:11:29 AM, you wrote:

 For such a small self-contained task, I don't think Haskell
 is any better than Python.

i disagree. while it's hard to beat Python version in number of lines,
Haskell version may have the same length and better performance.

for this particular task, using list as intermediate datastructure
make program both long and inefficient. if ByteString will include
array-based splitting and joining, the things will become much better

main = B.interact $ B.unlines . map doline . B.lines
where doline= B.joinArray comma . mapElem 9 fixup . B.splitArray ','
  fixup s   = M.findWithDefault s s
  comma = B.pack ,

mapElem n func arr = arr//[(n,func (arr!n))]


if mapElem, splitArray, joinArray will be library functions (i think
they are good candidates) this program will be not longer than Python
one


-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

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


[Haskell-cafe] Typeclass vs. Prolog programming

2006-10-02 Thread Martin Sulzmann


[EMAIL PROTECTED] writes:
  
  There is a great temptation to read the following declarations
  
   class Pred a b
   instance (Ord b, Eq b) = Pred [Int] b
   instance Pred Bool Bool
  
  as a Prolog program:
  
   pred([int],B) - ord(B),eq(B).
   pred(bool,bool).
  
  (In Prolog, the names of variables are capitalized and the names of
  constants begin with a lower-case letter. In Haskell type expressions,
  it's the other way around).
  

Why not simply say that type classes have a natural interpretation
in terms of Constraint Handling Rules (CHRs). For example,
the above instances translate to

rule  Pred [Int] == Ord b, Eq b
rule  Pred Bool Bool == True

CHRs specify rewrite rules among constraints (from left to right).
A CHRs fires if we find a constraint which matches the left hand
side (compare this to Prolog where we use unification).

Why CHRs are great for reasoning about type classes is well-documented
here:
http://www.comp.nus.edu.sg/~sulzmann/research/node4.html

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


Re: [Haskell-cafe] non lazy io

2006-10-02 Thread Matt Roberts

Cheers mate,

That was exactly what I was looking for

matt

On 02/10/2006, at 4:18 PM, Bulat Ziganshin wrote:


Hello Matt,

Friday, September 29, 2006, 10:31:10 AM, you wrote:


I would like a non-lazy alternative to



readFile



is there such a thing in the libraries?


i recommend you to use readFile from FPS library. it's strict (unles
you use Lazy module) and then you can either use returned ByteString
directly with a plenty of supported operations, or convert it to
String using unpack. it will be faster than solution by Neil


--
Best regards,
 Bulatmailto:[EMAIL PROTECTED]



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


Re: [Haskell-cafe] question - which monad to use?

2006-10-02 Thread Matthias Fischmann


hi, i don't fully understand your problem, but perhaps you could use
iterate to produce a list or type [Result a], ie, of all computation
steps, and then use this function to extract either result or error
from the list:


type Failmessage = Int
data Result a = Root a | Failure Failmessage  deriving (Show)

f :: [Result a] - Either a (Int, [Result a])
f cs = f [] cs
where
f (Root r:_) [] = Left r
f l [Failure i] = Right (i, reverse l)
f l (x:xs)  = f (x:l) xs

cs = [Root 1.2, Root 1.4, Root 1.38, Root 1.39121]
cs' = [Root 1.2, Root 1.4, Root 1.38, Failure 1]

-- f cs  == Left 1.39121
-- f cs' == Right (1,[Root 1.2,Root 1.4,Root 1.38])


(although this way you probably have the list still floating around
somewhere if you process the error returned by f, so f should probably
just drop the traversed part of the list.)

hth,
matthias



On Sun, Oct 01, 2006 at 06:00:43PM -0400, Tamas K Papp wrote:
 To: Haskell Cafe haskell-cafe@haskell.org
 From: Tamas K Papp [EMAIL PROTECTED]
 Date: Sun, 1 Oct 2006 18:00:43 -0400
 Subject: [Haskell-cafe] question - which monad to use?
 
 Hi,
 
 I have a computation where a function is always applied to the
 previous result.  However, this function may not return a value (it
 involves finding a root numerically, and there may be no zero on the
 interval).  The whole problem has a parameter c0, and the function is
 also parametrized by the number of steps that have been taken
 previously.
 
 To make things concrete,
 
 type Failmessage = Int  -- this might be something more complex
 data Result a = Root a | Failure Failmessage -- guess I could use Either too
 
 f :: Double - Int - Double 0 - Result Double
 f c0 0 _ = c0
 f c0 j x = {- computation using x, parameters calculated from c0 and j -}
 
 Then
 
 c1 = f c0 0 c0
 c2 = f c0 1 c1
 c3 = f c0 2 c2
 ...
 
 up to cn.
 
 I would like to
 
 1) stop the computation when a Failure occurs, and store that failure
 
 2) keep track of intermediate results up to the point of failure, ie
 have a list [c1,c2,c3,...] at the end, which would go to cn in the
 ideal case of no failure.
 
 I think that a monad would be the cleanest way to do this.  I think I
 could try writing one (it would be a good exercise, I haven't written
 a monad before).  I would like to know if there is a predefined one
 which would work.
 
 Thank you,
 
 Tamas
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

-- 
Institute of Information Systems, Humboldt-Universitaet zu Berlin

web:  http://www.wiwi.hu-berlin.de/~fis/
e-mail:   [EMAIL PROTECTED]
tel:  +49 30 2093-5742
fax:  +49 30 2093-5741
office:   Spandauer Strasse 1, R.324, 10178 Berlin, Germany
pgp:  AD67 CF64 7BB4 3B9A 6F25  0996 4D73 F1FD 8D32 9BAA


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


Re: [Haskell-cafe] question - which monad to use?

2006-10-02 Thread Tamas K Papp
Matthias,

Sorry if I was not clear in stating the problem.  Your solution works
nicely, but I would like to try writing a monad.  This is what I came
up with:

type Failure = String
data Computation a = Computation (Either Failure a) [a]

instance Monad Computation where
(Computation (Left e) h) = f = Computation (Left e) h -- do not proceed
(Computation (Right a) h) = f = let r = f a -- result
  h' = case r of
 Left e - h
 Right a' - a':h
  in
Computation r h'
return (s,c) = Computation (Right (s,c)) [(s,c)]

Basically, I want the = operator to call f on the last result, if it
is not a failure, and append the new result to the list (if it didn't
fail).

However, I am getting the following error message:

/home/tpapp/doc/research/pricespread/Main.hs:62:58:
Couldn't match the rigid variable `b' against the rigid variable `a'
  `b' is bound by the type signature for `='
  `a' is bound by the type signature for `='
  Expected type: [b]
  Inferred type: [a]
In the second argument of `Computation', namely `h'
In the definition of `=':
= (Computation (Left e) h) f = Computation (Left e) h

I don't know what the problem is.

Thanks,

Tamas

On Mon, Oct 02, 2006 at 03:54:23PM +0200, Matthias Fischmann wrote:

 hi, i don't fully understand your problem, but perhaps you could use
 iterate to produce a list or type [Result a], ie, of all computation
 steps, and then use this function to extract either result or error
 from the list:
 
 
 type Failmessage = Int
 data Result a = Root a | Failure Failmessage  deriving (Show)
 
 f :: [Result a] - Either a (Int, [Result a])
 f cs = f [] cs
 where
 f (Root r:_) [] = Left r
 f l [Failure i] = Right (i, reverse l)
 f l (x:xs)  = f (x:l) xs
 
 cs = [Root 1.2, Root 1.4, Root 1.38, Root 1.39121]
 cs' = [Root 1.2, Root 1.4, Root 1.38, Failure 1]
 
 -- f cs  == Left 1.39121
 -- f cs' == Right (1,[Root 1.2,Root 1.4,Root 1.38])
 
 
 (although this way you probably have the list still floating around
 somewhere if you process the error returned by f, so f should probably
 just drop the traversed part of the list.)
 
 hth,
 matthias
 
 
 
 On Sun, Oct 01, 2006 at 06:00:43PM -0400, Tamas K Papp wrote:
  To: Haskell Cafe haskell-cafe@haskell.org
  From: Tamas K Papp [EMAIL PROTECTED]
  Date: Sun, 1 Oct 2006 18:00:43 -0400
  Subject: [Haskell-cafe] question - which monad to use?
  
  Hi,
  
  I have a computation where a function is always applied to the
  previous result.  However, this function may not return a value (it
  involves finding a root numerically, and there may be no zero on the
  interval).  The whole problem has a parameter c0, and the function is
  also parametrized by the number of steps that have been taken
  previously.
  
  To make things concrete,
  
  type Failmessage = Int  -- this might be something more complex
  data Result a = Root a | Failure Failmessage -- guess I could use Either too
  
  f :: Double - Int - Double 0 - Result Double
  f c0 0 _ = c0
  f c0 j x = {- computation using x, parameters calculated from c0 and j -}
  
  Then
  
  c1 = f c0 0 c0
  c2 = f c0 1 c1
  c3 = f c0 2 c2
  ...
  
  up to cn.
  
  I would like to
  
  1) stop the computation when a Failure occurs, and store that failure
  
  2) keep track of intermediate results up to the point of failure, ie
  have a list [c1,c2,c3,...] at the end, which would go to cn in the
  ideal case of no failure.
  
  I think that a monad would be the cleanest way to do this.  I think I
  could try writing one (it would be a good exercise, I haven't written
  a monad before).  I would like to know if there is a predefined one
  which would work.
  
  Thank you,
  
  Tamas
  ___
  Haskell-Cafe mailing list
  Haskell-Cafe@haskell.org
  http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 -- 
 Institute of Information Systems, Humboldt-Universitaet zu Berlin
 
 web:  http://www.wiwi.hu-berlin.de/~fis/
 e-mail:   [EMAIL PROTECTED]
 tel:  +49 30 2093-5742
 fax:  +49 30 2093-5741
 office:   Spandauer Strasse 1, R.324, 10178 Berlin, Germany
 pgp:  AD67 CF64 7BB4 3B9A 6F25  0996 4D73 F1FD 8D32 9BAA



 ___
 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] question - which monad to use?

2006-10-02 Thread Tamas K Papp
On Mon, Oct 02, 2006 at 11:35:40AM -0400, Tamas K Papp wrote:
 Matthias,
 
 Sorry if I was not clear in stating the problem.  Your solution works
 nicely, but I would like to try writing a monad.  This is what I came
 up with:
 
 type Failure = String
 data Computation a = Computation (Either Failure a) [a]
 
 instance Monad Computation where
 (Computation (Left e) h) = f = Computation (Left e) h -- do not proceed
 (Computation (Right a) h) = f = let r = f a -- result
   h' = case r of
  Left e - h
  Right a' - a':h
   in
 Computation r h'
 return (s,c) = Computation (Right (s,c)) [(s,c)]

sorry, I pasted an older version.  This line should be

return a = Computation (Right a) [a]

 Basically, I want the = operator to call f on the last result, if it
 is not a failure, and append the new result to the list (if it didn't
 fail).
 
 However, I am getting the following error message:
 
 /home/tpapp/doc/research/pricespread/Main.hs:62:58:
 Couldn't match the rigid variable `b' against the rigid variable `a'
   `b' is bound by the type signature for `='
   `a' is bound by the type signature for `='
   Expected type: [b]
   Inferred type: [a]
 In the second argument of `Computation', namely `h'
 In the definition of `=':
   = (Computation (Left e) h) f = Computation (Left e) h
 
 I don't know what the problem is.
 
 Thanks,
 
 Tamas
 
 On Mon, Oct 02, 2006 at 03:54:23PM +0200, Matthias Fischmann wrote:
 
  hi, i don't fully understand your problem, but perhaps you could use
  iterate to produce a list or type [Result a], ie, of all computation
  steps, and then use this function to extract either result or error
  from the list:
  
  
  type Failmessage = Int
  data Result a = Root a | Failure Failmessage  deriving (Show)
  
  f :: [Result a] - Either a (Int, [Result a])
  f cs = f [] cs
  where
  f (Root r:_) [] = Left r
  f l [Failure i] = Right (i, reverse l)
  f l (x:xs)  = f (x:l) xs
  
  cs = [Root 1.2, Root 1.4, Root 1.38, Root 1.39121]
  cs' = [Root 1.2, Root 1.4, Root 1.38, Failure 1]
  
  -- f cs  == Left 1.39121
  -- f cs' == Right (1,[Root 1.2,Root 1.4,Root 1.38])
  
  
  (although this way you probably have the list still floating around
  somewhere if you process the error returned by f, so f should probably
  just drop the traversed part of the list.)
  
  hth,
  matthias
  
  
  
  On Sun, Oct 01, 2006 at 06:00:43PM -0400, Tamas K Papp wrote:
   To: Haskell Cafe haskell-cafe@haskell.org
   From: Tamas K Papp [EMAIL PROTECTED]
   Date: Sun, 1 Oct 2006 18:00:43 -0400
   Subject: [Haskell-cafe] question - which monad to use?
   
   Hi,
   
   I have a computation where a function is always applied to the
   previous result.  However, this function may not return a value (it
   involves finding a root numerically, and there may be no zero on the
   interval).  The whole problem has a parameter c0, and the function is
   also parametrized by the number of steps that have been taken
   previously.
   
   To make things concrete,
   
   type Failmessage = Int  -- this might be something more complex
   data Result a = Root a | Failure Failmessage -- guess I could use Either 
   too
   
   f :: Double - Int - Double 0 - Result Double
   f c0 0 _ = c0
   f c0 j x = {- computation using x, parameters calculated from c0 and j -}
   
   Then
   
   c1 = f c0 0 c0
   c2 = f c0 1 c1
   c3 = f c0 2 c2
   ...
   
   up to cn.
   
   I would like to
   
   1) stop the computation when a Failure occurs, and store that failure
   
   2) keep track of intermediate results up to the point of failure, ie
   have a list [c1,c2,c3,...] at the end, which would go to cn in the
   ideal case of no failure.
   
   I think that a monad would be the cleanest way to do this.  I think I
   could try writing one (it would be a good exercise, I haven't written
   a monad before).  I would like to know if there is a predefined one
   which would work.
   
   Thank you,
   
   Tamas
   ___
   Haskell-Cafe mailing list
   Haskell-Cafe@haskell.org
   http://www.haskell.org/mailman/listinfo/haskell-cafe
  
  -- 
  Institute of Information Systems, Humboldt-Universitaet zu Berlin
  
  web:  http://www.wiwi.hu-berlin.de/~fis/
  e-mail:   [EMAIL PROTECTED]
  tel:  +49 30 2093-5742
  fax:  +49 30 2093-5741
  office:   Spandauer Strasse 1, R.324, 10178 Berlin, Germany
  pgp:  AD67 CF64 7BB4 3B9A 6F25  0996 4D73 F1FD 8D32 9BAA
 
 
 
  ___
  Haskell-Cafe mailing list
  Haskell-Cafe@haskell.org
  http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 

[Haskell-cafe] cumulative sum

2006-10-02 Thread Tamas K Papp
Hi,

I have two lists, p and lambda (both are finite).  I would like to
calculate

1) the cumulative sum of lambda, ie if

lambda = [lambda1,lambda2,lambda3,...]

then

csum lambda = [lambda1,lambda1+lambda2,lambda1+lambda2+lambda3,...]

2) the cumulative sum of p*lambda (multiplication elementwise)

Once I know how to do the first, I know how to do the second I guess
(doing the multiplication using zipWith to get the p*lambda list, but
I would be interested in any other suggestions).  Currently I take and
sum, but then I am calculating the same sums many times.

Thanks,

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


Re: [Haskell-cafe] cumulative sum

2006-10-02 Thread Michael Shulman

On 10/2/06, Tamas K Papp [EMAIL PROTECTED] wrote:

Hi,

I have two lists, p and lambda (both are finite).  I would like to
calculate

1) the cumulative sum of lambda, ie if

lambda = [lambda1,lambda2,lambda3,...]

then

csum lambda = [lambda1,lambda1+lambda2,lambda1+lambda2+lambda3,...]


Try (scanl1 (+) lambda)

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


Re: [Haskell-cafe] question - which monad to use?

2006-10-02 Thread Matthias Fischmann

On Mon, Oct 02, 2006 at 11:42:22AM -0400, Tamas K Papp wrote:
 To: haskell-cafe@haskell.org
 From: Tamas K Papp [EMAIL PROTECTED]
 Date: Mon, 2 Oct 2006 11:42:22 -0400
 Subject: Re: [Haskell-cafe] question - which monad to use?
 
 On Mon, Oct 02, 2006 at 11:35:40AM -0400, Tamas K Papp wrote:
  Matthias,
  
  Sorry if I was not clear in stating the problem.  Your solution works

no problem, i like to be confused by missing facts.  (-: and you gave
enough input for a discussion.


  type Failure = String
  data Computation a = Computation (Either Failure a) [a]
  
  instance Monad Computation where
  (Computation (Left e) h) = f = Computation (Left e) h -- do not 
  proceed
  (Computation (Right a) h) = f = let r = f a -- result
h' = case r of
   Left e - h
   Right a' - a':h
in
  Computation r h'
  return (s,c) = Computation (Right (s,c)) [(s,c)]
 
 sorry, I pasted an older version.  This line should be
 
 return a = Computation (Right a) [a]

yeah, that works.  the (=) part has two problems:

 (1) according to the Monad class, the type of f is (a - m b), and
 the type of (a = f) is m b.  but in your definition, (a = f)
 has the same type as a, no matter what f.

 (2) the cases in the definition of h' shouldn't be of type (Either
 Failure a), but of type (Computation b).

the second one is easy to fix, just add the constructors to the case
switches.  the first is more of a conceptual problem: you want to have
elements of potentially different types in the computation history h.
this is unfortunate, given that you don't want make use of this
flexibility of the class type, but i don't see a quick way around
this.  

i have been meaning to read this for a while, perhaps that could help
you (but i sense it's somewhat of an overkill in your case): Oleg
Kiselyov, Ralf Laemmel, Keean Schupke: Strongly typed heterogeneous
collections, http://homepages.cwi.nl/~ralf/HList/.

donno...


matthias



  Basically, I want the = operator to call f on the last result, if it
  is not a failure, and append the new result to the list (if it didn't
  fail).
  
  However, I am getting the following error message:
  
  /home/tpapp/doc/research/pricespread/Main.hs:62:58:
  Couldn't match the rigid variable `b' against the rigid variable `a'
`b' is bound by the type signature for `='
`a' is bound by the type signature for `='
Expected type: [b]
Inferred type: [a]
  In the second argument of `Computation', namely `h'
  In the definition of `=':
  = (Computation (Left e) h) f = Computation (Left e) h
  
  I don't know what the problem is.
  
  Thanks,
  
  Tamas
  
  On Mon, Oct 02, 2006 at 03:54:23PM +0200, Matthias Fischmann wrote:
  
   hi, i don't fully understand your problem, but perhaps you could use
   iterate to produce a list or type [Result a], ie, of all computation
   steps, and then use this function to extract either result or error
   from the list:
   
   
   type Failmessage = Int
   data Result a = Root a | Failure Failmessage  deriving (Show)
   
   f :: [Result a] - Either a (Int, [Result a])
   f cs = f [] cs
   where
   f (Root r:_) [] = Left r
   f l [Failure i] = Right (i, reverse l)
   f l (x:xs)  = f (x:l) xs
   
   cs = [Root 1.2, Root 1.4, Root 1.38, Root 1.39121]
   cs' = [Root 1.2, Root 1.4, Root 1.38, Failure 1]
   
   -- f cs  == Left 1.39121
   -- f cs' == Right (1,[Root 1.2,Root 1.4,Root 1.38])
   
   
   (although this way you probably have the list still floating around
   somewhere if you process the error returned by f, so f should probably
   just drop the traversed part of the list.)
   
   hth,
   matthias
   
   
   
   On Sun, Oct 01, 2006 at 06:00:43PM -0400, Tamas K Papp wrote:
To: Haskell Cafe haskell-cafe@haskell.org
From: Tamas K Papp [EMAIL PROTECTED]
Date: Sun, 1 Oct 2006 18:00:43 -0400
Subject: [Haskell-cafe] question - which monad to use?

Hi,

I have a computation where a function is always applied to the
previous result.  However, this function may not return a value (it
involves finding a root numerically, and there may be no zero on the
interval).  The whole problem has a parameter c0, and the function is
also parametrized by the number of steps that have been taken
previously.

To make things concrete,

type Failmessage = Int  -- this might be something more complex
data Result a = Root a | Failure Failmessage -- guess I could use 
Either too

f :: Double - Int - Double 0 - Result Double
f c0 0 _ = c0
f c0 j x = {- computation using x, parameters calculated from c0 and j 
-}

Then

c1 = f c0 0 c0
c2 = f c0 1 c1
c3 = f c0 2 c2
...

 

Re: [Haskell-cafe] cumulative sum

2006-10-02 Thread Chad Scherrer

Tamas,

try
scanl (+) 0
for the cumulative sum


From there the zipWith idea you mentioned seems like the way to go.


-Chad


Hi,

I have two lists, p and lambda (both are finite).  I would like to
calculate

1) the cumulative sum of lambda, ie if

lambda = [lambda1,lambda2,lambda3,...]

then

csum lambda = [lambda1,lambda1+lambda2,lambda1+lambda2+lambda3,...]

2) the cumulative sum of p*lambda (multiplication elementwise)

Once I know how to do the first, I know how to do the second I guess
(doing the multiplication using zipWith to get the p*lambda list, but
I would be interested in any other suggestions).  Currently I take and
sum, but then I am calculating the same sums many times.

Thanks,

Tamas

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


Re: [Haskell-cafe] cumulative sum

2006-10-02 Thread Matthias Fischmann


 try
 scanl (+) 0
 for the cumulative sum

and it is probably worth pointing out once more that (as i have
learned only recently :-) Hoogle can help you even quicker than this
list with questions like these: scanl is the fifth answer if you ask
for a - [a] - [a].

also, the url is easy to remember: http://www.haskell.org/hoogle/


cheers,
m.


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


[Haskell-cafe] Re: question - which monad to use?

2006-10-02 Thread apfelmus
Tamas K Papp wrote:
 Hi,
 
 I have a computation where a function is always applied to the
 previous result.  However, this function may not return a value (it
 involves finding a root numerically, and there may be no zero on the
 interval).  The whole problem has a parameter c0, and the function is
 also parametrized by the number of steps that have been taken
 previously.
 
 To make things concrete,
 
 type Failmessage = Int  -- this might be something more complex
 data Result a = Root a | Failure Failmessage -- guess I could use Either too
 
 f :: Double - Int - Double 0 - Result Double
 f c0 0 _ = c0
 f c0 j x = {- computation using x, parameters calculated from c0 and j -}
 
 Then
 
 c1 = f c0 0 c0
 c2 = f c0 1 c1
 c3 = f c0 2 c2
 
 
 up to cn.
 
 I would like to
 
 1) stop the computation when a Failure occurs, and store that failure
 
 2) keep track of intermediate results up to the point of failure, ie
 have a list [c1,c2,c3,...] at the end, which would go to cn in the
 ideal case of no failure.
 
 I think that a monad would be the cleanest way to do this.  I think I
 could try writing one (it would be a good exercise, I haven't written
 a monad before).  I would like to know if there is a predefined one
 which would work.

There are a several ways to achieve your goal, most do not use monads.

*a) The underappreciated unfold*

unfoldr :: (a - Maybe (b,a)) - a - [b]

basically iterates a function

g :: a - Maybe (b,a)

and collects [b] until f fails with Nothing.

With your given function f, one can define

g c0 (j,c) = case f c0 j c of
Root c' - Just (c',(j+1,c'))
_   - Nothing

and get the job done by

results = unfoldr (g c0) (0,c0)

The only problem is that the failure message is lost. You can write your
own unfold, though:

   unfoldE ::
   (a - Either Failmessage a) - a - ([a], Maybe Failmessage)



*b) tying the knot, building an infinite list*

cs = Root c0 : [f c0 j ck | (j,Root ck) - zip [0..] cs]

will yield

cs [Root c0, Root c1, ..., Failure i] ++ _|_

Then, you just have to collect results:

collect xs = (failure, [ck | Root ck - ys])
where
isFailure (Failure i) = True
isFailure _ = False
(ys,failure:_) = break isFailure
results = collect cs

Note that in this case, you always have to end your values with a
failure (success as failure). Alas, you didn't mention a stopping
condition, did you?



*c) the monadic way*
This is not the preferred solution and I'll only sketch it here. It only
makes sense if you have many different f whose calling order depends
heavily on their outcomes. Basically, your monad does: 2) keep track of
results (MonadWriter) and 2) may yield an error (MonadError). Note that
you want to keep track of results even if an error is yielded, so you
end up with

type MyMonad a = ErrorT (Either Failmessage) (Writer [Double]) a

where ErrorT and Writer are from the Control.Monad.* modules.

f :: Double - Int - Double - MyMonad Double
f c0 j ck = do
{computation}
if {screwed up}
then fail you too, Brutus
else tell {c_{k+1}}
return {c_{k+1}}



*d) reconsider your definition of f, separate concerns *
The fact that the computation of ck depends on the iteration count j
makes me suspicious. If you are using j for convergence tests etc. only,
then it's not good.
The most elegant way is to separate concerns: first generate an infinite
list of approximations

f :: Double - Double - Double
f c0 ck = {c_{k+1}}

cs = iterate (f c0)

and then look for convergence

epsilon = 1e-12
takeUntilConvergence []  = []
takeUntilConvergence [x] = [x]
takeUntilConvergence (x:x2:xs) =
if abs (x - x2) = epsilon
then [x]
else x:takeUntilConvergence (x2:xs)

or anything else (irregular behaviour, ...). If it's difficult to tell
from the cs whether things went wrong, but easy to tell from within f
(division by almost 0.0 etc.), you can always blend the separate
concerns approach into a) and b):

-- iterate as infinite list
iterate f x0 = let xs = x0 : map f xs in xs
-- iterate as unfoldr
iterate f x0 = unfoldr g x0
where g x = let x' = f x in Just (x',x')


Regards,
apfelmus

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


[Haskell-cafe] Re: How would you replace a field in a CSV file?

2006-10-02 Thread apfelmus
Pete Kazmier wrote:
 import Data.ByteString.Lazy.Char8 as B hiding (map,foldr)
 import Data.List (map)
 import Data.Map as M hiding (map)
 
 -- This will be populated from a file
 dict = M.singleton (B.pack Pete) (B.pack Kazmier)
 
 main = B.interact $ B.unlines . map doline . B.lines
 where doline= B.join comma . mapIndex fixup . B.split ','
   comma = B.singleton ','
   fixup 3 s = M.findWithDefault s s dict
   fixup n s = s
 
 -- f is supplied the index of the current element being processed
 mapIndex :: (Int - ByteString - ByteString) - [ByteString] -
 [ByteString]
 mapIndex f xs = m xs 0
 where m []  _ = []
   m (x:xs') i = f i x : (m xs' $! i+1)

How about

import Data.ByteString.Lazy.Char8 as B hiding (map,foldr)
import Data.List (map)
import Data.Map as M hiding (map)

dict = M.singleton (B.pack Pete) (B.pack Kazmier)

main = B.interact $ B.unlines . map doline . B.lines
where
doline  = B.join comma . zipWith ($) fixup9 . B.split ','
fixup9  = fixup 9
fixup n = replicate n id
  ++ [\s - M.findWithDefault s s dict] ++ repeat id

Note that fixup9 is shared intentionally across different invocations of
doline. The index n starts at 0.


Also note that because (compare :: (Byte)String - ..) takes time
proportional to the string length, the use of Map will inevitably
introduce a constant factor. But I'm still happy you didn't use arrays
or hash tables (urgh!) :)

In any case, tries are *the* natural data structure for (in)finite maps
in functional languages, see also

Ralf Hinze. Generalizing generalized tries. Journal of Functional
Programming, 10(4):327-351, July 2000
http://www.informatik.uni-bonn.de/~ralf/publications/GGTries.ps.gz


Regards,
apfelmus

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


[Haskell-cafe] Re: Problematic irrefutable pattern matching of existentials

2006-10-02 Thread apfelmus
[EMAIL PROTECTED] wrote:
 I have come to realize that irrefutable pattern matching of
 existentials may indeed be problematic. Let us consider the following
 existential data type
 
 data FE = forall a. Typeable a = Foo a
| forall a. Typeable a = Bar a
 
 The following tests type and run (the latter raising the expected
 exception).
 
 test1 = case (Foo ()) of Foo x - show $ typeOf x
 test2 = case (Bar True) of Foo x - show $ typeOf x
 
 Let us suppose that irrefutable pattern matching of existentials is
 allowed. What would be the value of the expression
 
   case (Bar True) of ~(Foo x) - show $ typeOf x
 then?

Interesting, interesting. Without those unsafe Typeables and further
simplified, we can say

class Foo a where
foo :: a - Int

instance Foo ()   where foo = const 1
instance Foo Bool where foo = const 2

data Bar = forall a . Foo a = Bar a

culprit :: Bar - Int
culprit ~(Bar x) = foo x

Now, hugs -98 gives

 culprit (Bar (undefined :: ()))
1
 culprit (Bar (undefined :: Bool))
2

The interesting part, however is

 culprit undefined
Program error: Prelude.undefined

 culprit (Bar undefined)
ERROR - Unresolved overloading
*** Type   : Foo a = Int
*** Expression : culprit (Bar undefined)

But both should be the same, shouldn't they? In the first case, the
implementation gets an undefined dictionary. But in the second case, the
type system complains.

If we used explicit dictionaries as in
data Bar = forall a . Bar (a-Int, a)
the second case would yield _|_, too,

 One may claim that the existential pattern match also binds an
 implicit dictionary argument, which is demanded above. That
 explanation is quite unsatisfactory, because it breaks the abstraction
 of type classes.

Indeed, the above shows the subtle difference: with type classes, one
may not pass an undefined dictionary as this is an unresolved
overloading. Irrefutable patterns for existential types somehow disturb
things.


Regards,
apfelmus

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


[Haskell-cafe] DiffTime in Data.Time

2006-10-02 Thread Chad Scherrer

I'm trying to use Data.Time, and I'm totally confused. DiffTime is
abstract, and I don't see anything that maps into it. How do I
construct one? I would like to then use the result to create a value
of type UTCTime, but it seems (currently) like this might be easier.

Thanks,

--

Chad Scherrer

Time flies like an arrow; fruit flies like a banana -- Groucho Marx
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] irrefutable patterns for existential types / GADTs

2006-10-02 Thread John Meacham
On Sat, Sep 30, 2006 at 01:38:28AM -0700, [EMAIL PROTECTED] wrote:
 
 It seems that irrefutable pattern match with existentials is safe. The
 fact that irrefutable pattern match with GADT is unsafe has been
 demonstrated back in September 2004.

it is safe in that you can't express coerce, but irrefutable pattern
matching on existentials has other practical issues for an
implementation. namely, even though you may not ever evaluate the
irrefutable pattern, its type is probably used in the underlying
translation somewhere like. if you happen to use any polymorphic
functions. for instance

 data Foo = exists a . Foo a
 f ~(Foo x) = const 4 (id x)

now, you would think that f _|_ would evaluate to 4, but (depending on
your translation) it might evaluate to _|_.

the reason being that id translates internally to 

 id = /\a . \x::a . x 

since you cannot pull out an appropriate type to pass to id without
evaluating the 'irrefutable' pattern, you end up with _|_ instead of 4.

basically, allowing irrefutable matching on existentials would expose
whether the underlying implementation performs type-erasure. even if an
implementation does type erasure like ghc. suddenly odd things occur,
like adding an unusned class constraint to a type signature might change
the behavior of a program. (since it will suddenly have to pull out a
dictionary)

All in all, even though type-safety is not broken, irrefutable patterns
should not be allowed for existentials.

John

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


[Haskell-cafe] Either as a Monad instance

2006-10-02 Thread Thomas Conway

Hi All,

I've been [trying to] grapple with the various monads and
transformers, and it occurs to me that the standard instance for
Either as a monadic type is unnecessarily restrictive. Is there a
compelling reason that it is not just

instance Monad (Either e) where
   return = Right
   (Left e) = f = Left e
   (Right x) = f = f x

abort = Left

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


[Haskell-cafe] Re: DiffTime in Data.Time

2006-10-02 Thread Ashley Yakeley

Chad Scherrer wrote:

I'm trying to use Data.Time, and I'm totally confused. DiffTime is
abstract, and I don't see anything that maps into it. How do I
construct one? I would like to then use the result to create a value
of type UTCTime, but it seems (currently) like this might be easier.


It's an instance of Num etc. (as seconds).

--
Ashley Yakeley
Seattle WA

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


[Haskell-cafe] Re: DiffTime in Data.Time

2006-10-02 Thread Chad Scherrer

Ok, that's much simpler than I was making it. fromIntegral or
fromRational does the trick. Obvious in hindsight, I guess. Thanks!

-Chad

On 10/2/06, Ashley Yakeley [EMAIL PROTECTED] wrote:

Chad Scherrer wrote:
 I'm trying to use Data.Time, and I'm totally confused. DiffTime is
 abstract, and I don't see anything that maps into it. How do I
 construct one? I would like to then use the result to create a value
 of type UTCTime, but it seems (currently) like this might be easier.

It's an instance of Num etc. (as seconds).

--
Ashley Yakeley
Seattle WA




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