Skip to content

Commit

Permalink
Remove type output to stderr for default command (#544)
Browse files Browse the repository at this point in the history
Fixes #539

The default `dhall` command no longer outputs types to `stderr`.
Instead, the user can opt into a type annotation using a newly added
`--annotation` switch.
  • Loading branch information
Gabriella439 committed Aug 12, 2018
1 parent 5ac9984 commit a5b713f
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/Dhall/Main.hs
Expand Up @@ -20,12 +20,12 @@ module Dhall.Main

import Control.Applicative (optional, (<|>))
import Control.Exception (Exception, SomeException)
import Data.Monoid (mempty, (<>))
import Data.Monoid ((<>))
import Data.Text (Text)
import Data.Text.Prettyprint.Doc (Pretty)
import Data.Version (showVersion)
import Dhall.Binary (ProtocolVersion)
import Dhall.Core (Expr, Import)
import Dhall.Core (Expr(..), Import)
import Dhall.Import (Imported(..))
import Dhall.Parser (Src)
import Dhall.Pretty (annToAnsiStyle, prettyExpr, layoutOpts)
Expand Down Expand Up @@ -70,7 +70,7 @@ data Options = Options

-- | The subcommands for the @dhall@ executable
data Mode
= Default
= Default { annotate :: Bool }
| Version
| Resolve
| Type
Expand Down Expand Up @@ -124,7 +124,7 @@ parseMode =
<|> subcommand "lint" "Improve Dhall code" parseLint
<|> formatSubcommand
<|> freezeSubcommand
<|> pure Default
<|> parseDefault
where
subcommand name description modeParser =
Options.Applicative.subparser
Expand Down Expand Up @@ -169,6 +169,13 @@ parseMode =
where
parseFreeze = Freeze <$> optional parseInplace

parseDefault = Default <$> parseAnnotate
where
parseAnnotate =
Options.Applicative.switch
( Options.Applicative.long "annotate"
)

data ImportResolutionDisabled = ImportResolutionDisabled deriving (Exception)

instance Show ImportResolutionDisabled where
Expand Down Expand Up @@ -253,18 +260,21 @@ command (Options {..}) = do
Version -> do
putStrLn (showVersion Meta.version)

Default -> do
Default {..} -> do
expression <- getExpression

resolvedExpression <- State.evalStateT (Dhall.Import.loadWith expression) status

inferredType <- throws (Dhall.TypeCheck.typeOf resolvedExpression)

render System.IO.stderr (Dhall.Core.normalize inferredType)
let normalizedExpression = Dhall.Core.normalize resolvedExpression

Data.Text.IO.hPutStrLn System.IO.stderr mempty
let annotatedExpression =
if annotate
then Annot normalizedExpression inferredType
else normalizedExpression

render System.IO.stdout (Dhall.Core.normalize resolvedExpression)
render System.IO.stdout annotatedExpression

Resolve -> do
expression <- getExpression
Expand Down

0 comments on commit a5b713f

Please sign in to comment.