Skip to content

Commit

Permalink
Use strict Writer for P79
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhijit Sarkar committed Jan 7, 2024
1 parent 9fdda15 commit 6d45393
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
8 changes: 6 additions & 2 deletions ninety-nine-haskell.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ library
Paths_ninety_nine_haskell
hs-source-dirs:
src
ghc-options: -XTupleSections -Werror -Weverything -Wno-missing-import-lists -Wno-missed-specializations -Wno-all-missed-specializations -Wno-missing-local-signatures -Wno-monomorphism-restriction -Wno-missing-safe-haskell-mode -Wno-safe -Wno-unsafe -Wno-implicit-prelude -Wno-prepositive-qualified-module -Wno-missing-kind-signatures -Wno-unused-top-binds -Wno-missing-export-lists
default-extensions:
TupleSections
ghc-options: -Werror -Weverything -Wno-missing-import-lists -Wno-missed-specializations -Wno-all-missed-specializations -Wno-missing-local-signatures -Wno-monomorphism-restriction -Wno-missing-safe-haskell-mode -Wno-safe -Wno-unsafe -Wno-implicit-prelude -Wno-prepositive-qualified-module -Wno-missing-kind-signatures -Wno-unused-top-binds -Wno-missing-export-lists
build-depends:
array
, base >=4.7 && <5
Expand Down Expand Up @@ -82,7 +84,9 @@ test-suite ninety-nine-test
Paths_ninety_nine_haskell
hs-source-dirs:
test
ghc-options: -XTupleSections -Werror -Weverything -Wno-missing-import-lists -Wno-missed-specializations -Wno-all-missed-specializations -Wno-missing-local-signatures -Wno-monomorphism-restriction -Wno-missing-safe-haskell-mode -Wno-safe -Wno-unsafe -Wno-implicit-prelude -Wno-prepositive-qualified-module -Wno-missing-kind-signatures -Wno-unused-top-binds -Wno-missing-export-lists
default-extensions:
TupleSections
ghc-options: -Werror -Weverything -Wno-missing-import-lists -Wno-missed-specializations -Wno-all-missed-specializations -Wno-missing-local-signatures -Wno-monomorphism-restriction -Wno-missing-safe-haskell-mode -Wno-safe -Wno-unsafe -Wno-implicit-prelude -Wno-prepositive-qualified-module -Wno-missing-kind-signatures -Wno-unused-top-binds -Wno-missing-export-lists
build-depends:
QuickCheck
, base >=4.7 && <5
Expand Down
4 changes: 3 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ dependencies:
- split
- containers

default-extensions:
- TupleSections

ghc-options:
- -XTupleSections
- -Werror
# For details on warnings: https://downloads.haskell.org/~ghc/master/users-guide/using-warnings.html
# This list originally taken from https://medium.com/mercury-bank/enable-all-the-warnings-a0517bc081c3
Expand Down
25 changes: 20 additions & 5 deletions src/Monads.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}

module Monads
( Operator (..),
Expand All @@ -10,12 +11,12 @@ module Monads
where

import qualified Control.Monad as M
import Control.Monad.State (State)
import Control.Monad.State (MonadState, State)
import qualified Control.Monad.State as S
import Control.Monad.Trans.Maybe (MaybeT)
import qualified Control.Monad.Trans.Maybe as Mb
import Control.Monad.Writer (Writer, WriterT)
import qualified Control.Monad.Writer as W
import Control.Monad.Writer.Strict (MonadWriter, Writer, WriterT)
import qualified Control.Monad.Writer.Strict as W
import Data.Monoid (Sum (..))
import qualified Data.Monoid as Md

Expand Down Expand Up @@ -159,6 +160,12 @@ type Result = Maybe Integer
Stack order matters. The output is in the reverse order,
i.e. the innermost monad result wraps the others.
((a, w), s), where a is Result, w is Logs, and s is Stack.
This implementation uses the "tagless final" approach:
- Functions are declared using type constraints instead of
concrete types.
- Instantiation of a polymorphic function to a concrete type
(aka interpreter) happens "at the end":
-}
type Calculation = MaybeT (WriterT Logs (State Stack)) Integer

Expand All @@ -167,14 +174,22 @@ calculatePostfix = fst . chain . calc
where
chain = flip S.runState [] . W.runWriterT . Mb.runMaybeT

calc :: [Element] -> Calculation
calc ::
(MonadWriter Logs m, MonadState Stack m, MonadFail m) =>
[Element] ->
m Integer
calc elems =
S.get >>= case elems of
[] -> result
(Operand n : xs) -> loop xs Nothing . (n :)
(Operator op : xs) -> runOp op M.>=> loop xs (Just op)

loop :: [Element] -> Maybe Operator -> Stack -> Calculation
loop ::
(MonadWriter Logs m, MonadState Stack m, MonadFail m) =>
[Element] ->
Maybe Operator ->
Stack ->
m Integer
loop xs op s = W.tell [(s, op)] >> S.put s >> calc xs

{-
Expand Down

0 comments on commit 6d45393

Please sign in to comment.