Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polymorphic entrypoints #2544

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 35 additions & 20 deletions dhall/src/Dhall.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module Dhall
, parseWithSettings
, resolveWithSettings
, typecheckWithSettings
, checkWithSettings
, expectWithSettings
, normalizeWithSettings

Expand All @@ -59,6 +60,7 @@ module Dhall
) where

import Control.Applicative (Alternative, empty)
import Control.Monad.Catch (MonadThrow, throwM)
import Data.Either.Validation (Validation (..))
import Data.Void (Void)
import Dhall.Import (Imported (..))
Expand Down Expand Up @@ -207,39 +209,52 @@ instance HasEvaluateSettings EvaluateSettings where
evaluateSettings = id

-- | Parse an expression, using the supplied `InputSettings`
parseWithSettings :: InputSettings -> Text -> IO (Expr Src Import)
parseWithSettings settings text = do
Core.throws (Dhall.Parser.exprFromText (view sourceName settings) text)
parseWithSettings :: MonadThrow m => InputSettings -> Text -> m (Expr Src Import)
parseWithSettings settings text =
either throwM return (Dhall.Parser.exprFromText (view sourceName settings) text)

-- | Type-check an expression, using the supplied `InputSettings`
typecheckWithSettings :: InputSettings -> Expr Src Void -> IO ()
typecheckWithSettings settings expression = do
_ <- Core.throws (Dhall.TypeCheck.typeWith (view startingContext settings) expression)
typecheckWithSettings :: MonadThrow m => InputSettings -> Expr Src Void -> m ()
typecheckWithSettings settings expression =
either throwM (return . const ()) (Dhall.TypeCheck.typeWith (view startingContext settings) expression)

return ()

{-| Type-check an expression against a `Decoder`'s expected type, using the
supplied `InputSettings`
{-| Type-check an expression against a type provided as a Dhall expreession,
using the supplied `InputSettings`
-}
expectWithSettings :: InputSettings -> Decoder a -> Expr Src Void -> IO ()
expectWithSettings settings Decoder{..} expression = do
expected' <- case expected of
Success x -> return x
Failure e -> Control.Exception.throwIO e

let suffix = Dhall.Pretty.Internal.prettyToStrictText expected'
checkWithSettings ::
MonadThrow m =>
-- | The input settings
InputSettings ->
-- | The expected type of the expression
Expr Src Void ->
-- | The expression to check
Expr Src Void ->
m ()
checkWithSettings settings type_ expression = do
let suffix = Dhall.Pretty.Internal.prettyToStrictText type_

let annotated = case expression of
Note (Src begin end bytes) _ ->
Note (Src begin end bytes') (Annot expression expected')
Note (Src begin end bytes') (Annot expression type_)
where
bytes' = bytes <> " : " <> suffix
_ ->
Annot expression expected'
Annot expression type_

typecheckWithSettings settings annotated

return ()
{-| Type-check an expression against a `Decoder`'s expected type, using the
supplied `InputSettings`.
This is equivalent of using the 'expected' type of a @Decoder@ as the second
argument to 'checkWithSettings'.
-}
expectWithSettings :: MonadThrow m => InputSettings -> Decoder a -> Expr Src Void -> m ()
expectWithSettings settings Decoder{..} expression = do
expected' <- case expected of
Success x -> return x
Failure e -> throwM e

checkWithSettings settings expected' expression

{-| Resolve an expression, using the supplied `InputSettings`

Expand Down
Loading