Skip to content

Commit

Permalink
Added Dhall.checkWithSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
mmhat committed Oct 25, 2023
1 parent df8ff09 commit 3e4ef4a
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 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 Down Expand Up @@ -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
Expand Down

0 comments on commit 3e4ef4a

Please sign in to comment.