packages feed

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

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

-- | A utility which instantiates a nonrecursive type with default values

module Hydra.Core.Templates where

import qualified Hydra.Core.Decode.Model as DecodeModel
import qualified Hydra.Core.Errors as Errors
import qualified Hydra.Core.Graph as Graph
import qualified Hydra.Core.Overlay.Haskell.Lib.Eithers as Eithers
import qualified Hydra.Core.Overlay.Haskell.Lib.Logic as Logic
import qualified Hydra.Core.Overlay.Haskell.Lib.Maps as Maps
import qualified Hydra.Core.Overlay.Haskell.Lib.Optionals as Optionals
import qualified Hydra.Core.Overlay.Haskell.Lib.Sets as Sets
import qualified Hydra.Core.Overlay.Haskell.Lib.Strings as Strings
import qualified Hydra.Core.Model as Model
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 Hydra.Core.Overlay.Haskell.Lib.Literals as Literals

-- | Decode a list of type-encoding bindings into a map of named types
graphToSchema :: t0 -> Graph.Graph -> [Model.Binding] -> Either Errors.DecodingError (M.Map Model.Name Model.Type)
graphToSchema cx graph els =

      let toPair =
              \el ->
                let name = Model.bindingName el
                in (Eithers.bind (DecodeModel.type_ graph (Model.bindingTerm el)) (\t -> Right (name, t)))
      in (Eithers.bind (Eithers.mapList toPair els) (\pairs -> Right (Maps.fromList pairs)))

-- | Given a graph schema and a nonrecursive type, instantiate it with default values. If the minimal flag is set, the smallest possible term is produced; otherwise, exactly one subterm is produced for constructors which do not otherwise require one, e.g. in lists and optionals. The name parameter provides the element name for nominal type construction.
instantiateTemplate :: t0 -> Bool -> M.Map Model.Name Model.Type -> Model.Name -> Model.Type -> Either Errors.Error Model.Term
instantiateTemplate cx minimal schema tname t =

      let inst = \tn -> instantiateTemplate cx minimal schema tn
          noPoly =
                  Left (Errors.ErrorExtraction (Errors.ExtractionErrorUnexpectedShape (Errors.UnexpectedShapeError {
                    Errors.unexpectedShapeErrorExpected = "non-polymorphic type",
                    Errors.unexpectedShapeErrorActual = "polymorphic or function type"})))
          forFloat =
                  \ft -> case ft of
                    Model.FloatTypeFloat32 -> Model.FloatValueFloat32 0.0
                    Model.FloatTypeFloat64 -> Model.FloatValueFloat64 0.0
          forInteger =
                  \it -> case it of
                    Model.IntegerTypeBigint -> Model.IntegerValueBigint 0
                    Model.IntegerTypeInt8 -> Model.IntegerValueInt8 0
                    Model.IntegerTypeInt16 -> Model.IntegerValueInt16 0
                    Model.IntegerTypeInt32 -> Model.IntegerValueInt32 0
                    Model.IntegerTypeInt64 -> Model.IntegerValueInt64 0
                    Model.IntegerTypeUint8 -> Model.IntegerValueUint8 0
                    Model.IntegerTypeUint16 -> Model.IntegerValueUint16 0
                    Model.IntegerTypeUint32 -> Model.IntegerValueUint32 0
                    Model.IntegerTypeUint64 -> Model.IntegerValueUint64 0
          forLiteral =
                  \lt -> case lt of
                    Model.LiteralTypeBinary -> Model.LiteralString ""
                    Model.LiteralTypeBoolean -> Model.LiteralBoolean False
                    Model.LiteralTypeDecimal -> Model.LiteralDecimal (Literals.stringToDecimal "0")
                    Model.LiteralTypeInteger v0 -> Model.LiteralInteger (forInteger v0)
                    Model.LiteralTypeFloat v0 -> Model.LiteralFloat (forFloat v0)
                    Model.LiteralTypeString -> Model.LiteralString ""
      in case t of
        Model.TypeAnnotated v0 -> inst tname (Model.annotatedTypeBody v0)
        Model.TypeApplication _ -> noPoly
        Model.TypeFunction _ -> noPoly
        Model.TypeForall _ -> noPoly
        Model.TypeList v0 -> Logic.ifElse minimal (Right (Model.TermList [])) (Eithers.bind (inst tname v0) (\e -> Right (Model.TermList [
          e])))
        Model.TypeLiteral v0 -> Right (Model.TermLiteral (forLiteral v0))
        Model.TypeMap v0 ->
          let kt = Model.mapTypeKeys v0
              vt = Model.mapTypeValues v0
          in (Logic.ifElse minimal (Right (Model.TermMap Maps.empty)) (Eithers.bind (inst tname kt) (\ke -> Eithers.bind (inst tname vt) (\ve -> Right (Model.TermMap (Maps.singleton ke ve))))))
        Model.TypeOptional v0 -> Logic.ifElse minimal (Right (Model.TermOptional Nothing)) (Eithers.bind (inst tname v0) (\e -> Right (Model.TermOptional (Just e))))
        Model.TypeRecord v0 ->
          let toField =
                  \ft -> Eithers.bind (inst tname (Model.fieldTypeType ft)) (\e -> Right (Model.Field {
                    Model.fieldName = (Model.fieldTypeName ft),
                    Model.fieldTerm = e}))
          in (Eithers.bind (Eithers.mapList toField v0) (\dfields -> Right (Model.TermRecord (Model.Record {
            Model.recordTypeName = tname,
            Model.recordFields = dfields}))))
        Model.TypeSet v0 -> Logic.ifElse minimal (Right (Model.TermSet Sets.empty)) (Eithers.bind (inst tname v0) (\e -> Right (Model.TermSet (Sets.fromList [
          e]))))
        Model.TypeVariable v0 -> Optionals.match (Maps.lookup v0 schema) (Left (Errors.ErrorResolution (Errors.ResolutionErrorUnexpectedShape (Errors.UnexpectedShapeError {
          Errors.unexpectedShapeErrorExpected = "bound type variable",
          Errors.unexpectedShapeErrorActual = (Strings.concat2 "unbound variable " (Model.unName v0))})))) (inst v0)
        Model.TypeWrap v0 -> Eithers.bind (inst tname v0) (\e -> Right (Model.TermWrap (Model.WrappedTerm {
          Model.wrappedTermTypeName = tname,
          Model.wrappedTermBody = e})))