Skip to content

Commit

Permalink
[refactor] pullPorts again, with StateT + mapM (#64)
Browse files Browse the repository at this point in the history
`StateT` captures that we both pass the list of available things into `pull1Port` and then return the reduced version thereof. `mapM` captures that each invocation of `pull1Port` returns a single thing that was pulled, or an error.
  • Loading branch information
acl-cqc authored Dec 6, 2024
1 parent 5463a7f commit 05310cc
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions brat/Brat/Checker/Helpers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import Bwd
import Hasochism
import Util (log2)

import Control.Monad.State.Lazy (StateT(..), runStateT)
import Control.Monad.Freer (req)
import Data.Bifunctor
import Data.Foldable (foldrM)
Expand Down Expand Up @@ -108,21 +109,18 @@ pullPortsSig :: Show ty
-> Checking [(PortName, ty)]
pullPortsSig = pullPorts id showSig

pullPorts :: forall a ty. Show ty
=> (a -> PortName) -- A way to get a port name for each element
pullPorts :: forall a ty
. (a -> PortName) -- A way to get a port name for each element
-> ([(a, ty)] -> String) -- A way to print the list
-> [PortName] -- Things to pull to the front
-> [(a, ty)] -- The list to rearrange
-> Checking [(a, ty)]
pullPorts _ _ [] types = pure types
pullPorts toPort showFn (p:ports) types = do
(x, types) <- pull1Port p types
(x:) <$> pullPorts toPort showFn ports types
pullPorts toPort showFn to_pull types =
-- the "state" here is the things still available to be pulled
(\(pulled, rest) -> pulled ++ rest) <$> runStateT (mapM pull1Port to_pull) types
where
pull1Port :: PortName
-> [(a, ty)]
-> Checking ((a, ty), [(a, ty)])
pull1Port p available = case partition ((== p) . toPort . fst) available of
pull1Port :: PortName -> StateT [(a, ty)] Checking (a, ty)
pull1Port p = StateT $ \available -> case partition ((== p) . toPort . fst) available of
([], _) -> err $ BadPortPull $ "Port not found: " ++ p ++ " in " ++ showFn available
([found], remaining) -> pure (found, remaining)
(_, _) -> err $ AmbiguousPortPull p (showFn available)
Expand Down

0 comments on commit 05310cc

Please sign in to comment.