on master help sign in
Better behavior when unifying with yourself

The OCaml and Haskell implementations both failed in weird ways when unifying points with themselves. This change makes it so the result is pretty nice.

commit20fa5f279a3eceaa1df5b52eead43caf2f6dee81 authorEvan Czaplicki <evancz@users.noreply.github.com> dateThu 1 Jun 2017, 23:28 (9 years ago) parent7729527
1 file changed+17−16
src/Type/UnionFind.hs+17−16
src/Type/UnionFind.hs+17−16
@@ -133,25 +133,26 @@ union p1 p2 newDesc =
133133 do point1@(Pt ref1) <- repr p1
134134 point2@(Pt ref2) <- repr p2
135135
136− -- assert (point1 /= point2)
137−
138136 Info w1 d1 <- readIORef ref1
139137 Info w2 d2 <- readIORef ref2
140138
141− weight1 <- readIORef w1
142− weight2 <- readIORef w2
143−
144− let !newWeight = weight1 + weight2
145−
146− if weight1 >= weight2
147− then
148− do writeIORef ref2 (Link point1)
149− writeIORef w1 newWeight
150− writeIORef d1 newDesc
151− else
152− do writeIORef ref1 (Link point2)
153− writeIORef w2 newWeight
154− writeIORef d2 newDesc
139+ if point1 == point2
140+ then writeIORef d1 newDesc
141+ else do
142+ weight1 <- readIORef w1
143+ weight2 <- readIORef w2
144+
145+ let !newWeight = weight1 + weight2
146+
147+ if weight1 >= weight2
148+ then
149+ do writeIORef ref2 (Link point1)
150+ writeIORef w1 newWeight
151+ writeIORef d1 newDesc
152+ else
153+ do writeIORef ref1 (Link point2)
154+ writeIORef w2 newWeight
155+ writeIORef d2 newDesc
155156
156157
157158equivalent :: Point a -> Point a -> IO Bool