8000 kind of squish the root node onto the path vertex · github/semantic@151fb59 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 151fb59

Browse files
committed
kind of squish the root node onto the path vertex
1 parent de53808 commit 151fb59

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

semantic-scope-graph/src/Control/Effect/ScopeGraph.hs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ import Control.Effect.Fresh
3838
import Control.Effect.Reader
3939
import Control.Lens
4040
import Data.List.NonEmpty
41+
import qualified Data.List.NonEmpty as NonEmpty
4142
import Data.Map.Strict (Map)
4243
import qualified Data.Map.Strict as Map
4344
import qualified Data.Module as Module
4445
import qualified Data.ScopeGraph as ScopeGraph
4546
import Data.Semilattice.Lower
47+
import qualified Data.Set as Set
4648
import Data.Text (Text)
4749
import GHC.Records
4850
import qualified Scope.Reference as Reference
@@ -61,6 +63,7 @@ import qualified Control.Effect.ScopeGraph.Properties.Reference as Props
6163
import qualified Control.Effect.ScopeGraph.Properties.Reference as Props.Reference
6264
import Control.Effect.State
6365

66+
import qualified Algebra.Graph as Graph
6467
import qualified Algebra.Graph.Class as Class
6568

6669
-- | Extract the 'Just' of a 'Maybe' in an 'Applicative' context or, given 'Nothing', run the provided action.
@@ -132,9 +135,16 @@ addDeclarations names = do
132135
graph <- get @(Stack.Graph Stack.Node)
133136
CurrentScope current <- currentScope
134137

135-
let graph' = foldr (\(ann, name) graph ->
136-
graph -<< (Stack.declaration "reference") -<< (Stack.pushSymbol name)) mempty names
137-
put (Stack.simplify (Class.overlay (Stack.scope current >>- graph') graph))
138+
let graph' = foldr (\(_, name) graph ->
139+
graph -<< (Stack.popSymbol "member") -<< (Stack.declaration name)) mempty (NonEmpty.init names)
140+
graph'' = graph' >>- (Stack.declaration (snd $ NonEmpty.last names))
141+
graph''' = foldr (\(_, name) graph ->
142+
graph -<< (Stack.pushSymbol "member") -<< (Stack.reference name)) mempty (NonEmpty.init $ NonEmpty.reverse names)
143+
graph'''' = graph'' >>- graph''' >>- (Stack.reference (snd $ NonEmpty.head names))
144+
currentEdges = Set.filter (\(left, right) -> left == Stack.Scope current) (Stack.edgeSet graph)
145+
rootNodes = Set.map snd currentEdges
146+
graphh = foldMap (\(left, right) -> Stack.removeEdge left right graph) currentEdges
147+
put (Stack.simplify (Class.overlay (Stack.scope current >>- graph'''' >>- Class.vertex (Set.elemAt 0 rootNodes)) graphh))
138148

139149
-- | Takes an edge label and a list of names and inserts an import edge to a hole.
140150
newEdge :: ScopeGraphEff sig m => ScopeGraph.EdgeLabel -> NonEmpty Name -> m ()

semantic-scope-graph/src/Stack/Graph.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ module Stack.Graph
2222
, Class.connect
2323
, Class.edges
2424
, simplify
25+
, edgeSet
26+
, removeEdge
2527
-- * Smart constructors
2628
, scope
2729
, newScope
@@ -53,6 +55,8 @@ import Data.Map.Strict (Map)
5355
import qualified Data.Map.Strict as Map
5456
import Data.Maybe
5557
import Data.Semilattice.Lower
58+
import Data.Set (Set)
59+
import qualified Data.Set as Set
5660
import qualified Scope.Types as Scope
5761

5862
type Symbol = Name
@@ -118,6 +122,9 @@ pushSymbol = Class.vertex . PushSymbol
118122
root :: Graph Node
119123
root = Graph (Algebraic.vertex Root)
120124

125+
edgeSet :: Graph Node -> Set (Node, Node)
126+
edgeSet graph = Algebraic.edgeSet (unGraph graph)
127+
121128
tagGraphUniquely :: Graph Node -> Graph (Tagged Node)
122129
tagGraphUniquely
123130
= simplify
@@ -152,6 +159,9 @@ newScope name edges graph =
152159
simplify :: Ord a => Graph a -> Graph a
153160
simplify = Graph . Algebraic.simplify . unGraph
154161

162+
removeEdge :: Ord a => a -> a -> Graph a -> Graph a
163+
removeEdge a b = Graph . Algebraic.removeEdge a b . unGraph
164+
155165
maybeM :: Applicative f => f a -> Maybe a -> f a
156166
maybeM f = maybe f pure
157167

0 commit comments

Comments
 (0)
0