Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Monadic Logging & Evaluation in Haskell #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mkanta
Copy link
Contributor

@mkanta mkanta commented Mar 4, 2019

This is the Haskell version of safe evaluation and division counting plus a combination of both using
monad transformers. Mostly untested, but it illustrates the monad manipulations involved.

Also some updates concerning the January meeting, some questions answered in more detail.

Copy link
Contributor

@vgrocha vgrocha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Added some comments and questions
  • Suggested changes to MonadEval.hs so it can be loaded in ghci

logDivs f arg@(Div (x,y)) = logDivs f x >>= logSecond
where
logSecond n = logDivs f y >>= combineWith n
combineWith nm den = writer (f $ Div (Cons nm, Cons den),1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an interesting idea to have logDivs wrap around the evalSim, but it looks like this implementation will apply f every time it recurses, leading to unnecessary evaluations. Was this intended?

-- where the @Identity@ monad comes in handy because it allows to
-- pack the value of @evalSim@ for use with @logDivsU@:
logDivUSim :: Term -> WriterT (Sum Int) Identity Int
logDivUSim = logDivU (Identity . evalSim)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I see. You wrap it in an Identity Monad when you want to turn a simple value into a monadic value to be elsewhere

module MonadEval
where

import Control.Monad.Writer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import Control.Monad.Writer
import Control.Monad
import Control.Monad.Identity
import Control.Monad.Writer
import Data.Monoid


-- | This can be used with @evalSafe@
logDivUSafe :: Term -> WriterT (Sum Int) (Either String) Int
logDivUSafe = logDivU evalSafe
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logDivUSafe = logDivU evalSafe
logDivUSafe = logDivsU evalSafe

-- where the @Identity@ monad comes in handy because it allows to
-- pack the value of @evalSim@ for use with @logDivsU@:
logDivUSim :: Term -> WriterT (Sum Int) Identity Int
logDivUSim = logDivU (Identity . evalSim)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logDivUSim = logDivU (Identity . evalSim)
logDivUSim = logDivsU (Identity . evalSim)

@mkanta
Copy link
Contributor Author

mkanta commented Mar 11, 2019 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants