fromBranch0 :: Applicative m => Branch0 m -> (Branches m, Dependencies)
fromBranch0 b =
  ( fromChildren (Branch._children b)
  , fromTermsStar (Branch._terms b)
    <> fromTypesStar (Branch._types b)
    <> fromEdits (Branch._edits b) )
  where
  fromChildren :: Applicative m => Map NameSegment (Branch m) -> Branches m
  fromChildren m = [ (Branch.headHash b, Just (pure b)) | b <- toList m ]
  references :: Branch.Star r NameSegment -> [r]
  references = toList . R.dom . Star3.d1
  mdValues :: Branch.Star r NameSegment -> [Reference]
  mdValues = fmap snd . toList . R.ran . Star3.d3
  fromTermsStar :: Branch.Star Referent NameSegment -> Dependencies
  fromTermsStar s = Dependencies mempty terms decls where
    terms = Set.fromList $
      [ i | Referent.Ref (DerivedId i) <- references s] ++
      [ i | DerivedId i <- mdValues s]
    decls = Set.fromList $
      [ i | Referent.Con (DerivedId i) _ _ <- references s ]
  fromTypesStar :: Branch.Star Reference NameSegment -> Dependencies
  fromTypesStar s = Dependencies mempty terms decls where
    terms = Set.fromList [ i | DerivedId i <- mdValues s ]
    decls = Set.fromList [ i | DerivedId i <- references s ]
  fromEdits :: Map NameSegment (EditHash, m Patch) -> Dependencies
  fromEdits m = Dependencies (Set.fromList . fmap fst $ toList m) mempty mempty
fromBranch0 :: Applicative m => Branch0 m -> (Branches m, Dependencies)
fromBranch0 b =
  ( fromChildren (Branch._children b)
  , fromTermsStar (Branch._terms b)
    <> fromTypesStar (Branch._types b)
    <> fromEdits (Branch._edits b) )
  where
  fromChildren :: Applicative m => Map NameSegment (Branch m) -> Branches m
  fromChildren m = [ (Branch.headHash b, Just (pure b)) | b <- toList m ]
  references :: Branch.Star r NameSegment -> [r]
  references = toList . R.dom . Star3.d1
  mdValues :: Branch.Star r NameSegment -> [Reference]
  mdValues = fmap snd . toList . R.ran . Star3.d3
  fromTermsStar :: Branch.Star Referent NameSegment -> Dependencies
  fromTermsStar s = Dependencies mempty terms decls where
    terms = Set.fromList $
      [ i | Referent.Ref (DerivedId i) <- references s] ++
      [ i | DerivedId i <- mdValues s]
    decls = Set.fromList $
      [ i | Referent.Con (DerivedId i) _ _ <- references s ]
  fromTypesStar :: Branch.Star Reference NameSegment -> Dependencies
  fromTypesStar s = Dependencies mempty terms decls where
    terms = Set.fromList [ i | DerivedId i <- mdValues s ]
    decls = Set.fromList [ i | DerivedId i <- references s ]
  fromEdits :: Map NameSegment (EditHash, m Patch) -> Dependencies
  fromEdits m = Dependencies (Set.fromList . fmap fst $ toList m) mempty mempty
fromBranch0 b =
  ( fromChildren (Branch._children b)
  , fromTermsStar (Branch._terms b)
    <> fromTypesStar (Branch._types b)
    <> fromEdits (Branch._edits b) )
  where
  fromChildren :: Applicative m => Map NameSegment (Branch m) -> Branches m
  fromChildren m = [ (Branch.headHash b, Just (pure b)) | b <- toList m ]
  references :: Branch.Star r NameSegment -> [r]
  references = toList . R.dom . Star3.d1
  mdValues :: Branch.Star r NameSegment -> [Reference]
  mdValues = fmap snd . toList . R.ran . Star3.d3
  fromTermsStar :: Branch.Star Referent NameSegment -> Dependencies
  fromTermsStar s = Dependencies mempty terms decls where
    terms = Set.fromList $
      [ i | Referent.Ref (DerivedId i) <- references s] ++
      [ i | DerivedId i <- mdValues s]
    decls = Set.fromList $
      [ i | Referent.Con (DerivedId i) _ _ <- references s ]
  fromTypesStar :: Branch.Star Reference NameSegment -> Dependencies
  fromTypesStar s = Dependencies mempty terms decls where
    terms = Set.fromList [ i | DerivedId i <- mdValues s ]
    decls = Set.fromList [ i | DerivedId i <- references s ]
  fromEdits :: Map NameSegment (EditHash, m Patch) -> Dependencies
  fromEdits m = Dependencies (Set.fromList . fmap fst $ toList m) mempty mempty