packages feed

hydra-kernel-0.18.1: src/main/haskell/Hydra/Core/Validate/Paths.hs

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

-- | The TermGraph : TypeGraph conformance relation: path erasure (a subterm position maps to the subtype position of its type) plus node typing, partial on the computation fragment.

module Hydra.Core.Validate.Paths where

import qualified Hydra.Core.Overlay.Haskell.Lib.Lists as Lists
import qualified Hydra.Core.Overlay.Haskell.Lib.Logic as Logic
import qualified Hydra.Core.Overlay.Haskell.Lib.Optionals as Optionals
import qualified Hydra.Core.Overlay.Haskell.Lib.Pairs as Pairs
import qualified Hydra.Core.Paths as Paths
import Prelude hiding  (Enum, Ordering, decodeFloat, encodeFloat, fail, lines, map, pure, sum, unlines)
import qualified Data.Scientific as Sci
import Data.Void

-- | Erase a subterm path to the subtype path of its type, if wholly within the data fragment
eraseSubtermPath :: Paths.SubtermPath -> Maybe Paths.SubtypePath
eraseSubtermPath path =

      let steps = Paths.unSubtermPath path
          go =
                  \acc -> \step -> Optionals.match acc Nothing (\stateP ->
                    let out = Pairs.first stateP
                        pendingEntry = Pairs.second stateP
                    in (Logic.ifElse pendingEntry (case step of
                      Paths.SubtermStepPairFirst -> Just (Lists.concat2 out [
                        Paths.SubtypeStepMapKeys], False)
                      Paths.SubtermStepPairSecond -> Just (Lists.concat2 out [
                        Paths.SubtypeStepMapValues], False)
                      _ -> Nothing) (case step of
                      Paths.SubtermStepMapEntry _ -> Just (out, True)
                      _ -> Optionals.map (\st -> (Lists.concat2 out [
                        st], False)) (eraseSubtermStep step))))
          result = Lists.foldl go (Just ([], False)) steps
      in (Optionals.bind result (\stateP -> Logic.ifElse (Pairs.second stateP) Nothing (Just (Paths.SubtypePath (Pairs.first stateP)))))

-- | The subtype-side analog of a subterm step on the data fragment, or nothing on the computation fragment
eraseSubtermStep :: Paths.SubtermStep -> Maybe Paths.SubtypeStep
eraseSubtermStep step =
    case step of
      Paths.SubtermStepListElement _ -> Just Paths.SubtypeStepListElement
      Paths.SubtermStepSetElement _ -> Just Paths.SubtypeStepSetElement
      Paths.SubtermStepOptionalGiven -> Just Paths.SubtypeStepOptionalElement
      Paths.SubtermStepInjectField v0 -> Just (Paths.SubtypeStepUnionField v0)
      Paths.SubtermStepRecordField v0 -> Just (Paths.SubtypeStepRecordField v0)
      _ -> Nothing