
{- From Learn Haskell Fast and Hard : 4.3.1. Maybe is a monad http://yannesposito.com/Scratch/en/blog/Haskell-the-Hard-Way/#maybe-monad Concerning the code below I have the following questions: 1) Are eligibleLet and eligibleEquational operationally equivalent (i.e. perform the same operations) and/or semantically equivalent(i.e. Church-Rosser)? 2) Apart from the return type of Maybe instead of Bool how does eligibleMonad differ from eligibleLet? Regards, Pat -} deposit value account = account + value withdraw value account = account - value -- You are eligible for a bonus only if your sequence of transactions stays out of the red. eligibleLet :: (Num a,Ord a) => a -> Bool eligibleLet account = let account1 = deposit 100 account in if (account1 < 0) then False else let account2 = withdraw 200 account1 in if (account2 < 0) then False else let account3 = deposit 100 account2 in if (account3 < 0) then False else let account4 = withdraw 300 account3 in if (account4 < 0) then False else let account5 = deposit 1000 account4 in if (account5 < 0) then False else True -- No let expressions, hence intermediate calculations are performed multiple times. -- Should terminates on first point of failure eligibleEquational :: (Num a,Ord a) => a -> Bool eligibleEquational account = if (deposit 100 account) < 0 then False else if (withdraw 200 (deposit 100 account)) < 0 then False else if (deposit 100 (withdraw 200 (deposit 100 account))) < 0 then False else if (withdraw 300 (deposit 100 (withdraw 200 (deposit 100 account)))) < 0 then False else if (deposit 1000 (withdraw 300 (deposit 100 (withdraw 200 (deposit 100 account))))) < 0 then False else True -- Monadic version depositM :: (Num a) => a -> a -> Maybe a depositM value account = Just (account + value) withdrawM :: (Num a,Ord a) => a -> a -> Maybe a withdrawM value account = if (account < value) then Nothing else Just (account - value) eligibleMonad :: (Num a, Ord a) => a -> Maybe Bool eligibleMonad account = do account1 <- depositM 100 account account2 <- withdrawM 200 account1 account3 <- depositM 100 account2 account4 <- withdrawM 300 account3 account5 <- depositM 1000 account4 Just True main = do print $ eligibleLet 300 -- True print $ eligibleLet 299 -- Falsen print $ eligibleEquational 300 -- True print $ eligibleEquational 299 -- False print $ eligibleMonad 300 -- Just True print $ eligibleMonad 299 -- Nothing -- This email originated from DIT. If you received this email in error, please delete it from your system. Please note that if you are not the named addressee, disclosing, copying, distributing or taking any action based on the contents of this email or attachments is prohibited. www.dit.ie Is ó ITBÁC a tháinig an ríomhphost seo. Má fuair tú an ríomhphost seo trí earráid, scrios de do chóras é le do thoil. Tabhair ar aird, mura tú an seolaí ainmnithe, go bhfuil dianchosc ar aon nochtadh, aon chóipeáil, aon dáileadh nó ar aon ghníomh a dhéanfar bunaithe ar an ábhar atá sa ríomhphost nó sna hiatáin seo. www.dit.ie Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to Grangegorman http://www.dit.ie/grangegorman

Hello,
Le mer. 28 oct. 2015 à 15:59, PATRICK BROWNE
{- From Learn Haskell Fast and Hard : 4.3.1. Maybe is a monad http://yannesposito.com/Scratch/en/blog/Haskell-the-Hard-Way/#maybe-monad
Concerning the code below I have the following questions: 1) Are eligibleLet and eligibleEquational operationally equivalent (i.e. perform the same operations) and/or semantically equivalent(i.e. Church-Rosser)?
A priori, they're only semantically equivalent since in eligibleEquational intermediary computation are repeated whereas in eligibleLet they're only effectued once.
2) Apart from the return type of Maybe instead of Bool how does eligibleMonad differ from eligibleLet?
eligibleMonad is nice and readable while eligibleLet is a tiresome mess to write and maintain... Basically in eligibleMonad the logic that checks whether an operation is possible is rolled into the monadic operations themselves whereas in eligibleLet you have to check every operation validity yourself (so you can forget one or do it incorrectly). Note that with more familiarity with monads, you would probably rewrite eligibleMonad to avoid naming the intermediary account (and thus avoid any confusion between versions) : eligibleMonad account = depositM 100 account
= withdrawM 200 = depositM 100 = withdrawM 300 = depositM 1000 Just True
This would make it very easy to add or remove operations. -- Jedaï

Chaddaï,
Thank you very much for your response to my question. It confirms my
intuition, but I was not sure.
Is it true in general that let expressions (e.g. eligibleLet) are always
semantically equivalent to their equational counterparts (e.g.
eligibleEquational)? Would it be fair to say that "let" is syntactic sugar
for an equational equivalent? Or is there more to it?
With respect to the ordering of the operations is generally true that a
monadic version is semantically equivalent to a set of "let expressions"
in a nested if-then-else?
Thanks,
Pat
On 3 November 2015 at 06:56, Chaddaï Fouché
Hello,
Le mer. 28 oct. 2015 à 15:59, PATRICK BROWNE
a écrit : {- From Learn Haskell Fast and Hard : 4.3.1. Maybe is a monad
http://yannesposito.com/Scratch/en/blog/Haskell-the-Hard-Way/#maybe-monad
Concerning the code below I have the following questions: 1) Are eligibleLet and eligibleEquational operationally equivalent (i.e. perform the same operations) and/or semantically equivalent(i.e. Church-Rosser)?
A priori, they're only semantically equivalent since in eligibleEquational intermediary computation are repeated whereas in eligibleLet they're only effectued once.
2) Apart from the return type of Maybe instead of Bool how does eligibleMonad differ from eligibleLet?
eligibleMonad is nice and readable while eligibleLet is a tiresome mess to write and maintain... Basically in eligibleMonad the logic that checks whether an operation is possible is rolled into the monadic operations themselves whereas in eligibleLet you have to check every operation validity yourself (so you can forget one or do it incorrectly).
Note that with more familiarity with monads, you would probably rewrite eligibleMonad to avoid naming the intermediary account (and thus avoid any confusion between versions) :
eligibleMonad account = depositM 100 account
= withdrawM 200 = depositM 100 = withdrawM 300 = depositM 1000 Just True
This would make it very easy to add or remove operations.
-- Jedaï
-- This email originated from DIT. If you received this email in error, please delete it from your system. Please note that if you are not the named addressee, disclosing, copying, distributing or taking any action based on the contents of this email or attachments is prohibited. www.dit.ie Is ó ITBÁC a tháinig an ríomhphost seo. Má fuair tú an ríomhphost seo trí earráid, scrios de do chóras é le do thoil. Tabhair ar aird, mura tú an seolaí ainmnithe, go bhfuil dianchosc ar aon nochtadh, aon chóipeáil, aon dáileadh nó ar aon ghníomh a dhéanfar bunaithe ar an ábhar atá sa ríomhphost nó sna hiatáin seo. www.dit.ie Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to Grangegorman http://www.dit.ie/grangegorman

Le mar. 3 nov. 2015 à 13:24, PATRICK BROWNE
Chaddaï, Thank you very much for your response to my question. It confirms my intuition, but I was not sure.
Is it true in general that let expressions (e.g. eligibleLet) are always semantically equivalent to their equational counterparts (e.g. eligibleEquational)?
Yes, in Haskell that is true because it is pure/referentially transparent (absent case of misuse of the "escape hatch"es like unsafePerformIO). That is one of the big advantage : that you're always able to reason equationally. (note that this remains true with monads, they're not impure, they're just a notation that can describe impure computations)
Would it be fair to say that "let" is syntactic sugar for an equational equivalent? Or is there more to it?
"let x = stuff in expr" is syntactic sugar for "(\x -> expr) stuff" basically. Though there's a few syntactical conveniences that complicate the translation.
With respect to the ordering of the operations is generally true that a monadic version is semantically equivalent to a set of "let expressions" in a nested if-then-else?
Well with the Maybe monad like here yes, though that's more of a nested case-of. With IO this is more complicated, especially if you use forkIO (concurrency).
Thanks,
You're welcome ! -- Chaddaï
participants (2)
-
Chaddaï Fouché
-
PATRICK BROWNE