Re: Yet Another Monad Tutorial

2003-08-15 Thread Lars Lundgren
On Thu, 14 Aug 2003, blaat blaat wrote:

 
 To many questions, not enough mail. First, thanks for all your replies. 
 Second, I stand totally corrected on the fact that we cannot break down 
 monads. Functions of type m a-b are called impure,

Haskell is a pure language, so there are *no* impure functions.
Expressions in haskell are also referentially transparent, except... there
are a few extensions that allow one to disguise a (possibly)
side-effect-full expression into one that haskell believes is RT (FFI or
unsafePerformIO comes to mind). This is really unsafe, since haskell might
evaluate it (which cause execution) any number of times including Zero and
One Zillion. The by newbies often requested IO a - a extraction
function is a bad idea for this reason, and generally just means that the 
newbie has not understood how to deal with actions in haskell.

It is common for Monads to have a runme function of type
runme :: Monad m = m a - a

Nothing strange about that.

 see also 
 unsafePerformIO. I am questioning (a) the exact relation between monads, 
 monadic IO, IO in general and (b) whether the impure restriction is actually 
 a warranted restriction, which relates to, what I find, strange evaluation 
 behavior of the IO monad.
 
 What is the difference between putStr a, (putStr a, putStr a), putStr 
 (putStr a), putStr (show (putStr a))?
 
 At the moment objects of type IO a _only_ get evaluted when they are the 
 _sole_ result of a Haskell program. What kind of strange dichotomy is that?

A haskell program has the type IO () and will be executed bu the haskell
runtime. The situation is a little more fuzzy when you use a interpreter.
The interpreters usualy execute entered expressions of type IO a, and
evaluate all other expressions. I can agree that this might not be
obvious.
 
/Lars L


___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: Pure File Reading (was: Dealing with configuration data)

2002-09-26 Thread Lars Lundgren

On 26 Sep 2002, Yoann Padioleau wrote:

 Koen Claessen [EMAIL PROTECTED] writes:
 
 i find your idea very good.
 indeed for the library GetOpt, the argument of a program never change so it
  make sense to make this library without using IO monad, same for argv and for the 
enviroment.

 for openFile it seems harder, it would require to have a lock on the file
  to be sure that no one modify the file, same for a webpage.
 

No, not at all. All that is required is to make sure all calls to openFile
f returns the same string, i.e the contents of the file when it was read
the first time.

/Lars L


___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell



Re: Pure File Reading (was: Dealing with configuration data)

2002-09-26 Thread Lars Lundgren

On 26 Sep 2002, Yoann Padioleau wrote:

 Koen Claessen [EMAIL PROTECTED] writes:
 
 i find your idea very good.
 indeed for the library GetOpt, the argument of a program never change so it
  make sense to make this library without using IO monad, same for argv and for the 
enviroment.

 for openFile it seems harder, it would require to have a lock on the file
  to be sure that no one modify the file, same for a webpage.
 

No, not at all. All that is required is to make sure all calls to openFile
f returns the same string, i.e the contents of the file when it was read
the first time.

/Lars L


___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe



Re: monad binding and garbage collection

2002-01-17 Thread Lars Lundgren

On Thu, 17 Jan 2002, Christian Schuhegger wrote:

 buildIOList :: [a] - IO [a]
 buildIOList li = return li
 
 main = do {li - buildIOList [1..];
putStr (show (take 1 li));
   }

I take it you know that

do li - buildIOList [1..]
   ...

is equal to 

do let li = [1..] 
   ...

There is no need to to lift a value to the IO monad, if all you are
going to do is immediatelly extract it.

Mvh
/Lars L


___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe



Re: Tree handling

2001-02-26 Thread Lars Lundgren



On Mon, 26 Feb 2001, Martin Gustafsson wrote:

 Hello 
 
 I'm a haskell newbie that tries to create a tree with arbitary numbers of childs. 
 I create the data structure but i can't do anything on it can someone please help
 me with a small function that sums the values of the leafs, so i dont loose my hair
 so fast.
 
 The datastructure looks like this and a binary tree built with it would look like 
this:
 
 
 data GeneralTree  = Nil | Node (Integer,[GeneralTree])
 

As you said you were a newbie I will ask a few questions about your
datastructure.

Do you know that there is no need to tuple the elements in the Node if you
do not want to. You can write:

data GeneralTree  = Nil | Node Integer [GeneralTree]


What is the intended difference between (Node 5 []) and (Node 5 [Nil]) ?


 
 tree = 
   (20,
[
 (-20,[(30,[Nil]),(20,[Nil])]),
 (40,[(65,[Nil]),(-40,[Nil])])
]
   )

This is not of type GeneralTree! (And its layout is messed up)

Hint: write the type of every expression you write, and debugging will be
much easier.

tree :: GeneralTree

ERROR tree.hs:8 - Type error in explicitly typed binding
*** Term   : tree
*** Type   : (a,[(b,[(c,[GeneralTree])])])
*** Does not match : GeneralTree

This is an expression with type GeneralTree:

tree :: GeneralTree
tree = Node 20 [Node (-20) [Node 30 [Nil], Node 20 [Nil]],
Node 40[Node 65 [Nil], Node (-40) [Nil]]]

Now it should be very easy to write a function to sum the nodes in a tree

sumTree :: GeneralTree - Integer
sumTree Nil = 0
sumTree (Node n ts) = ... write this yourself 

hint - sum and map are very useful functions (defined in the prelude) as
are recursion.

Good luck!

/Lars L




___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe



Re: unliftM

2001-02-22 Thread Lars Lundgren



On 23 Feb 2001, Julian Assange wrote:

 
 Is there a standard construct for something of this ilk:
 
 unliftM :: Monad m a - a
 

I do not know if it is a standard, but every monad usually has a
"runMonad" function. For ST you have runST, for IO you have
unsafePerformIO and for your own monad you need to define it.

Note, that if you use unsafePerformIO you, the action must not have
(important) sideeffects. The burden of proof is upon YOU.

/Lars L


___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe



Re: Type class inference trouble

2001-02-19 Thread Lars Lundgren



On Sat, 17 Feb 2001, Ken Shan wrote:

 On 2001-02-16T09:52:41+0100, Lars Lundgren wrote:
  This is ad hoc overloading, and IMHO bad style, at least in haskell. As I
  understand it, haskell type classes were never intended to support this.
 
 Well, whether this is ad hoc overloading depends on whether you find
 ad hoc this particular theory of how sentence fragments in natural
 language combine with each other... 

That particular theory is certainly unknown to the type class mechanism
and, I guess, hard to encode in it.

 I don't particularly want to
 overload like I can in C++; I just want to overload like people (are
 theorized by some linguists to) overload in English.
 

That is very fine, I just wanted to warn you that type classes might not
be the best tool for the job. It's hard to appreciate type classes if you
think it is something it isn't. A screwdriver is a very useful tool, but
if you try to use it as a hammer you will get dissapointed.

Overloading in natural languages is very free, and very hard to formalize.
Overloading in haskell is intentionally much more restricted. The
overloading in C++ might be a better fit.


 In any case, I can certainly dump everything (Int, Int-Int-Int, and
 whatnot) into a single sum type, and perform my own type checking, but
 I'd rather not duplicate the work that Haskell has done for me.  A few
 minutes ago I came up with the idea of turning polymorphic types into
 non-polymorphic types using sum types, thus allowing polymorphic
 values, but I'll have to make things more concrete before I can say
 more about this idea.
 

You lost me. I do not understand the connection between this and
overloading. I.e overloading is about syntax.

/Lars L


___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell



Re: Learning Haskell and FP

2001-01-04 Thread Lars Lundgren

On Wed, 3 Jan 2001, Michael Zawrotny wrote:

 1.  How the #$!? do I read some data from a file.  Good, I've
 got the data, now I can work on it.  Nope, now I have an "IO
 thingie" whatever that is, but all of the standard functions want
 a regular "thingie" now what?
 

I do not know if you actually wanted an answer to this, but I'm sick of
hearing this FAQ everywhere when the answer is so simple. There are
exactly two ways to do this (one of them is actually syntactic sugar for
the other).

1. Use the do notation:

   do regularThingie - IOThingie
  return (doWhateverYouWantWith regularThingie)

2. Use bind ( = ):

   IOThingie =
   \regularThingie - return (doWhateverYouWantWith regularThingie)


Note: Both constructs produces IO thingies. This is the real beauty of it,
if you have a value that is dependent on the environment (i.e. a IO value)
you can use it as a regular value inside one of the above constructs, but
the result will always be an IO value (The result will depend on the
environment because it uses a value dependent of the environment). This is
no problem, just accept it.

Can everyone include an answer to this FAQ everywhere, phleaze!

/Lars L




___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell



Re: class instance with nested types

2000-10-27 Thread Lars Lundgren

On Fri, 27 Oct 2000, Matthias Höchsmann wrote:

 Hello,
 
 I have the following problem:
 
 basic datatypes
 
  type Sequence a = [a]
  data Tree a = N a (Forest a) deriving (Ord,Eq,Show)
  type Forest a = Sequence (Tree a)
 
 i want to construct a class Xy
 
  class Xy s a where
   test :: s a - a
 
 and make an instance for list of characters
 
  instance Xy [] Char where
   test [a] = a
 
 this works, and an instance for a forest and tried something like this
 
  instance  ([] Tree) Char where
  test x@(N a xs):txs = a
 

Don't you mean

test (N a xs:txs) = a

?

/Lars L



___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell



Re: pronunciation of =

2000-10-18 Thread Lars Lundgren

On Wed, 18 Oct 2000, Scott Turner wrote:

 Is there a common way to pronounce "=" in discussions or when teaching?
 I've learned all my Haskell from printed/visual documents.

How about 'bind'? and "" = 'then'.

/Lars L



___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell



Re: more detailed explanation about forall in Haskell

2000-05-19 Thread Lars Lundgren

On 19 May 2000, Ketil Malde wrote:

 Frank Atanassow [EMAIL PROTECTED] writes:
 
  I agree, and I think it's hopeless. This is my last message to the Haskell
  list on the subject. There is nothing Haskell-specific any longer about this
  discussion.
 
 Uh, I feel I'm a bit of a hobbyist on this list, but what exactly is
 the relevance of all this?  Flipping through these messages, I feel I
 have aquired some grasp of forall, but in what contexts is forall used 
 in Haskell - can anybody point me to an URL that explains or
 exemplifies?  
 

How about:

http://haskell.org/onlinereport/decls.html
http://www.haskell.org/ghc/docs/latest/users_guide/universal-quantification.html
http://www.cse.ogi.edu/PacSoft/projects/Hugs/pages/hugsman/exts.html
http://www.cs.chalmers.se/~augustss/hbc/decls.html


/Lars L






Re: more detailed explanation about forall in Haskell

2000-05-16 Thread Lars Lundgren

On Tue, 16 May 2000, Jan Brosius wrote:

 Ok I understand this isomorphism better. However this remark seems to be of
 no value to functional programmers.
 Why trying to mix terms( otr types) with relations ?
 

What is a 'type' in your oppinion?

Isn't a type a statement about pre- and post-conditions, i.e. a formula?

/Lars L






Re: more detailed explanation about forall in Haskell

2000-05-11 Thread Lars Lundgren

On Thu, 11 May 2000, Jan Brosius wrote:

 Marcin Kowalczyk wrote at Wed, May 10, 2000 7:54 PM :
 
  Types can be treated as logical formulas, according to the Curry-Howard
  isomorphism.
 
 Sorry, never heard of in logic. But perhaps you can explain.
 

M H Sørensen and P Urzyczyn.
Lectures on the Curry-Howard Isomorphism. 
Available as DIKU Rapport 98/14, 1998. 

Keywords: The Curry-Howard isomorphism, Logic, Lambda-Calculus 

http://www.diku.dk/research-groups/topps/bibliography/1998.html#D-368

/Lars L






Re: How to debug Haskell programs?

2000-04-26 Thread Lars Lundgren

On 26 Apr 2000, Friedrich Dominicus wrote:

 This question may sound a bit strange. I'll try to explain. I have
 written a piece of Haskell and found that I did not understand
 things. I tried to look what I got wrong and searched for a
 possibility to give me output. Now obvious is that Haskell has not
 debugger so I was thinking helping myself with adding some simple
 output stuff but that did not seem to work either. So guess I have
 
 sum:: [Int] - Int
 sum [] = 0
 sum (x:xs) = 
 
 Now how can I do the following print the actual sum in every step and
 how to see e.g what x or xs is on this step?
 
 I tried it with simple ouput but did not get it right.
 
 Any suggestions?
 Friedrich
 


import IOExts

sum:: [Int] - Int
sum [] = 0
sum (x:xs) = trace (show (x:xs) ++ "\n") 


Note, this is an unpure extension to haskell that should be used for
debugging purposes only. It can sometimes be very hard to interpret the
output since it will be produced in the order the expressions are
evaluated.

Anyway, it is sometimes useful.

/Lars L






Re: openfile :: String - String

2000-04-26 Thread Lars Lundgren

On Wed, 26 Apr 2000, Hamilton Richards wrote:

 The Gofer prelude had a function
 
   openfile :: String - String
 
 which mapped file path names to strings containing the named files' contents.
 
 The Hugs98 Prelude doesn't seem to have anything like that function.
 Instead, it has things like
 
   readFile :: String - IO String
 
 Why would I want openfile instead of readFile? The problem in which the
 need for openfile arose is this:
 
I want to load a binary search tree and a list with
words read from a file, and then perform, interactively,
several tests comparing the cost of searching the
tree with that of searching the list. In addition to
performing the tests interactively, I want to separate
the cost of searching the list and the tree from the
cost of constructing them.
 
 In order for the list and the tree to be used in several successive
 command-line evaluations without being reconstructed each time, they must
 be named globally. This is no problem with openfile, but readFile forces
 their names to be local to an IO command.
 

Why is this a problem? After all, the names ARE dependent on IO.

 Can anyone suggest a solution?


I do not understand the problem.

Is it something like this you want?

parseTree :: String - Tree
parseList :: String - List

buildTree:: IO Tree
buildTree = do f - readFile "thetree"
   return (parseTree f)

buildList:: IO List
buildList = do f - readFile "thelist"
   return (parseList f)  

test :: IO()
test = do tree - buildTree
  list - buildList
  dowhateverYouwant_interactive_or_not tree list


dowhateverYouwant_interactive_or_not :: Tree - List - IO()

 
/Lars L







Re: File handling

2000-03-27 Thread Lars Lundgren

On Sat, 25 Mar 2000, T. D. Stones wrote:

 Hello there
 
 I am A 3rd year student at Leicester Uni, and I am doing my project
 using Hugs 1.4.  I've noticed that things like EOF

What is it you want to do with EOF?

 (I need to read a file 

aha, so why dont you use readFile :: FilePath - IO String

 and also write back to file

and writeFile :: FilePath - String - IO () ?

) are not implimented in our version.
 So I was hopping that some one could point me in the right direction.
 I.e. is there a module that contains the functiuons that I will need.  I
 am working on the computers a Uni so I don't have root access
 
 Sorry but I didn't know where else to turn.
 
 Thanks in advance
 Torben
 

/Lars L






Re: your mail

2000-02-10 Thread Lars Lundgren

On Wed, 9 Feb 2000, Stefan Friedel wrote:

 Hi everybody, I have a problem. I'm new to haskell and
 I have to write a function that takes the following
 list and finds the average by using recursion and
 adding the numbers together. I'm completely STUCK!
 Thank you. 
 
  sales :: Int - Float
  sales n
  | n == 0 = 23.89
  | n == 1 = 89.30
  | n == 2 = 203.59
  | n == 3 = 157.67
  | n == 4 = 58.35
  | n == 5 = 78.75
  | n == 6 = 199.35
  | n == 7 = 359.25
  | n == 8 = 539.67
  | n == 9 = 259.58
  | n  9  n  52 = 350.00
 
That may be because what you present is *not* a list, but a function, a
function with the type Int - Float as you wrote yourself.

A list is something constructed by the two constructor (functions):

(:) :: a - [a] - [a]  -- Note, this is an *infix* operator. 
[]  :: [a]  -- The empty list

To begin with, lets construct the list I guess you want.

We start with [], the empty list. then we add the first number:

259.58 : []   

and the next:

539.67 : 259.58 : []

and so on:

23.89 : 89.30 : 203.59 : 157.67 : 58.35 : 78.75 : 199.35 : 359.25 : 539.67
: 259.58 : []

There is a special syntactic sugar for lists, they can instead be written
as:

[23.89,89.3,203.59,157.67,58.35,78.75,199.35,359.25,539.67,259.58]

But remember, this is only syntactic sugar.

But hold on, there were more numbers, but we do not want to enumerate them
all. Lets use a predefined function (actually defined in Prelude.hs).

replicate 42 350.00

This will create a list with 42 350.00 elements.

[23.89,89.3,203.59,157.67,58.35,78.75,199.35,359.25,539.67,259.58] ++
(replicate 42 350.00)

This will concatenate (++) the two lists.

Now, you have your list.

To continue, I suggest you go to:

http://www.haskell.org/
  The Haskell Bookshelf
Papers available on the Web
  General Introductions to Haskell


(comp.lang.functional might be a better forum for this kinds of questions)
/Lars L





Re: file manipulation

1999-12-06 Thread Lars Lundgren

On Mon, 6 Dec 1999, c_stanto wrote:

 
 here is my code and I am guess that the problem is that openFile returns
 an IO Handle and getContents takes just a Handle.  How do I go about
 fixing this?  
 
 
 module Main(main) where
 
 import IO
 
 main = putStrLn (getContents (openFile "./joseph.txt" ReadMode))
   

why not just:

main = readFile "./joseph.txt" = putStrLn

(Think of = as a unix pipe | )

/Lars L





Re: OO in Haskell

1999-10-11 Thread Lars Lundgren

On Mon, 11 Oct 1999, Adrian Hey wrote:

 On Mon 11 Oct, Lars Lundgren wrote:
  I'm sure a lot of poeple have gotten this wrong. I would be surprised if
  not all the experienced haskellers has this view though.
 
 Probably so, but this view seems in complete contradiction to 
 that of the Clean world. So I'm still confused :-)
 

I just took a glance at Clean. (Glanced through "The Ins and Outs of Clean
I/O" by Peter Achten and Rinus Plasmeijer.) I think their solution with
unique types is really neat. 

One downside may be that they have made the type system more complex
since it has to handle all the uniqness tags.

They deal with side effects (IO) by tagging the values with * and calling
them unique. Haskell deals with side effects (IO) by using an abstract
data type IO a which denotes an action [with clean type *World -
(a,*World) ]. In both cases, the compiler is notified that it is not ok to
change order of evaluation.


In the Related work section, they mention Monadic IO and writes "To our
knowledge combining monads of different type is a rather tedious task..."

I'm reluctant to say that I agree. I have written a few programs using
monad transformers and while everything works in principle, it is, well -
tedious... 

I also do not like the tendency to put more things in the IO monad (I'm
thinking about the extensions with IORef). I like stToIO better, but
somehow it still feels like a hack. Maybe some library support for monad
transformers and maybe even some syntactig sugar would do the trick.

They also wrote "[The monadic IO approach] over determines order of
evaluation". I'm a bit puzzled about that statement. Is it true? Comments
anyone?

/Lars L









Re: OO in Haskell

1999-10-11 Thread Lars Lundgren

On Fri, 8 Oct 1999, Adrian Hey wrote:

 On Fri 08 Oct, Lars Lundgren wrote:
  A value (IO a) *denotes* a program possibly interacting with the world.
  *That* program is of course not referentially transparent. A haskell
  program generating an (IO a) on the other hand *is* referetially
  transparent. 
 
 So a value of type (IO a) is _not_ a function, if I understand you
 correctly.
 

From haskell point of view, it's a value, end of story. The value denotes
an action possibly doing IO. When this action is executed, it will
probably have side effects (thus, it is not a pure function), after all
thats the sole purpose of IO.

 I think this is really the correct interpretation, but I'm not sure
 if there's any real enthusiasm for this view in the FP community at
 large. (When I suggested a similar approach on another list it didn't
 seem to go down to well.)
 

I'm sure a lot of poeple have gotten this wrong. I would be surprised if
not all the experienced haskellers has this view though.

 Also, the fact that the machine which executes values of type (IO a)
 is not regarded as part of Haskell is also not widely understood
 I think. (Well, at least I had not understood this before.)  
 

I agree with this.

/Lars L







Re: OO in Haskell

1999-10-08 Thread Lars Lundgren

On Thu, 7 Oct 1999, Adrian Hey wrote:

 On Thu 07 Oct, Manuel M. T. Chakravarty wrote:
  Check out the type signatures of the `MVar'-related
  operations and you will find that they are all nicely
  encapsulated in the `IO' monad.  
 
 This is true, but I think the point of contention is does the IO monad
 itself provide referential transparency. My opinion is that even thinking
 in such terms for IO is pretty meaningless. I am aware of various
 attempts to fix up the IO semantics with world models, but none of these
 accurately model the world. How could they?
 
 So what difference does it make if you regard unpredictablity in the result
 of IO operations as caused by non-deterministic world models or Side
 Effect Goblins? Both theories seem equally valid, and both tell us
 very little about the nature or behaviour of the real world.
 

I'm really confused about all the fuzz about The IO monad not providing
referential transparency. As I understand it, this is really simple.

A value (IO a) *denotes* a program possibly interacting with the world.
*That* program is of course not referentially transparent. A haskell
program generating an (IO a) on the other hand *is* referetially
transparent. 

This is analogous to a state monad that you yourself can create using the
subset of haskell that everybody agrees on preserving referential
transparency. You can see the operations you create on your state monad 
as a language embedded in haskell. A program in *that embedded language*
need not be referentially transparant, but that does not affect the
properties of haskell, the host language. So:

Of course the state monad - a construct in haskell - 
preserves referential transparency.

Of course the IO monad - a construct in haskell -
preserves referential transparency. 


One could question whether or not haskell preserves referential
transparency. The reason for this is that when you compile and execute a
haskell program, it does not only evaluate to an IO value - *The program
that the value denotes is actually executed* sort of automagically. 


/Lars L








Re: Haskell and Parallelism (was: What is a functional language?)

1999-09-29 Thread Lars Lundgren

On Tue, 28 Sep 1999, S. Alexander Jacobson wrote:

 My simple point is that claims about the correctness or incorrectness of
 the behavior of a function are incoherent outside the function's domain;
 that, as an language implementation matter, it is handy to make _|_ a
 member of all types is irrelevant.  
 
 Haskell's () only models the logical AND when it is passed booleans.
 To say that _|_ is a Haskell Boolean, is to create another concept domain
 (Haskell Boolean), which shares many properties with Logic Boolean but is
 not identical with it.
 

Exactly! And this domain which you call (Haskell Booleans) is in haskell
called 'Bool'. By the way, when we speak about functions in this forum, I
think most people are referring to *haskell* functions as opposed to
functions in math (or logic). 

By the way, the domain of (not) and the codomain of () in haskell is:

 False True
   \/
_|_


 Understood in this manner, I don't think Parallel Haskell implementations
 need to make any promises about the sanity of the behvaior of functions
 when passed _|_.

If the semantics of the operation changes, then you'd have to give it
another name, or else it is not an implementation of haskell. As I see it
it's as simple as that. I don't really understand what you're getting at.


  As a practical matter, it would be nice if
 program success/failure was consistent accross Haskell implementations,
 but this does not require that functions behave in any particularly sane
 manner when passed _|_, only that they behave consistently.
 
 -Alex-
 
 PS Charity claims to eliminate _|_.  I don't know enough about category
 theory and programming language design to know the costs of doing so.
 

For one thing, general recursion is out. That seems to mee to be a very
big cost, but I have not tried it in practice.


 ___
 S. Alexander Jacobson   Shop.Com
 1-212-697-0184 voiceThe Easiest Way To Shop
 
 

/Lars L








RE: Haskell Wish list: library documentation

1999-09-16 Thread Lars Lundgren

On Thu, 16 Sep 1999, Lars Lundgren wrote:

[snip]
 myProg:: StateMT IO Int
 myProg = return 5
 
 main = do n - runSMT myProg
 print n
 

I forgot to show something interesting in my first example, try this
instead:

myProg:: StateMT IO Int
myProg = do lift $ putStrLn "HelloWorld!"   -- a lifted IO value
return 5-- a StateMT value



/Lars L







Re: Haskell Wish list: library documentation

1999-09-09 Thread Lars Lundgren

On Thu, 9 Sep 1999, George Russell wrote:

 Here is my revised version of the documentation.  Sorry I can't
 manage the pretty formatting:
 
 unzip :: [(a,b)] - ([a],[b])
 -
 
 Description:
unzip takes a list of pairs and returns a pair of lists.
 
 Examples:
unzip [(1,2),(3,4),(5,6)] = ([1,3,5],[2,4,6])
 
 Definition:
unzip = foldr (\(a,b) ~(as,bs) - (a:as, b:bs)) ([],[])

 See Also:
unzip3
 

I think newbies are very much helped by a translation of the type to
english, but they are even more helped if the fact that it is only a
translation (of some of the information in the type) is stressed. How
about:

unzip
=

Type:
   [(a,b)] - ([a],[b])
   unzip takes a list of pairs and returns a pair of lists.

Description:
   -

Examples:
.
.
.

/Lars L







Re: Wanted: Union-Find library for Haskell

1999-08-17 Thread Lars Lundgren

On Mon, 16 Aug 1999, Keith Wansbrough wrote:

 Hi all... has anyone implemented the Union-Find algorithm in Haskell?  
 I've looked at the various libraries listed at haskell.org and found 
 nothing, but don't want to re-invent the wheel if someone else has done 
 it already.
 

Hmm, yes I have now isolated the disjoint set ADT (not much left).
If you want the more fancy ADT with extendable sets and combined
representation of a relation and the 'antirelation' you have to ask for it
and wait. I blush when I show my uncleaned personal hacks in public ;) 

Here it is:


module DisjointSet where
import ST


type DisjSet s x   = STArray s Int (Maybe Int,x)

find :: Int - DisjSet s x - ST s Int
find n s = do (m,x) - readSTArray s n 
  maybe (return n) (`find` s) m

-- a and b must be root elements
union :: Int - Int -  DisjSet s x- ST s ()
union a b s = do (_,x) - readSTArray s a
 (writeSTArray s a (Just b,x))

isRoot :: Int - DisjSet s x- ST s Bool
isRoot n s = do (m,_) - readSTArray s n 
maybe (return True) (\_ - return False) m

mkDSet:: Int - ST s (DisjSet s Char)
mkDSet n = newSTArray (1,n) (Nothing,'A')


test :: Int - Int
test n = runST(
do s - mkDSet 10
   union 2 3 s
   union 7 1 s
   find n s
 )




/Lars L
If a trainstation is where the trains stop, what is then a workstation...







Re: Wanted: Union-Find library for Haskell

1999-08-17 Thread Lars Lundgren

On Mon, 16 Aug 1999, Keith Wansbrough wrote:

 Hi all... has anyone implemented the Union-Find algorithm in Haskell?  
 I've looked at the various libraries listed at haskell.org and found 
 nothing, but don't want to re-invent the wheel if someone else has done 
 it already.
 

Yes I have. I used the ST monad. 

Nothing fancy though, but it would be almost trivial to add
pathcompression and union by height. (I havn't done it because it was
not compatible with other operations I wanted).

A problem may be that the ADT I created contains a lot of stuff you would
not be interestd in. For example, I use a dynamic array to be able to add
elements to the disjoint set, and implement a function taking
two disjoint sets and calculating the intersection. (I used it to maintain
an equivalence relation between subformulas to
implement an extension of Stålmarck's proof procedure to many sorted first
order logic in haskell. http://www.dtek.chalmers.se/~d95lars/rapport.ps.
[Please don't look to much at the source code, it should really be
cleaned up])

If you are interested I can isolate the disjoint set things and give the
code to you.


/Lars L







Re: types

1999-06-14 Thread Lars Lundgren

On Mon, 14 Jun 1999, Jose Bernardo Barros wrote:

 
 According to the definition of the class Bounded, minBound and maxBound
 have types
 
   minBound :: Bounded a = a
   maxBound :: Bounded a = a

 Suppose I define the function 
 
 f (minBound, maxBound) = (maxBound, minBound)
 
 shouldn't its type be 
 
   f :: (Bounded a, Bounded b, Bounded c, Bounded d) = (a,b) - (c,d)  ?
 
 hugs gives me the following answer
 
 Main :t f
 f :: (a,b) - (b,a)   
 
 jbb
 

f (minBound, maxBound) = (maxBound, minBound)

is equal to 

f (a,b) = (b,a)

and its type is indeed the inferred one. The name you choose for the free
variables (minBound or a) does not matter, and has no connection at all
with the functions minBound and maxBound.

You can give the function the following type:

f :: (Bounded a, Bounded b) = (a,b) - (b,a)


/Lars L







Re: how to write a simple cat

1999-06-10 Thread Lars Lundgren

On Wed, 9 Jun 1999, Friedrich Dominicus wrote:
[snip]
 What's a HOF?
 
A Higher Order Function, the key to code reuse and abstraction.

  that first splits something up to a list using splitFn
  (or with the generalization I mentioned, to a monad), then maps a
  function over that list (namely beforeMap, because it's mapped
  *before* the filter), filters something out (using the filterPredicate),
  then again maps a function (namely afterMap, because it's mapped
  *after* the filter), then somehow joins the list (or monad), using
  unSplitFn.
 
 I would like names which tell me what is done.
 
 I read in contents of a file
 I process it, I build a string ...
 
 etc
 

Sounds fine, and the translation to haskell is very natural:

main :: IO()
main = do contents - readFile filename
  putStr (process contents)

process :: String - String
..

numberStrPair2String
  more accurately.
 
 why not build_number_string_pair?


because it does not build a number-string pair, it builds a string (from a
number-string pair).
 
 Sorry I havn't the Signature of Show in my mind bu of course it just
 builds a string.


If you know what it is doing it is easy to figure out the type.
Ok, so it takes something and converts it to a string - aha, the type must
be 

a - String
 
  I think exercise with the purely functional, non-I/O core (and perhaps
  interact like someone else suggested) teaches you the mode of
  thinking in purely functional languages. That thinking can also
  help you understand the way I/O is implemented in a referentially
  transparent way.
 
 I disagree, small scripts spend most of the time doing I/O if I don't
 understand how to do that I'm not able to even write the most simple
 things. This is eg. true for my cat ...

If you just want to read stdin and write to stdout - use interact, if you
whant to read a file - use readFile, if you want to write to a file - use
writeFile. What is the problem? I think you are making it harder than it
is.



/Lars L 









Re: Haskell 2 -- Dependent types?

1999-02-17 Thread Lars Lundgren

On 16 Feb 1999, Carl R. Witty wrote:

 Lars Lundgren [EMAIL PROTECTED] writes:
 
  We have already accepted undecidable type checking, so why not take a
  big step forward, and gain expressive power of a new magnitude, by
  extending the type system to allow dependent types.
 
 Wait a minute...who has accepted undecidable type checking?  Are you
 talking about the new type class features in GHC?  As far as I know,
 those are explicitly documented as experimental, and must be enabled
 by a command-line option.  I'm not sure that anybody has "accepted"
 undecidable type checking.
 

Yes, I was refering to various experimental features in Gofer, and
recently also in GHC. I should not have used the word 'accepted' as I did.
What I meant was that since those features are candidates for haskell 2,
(At least, it is my impression that they are)
we can also consider other extensions wich leads to undecidable type
checking.

Of course, this is not a desirable property, but it may not be that bad in
practice. The type checker can use some default which works in in 95% of
the programs, and the really complex programs can be checked by using a
compiler flag which increases some limit. 

Decidability should not be given up to easily, but i think that dependent
types has a very good price/performance ratio.

/Lars L








Haskell 2 -- Dependent types?

1999-02-16 Thread Lars Lundgren

The trend seems to be define wishes for haskell 2, so here are mine:

We have already accepted undecidable type checking, so why not take a
big step forward, and gain expressive power of a new magnitude, by
extending the type system to allow dependent types.

Cayenne, http://www.cs.chalmers.se/~augustss/cayenne, already has
dependent types, but seems to be an experimental language only with a very
small user base. 

Just a thought.
/Lars L






Re: f :: IO x - x

1999-01-03 Thread Lars Lundgren

On Fri, 5 Nov 1999, Dusan Kolar wrote:

 Hi,
 
   Could anyone, please, give me reference to or a sophisticated
 answer?
 
   I am going to have maybe a very stupid question.

Well. it's definitely a FAQ.

 Playing with IO monads I made to me a task. Create
 a function which counts lines of a file and returns the
 number of lines as a result:
 
   lineNo :: String - Int
 fname number of lines
 

The thing is, this *is not* a function. A function is something that
uniquely maps its arguments to values. The program you want is dependent,
not only on the fname argument, but also on the contents of a file. To
mark that the result has this dependency, you write IO in front of it. So
the function you want to write *has* the type:

lineNo :: String - IO Int 

 To access files I'm using standard file access and operators
 defined in class Monad. The problem is, that when I count
 the lines, I'm imprisoned by IO monad. Is there any way
 (e.g. by converting value to String and obtaining this string as a result)
 how to break IO monads? How to make a function of type
 
   f :: IO [Char] - [Char]
 

Is there any way to make the function you want to write not dependent of
the contents of a file? This question and yours have the same answer.

 I know, that usually it is possible to write whole the
 program such a way so that it would not matter to have
 a IO monad as a top type, but having a function which counts number
 of lines in the file (or watever else) is also not too bad,
 at least as fas as I think, but maybe I'm completely wrong.
 

Well, you could define a function

   lineNo' :: String -  Int
  fcontents  number of lines


and then write

lineNo :: String - IO Int
lineNo fname = do fcontents - readFile fname
  return (lineNo' fcontents)


In this way you have separated the computation and the IO stuff. That is
usually a good idea.

/Lars L