[Haskell-cafe] The simple programming language,

2006-03-26 Thread pinturicchio

if somebody can help me wid this i wil b realy thankful

Finally, we come to the program. The simple programming language,
which includes loops, conditionals and sequential composition will be
expressed
a datatype in Haskell.
data Prg =
Skip
| PrintS String
| PrintE Exp
| Declare Variable
| Variable := Exp
| Prg : Prg
| IfThenElse Exp (Prg, Prg)
| While Exp Prg

The program Skip does nothing. The program PrintS ‘prints’ the given
string to the output. Similarly, PrintE prints the value of the given
expression
to the output.
Declare declares a variable, assigning its initial value to 0. The :=
operator
performs variable assignment.
The : operator is sequential composition, while IfThenElse and While
are conditional and while-loops respectively. The condition in both cases
resolves to true if the value of the expression is not zero.
A factorial program can be programmed in this small language as follows:
counter = counter
result = result
factorial n =
Declare counter
: Declare result
: result := Val 1
: counter := Val n
: While (Var counter)
( result := (Var result) :*: (Var counter)
: counter := (Var counter) :-: (Val 1)
)
: PrintS (Factorial of ++show n++ is:)
: PrintE (Var result)
(a) The simulate function takes a Store and a program, and returns an
updated store and list of output strings:
simulate :: Store - Prg - (Store, [String])
Using pattern matching, define simulate for Skip, PrintS and variable
declarations.
(b) Add the cases for PrintE and variable assignment.
(c) Sequential composition can be defined by simulating the two instructions
in sequence. Complete the following case:
simulate store (p1 : p2) =
let (store’, outs1) = simulate store p1
(store’’,outs2) = simulate store’ p2
in ...
(d) Define simulate for conditionals.
(e) We can ‘cheat’ by simulating while-loops in terms of conditionals and
sequential compositi
simulate store (While condition program) =
simulate store
(IfThenElse condition
( program : While condition program
, Skip
)
)
Using this definition, test your program with the factorial program.

--
View this message in context: 
http://www.nabble.com/The-simple-programming-language%2C-t1344638.html#a3596582
Sent from the Haskell - Haskell-Cafe forum at Nabble.com.

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


[Haskell-cafe] pls fast help me with this simple question

2006-03-26 Thread iliali16

type Data = Integer
type Variable = String
type Store = [(Variable, Data)]

Store = [(,0)]
emptystore :: Store 
emptystore = [(,0)]

getVal :: Store - Variable - Data

thats my code i just have to continue and get the value of a variable from
the store but i don't know how the task is :

Define a function getVal, which takes a Store and a variable name,
and returns its value. You may assume that the variable is defined
in the Store.
getVal :: Store - Variable - Data

(c) Define a function setVar, which takes a Store, a variable name and a
value, and returns an updated Store with the variable value updated.
setVal :: Store - (Variable, Data) - Store 

--
View this message in context: 
http://www.nabble.com/pls-fast-help-me-with-this-simple-question-t1345292.html#a3598605
Sent from the Haskell - Haskell-Cafe forum at Nabble.com.

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


[Haskell-cafe] Equirecursive types?

2006-03-26 Thread Jim Apple
According to Pierce's book, O'Caml has an optional equirecursive type
extension that turns off the occurs check. Is there any particular
reason Haskell doesn't have that available?

Here's what got me thinking about it:
http://lambda-the-ultimate.org/node/1360#comment-15656

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


Re: [Haskell-cafe] pls fast help me with this simple question

2006-03-26 Thread Sebastian Sylvan
On 3/26/06, iliali16 [EMAIL PROTECTED] wrote:

 type Data = Integer
 type Variable = String
 type Store = [(Variable, Data)]

 Store = [(,0)]
 emptystore :: Store
 emptystore = [(,0)]

 getVal :: Store - Variable - Data

 thats my code i just have to continue and get the value of a variable from
 the store but i don't know how the task is :

 Define a function getVal, which takes a Store and a variable name,
 and returns its value. You may assume that the variable is defined
 in the Store.
 getVal :: Store - Variable - Data

 (c) Define a function setVar, which takes a Store, a variable name and a
 value, and returns an updated Store with the variable value updated.
 setVal :: Store - (Variable, Data) - Store


So you have a store which is a list of (name,value) pairs. You want to
write a function which takes a name, a store, and returns the value.
So:
 getVal [(x,4),(y,5)] x
should return 4

You could either use functions in the standard library (check out
lookup in Data.List and fromJust in Data.Maybe), or just do the
recursion yourself:
getVal [] _ = error Variable wasn't in the Store, not supposed to happen!
getVal ((id,val):xs) var = ...

Replace the ... with your own logic. If var is equal to id then you
should return val, else you need to check the rest of the store (xs).


/S

--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] A Haskell Tutorial

2006-03-26 Thread Jonathan Tang
Hi all,

I've written a Haskell tutorial that walks the reader through the implementation of a Scheme interpreter:

http://halogen.note.amherst.edu/~jdtang/scheme_in_48/tutorial/overview.html

Instead of focusing on small examples at the Haskell REPL, it tries to
show the reader how to construct a real program with Haskell. It
introduces monads and IO early, and also includes error checking,
state, file I/O, and all the other hard stuff that's frequently
omitted from beginner tutorials.

Am looking for feedback on two main axes:

1.) Experienced Haskell users: is this more-or-less idiomatic
Haskell? Did I miss any library or language features that could
make the programmer's job easier? It was my first substantial
Haskell program, so I worry that I may have missed some common ways of
doing things.

2.) Novices: is it clear and easy to understand? I had a tough
time figuring out how to do anything practical in Haskell, because most
tutorials omit IO, gloss over monads, and don't pay any attention to
state or error-checking. Does this rectify those shortcomings?

Comments/critiques are appreciated.

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


Re: [Haskell-cafe] The simple programming language,

2006-03-26 Thread Cale Gibbard
Well, I'm sure that lots of people here would be willing to help if
you'd be more specific about what you're having trouble with. Just
posting an assignment problem tends not to generate much discussion
until after the assignment deadline. What have you already tried? What
part are you stuck on?

Also, if you're interested in more immediate responses, try #haskell
on irc.freenode.net.

For help with asking good questions, check out
http://www.haskell.org/hawiki/HomeworkHelp

 - Cale

P.S.:
(Just to let everyone know, this is an assignment problem from the CSA
1080 Declarative Programming course at the University of Malta. The
relevant URL is here: http://www.cs.um.edu.mt/~gpac1/ I've gotten a
few questions about it lately, and there was a post to the libraries
list about another problem on it.)


On 26/03/06, pinturicchio [EMAIL PROTECTED] wrote:

 if somebody can help me wid this i wil b realy thankful

 Finally, we come to the program. The simple programming language,
 which includes loops, conditionals and sequential composition will be
 expressed
 a datatype in Haskell.
 data Prg =
 Skip
 | PrintS String
 | PrintE Exp
 | Declare Variable
 | Variable := Exp
 | Prg : Prg
 | IfThenElse Exp (Prg, Prg)
 | While Exp Prg

 The program Skip does nothing. The program PrintS 'prints' the given
 string to the output. Similarly, PrintE prints the value of the given
 expression
 to the output.
 Declare declares a variable, assigning its initial value to 0. The :=
 operator
 performs variable assignment.
 The : operator is sequential composition, while IfThenElse and While
 are conditional and while-loops respectively. The condition in both cases
 resolves to true if the value of the expression is not zero.
 A factorial program can be programmed in this small language as follows:
 counter = counter
 result = result
 factorial n =
 Declare counter
 : Declare result
 : result := Val 1
 : counter := Val n
 : While (Var counter)
 ( result := (Var result) :*: (Var counter)
 : counter := (Var counter) :-: (Val 1)
 )
 : PrintS (Factorial of ++show n++ is:)
 : PrintE (Var result)
 (a) The simulate function takes a Store and a program, and returns an
 updated store and list of output strings:
 simulate :: Store - Prg - (Store, [String])
 Using pattern matching, define simulate for Skip, PrintS and variable
 declarations.
 (b) Add the cases for PrintE and variable assignment.
 (c) Sequential composition can be defined by simulating the two instructions
 in sequence. Complete the following case:
 simulate store (p1 : p2) =
 let (store', outs1) = simulate store p1
 (store'',outs2) = simulate store' p2
 in ...
 (d) Define simulate for conditionals.
 (e) We can 'cheat' by simulating while-loops in terms of conditionals and
 sequential compositi
 simulate store (While condition program) =
 simulate store
 (IfThenElse condition
 ( program : While condition program
 , Skip
 )
 )
 Using this definition, test your program with the factorial program.

 --
 View this message in context: 
 http://www.nabble.com/The-simple-programming-language%2C-t1344638.html#a3596582
 Sent from the Haskell - Haskell-Cafe forum at Nabble.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] Re: Positive integers

2006-03-26 Thread Daniel McAllansmith
On Friday 24 March 2006 14:37, you wrote:
  An additive torsor is?

 Surprisingly, there is a page on MathWorld about Torsors but it is
 empty. Google turned up the following page with a good explanation.

 http://math.ucr.edu/home/baez/torsors.html

Nice clear explanation that.  Thanks for the link Jared.


  I'd maintain that the difference between two lengths is an entirely
  different quantity from the length of a list.

 (Maybe this is a good example of what the term torsor captures.)

 Thanks to Aaron for expanding our vocabulary.

Yep.  Now I agree with Aaron.
And now I won't get offended if someone calls me a closet torsor user! :)


   Jared.
 --
 http://www.updike.org/~jared/
 reverse )-:
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Positive integers

2006-03-26 Thread Daniel McAllansmith
On Friday 24 March 2006 16:16, Ben Rudiak-Gould wrote:
 Daniel McAllansmith wrote:
  I can see the domain bounds check would be a problem in theory, but in
  practice doesn't the type enforce that?  Keeping Word positive costs
  nothing because it just overflows.  Wouldn't it be much the same?

 If you're planning wraparound semantics then you're better off with Int
 indexes. 

I wasn't really _planning_ wrap around semantics.
More just making the point that saying the cost of using Word is greater than 
the cost of using Int, due to the bounds checking required, seems unfair.
I see your point about quick detection of bad indices, though I'd still rather 
have the type document the fact that only positive integers are expected.

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


Re: [Haskell-cafe] Positive integers

2006-03-26 Thread Daniel McAllansmith
On Friday 24 March 2006 23:29, Malcolm Wallace wrote:
 Daniel McAllansmith [EMAIL PROTECTED] wrote:
  Unless I've missed it, there is no typeclass for positive integers in
  GHC. Is there any particular reason it doesn't exist?
 
  Also, it seems Word would be a far better type in the likes of (!!),
  length,  etc.  Is it just tradition that resulted in the use of Int?

 There is a good argument that what you really want here is the lazy
 infinite natural numbers.  Think Peano:

 data Natural = Zero | Succ Natural

 The clear correspondance between lazy lists and the size/index of a lazy
 list makes this type fairly compelling.  It can be jolly useful,
 particularly with infinite streams, to be able to compute
 (length xs  n)
 without forcing the evaluation of the list to more than n places.

 Of course, if the naturals were actually represented in Peano (unary)
 form, that would be somewhat inefficient, but there are various
 strategies that can be used to make the representation smaller whilst
 retaining their nice properties.

I was thinking about several things in this thread, torsors, overflow 
semantics, bounds checking...
I wonder if there would be any merit in being able to define constrained 
subsets of integers and the semantics when/if they overflow.

For example, take UNIX nice levels -20 to 19.  You could have
  data ConstrainedInteger = CI {distToMax :: Natural, distToMin :: Natural}
this would ensure only the 40 integers can be represented.
Then you could have _something_ that defined what happened on overflow, 
whether it wraps, reflects, errors, truncates or whatever.

When it comes to compile, or source preprocessing, time these numbers could be 
realigned to a primitive Int or Word representation of suitable size and have 
the appropriate overflow code written in.  And, in the case of error or wrap 
overflow semantics on a set of the right size, the overflow checks could be 
disabled, like other assertions, at runtime.

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


Re: [Haskell-cafe] Re: Positive integers

2006-03-26 Thread ajb
G'day all.

Quoting Jared Updike [EMAIL PROTECTED]:

 Surprisingly, there is a page on MathWorld about Torsors but it is
 empty. Google turned up the following page with a good explanation.

 http://math.ucr.edu/home/baez/torsors.html

Ah, right.  So torsor is just a short name for regular group
action,which in turn is a short name for free transitive group
action.

We discussed these previously in the context of other kinds of
difference types:

http://www.haskell.org/pipermail/libraries/2005-May/003899.html

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


Re: [Haskell-cafe] Building Monads from Monads

2006-03-26 Thread Daniel McAllansmith
On Friday 24 March 2006 16:42, Cale Gibbard wrote:

Excellent help thanks, Cale.

A lot of my misunderstandings stemmed from not finding any 'instance 
MonadState ReaderT' when reading the code in Reader.hs, not realising that 
there was an instance defined in State.hs, and yet being able to use get on 
what I thought would just be a Reader.
I think I will now have an enduring friendship with GHCi's ':info'.



  Q. 4) Is it possible to give a type to the lifted function so that the
  monad of the correct class _and_ type is used?  E.g. dig into a String
  Reader rather than an Int Reader.

 I'm not completely sure what you're after here -- basically, you just
 lift things into whichever monad you're using. If you want to be
 polymorphic, but insist on a particular instance of MonadReader,
 that's easy enough, just put a constraint like (MonadReader String m)
 or something similar on your type.

Not really a valid question now that I've cleared up my misconceptions of 
lifting, but...
I meant something along the lines of
  i - ((lift get) :: Int) --dig into the nearest Int state
  s - ((lift get) :: String) --dig into the nearest String state

  Q. 6) Is it safe to always use liftIO, even in plain IO monad?

 It's safe, sure, though a little awkward. It's easy enough to lift
 whole IO computations later on anyway. The only benefit would be if
 you wanted to later intersperse actions into the code which came from
 a transformed version.

Yeah, I meant being able to go back and intersperse the use of state, or 
whatever, in a monadic expression that had, until then, only done IO.


[snip various good suggestions and improved code]

Good advice, thanks.


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


Re: [Haskell-cafe] Equirecursive types?

2006-03-26 Thread Ross Paterson
On Sun, Mar 26, 2006 at 01:22:17PM -0500, Jim Apple wrote:
 According to Pierce's book, O'Caml has an optional equirecursive type
 extension that turns off the occurs check. Is there any particular
 reason Haskell doesn't have that available?

It's certainly feasible, though turning off the occurs check doesn't
do it: you need to use the unification algorithm for regular trees (see
e.g. section 6.7 of the revised Dragon Book).  You also need to decide
what to do about

type T = T

I think the arguments against it are:
 * many common errors will lead to perfectly legal types.
 * it adds convenience, but not expressive power.
 * it only works for regular types, where the arguments of uses of the
   type constructor on the rhs are the same as (or a permutation of)
   the arguments on the lhs, so this is out:

type T a = Either a (T (a,a))

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


[Haskell-cafe] error vs. MonadError vs. fail

2006-03-26 Thread Daniel McAllansmith
Is there a consensus on how anticipatable failure situations should be 
handled?


There was a thread, haskell programming guidelines, from 2006-02-25 where 
John Meacham and Cale Gibbard had a bit of back-and-forth about using 
Monad.fail or a purpose specific MonadFail class.

I believe a consensus was reached that using error should only be for 
'impossible' situations and signaling buggy code.
[Apologies if I've put words in anyones mouth.]


Using fail certainly seems quick and easy, but I find it a bit distasteful for 
a few different reasons:
 users of a library can't discriminate between a failure indicating they've 
supplied bad input and a failure indicating that teh library writer has got a 
bad pattern match somewhere,
 it doesn't seem to force the potential for failure to be explicit in the api 
of a library,
 it doesn't seem to allow distinct, and rich, failure constructs at system 
layer boundaries, or the containment of them.

Maybe the last two aren't a problem when programming in Haskell, but by itself 
the first seems pretty nasty.


Apparently the advantage of fail is that user of the library can choose to 
receive failures as eg Maybes, Eithers, [], or whatever they like.

But surely a MonadFail could allow the best of both worlds, letting the 
library throw as detailed an error construct as it can, and letting the 
library user choose MonadFail instance such that error constructs are turned 
into Maybes, Eithers, a new construct appropriate for a higher system layer, 
etc?


MonadError is not up to this task as far as I can tell.  Has anybody whipped 
up an alternative, or can explain why it can't be done?


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


[Haskell-cafe] Re: Positive integers

2006-03-26 Thread Aaron Denney
On 2006-03-26, Daniel McAllansmith [EMAIL PROTECTED] wrote:
 I was thinking about several things in this thread, torsors, overflow 
 semantics, bounds checking...
 I wonder if there would be any merit in being able to define constrained 
 subsets of integers and the semantics when/if they overflow.

 For example, take UNIX nice levels -20 to 19.  You could have
   data ConstrainedInteger = CI {distToMax :: Natural, distToMin :: Natural}
 this would ensure only the 40 integers can be represented.
 Then you could have _something_ that defined what happened on overflow, 
 whether it wraps, reflects, errors, truncates or whatever.

 When it comes to compile, or source preprocessing, time these numbers
 could be realigned to a primitive Int or Word representation of
 suitable size and have the appropriate overflow code written in.  And,
 in the case of error or wrap overflow semantics on a set of the right
 size, the overflow checks could be disabled, like other assertions, at
 runtime.

Now that is an interesting idea.

-- 
Aaron Denney
--

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


[Haskell-cafe] Re: (!!) operator usage

2006-03-26 Thread Tom Davies
Tom Davies tomdavies at exemail.com.au writes:

[snip]

Apologies for the complete misinformation! I don't know what I was thinking!


Tom


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