Skip to content

Commit

Permalink
address sequenceTuple issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Jul 19, 2023
1 parent 0a4965c commit bdc819c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/Swarm/Game/Scenario/Topography/Navigation/Portal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -257,22 +257,36 @@ ensureSpatialConsistency xs =
nonUniform :: Map (SubworldName, SubworldName) (NonEmpty (V2 Int32))
nonUniform = M.filter ((not . allEqual) . NE.toList) vectorized

-- | This signature looks a lot like the Traversable instance:
-- |
-- An implementation of 'sequenceA' for 'Signed' that does not
-- require an 'Applicative' instance for the inner 'Functor'.
--
-- == Discussion
-- Compare to the 'Traversable' instance of 'Signed':
-- @
-- instance Traversable Signed where
-- traverse f (Positive x) = Positive <$> f x
-- traverse f (Negative x) = Negative <$> f x
-- @
--
-- if we were to substitute "id" for f:
-- if we were to substitute 'id' for f:
-- @
-- traverse id (Positive x) = Positive <$> id x
-- traverse id (Negative x) = Negative <$> id x
-- @
-- our implementation essentially becomes @traverse id@.
--
-- However, we cannot simply write our implementation as @traverse id@, because
-- the 'traverse' function has an 'Applicative' constraint, which is superfluous
-- for our purpose.
--
-- Perhaps there is an opportunity to invent a typeclass for datatypes which
-- consist exclusively of unary (or more ambitiously, non-nullary?) data constructors,
-- for which a less-constrained 'sequence' function could be automatically derived.
sequenceSigned ::
Functor f =>
Signed (f a) ->
f (Signed a)
sequenceSigned = \case
Positive x -> Positive <$> x
Negative x -> Negative <$> x
Negative x -> Negative <$> x
2 changes: 1 addition & 1 deletion src/Swarm/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Swarm.Util (
findDup,
both,
allEqual,

-- * Directory utilities
readFileMay,
readFileMayT,
Expand Down

0 comments on commit bdc819c

Please sign in to comment.