Skip to content

Commit

Permalink
refactor HasDependencies to allow multiple keys
Browse files Browse the repository at this point in the history
  • Loading branch information
bristermitten committed May 4, 2024
1 parent b64ff47 commit 5d1490e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/Elara/AST/Shunted.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,13 @@ type ShuntedTypeDeclaration = Generic.TypeDeclaration 'Shunted

instance HasDependencies ShuntedDeclaration where
type Key ShuntedDeclaration = Qualified Name
key = view (_Unwrapped % unlocated % field' @"name" % unlocated)
dependencies decl = traceShowId $ case decl ^. _Unwrapped % unlocated % field' @"body" % _Unwrapped % unlocated of

keys sd =
view (_Unwrapped % unlocated % field' @"name" % unlocated) sd :| case sd ^. _Unwrapped % unlocated % field' @"body" % _Unwrapped % unlocated of
Generic.TypeDeclaration _ (Located _ (Generic.ADT ctors)) _ ->
toList (NTypeName <<$>> (ctors ^.. each % _1 % unlocated))
_ -> []
dependencies decl = case decl ^. _Unwrapped % unlocated % field' @"body" % _Unwrapped % unlocated of
Generic.Value e _ t _ -> valueDependencies e <> (maybeToList t >>= typeDependencies)
Generic.TypeDeclaration{} -> []

Expand Down
17 changes: 12 additions & 5 deletions src/Elara/Data/TopologicalGraph.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ import Text.Show qualified as Show

class Ord (Key a) => HasDependencies a where
type Key a
keys :: a -> NonEmpty (Key a)
keys = pure . key

key :: a -> Key a
key = head . keys

dependencies :: a -> [Key a]

{-# MINIMAL (keys | key), dependencies #-}

instance HasDependencies a => HasDependencies (a, b) where
type Key (a, b) = Key a
key = key . fst
keys = keys . fst
dependencies = dependencies . fst

data TopologicalGraph a = TopologicalGraph
Expand Down Expand Up @@ -68,17 +75,17 @@ genericGraphTraverse_ f f' g = do
traverse_ f' sortedNodes

createGraph :: HasDependencies a => [a] -> TopologicalGraph a
createGraph = uncurry3 TopologicalGraph . graphFromEdges . fmap createEdge
createGraph = uncurry3 TopologicalGraph . graphFromEdges . mconcat . fmap (toList . createEdge)

createEdge ::
forall a.
HasDependencies a =>
a ->
(a, Key a, [Key a])
NonEmpty (a, Key a, [Key a])
createEdge m = do
let mn = key m
let mns = keys m
let mImports = dependencies m
(m, mn, mImports)
(m,,mImports) <$> mns

allEntries :: TopologicalGraph m -> [m]
allEntries g = g ^.. moduleGraph % to vertices % each % to (g ^. nodeFromVertex) % _1
Expand Down

0 comments on commit 5d1490e

Please sign in to comment.