module U.Codebase.Sqlite.V2.HashHandle
  ( v2HashHandle,
  )
where

import Data.Function ((&))
import Data.Set qualified as Set
import U.Codebase.Branch.Hashing qualified as H2
import U.Codebase.Causal.Hashing qualified as H2
import U.Codebase.Decl.Hashing qualified as H2
import U.Codebase.HashTags (BranchHash (..), PatchHash (..))
import U.Codebase.Sqlite.Branch.Format qualified as BranchFormat
import U.Codebase.Sqlite.HashHandle
import U.Codebase.Sqlite.Patch.Format qualified as PatchFormat
import U.Codebase.Term.Hashing as H2
import U.Util.Type (removeAllEffectVars)
import Unison.Hashing.V2 qualified as H2
import Unison.Hashing.V2.Convert2 (h2ToV2Reference, hashBranchFormatToH2Branch, hashPatchFormatToH2Patch, v2ToH2Type, v2ToH2TypeD)

v2HashHandle :: HashHandle
v2HashHandle :: HashHandle
v2HashHandle =
  HashHandle
    { $sel:toReference:HashHandle :: Type Symbol -> Reference
toReference = Reference -> Reference
h2ToV2Reference (Reference -> Reference)
-> (Type Symbol -> Reference) -> Type Symbol -> Reference
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type Symbol () -> Reference
forall v a. (Ord v, Show v) => Type v a -> Reference
H2.typeToReference (Type Symbol () -> Reference)
-> (Type Symbol -> Type Symbol ()) -> Type Symbol -> Reference
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type Symbol -> Type Symbol ()
forall v. Ord v => TypeR Reference v -> Type v ()
v2ToH2Type (Type Symbol -> Type Symbol ())
-> (Type Symbol -> Type Symbol) -> Type Symbol -> Type Symbol ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type Symbol -> Type Symbol
forall v r. Var v => TypeR r v -> TypeR r v
removeAllEffectVars,
      $sel:toReferenceMentions:HashHandle :: Type Symbol -> Set Reference
toReferenceMentions = (Reference -> Reference) -> Set Reference -> Set Reference
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map Reference -> Reference
h2ToV2Reference (Set Reference -> Set Reference)
-> (Type Symbol -> Set Reference) -> Type Symbol -> Set Reference
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type Symbol () -> Set Reference
forall v a. (Ord v, Show v) => Type v a -> Set Reference
H2.typeToReferenceMentions (Type Symbol () -> Set Reference)
-> (Type Symbol -> Type Symbol ()) -> Type Symbol -> Set Reference
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type Symbol -> Type Symbol ()
forall v. Ord v => TypeR Reference v -> Type v ()
v2ToH2Type (Type Symbol -> Type Symbol ())
-> (Type Symbol -> Type Symbol) -> Type Symbol -> Type Symbol ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type Symbol -> Type Symbol
forall v r. Var v => TypeR r v -> TypeR r v
removeAllEffectVars,
      $sel:toReferenceDecl:HashHandle :: Hash -> TypeD Symbol -> Reference
toReferenceDecl = \Hash
h -> Reference -> Reference
h2ToV2Reference (Reference -> Reference)
-> (TypeD Symbol -> Reference) -> TypeD Symbol -> Reference
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type Symbol () -> Reference
forall v a. (Ord v, Show v) => Type v a -> Reference
H2.typeToReference (Type Symbol () -> Reference)
-> (TypeD Symbol -> Type Symbol ()) -> TypeD Symbol -> Reference
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash -> TypeD Symbol -> Type Symbol ()
forall v. Ord v => Hash -> TypeD v -> Type v ()
v2ToH2TypeD Hash
h (TypeD Symbol -> Type Symbol ())
-> (TypeD Symbol -> TypeD Symbol) -> TypeD Symbol -> Type Symbol ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TypeD Symbol -> TypeD Symbol
forall v r. Var v => TypeR r v -> TypeR r v
removeAllEffectVars,
      $sel:toReferenceDeclMentions:HashHandle :: Hash -> TypeD Symbol -> Set Reference
toReferenceDeclMentions = \Hash
h -> (Reference -> Reference) -> Set Reference -> Set Reference
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map Reference -> Reference
h2ToV2Reference (Set Reference -> Set Reference)
-> (TypeD Symbol -> Set Reference) -> TypeD Symbol -> Set Reference
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Type Symbol () -> Set Reference
forall v a. (Ord v, Show v) => Type v a -> Set Reference
H2.typeToReferenceMentions (Type Symbol () -> Set Reference)
-> (TypeD Symbol -> Type Symbol ())
-> TypeD Symbol
-> Set Reference
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash -> TypeD Symbol -> Type Symbol ()
forall v. Ord v => Hash -> TypeD v -> Type v ()
v2ToH2TypeD Hash
h (TypeD Symbol -> Type Symbol ())
-> (TypeD Symbol -> TypeD Symbol) -> TypeD Symbol -> Type Symbol ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TypeD Symbol -> TypeD Symbol
forall v r. Var v => TypeR r v -> TypeR r v
removeAllEffectVars,
      $sel:hashBranch:HashHandle :: forall (m :: * -> *). Monad m => Branch m -> m BranchHash
hashBranch = Branch m -> m BranchHash
forall (m :: * -> *). Monad m => Branch m -> m BranchHash
H2.hashBranch,
      $sel:hashBranchV3:HashHandle :: forall (m :: * -> *). BranchV3 m -> BranchHash
hashBranchV3 = BranchV3 m -> BranchHash
forall (m :: * -> *). BranchV3 m -> BranchHash
H2.hashBranchV3,
      $sel:hashCausal:HashHandle :: BranchHash -> Set CausalHash -> CausalHash
hashCausal = BranchHash -> Set CausalHash -> CausalHash
H2.hashCausal,
      HashBranchLocalIds -> LocalBranch -> BranchHash
hashBranchFormatFull :: HashBranchLocalIds -> LocalBranch -> BranchHash
$sel:hashBranchFormatFull:HashHandle :: HashBranchLocalIds -> LocalBranch -> BranchHash
hashBranchFormatFull,
      HashPatchLocalIds -> LocalPatch -> PatchHash
hashPatchFormatFull :: HashPatchLocalIds -> LocalPatch -> PatchHash
$sel:hashPatchFormatFull:HashHandle :: HashPatchLocalIds -> LocalPatch -> PatchHash
hashPatchFormatFull,
      $sel:verifyTermFormatHash:HashHandle :: ComponentHash -> HashTermFormat -> Maybe HashMismatch
verifyTermFormatHash = ComponentHash -> HashTermFormat -> Maybe HashMismatch
H2.verifyTermFormatHash,
      $sel:verifyDeclFormatHash:HashHandle :: ComponentHash -> HashDeclFormat -> Maybe DeclHashingError
verifyDeclFormatHash = ComponentHash -> HashDeclFormat -> Maybe DeclHashingError
H2.verifyDeclFormatHash
    }
  where
    hashBranchFormatFull :: HashBranchLocalIds -> LocalBranch -> BranchHash
hashBranchFormatFull HashBranchLocalIds
localIds LocalBranch
localBranch =
      HashBranchLocalIds -> LocalBranch -> HashBranch
BranchFormat.localToHashBranch HashBranchLocalIds
localIds LocalBranch
localBranch
        HashBranch -> (HashBranch -> Branch) -> Branch
forall a b. a -> (a -> b) -> b
& HashBranch -> Branch
hashBranchFormatToH2Branch
        Branch -> (Branch -> Hash) -> Hash
forall a b. a -> (a -> b) -> b
& Branch -> Hash
forall a. ContentAddressable a => a -> Hash
H2.contentHash
        Hash -> (Hash -> BranchHash) -> BranchHash
forall a b. a -> (a -> b) -> b
& Hash -> BranchHash
BranchHash
    hashPatchFormatFull :: HashPatchLocalIds -> LocalPatch -> PatchHash
hashPatchFormatFull HashPatchLocalIds
localIds LocalPatch
localPatch =
      HashPatchLocalIds -> LocalPatch -> HashPatch
PatchFormat.localPatchToHashPatch HashPatchLocalIds
localIds LocalPatch
localPatch
        HashPatch -> (HashPatch -> Patch) -> Patch
forall a b. a -> (a -> b) -> b
& HashPatch -> Patch
hashPatchFormatToH2Patch
        Patch -> (Patch -> Hash) -> Hash
forall a b. a -> (a -> b) -> b
& Patch -> Hash
forall a. ContentAddressable a => a -> Hash
H2.contentHash
        Hash -> (Hash -> PatchHash) -> PatchHash
forall a b. a -> (a -> b) -> b
& Hash -> PatchHash
PatchHash