Skip to content

Commit dfa7dd0

Browse files
treefmt
1 parent 405dd31 commit dfa7dd0

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

src/Hell.hs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
{-# LANGUAGE BangPatterns #-}
21
{-# LANGUAGE AllowAmbiguousTypes #-}
2+
{-# LANGUAGE BangPatterns #-}
33
{-# LANGUAGE BlockArguments #-}
44
{-# LANGUAGE CPP #-}
55
{-# LANGUAGE DataKinds #-}
@@ -50,12 +50,12 @@ import Control.Monad
5050
-- e.g. 'Data.Graph' becomes 'Graph', and are then exposed to the Hell
5151
-- guest language as such.
5252

53-
import Criterion.Measurement
5453
import Control.Applicative (Alternative (..), optional)
5554
import qualified Control.Concurrent as Concurrent
5655
import Control.Exception (evaluate)
5756
import Control.Monad.Reader
5857
import Control.Monad.State.Strict
58+
import Criterion.Measurement
5959
import Data.Aeson (Value)
6060
import qualified Data.Aeson as Json
6161
import qualified Data.Aeson.KeyMap as KeyMap
@@ -156,8 +156,9 @@ commandParser :: Options.Parser Command
156156
commandParser =
157157
Options.asum
158158
[ Run <$> Options.strArgument (Options.metavar "FILE" <> Options.help "Run the given .hell file"),
159-
Check <$> Options.strOption (Options.long "check" <> Options.metavar "FILE" <> Options.help "Typecheck the given .hell file") <*>
160-
Options.flag NoStats (PrintStats 0) (Options.long "compiler-stats" <> Options.internal),
159+
Check
160+
<$> Options.strOption (Options.long "check" <> Options.metavar "FILE" <> Options.help "Typecheck the given .hell file")
161+
<*> Options.flag NoStats (PrintStats 0) (Options.long "compiler-stats" <> Options.internal),
161162
Version <$ Options.flag () () (Options.long "version" <> Options.help "Print the version")
162163
]
163164

@@ -184,34 +185,34 @@ compileFile stats filePath = do
184185
t0 <- getTime
185186
!result <- parseFile (nestStat stats) filePath
186187
t1 <- getTime
187-
emitStat stats "parse" (t1-t0)
188+
emitStat stats "parse" (t1 - t0)
188189
case result of
189190
Left e -> error $ e
190191
Right File {terms, types}
191192
| anyCycles terms -> error "Cyclic bindings are not supported!"
192193
| anyCycles types -> error "Cyclic types are not supported!"
193194
| otherwise -> do
194195
t2 <- getTime
195-
emitStat stats "cycle_detect" (t2-t1)
196+
emitStat stats "cycle_detect" (t2 - t1)
196197
case desugarAll types terms of
197198
Left err -> error $ prettyString err
198199
Right !dterms -> do
199200
t3 <- getTime
200-
emitStat stats "desugar" (t3-t2)
201+
emitStat stats "desugar" (t3 - t2)
201202
case lookup "main" dterms of
202203
Nothing -> error "No main declaration!"
203204
Just main' -> do
204-
inferred <- inferExp (nestStat stats) mempty main'
205+
inferred <- inferExp (nestStat stats) main'
205206
case inferred of
206207
Left err -> error $ prettyString err
207208
Right uterm -> do
208209
t4 <- getTime
209-
emitStat stats "infer" (t4-t3)
210+
emitStat stats "infer" (t4 - t3)
210211
case check uterm Nil of
211212
Left err -> error $ prettyString err
212213
Right (Typed t ex) -> do
213214
t5 <- getTime
214-
emitStat stats "check" (t5-t4)
215+
emitStat stats "check" (t5 - t4)
215216
case Type.eqTypeRep (typeRepKind t) (typeRep @Type) of
216217
Nothing -> error $ "Kind error, that's nowhere near an IO ()!"
217218
Just Type.HRefl ->
@@ -223,11 +224,11 @@ compileFile stats filePath = do
223224
emitStat :: StatsEnabled -> Text -> Double -> IO ()
224225
emitStat NoStats _ _ = pure ()
225226
emitStat (PrintStats n0) label s =
226-
t_putStrLn $ Text.replicate (n0*2) " " <> "stat: " <> label <> " = " <> Text.pack (secs s)
227+
t_putStrLn $ Text.replicate (n0 * 2) " " <> "stat: " <> label <> " = " <> Text.pack (secs s)
227228

228229
nestStat :: StatsEnabled -> StatsEnabled
229230
nestStat NoStats = NoStats
230-
nestStat (PrintStats n) = PrintStats (n+1)
231+
nestStat (PrintStats n) = PrintStats (n + 1)
231232

232233
--------------------------------------------------------------------------------
233234
-- Get declarations from the module
@@ -773,25 +774,28 @@ withClassConstraint forallLoc reps rep crep f go =
773774
| Type.App t _ <- rep,
774775
Just Type.HRefl <- Type.eqTypeRep (typeRepKind t) (TypeRep @(Type -> Type)),
775776
Just dict <- resolve1 (Type.App crep rep) crep t instances ->
776-
go reps (withDict dict f)
777+
go reps (withDict dict f)
777778
-- Cases that look like: Monad (Either (e :: *) (a :: *))
778779
-- Note: the kinds are limited to this exact specification in the signature above.
779780
| Type.App t _ <- rep,
780781
Just Type.HRefl <- Type.eqTypeRep (typeRepKind t) (TypeRep @(Type -> Type -> Type)),
781782
Just dict <- resolve1 (Type.App crep rep) crep t instances ->
782-
go reps (withDict dict f)
783+
go reps (withDict dict f)
783784
-- Cases that look like: Semigroup (Mod (f :: * -> *) (a :: *))
784785
-- Note: the kinds are limited to this exact specification in the signature above.
785786
| Type.App (Type.App t _a) _b <- rep,
786787
Just Type.HRefl <- Type.eqTypeRep (typeRepKind t) (TypeRep @((Type -> Type) -> Type -> Type)),
787788
Just dict <- resolve2 (Type.App crep rep) crep t instances ->
788-
go reps (withDict dict f)
789+
go reps (withDict dict f)
789790
-- Simple cases: Eq (a :: k)
790791
| Just dict <- resolve crep rep instances ->
791-
go reps (withDict dict f)
792+
go reps (withDict dict f)
792793
| otherwise ->
793-
problem $ "type " ++ show rep ++
794-
" doesn't appear to be an instance of " ++ show crep
794+
problem $
795+
"type "
796+
++ show rep
797+
++ " doesn't appear to be an instance of "
798+
++ show crep
795799
where
796800
problem :: forall x. String -> Either TypeCheckError x
797801
problem = Left . ConstraintResolutionProblem forallLoc (ClassConstraint rep crep f)
@@ -1365,26 +1369,25 @@ data InferError
13651369
-- determinate types.
13661370
inferExp ::
13671371
StatsEnabled ->
1368-
Map String (UTerm SomeTypeRep) ->
13691372
UTerm () ->
13701373
IO (Either InferError (UTerm SomeTypeRep))
1371-
inferExp stats _ uterm = do
1374+
inferExp stats uterm = do
13721375
t0 <- getTime
13731376
case elaborate uterm of
13741377
Left elabError -> pure $ Left $ ElabError elabError
13751378
Right (iterm, equalities) -> do
13761379
t1 <- getTime
1377-
emitStat stats "elaborate" (t1-t0)
1380+
emitStat stats "elaborate" (t1 - t0)
13781381
case unify equalities of
13791382
Left unifyError -> pure $ Left $ UnifyError unifyError
13801383
Right subs -> do
13811384
t2 <- getTime
1382-
emitStat stats "unify" (t2-t1)
1385+
emitStat stats "unify" (t2 - t1)
13831386
case traverse (zonkToStarType subs) iterm of
13841387
Left zonkError -> pure $ Left $ ZonkError $ zonkError
13851388
Right !sterm -> do
13861389
t3 <- getTime
1387-
emitStat stats "zonk" (t3-t2)
1390+
emitStat stats "zonk" (t3 - t2)
13881391
pure $ Right sterm
13891392

13901393
-- | Zonk a type and then convert it to a type: t :: *
@@ -2509,25 +2512,31 @@ data File = File
25092512
{ terms :: [(String, HSE.Exp HSE.SrcSpanInfo)],
25102513
types :: [(String, HSE.Type HSE.SrcSpanInfo)]
25112514
}
2515+
deriving (Eq, Show)
25122516

25132517
-- Parse a file into a list of decls, but strip shebangs.
25142518
parseFile :: StatsEnabled -> String -> IO (Either String File)
25152519
parseFile stats filePath = do
25162520
t0 <- getTime
25172521
string <- ByteString.readFile filePath
25182522
t1 <- getTime
2519-
emitStat stats "read_file" (t1-t0)
2520-
case HSE.parseModuleWithMode HSE.defaultParseMode {HSE.parseFilename = filePath, HSE.extensions = HSE.extensions HSE.defaultParseMode ++ [HSE.EnableExtension HSE.PatternSignatures, HSE.EnableExtension HSE.DataKinds, HSE.EnableExtension HSE.BlockArguments, HSE.EnableExtension HSE.TypeApplications, HSE.EnableExtension HSE.NamedFieldPuns]} (Text.unpack (dropShebang (Text.decodeUtf8 string))) of
2523+
emitStat stats "read_file" (t1 - t0)
2524+
parseText stats filePath $ Text.decodeUtf8 string
2525+
2526+
parseText :: StatsEnabled -> FilePath -> Text -> IO (Either String File)
2527+
parseText stats filePath text = do
2528+
t1 <- getTime
2529+
case HSE.parseModuleWithMode HSE.defaultParseMode {HSE.parseFilename = filePath, HSE.extensions = HSE.extensions HSE.defaultParseMode ++ [HSE.EnableExtension HSE.PatternSignatures, HSE.EnableExtension HSE.DataKinds, HSE.EnableExtension HSE.BlockArguments, HSE.EnableExtension HSE.TypeApplications, HSE.EnableExtension HSE.NamedFieldPuns]} (Text.unpack (dropShebang text)) of
25212530
HSE.ParseFailed l e -> pure $ Left $ "Parse error: " <> HSE.prettyPrint l <> ": " <> e
25222531
HSE.ParseOk !file -> do
25232532
t2 <- getTime
2524-
emitStat stats "parse_module_with_mode" (t2-t1)
2533+
emitStat stats "parse_module_with_mode" (t2 - t1)
25252534
case parseModule file of
25262535
HSE.ParseFailed l e ->
25272536
pure $ Left $ "Parse error: " <> HSE.prettyPrint l <> ": " <> e
25282537
HSE.ParseOk !file' -> do
25292538
t3 <- getTime
2530-
emitStat stats "resolve_module" (t3-t2)
2539+
emitStat stats "resolve_module" (t3 - t2)
25312540
pure $ Right file'
25322541

25332542
-- This should be quite efficient because it's essentially a pointer

0 commit comments

Comments
 (0)