Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Jul 19, 2023
1 parent ea93e46 commit 0a4965c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
24 changes: 22 additions & 2 deletions src/Swarm/Game/Scenario/Topography/Navigation/Portal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Linear (V2, negated)
import Swarm.Game.Location
import Swarm.Game.Scenario.Topography.Navigation.Waypoint
import Swarm.Game.Universe
import Swarm.Util (allEqual, binTuples, both, failT, quote, sequenceTuple)
import Swarm.Util (allEqual, binTuples, both, failT, quote)

type WaypointMap = M.Map WaypointName (NonEmpty Location)

Expand Down Expand Up @@ -249,10 +249,30 @@ ensureSpatialConsistency xs =

groupedBySubworldPair ::
Map (SubworldName, SubworldName) (NonEmpty (Signed (Location, Location)))
groupedBySubworldPair = binTuples $ map (sequenceTuple . fmap tuplify) normalizedOrdering
groupedBySubworldPair = binTuples $ map (sequenceSigned . fmap tuplify) normalizedOrdering

vectorized :: Map (SubworldName, SubworldName) (NonEmpty (V2 Int32))
vectorized = M.map (NE.map (getSigned . fmap (uncurry (.-.)))) groupedBySubworldPair

nonUniform :: Map (SubworldName, SubworldName) (NonEmpty (V2 Int32))
nonUniform = M.filter ((not . allEqual) . NE.toList) vectorized

-- | This signature looks a lot like the Traversable instance:
-- @
-- 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:
-- @
-- traverse id (Positive x) = Positive <$> id x
-- traverse id (Negative x) = Negative <$> id x
-- @
sequenceSigned ::
Functor f =>
Signed (f a) ->
f (Signed a)
sequenceSigned = \case
Positive x -> Positive <$> x
Negative x -> Negative <$> x
19 changes: 1 addition & 18 deletions src/Swarm/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ module Swarm.Util (
findDup,
both,
allEqual,
sequenceTuple,


-- * Directory utilities
readFileMay,
readFileMayT,
Expand Down Expand Up @@ -195,22 +194,6 @@ allEqual :: (Ord a) => [a] -> Bool
allEqual [] = True
allEqual (x : xs) = all (== x) xs

-- | This function has a lamentable basis.
-- The 'sequenceA' function requires an 'Applicative' instance
-- for the inner 'Functor'. However, the 'Applicative' instance
-- of @(,)@ (the two-element tuple) requires a 'Monoid' instance
-- for the first element!
-- See: https://hackage.haskell.org/package/base-4.18.0.0/docs/src/GHC.Base.html#line-523
--
-- The 'sequenceA' operation does not affect the first element
-- of the tuple, so it shouldn't matter whether it has a 'Monoid' instance!
-- To satisfy the compiler, we abuse a list to first wrap and then unwrap after a traversal.
sequenceTuple ::
Traversable f =>
f (a, b) ->
(a, f b)
sequenceTuple = first head . traverse (first pure)

------------------------------------------------------------
-- Directory stuff

Expand Down

0 comments on commit 0a4965c

Please sign in to comment.