Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Using monad with arrow (Jarno Antikainen)
2. Re: Using monad with arrow (Stephen Tetley)
3. substitution (John Moore)
4. Re: substitution (Stephen Tetley)
5. Re: substitution (Yitzchak Gale)
6. Re: Maximum length of a GHCi command? (Kwanghoon Choi)
7. Re: Using monad with arrow (Jarno Antikainen)
8. Re: substitution (Stephen Tetley)
----------------------------------------------------------------------
Message: 1
Date: Mon, 1 Mar 2010 13:31:33 +0200
From: Jarno Antikainen <[email protected]>
Subject: [Haskell-beginners] Using monad with arrow
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Hello,
I have a function that returns its value in a monad, and I'd like to use that
value in a function that returns an arrow, but I cannot figure out how to do
that. The monad in this case is CouchMonad and the arrow is ArrowXml, but I
guess that doesn't really matter.
For example, the function that returns its value in a monad:
toXNode :: String -> CouchMonad XNode
and a function that returns an arrow:
processReplacement :: (ArrowXml a) => String -> String -> a XmlTree XmlTree
In processReplacement I'd like to get a value from toXNode function, take it
out from the monad and use the plain XNode value. Were it all simply
CouchMonad, it would be something like this:
processReplacement elementId docId =
do
node <- toXNode docId
setNode node `when` idMatches elementId
But this is an arrow, so I need something different. I've read quite a few
arrow articles and tutorials, but couldn't yet grasp how to do this and what is
involved. Any ideas or pointers to examples?
Thanks!
BR,
Jarno
------------------------------
Message: 2
Date: Mon, 1 Mar 2010 13:25:20 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Using monad with arrow
To: Jarno Antikainen <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Jarno
A cursory look at the CouchMonad says it is build upon the IO monad so
you won't be able to safely extract values out of it. This suggests
that you should "run" HXT functions within the CouchMonad monad.
The HXT library has various run_ functions - runIOLA, runSt, ... - to
run a computation and produce a value. I don't know the HXT library,
so don't know which particular run function to use, but a viable
construction might look like one of these two:
processReplacement elementId docId state0 =
do
node <- toXNode docId
let ans = runSt (setNode node `when` idMatches elementId) state0
return ans
Or maybe runIOLA:
processReplacement elementId docId =
do
node <- toXNode docId
ans <- runIOLA (setNode node `when` idMatches elementId)
return ans
Best wishes
Stephen
------------------------------
Message: 3
Date: Mon, 1 Mar 2010 22:17:04 +0000
From: John Moore <[email protected]>
Subject: [Haskell-beginners] substitution
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Skipped content of type multipart/alternative-------------- next part
--------------
A non-text attachment was scrubbed...
Name: test5.hs
Type: application/octet-stream
Size: 3536 bytes
Desc: not available
Url :
http://www.haskell.org/pipermail/beginners/attachments/20100301/7c44c30c/test5-0001.obj
------------------------------
Message: 4
Date: Mon, 1 Mar 2010 22:58:43 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] substitution
To: John Moore <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi John
Your evalStep case for binary operations (Add Mult, etc) are only
going down the 'left-spine' when they don't match a Val
eg:
evalStep d(Add x y)
= case x of
(Val a) -> case y of
(Val b) -> Val (a+b)
left -> Add x (evalStep d y)
right -> Add (evalStep d x)y
The code wants to be something like this...
evalStep d(Add x y)
= case x of
(Val a) -> case y of
(Val b) -> Val (a+b)
left -> Add x (evalStep d y)
right -> Add (evalStep d x) (evalStep d y)
^^^^^^^^^
Best wishes
Stephen
On 1 March 2010 22:17, John Moore <[email protected]> wrote:
> Hi,
> Can anyone explain what I'm doing wrong in the subtitution of the Let
> statement. If you look at test three. It wont substitute the second var "x".
> I enclose the file because this program is getting too big to copy and
> paste.
>
> John
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
------------------------------
Message: 5
Date: Tue, 2 Mar 2010 00:59:29 +0200
From: Yitzchak Gale <[email protected]>
Subject: Re: [Haskell-beginners] substitution
To: John Moore <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
John Moore wrote:
> Can anyone explain what I'm doing wrong in the subtitution of the Let
> statement.
Look more carefully at the "right" case for "evalStep..Let".
The result should still have a Let in it - since e1 is not
a value yet, you're not ready yet to do the substitution.
Hope this helps,
Yitz
------------------------------
Message: 6
Date: Tue, 2 Mar 2010 01:24:36 +0900
From: Kwanghoon Choi <[email protected]>
Subject: [Haskell-beginners] Re: Maximum length of a GHCi command?
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Thank you all for your replies.
I am using Ubuntu and Emacs GTK+ Version 2.12.9. Nothing else is used by me.
My tool is just a Haskell program, and I load it using GHCi to run.
Is the emacs that limits the length of the command then?
Best regards,
Kwanghoon
On Sun, Feb 28, 2010 at 4:54 PM, Kwanghoon Choi <[email protected]> wrote:
>
> Dear Haskeller,
>
> I have a little issue in using GHCi as follows:
>
> ===
> Prelude> length
> "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
> <interactive>:1:254:
> lexical error in string/character literal at character '\EOT'
> ===
>
> It seems to me that GHCi doesn't allow me to type more than some specified
> length of a command.
> Is this interpretation correct? How can I make it longer?
>
> This issue is quite important at least to me. My tool is just a simple
> Haskell program. I don't like to add any extra interactive environment, but
> I like to just use GHCi as an interactive environment for my tool. I
> strongly believe that this ides is pervasive among many haskellers.
>
> Thanks in advance for your help/feedback in advance.
>
> Kwanghoon
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100301/7094df88/attachment-0001.html
------------------------------
Message: 7
Date: Tue, 2 Mar 2010 12:57:48 +0200
From: Jarno Antikainen <[email protected]>
Subject: Re: [Haskell-beginners] Using monad with arrow
To: Stephen Tetley <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Hi Stephen,
> A cursory look at the CouchMonad says it is build upon the IO monad so
> you won't be able to safely extract values out of it. This suggests
> that you should "run" HXT functions within the CouchMonad monad.
Right, that's pretty much what I was already doing, but I was thinking about a
too complex solution. I was thinking more along the lines of using CouchMonad
functions inside HXT arrow functions and getting a CouchMonad value out of my
HXT arrow composition. That lead me thinking about transforming CouchMonad to a
(Kleisli) arrow, but there the water is getting too deep for me and I'm not
even sure if that's the right (or possible) direction. But your reply helped me
to see an easier way.
> The HXT library has various run_ functions - runIOLA, runSt, ... - to
> run a computation and produce a value. I don't know the HXT library,
> so don't know which particular run function to use, but a viable
> construction might look like one of these two:
Currently I'm using runX from Text.XML.HXT.Arrow.XmlIOStateArrow. The
processReplacement is actually a part of a more complex arrow composition, and
I was hoping I could somehow use CouchMonad functions deep inside the arrow
composition and get the arrow machinery to propagate the CouchMonad value "up"
and out from a run_ function.
> processReplacement elementId docId =
> do
> node <- toXNode docId
> ans <- runIOLA (setNode node `when` idMatches elementId)
> return ans
This worked out nicely. Somehow I had missed that (in this case) I can just as
well do the CouchMonad stuff outside the arrow composition, extract the value
out of the monad and pass it to the arrow function, so that I don't need to
have any monad stuff inside the arrow functions.
I'm still left wondering what if the input to a monad function came from the
output of an arrow so that I couldn't do the monad operations outside the arrow
composition, but fortunately I don't have such a case yet :-)
Thanks!
Regards,
Jarno
------------------------------
Message: 8
Date: Tue, 2 Mar 2010 23:08:24 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] substitution
To: John Moore <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi John
I was wrong yesterday with my advice - the arithmetic ops are going
down the right spine but only after they have gone down the left spine
in one step. As I changed the code to go down both spines during one
step, the evaluator got an answer that looked right by cheating (it
was doing too much work at one step). For different input I don't
think it would have worked.
To maintain stepping, one option is for evalStep to return both the
one-step-transformed expression and a dictionary that represents the
values at current step.
With a non-stepping evaluator the dictionary can simply be passed down
through the tree, but that doesn't work for a stepping evaluator,
consider this example:
let x = 5 in (x + (x * 2)) : dict {}
evalStep is two operations:
first add the binding ==> x + (x * 5) : dict { x := 5 }
second perform one (recursive) evalStep ==> 5 + (x * 5) : dict { x := 5 }
return the expression ==> 5 + (x * 5)
Unfortunately because of the stepping you are throwing away the
dictionary with the binding { x := 5 } - the stepping function only
returns the partially evaluated expression, not the expression plus
modified dictionary.
So the next evaluation attempt has the input:
5 + (x * 5) : dict { }
... as there is nothing in the dictionary the lookup will fail.
Here's a version that changes the type of evalStep so that it returns
the modified dictionary - I took the liberty of changing the case
expressions to nested pattern matching within evalStep so that I could
copy/paste/modify the code more easily:
data Expression = Val Double
| Add Expression Expression
| Subtract Expression Expression
| Multiply Expression Expression
| Divide Expression Expression
| Var String
| Let String Expression Expression
deriving Show
demo1 = (Add(Multiply(Divide(Subtract(Val 25)(Val 5))(Val 10))(Val 7))(Val 30))
--let x = 1+1 in 3 - x
test1 = Let "x" (Add (Val 1) (Val 1)) (Subtract (Val 3) (Var "x"))
test6 = Let "y" (Add (Val 7)(Val 6)) (Subtract (Val 6)(Var "y"))
-- 4 * (let x = 1+1 in 3 + x)
test2 = Multiply (Val 4) test1
-- x * (let x = 1+1 in 3 + x)
test3 = Let "y" (Add (Val 4)(Val 7)) (Multiply test1 (Var "y"))
test5 = Add (test1) test6
type Dict =[(String,Expression)]
emptyDict :: Dict
emptyDict = []
addEntry :: String->Expression ->Dict -> Dict
addEntry n e d = (n,e): d
lookupEntry :: String -> Dict -> Maybe Expression
lookupEntry n [] = Nothing
lookupEntry n (x:xs) = if (n == k)
then (Just v)
else lookupEntry n xs
where (k,v) = x
evalStep :: Dict -> Expression -> (Dict,Expression)
evalStep d (Val x) = (d, Val x)
evalStep d (Add (Val a) (Val b)) = (d, Val (a+b))
evalStep d (Add (Val a) y ) = let (_,r) = evalStep d y in (d, Add
(Val a) r)
evalStep d (Add x y ) = let (_,l) = evalStep d x in (d, Add l y)
evalStep d (Subtract (Val a) (Val b)) = (d, Val (a-b))
evalStep d (Subtract (Val a) y ) = let (_,r) = evalStep d y in
(d, Subtract (Val a) r)
evalStep d (Subtract x y ) = let (_,l) = evalStep d x in
(d, Subtract l y)
evalStep d (Multiply (Val a) (Val b)) = (d,Val (a*b))
evalStep d (Multiply (Val a) y ) = let (_,r) = evalStep d y in
(d, Multiply (Val a) r)
evalStep d (Multiply x y ) = let (_,l) = evalStep d x in
(d, Multiply l y)
evalStep d (Divide (Val a) (Val b)) = (d, Val (a/b))
evalStep d (Divide (Val a) y ) = let (_,r) = evalStep d y in (d,
Divide (Val a) r)
evalStep d (Divide x y ) = let (_,l) = evalStep d x in (d,
Divide l y)
evalStep d (Let n (Val _) (Val b)) = (d, Val b) -- redundant let
evalStep d (Let n e1 e2 ) = evalStep (addEntry n e1 d) e2
evalStep d (Var x)
= case lookup x d of
Just e -> (d,e)
Nothing -> error "Error in expression -- no definition for variable!"
evaluate :: Dict-> [Expression] -> Expression -> IO()
evaluate d (x:xs) e = do
putStrLn (show e)
putStrLn "Do another step (y/n) or rollback (r)? :"
c <- getLine
case c of
"y" -> let (d',e') = (evalStep d e) in evaluate d' (e:x:xs) e'
-- build up history
"r" -> case (x:xs) of
(x:xs)-> evaluate d xs x
[]-> do { putStrLn "Empty"
;evaluate d(x:xs) e
}
"n" -> putStrLn $ "Ok you said no :" ++ c
runExpr expr i = step i expr emptyDict
where
step n e _ | n < 1 = e
step n e d = let (d',e') = evalStep d e in step (n-1) e' d'
isFree :: String -> Expression -> Bool
isFree n1 (Let n2 e1 e2)
|n1 == n2 = False
|otherwise = True
subst :: String -> Expression -> Expression->Expression
subst n e1 e2
|isFree n e2 = subst' n e1 e2
|otherwise = e2
subst' :: String -> Expression -> Expression->Expression
subst' n0 e1 (Var n1)
|n0 == n1 = e1
|otherwise = Var n1
subst' s v (Add e1 e2) = Add(subst' s v e1)(subst' s v e2)
subst' s v (Multiply e1 e2) = Multiply(subst' s v e1)(subst' s v e2)
subst' s v (Divide e1 e2) = Divide(subst' s v e1)(subst' s v e2)
subst' s v (Subtract e1 e2) = Subtract(subst' s v e1)(subst' s v e2)
subst' s v (Let n e1 e2)=
Let n (subst' s v e1)(subst' s v e2)
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 21, Issue 1
****************************************