diff options
Diffstat (limited to 'source/StructGraph.hs')
| -rw-r--r-- | source/StructGraph.hs | 125 |
1 files changed, 0 insertions, 125 deletions
diff --git a/source/StructGraph.hs b/source/StructGraph.hs deleted file mode 100644 index eb36714..0000000 --- a/source/StructGraph.hs +++ /dev/null @@ -1,125 +0,0 @@ -{-# LANGUAGE ImportQualifiedPost #-} -{-# LANGUAGE NoImplicitPrelude #-} -{-# LANGUAGE RecordWildCards #-} -{-# LANGUAGE ScopedTypeVariables #-} - - -module StructGraph - ( Struct - , structNoun - , structAncestors - , structInternalSymbols - , structSymbols - , StructGraph - , lookup - , lookupAncestors - , lookupInternalSymbols - , lookupSymbols - , isInternalSymbolIn - , isSymbolIn - , insert - , StructGraphDelta - , structGraphExtension - , applyStructGraphDelta - ) where - - -import Base -import Syntax.Internal - -import Data.Map.Strict qualified as Map -import Data.Set qualified as Set - - -data Struct = Struct - { structNoun :: StructPhrase - , structAncestors :: Set StructPhrase -- ^ All ancestors, including transitive ancestors. - , structInternalSymbols :: Set StructSymbol -- ^ Signature. - , structSymbols :: Set StructSymbol -- ^ Signature, including inherited symbols. - } deriving (Show, Eq, Ord) - -newtype StructGraph - = StructGraph {unStructGraph :: Map StructPhrase Struct} - deriving (Show, Eq, Semigroup, Monoid) - - --- | Lookup a struct by its name. -lookup :: StructPhrase -> StructGraph -> Maybe Struct -lookup str graph = Map.lookup str (unStructGraph graph) - --- | Returns the ancestors of the given StructPhrase in the graph. -lookupAncestors :: StructPhrase -> StructGraph -> Maybe (Set StructPhrase) -lookupAncestors str graph = - structAncestors <$> lookup str graph - -lookupInternalSymbols :: StructPhrase -> StructGraph -> Maybe (Set StructSymbol) -lookupInternalSymbols phrase graph = - structInternalSymbols <$> lookup phrase graph - --- | Lookup all symbols of a structure, including inherited symbols. -lookupSymbols :: StructPhrase -> StructGraph -> Maybe (Set StructSymbol) -lookupSymbols phrase graph = - structSymbols <$> lookup phrase graph - -isInternalSymbolIn :: StructSymbol -> Struct -> Bool -isInternalSymbolIn tok struct = Set.member tok (structInternalSymbols struct) - -isSymbolIn :: StructSymbol -> Struct -> Bool -isSymbolIn tok struct = Set.member tok (structSymbols struct) - --- | Insert a new struct into the graph. -insert - :: StructPhrase - -> Set StructPhrase - -> Set StructSymbol - -> Set StructSymbol - -> StructGraph - -> StructGraph -insert structNoun structAncestors structInternalSymbols structSymbols graph = - StructGraph (Map.insert structNoun Struct{..} (unStructGraph graph)) - - -newtype StructGraphDelta = StructGraphDelta - (Map StructPhrase Struct) - deriving (Show, Eq) - -structGraphExtension - :: StructGraph - -> StructGraph - -> Either Text StructGraphDelta -structGraphExtension previous current - | not - (all - (\(phrase, structure) -> - Map.lookup phrase currentRows - == Just structure) - (Map.toList previousRows)) = - Left "structure graph changed an imported structure" - | otherwise = - Right - (StructGraphDelta - (Map.difference currentRows previousRows)) - where - previousRows = - unStructGraph previous - currentRows = - unStructGraph current - -applyStructGraphDelta - :: StructGraphDelta - -> StructGraph - -> Either Text StructGraph -applyStructGraphDelta - (StructGraphDelta additions) - graph - | not - (Set.null - (Map.keysSet rows - `Set.intersection` Map.keysSet additions)) = - Left "imported modules define the same structure" - | otherwise = - Right - (StructGraph (Map.union rows additions)) - where - rows = - unStructGraph graph |
