packages feed

hydra-kernel-0.17.6: src/main/haskell/Hydra/Shredding.hs

-- Note: this is an automatically generated file. Do not edit.

-- | Shredding: the total, injective decomposition of a typed graph's bindings into flat, path-addressed links (edges, properties, attributes) — the link view of a graph.

module Hydra.Shredding where

import qualified Hydra.Ast as Ast
import qualified Hydra.Coders as Coders
import qualified Hydra.Core as Core
import qualified Hydra.Docs as Docs
import qualified Hydra.Error.Checking as Checking
import qualified Hydra.Error.Core as ErrorCore
import qualified Hydra.Error.File as ErrorFile
import qualified Hydra.Error.Packaging as ErrorPackaging
import qualified Hydra.Error.System as ErrorSystem
import qualified Hydra.Errors as Errors
import qualified Hydra.File as File
import qualified Hydra.Graph as Graph
import qualified Hydra.Json.Model as Model
import qualified Hydra.Overlay.Haskell.Lib.Eithers as Eithers
import qualified Hydra.Overlay.Haskell.Lib.Lists as Lists
import qualified Hydra.Overlay.Haskell.Lib.Logic as Logic
import qualified Hydra.Overlay.Haskell.Lib.Maps as Maps
import qualified Hydra.Overlay.Haskell.Lib.Optionals as Optionals
import qualified Hydra.Overlay.Haskell.Lib.Pairs as Pairs
import qualified Hydra.Overlay.Haskell.Lib.Sets as Sets
import qualified Hydra.Overlay.Haskell.Lib.Strings as Strings
import qualified Hydra.Packaging as Packaging
import qualified Hydra.Parsing as Parsing
import qualified Hydra.Paths as Paths
import qualified Hydra.Query as Query
import qualified Hydra.Regex as Regex
import qualified Hydra.Relational as Relational
import qualified Hydra.Rewriting as Rewriting
import qualified Hydra.System as System
import qualified Hydra.Tabular as Tabular
import qualified Hydra.Testing as Testing
import qualified Hydra.Time as Time
import qualified Hydra.Topology as Topology
import qualified Hydra.Typed as Typed
import qualified Hydra.Typing as Typing
import qualified Hydra.Util as Util
import qualified Hydra.Validation as Validation
import qualified Hydra.Variants as Variants
import Prelude hiding  (Enum, Ordering, decodeFloat, encodeFloat, fail, lines, map, pure, sum, unlines)
import qualified Data.Scientific as Sci
import Data.Void
import qualified Data.Map as M
import qualified Data.Set as S

-- | The link view of a typed graph: one node per bound term, each with the edge/property/attribute links found by a complete traversal of its term. Input must be typed (binding type schemes present); an untyped binding is a precondition failure, not a degraded mode.
shredGraph :: Graph.Graph -> Either Errors.Error Paths.SubtermGraph
shredGraph graph =

      let boundTerms = Graph.graphBoundTerms graph
          boundTypes = Graph.graphBoundTypes graph
      in (Eithers.map (\nodes -> Paths.SubtermGraph {
        Paths.subtermGraphNodes = nodes}) (Eithers.mapList (\nt ->
        let name = Pairs.first nt
            term = Pairs.second nt
        in (Optionals.match (Maps.lookup name boundTypes) (Left (Errors.ErrorOther (Errors.OtherError (Strings.concat2 "shredGraph: untyped binding: " (Core.unName name))))) (\ts -> shredTerm graph name ts term))) (Maps.toList boundTerms)))

-- | The link view of a schema: one node per named type. A Type.variable referencing a schema-bound name is an edge; a forall-bound variable in scope is a property; leaf types are properties.
shredSchema :: M.Map Core.Name Core.Type -> Either Errors.Error Paths.SubtypeGraph
shredSchema schema =

      let schemaNames =
              Maps.fromList (Lists.map (\nt -> (
                Pairs.first nt,
                Core.TypeScheme {
                  Core.typeSchemeVariables = [],
                  Core.typeSchemeBody = (Pairs.second nt),
                  Core.typeSchemeConstraints = Maps.empty})) (Maps.toList schema))
      in (Eithers.map (\nodes -> Paths.SubtypeGraph {
        Paths.subtypeGraphNodes = nodes}) (Eithers.mapList (\nt -> shredType schemaNames (Pairs.first nt) (Pairs.second nt)) (Maps.toList schema)))

-- | Shred one binding (name, type scheme, term) of a graph into a subterm node
shredTerm :: Graph.Graph -> Core.Name -> Core.TypeScheme -> Core.Term -> Either Errors.Error Paths.SubtermNode
shredTerm graph name ts term =
    Eithers.map (\links -> Paths.SubtermNode {
      Paths.subtermNodeName = name,
      Paths.subtermNodeType = ts,
      Paths.subtermNodeLinks = links}) (shredTermLinks graph Sets.empty (Paths.SubtermPath []) term)

-- | Compute the outgoing links of a term at the given path, given the local scope of names bound by lambda/let steps already taken. Leaf terms yield a property or edge; a variable is classified as an edge (bound in the graph), a property (locally bound or a primitive), or a failure (free).
shredTermLinks :: Graph.Graph -> S.Set Core.Name -> Paths.SubtermPath -> Core.Term -> Either Errors.Error [Paths.SubtermLink]
shredTermLinks graph scope path term =

      let steps = Paths.unSubtermPath path
          attrs = Lists.map (\x -> Paths.SubtermLinkAttribute x) (termAttributes path term)
          childScope =
                  case term of
                    Core.TermLambda v0 -> Sets.insert (Core.lambdaParameter v0) scope
                    Core.TermLet v0 -> Lists.foldl (\acc -> \b -> Sets.insert (Core.bindingName b) acc) scope (Core.letBindings v0)
                    _ -> scope
          leafLinks =
                  case term of
                    Core.TermLiteral _ -> Right [
                      Paths.SubtermLinkProperty (Paths.SubtermProperty {
                        Paths.subtermPropertyPath = path,
                        Paths.subtermPropertyTarget = term})]
                    Core.TermProject _ -> Right [
                      Paths.SubtermLinkProperty (Paths.SubtermProperty {
                        Paths.subtermPropertyPath = path,
                        Paths.subtermPropertyTarget = term})]
                    Core.TermUnit -> Right [
                      Paths.SubtermLinkProperty (Paths.SubtermProperty {
                        Paths.subtermPropertyPath = path,
                        Paths.subtermPropertyTarget = term})]
                    Core.TermUnwrap _ -> Right [
                      Paths.SubtermLinkProperty (Paths.SubtermProperty {
                        Paths.subtermPropertyPath = path,
                        Paths.subtermPropertyTarget = term})]
                    Core.TermVariable v0 -> Logic.ifElse (Sets.member v0 scope) (Right [
                      Paths.SubtermLinkProperty (Paths.SubtermProperty {
                        Paths.subtermPropertyPath = path,
                        Paths.subtermPropertyTarget = term})]) (Logic.ifElse (Maps.member v0 (Graph.graphBoundTerms graph)) (Right [
                      Paths.SubtermLinkEdge (Paths.SubtermEdge {
                        Paths.subtermEdgePath = path,
                        Paths.subtermEdgeTarget = v0})]) (Logic.ifElse (Maps.member v0 (Graph.graphPrimitives graph)) (Right [
                      Paths.SubtermLinkProperty (Paths.SubtermProperty {
                        Paths.subtermPropertyPath = path,
                        Paths.subtermPropertyTarget = term})]) (Left (Errors.ErrorOther (Errors.OtherError (Strings.concat2 "free variable in shredded term: " (Core.unName v0)))))))
                    _ -> Right []
      in (Eithers.bind (Eithers.mapList (\st ->
        let step = Pairs.first st
            child = Pairs.second st
            childPath = Paths.SubtermPath (Lists.concat2 steps [
                  step])
        in (shredTermLinks graph childScope childPath child)) (Rewriting.subtermsWithSteps term)) (\childLinkLists -> Eithers.bind leafLinks (\leaf -> Right (Lists.concat2 attrs (Lists.concat2 leaf (Lists.concat childLinkLists))))))

-- | Shred one named type of a schema into a subtype node
shredType :: M.Map Core.Name t0 -> Core.Name -> Core.Type -> Either Errors.Error Paths.SubtypeNode
shredType schema name typ =
    Eithers.map (\links -> Paths.SubtypeNode {
      Paths.subtypeNodeName = name,
      Paths.subtypeNodeLinks = links}) (shredTypeLinks schema Sets.empty (Paths.SubtypePath []) typ)

-- | Compute the outgoing links of a type at the given path, given the scope of forall-bound variables. A leaf type yields a property; a variable is an edge (a named type in the schema), a property (forall-bound in scope), or a failure (free).
shredTypeLinks :: M.Map Core.Name t0 -> S.Set Core.Name -> Paths.SubtypePath -> Core.Type -> Either Errors.Error [Paths.SubtypeLink]
shredTypeLinks schema scope path typ =

      let steps = Paths.unSubtypePath path
          attrs = Lists.map (\x -> Paths.SubtypeLinkAttribute x) (typeAttributes path typ)
          childScope =
                  case typ of
                    Core.TypeForall v0 -> Sets.insert (Core.forallTypeParameter v0) scope
                    _ -> scope
          leafLinks =
                  case typ of
                    Core.TypeLiteral _ -> Right [
                      Paths.SubtypeLinkProperty (Paths.SubtypeProperty {
                        Paths.subtypePropertyPath = path,
                        Paths.subtypePropertyTarget = typ})]
                    Core.TypeUnit -> Right [
                      Paths.SubtypeLinkProperty (Paths.SubtypeProperty {
                        Paths.subtypePropertyPath = path,
                        Paths.subtypePropertyTarget = typ})]
                    Core.TypeVoid -> Right [
                      Paths.SubtypeLinkProperty (Paths.SubtypeProperty {
                        Paths.subtypePropertyPath = path,
                        Paths.subtypePropertyTarget = typ})]
                    Core.TypeVariable v0 -> Logic.ifElse (Sets.member v0 scope) (Right [
                      Paths.SubtypeLinkProperty (Paths.SubtypeProperty {
                        Paths.subtypePropertyPath = path,
                        Paths.subtypePropertyTarget = typ})]) (Logic.ifElse (Maps.member v0 schema) (Right [
                      Paths.SubtypeLinkEdge (Paths.SubtypeEdge {
                        Paths.subtypeEdgePath = path,
                        Paths.subtypeEdgeTarget = v0})]) (Left (Errors.ErrorOther (Errors.OtherError (Strings.concat2 "free type variable in shredded type: " (Core.unName v0))))))
                    _ -> Right []
      in (Eithers.bind (Eithers.mapList (\st ->
        let step = Pairs.first st
            child = Pairs.second st
            childPath = Paths.SubtypePath (Lists.concat2 steps [
                  step])
        in (shredTypeLinks schema childScope childPath child)) (Rewriting.subtypesWithSteps typ)) (\childLinkLists -> Eithers.bind leafLinks (\leaf -> Right (Lists.concat2 attrs (Lists.concat2 leaf (Lists.concat childLinkLists))))))

-- | The attribute links contributed by a term constructor at the given path
termAttributes :: Paths.SubtermPath -> Core.Term -> [Paths.SubtermAttribute]
termAttributes path term =

      let attr =
              \a -> Paths.SubtermAttribute {
                Paths.subtermAttributePath = path,
                Paths.subtermAttributeTarget = a}
          one = \a -> [
                attr a]
      in case term of
        Core.TermCases v0 -> one (Paths.TermAttributeCasesTypeName (Core.caseStatementTypeName v0))
        Core.TermInject v0 -> one (Paths.TermAttributeInjectTypeName (Core.injectionTypeName v0))
        Core.TermLambda v0 -> Lists.concat2 [
          attr (Paths.TermAttributeLambdaParameter (Core.lambdaParameter v0))] (Optionals.match (Core.lambdaDomain v0) [] (\d -> [
          attr (Paths.TermAttributeLambdaDomainGiven d)]))
        Core.TermProject v0 -> [
          attr (Paths.TermAttributeProjectTypeName (Core.projectionTypeName v0)),
          (attr (Paths.TermAttributeProjectFieldName (Core.projectionFieldName v0)))]
        Core.TermRecord v0 -> one (Paths.TermAttributeRecordTypeName (Core.recordTypeName v0))
        Core.TermTypeApplication v0 -> one (Paths.TermAttributeTypeApplicationType (Core.typeApplicationTermType v0))
        Core.TermTypeLambda v0 -> one (Paths.TermAttributeTypeLambdaParameter (Core.typeLambdaParameter v0))
        Core.TermWrap v0 -> one (Paths.TermAttributeWrapTypeName (Core.wrappedTermTypeName v0))
        _ -> []

-- | The attribute links contributed by a type constructor at the given path
typeAttributes :: Paths.SubtypePath -> Core.Type -> [Paths.SubtypeAttribute]
typeAttributes path typ =

      let attr =
              \a -> Paths.SubtypeAttribute {
                Paths.subtypeAttributePath = path,
                Paths.subtypeAttributeTarget = a}
      in case typ of
        Core.TypeAnnotated v0 -> [
          attr (Paths.TypeAttributeAnnotatedAnnotation (Core.annotatedTypeAnnotation v0))]
        Core.TypeForall v0 -> [
          attr (Paths.TypeAttributeForallParameter (Core.forallTypeParameter v0))]
        _ -> []