Skip to content

Commit

Permalink
Small optimisations in unify
Browse files Browse the repository at this point in the history
Reviewed By: donsbot

Differential Revision: D67144007

fbshipit-source-id: 568d171bb3532ab960c04e61375fa578fba22ff3
  • Loading branch information
Simon Marlow authored and facebook-github-bot committed Dec 16, 2024
1 parent 858d59e commit 7a149dc
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions glean/db/Glean/Query/Typecheck/Unify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Control.Monad.Except
import Control.Monad.State
import qualified Data.IntMap as IntMap
import qualified Data.Map as Map
import qualified Data.Map.Merge.Lazy as Map
import Data.Maybe
import qualified Data.Text as Text
import Compat.Prettyprinter hiding ((<>), enclose)
Expand Down Expand Up @@ -90,11 +91,23 @@ unify a@(HasTy fa ra x) b@(HasTy fb rb y)
(Just x, Just y) | x /= y -> unifyError a b
(Nothing, _) -> return rb
_otherwise -> return ra
mapM_ (uncurry unify) $ Map.intersectionWith (,) fa fb
z <- freshTyVarInt
let all = HasTy (Map.union fa fb) rec z
extend x all
extend y all
union <- Map.mergeA
Map.preserveMissing
Map.preserveMissing
(Map.zipWithAMatched $ \_ a b -> do unify a b; return a)
fa fb
-- if either a or b is the same as the unified type, avoid creating
-- a new type variable.
let size = Map.size union
if size == Map.size fa && ra == rec
then extend y a
else if size == Map.size fb && rb == rec
then extend x b
else do
z <- freshTyVarInt
let all = HasTy union rec z
extend x all
extend y all

unify a@(HasTy _ (Just Sum) _) b@RecordTy{} =
unifyError a b
Expand All @@ -103,9 +116,8 @@ unify a@(HasTy m _ x) b@(RecordTy fs) = do
case Map.lookup f m of
Nothing -> return ()
Just ty' -> unify ty ty'
forM_ (Map.keys m) $ \n ->
when (n `notElem` map fieldDefName fs) $
unifyError a b
when (not (Map.null (foldr (Map.delete . fieldDefName) m fs))) $
unifyError a b
extend x (RecordTy fs)

unify a@(HasTy _ (Just Record) _) b@SumTy{} =
Expand Down

0 comments on commit 7a149dc

Please sign in to comment.