@@ -21,6 +21,7 @@ import Data.List qualified as List (sort)
2121import Data.Map qualified as Map
2222import Data.Set qualified as Set
2323import Unison.ABT
24+ import Unison.Debug qualified as Debug
2425import Unison.Hash (Hash )
2526import Unison.Hashing.V2.Tokenizable (Hashable1 , hash1 )
2627import Unison.Hashing.V2.Tokenizable qualified as Hashable
@@ -38,11 +39,9 @@ hashComponent ::
3839 Either IncompleteElementOrderingError (Hash , [(v , Term f v a )])
3940hashComponent byName = do
4041 let ts = Map. toList byName
41- -- First, compute a canonical hash ordering of the component, as well as an environment in which we can hash
42- -- individual names.
43- (hashes, env) = doHashCycle [] ts
44- when (List. nubOrd hashes /= hashes) $ do
45- Left IncompleteElementOrderingError
42+ -- First, compute a canonical hash ordering of the component, as well as an environment in which we can hash
43+ -- individual names.
44+ (hashes, env) <- doHashCycle [] ts
4645 -- Construct a list of tokens that is shared by all members of the component. They are disambiguated only by their
4746 -- name that gets tumbled into the hash.
4847 let commonTokens :: [Hashable. Token ]
@@ -124,14 +123,14 @@ hash' env = \case
124123 ++ show v
125124 ++ " environment = "
126125 ++ show env
127- Cycle' vs t -> hash1 (hashCycle vs env) undefined t
126+ Cycle' vs t -> hash1 (fromRight ( error " Encountered ambigous element ordering for component " ) . hashCycle vs env) undefined t
128127 Abs'' v t -> hash' (Right v : env) t
129128 Tm' t -> hash1 (\ ts -> (List. sort (map (hash' env) ts), hash' env)) (hash' env) t
130129 where
131- hashCycle :: [v ] -> [Either [v ] v ] -> [Term f v a ] -> ([Hash ], Term f v a -> Hash )
132- hashCycle cycle env ts =
133- let (ts', env') = doHashCycle env (zip cycle ts)
134- in (ts', hash' env')
130+ hashCycle :: [v ] -> [Either [v ] v ] -> [Term f v a ] -> Either IncompleteElementOrderingError ([Hash ], Term f v a -> Hash )
131+ hashCycle cycle env ts = do
132+ (ts', env') <- doHashCycle env (zip cycle ts)
133+ pure (ts', hash' env')
135134
136135-- | @doHashCycle env terms@ hashes cycle @terms@ in environment @env@, and returns the canonical ordering of the hashes
137136-- of those terms, as well as an updated environment with each of the terms' bindings in the canonical ordering.
@@ -140,16 +139,20 @@ doHashCycle ::
140139 (Eq v , Functor f , Hashable1 f , Show v ) =>
141140 [Either [v ] v ] ->
142141 [(v , Term f v a )] ->
143- ([Hash ], [Either [v ] v ])
144- doHashCycle env namedTerms =
145- (map (hash' newEnv) permutedTerms, newEnv)
142+ Either IncompleteElementOrderingError ([Hash ], [Either [v ] v ])
143+ doHashCycle env namedTerms = do
144+ Debug. debugM Debug. Temp " Unison.Hashing.V2.ABT.doHashCycle" (hashes, env, fst <$> namedTerms)
145+ when (List. nubOrd hashes /= hashes) $ Left IncompleteElementOrderingError
146+ pure $ (map (hash' newEnv) permutedTerms, newEnv)
146147 where
147148 names = map fst namedTerms
148149 -- The environment in which we compute the canonical permutation of terms
149150 permutationEnv = Left names : env
151+ hashes = (hash' permutationEnv . snd ) <$> namedTerms
150152 (permutedNames, permutedTerms) =
151- namedTerms
152- & sortOn (hash' permutationEnv . snd )
153+ zip namedTerms hashes
154+ & sortOn snd
155+ & fmap fst
153156 & unzip
154157 -- The new environment, which includes the names of all of the terms in the cycle, now that we have computed their
155158 -- canonical ordering
0 commit comments