From 3e4ef4a739a73a0104dd3ddeea37cf22d38594ea Mon Sep 17 00:00:00 2001 From: Mann mit Hut Date: Wed, 25 Oct 2023 13:55:55 +0200 Subject: [PATCH 1/2] Added Dhall.checkWithSettings --- dhall/src/Dhall.hs | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/dhall/src/Dhall.hs b/dhall/src/Dhall.hs index 310472647..e427de214 100644 --- a/dhall/src/Dhall.hs +++ b/dhall/src/Dhall.hs @@ -51,6 +51,7 @@ module Dhall , parseWithSettings , resolveWithSettings , typecheckWithSettings + , checkWithSettings , expectWithSettings , normalizeWithSettings @@ -218,29 +219,45 @@ typecheckWithSettings settings expression = do 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 :: + -- | The input settings + InputSettings -> + -- | The expected type of the expression + Expr Src Void -> + -- | The expression to check + Expr Src Void -> + IO () +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 :: 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 + + checkWithSettings settings expected' expression + {-| Resolve an expression, using the supplied `InputSettings` Note that this also applies any substitutions specified in the From 9735b7aff9e9d9c690a20abf55ab95d439a131d3 Mon Sep 17 00:00:00 2001 From: Mann mit Hut Date: Wed, 25 Oct 2023 14:25:22 +0200 Subject: [PATCH 2/2] Allow running some of the individual stages in pure code --- dhall/src/Dhall.hs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/dhall/src/Dhall.hs b/dhall/src/Dhall.hs index e427de214..ca4d6a4b0 100644 --- a/dhall/src/Dhall.hs +++ b/dhall/src/Dhall.hs @@ -60,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 (..)) @@ -208,28 +209,27 @@ 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) - - return () +typecheckWithSettings :: MonadThrow m => InputSettings -> Expr Src Void -> m () +typecheckWithSettings settings expression = + either throwM (return . const ()) (Dhall.TypeCheck.typeWith (view startingContext settings) expression) {-| Type-check an expression against a type provided as a Dhall expreession, using the supplied `InputSettings` -} checkWithSettings :: + MonadThrow m => -- | The input settings InputSettings -> -- | The expected type of the expression Expr Src Void -> -- | The expression to check Expr Src Void -> - IO () + m () checkWithSettings settings type_ expression = do let suffix = Dhall.Pretty.Internal.prettyToStrictText type_ @@ -243,18 +243,16 @@ checkWithSettings settings type_ expression = do 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 :: InputSettings -> Decoder a -> Expr Src Void -> IO () +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 -> Control.Exception.throwIO e + Failure e -> throwM e checkWithSettings settings expected' expression