Skip to content

Commit 3e77f3e

Browse files
authored
Fix all hlint warnings in interpreter/ (#307)
1 parent a05f829 commit 3e77f3e

File tree

5 files changed

+79
-87
lines changed

5 files changed

+79
-87
lines changed

.hlint.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
# - ignore: {name: Use let}
5252
# - ignore: {name: Use const, within: SpecialModule} # Only within certain modules
5353
- ignore: {name: Redundant do}
54+
- ignore: {name: Avoid lambda}
55+
- ignore: {name: Use tuple-section}
56+
- ignore: {name: Redundant lambda}
5457

5558
# Define some custom infix operators
5659
# - fixity: infixr 3 ~^#^~

interpreter/src/Language/Granule/Doc.hs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ import qualified Data.Map as M
2828

2929
import Control.Monad (when)
3030

31-
-- import Debug.Trace
32-
3331

3432
-- Doc top-level
3533
grDoc :: (?globals::Globals) => String -> AST () () -> IO ()
@@ -52,7 +50,7 @@ grDoc input ast = do
5250

5351
writeFile ("docs/modules/" <> modName <> ".html") (unpack moduleFile)
5452
-- Generate the Primitives file
55-
when info $ putStrLn $ "Generating docs index file."
53+
when info $ putStrLn "Generating docs index file."
5654
primFile <- generatePrimitivesPage
5755
writeFile "docs/modules/Primitives.html" (unpack primFile)
5856
-- Generate the index file
@@ -78,12 +76,12 @@ generateModulePage' modName title input ast =
7876
where
7977
inputLines = lines input
8078
content = (if Text.strip preamble == "" then "" else section "Meta-data" preamble)
81-
<> (section "Contents" ((internalNav headings')
79+
<> section "Contents" (internalNav headings'
8280
<> (if modName == "Primitives" then anchor "Built-in Types" else "")
83-
<> Text.concat contentDefs))
81+
<> Text.concat contentDefs)
8482
preamble = parsePreamble inputLines
85-
<> if M.keys (hiddenNames ast) == [] then ""
86-
else (tag "strong" "Module does not export: ") <> Text.intercalate ", " (map (pack . prettyDoc) $ M.keys (hiddenNames ast))
83+
<> if Prelude.null (M.keys (hiddenNames ast)) then ""
84+
else tag "strong" "Module does not export: " <> Text.intercalate ", " (map (pack . prettyDoc) $ M.keys (hiddenNames ast))
8785
(headings, contentDefs) = unzip (map prettyDef topLevelDefs)
8886
headings' =
8987
if modName == "Primitives"
@@ -92,35 +90,35 @@ generateModulePage' modName title input ast =
9290

9391

9492
-- Combine the data type and function definitions together
95-
topLevelDefs = sortOn startLine $ (map Left (dataTypes ast)) <> (map Right (definitions ast))
93+
topLevelDefs = sortOn startLine $ map Left (dataTypes ast) <> map Right (definitions ast)
9694
startLine = fst . startPos . defSpan'
9795
defSpan' (Left dataDecl) = dataDeclSpan dataDecl
9896
defSpan' (Right def) = defSpan def
9997

10098
prettyDef (Left d) =
10199
let (docs, heading) = scrapeDoc inputLines (dataDeclSpan d)
102100
in (heading,
103-
(maybe "" anchor heading)
101+
maybe "" anchor heading
104102
<> (codeDiv . pack . prettyDoc $ d)
105103
<> (if strip docs == "" then miniBreak else descDiv docs))
106104
prettyDef (Right d) =
107105
let (docs, heading) = scrapeDoc inputLines (defSpan d)
108106
in (heading
109-
, (maybe "" anchor heading)
110-
<> (codeDiv $ breakLine (internalName (defId d)) $ pack $ prettyDoc (defId d) <> " : " <> prettyDoc (defTypeScheme d))
107+
, maybe "" anchor heading
108+
<> codeDiv (breakLine (internalName (defId d)) $ pack $ prettyDoc (defId d) <> " : " <> prettyDoc (defTypeScheme d))
111109
<> (if strip docs == "" then miniBreak else descDiv docs))
112110

113111
breakLine id xs =
114112
if Text.length xs >= 65 && (Text.isInfixOf "forall" xs || Text.isInfixOf "exists" xs) then
115113
case Text.break (== '.') xs of
116114
(before, after) ->
117-
before <> "\n" <> (Data.Text.replicate (length id + 1) " ") <> after
115+
before <> "\n" <> Data.Text.replicate (length id + 1) " " <> after
118116
else xs
119117

120118
anchor :: Text -> Text
121119
anchor x = tagWithAttributes "a"
122120
("name = " <> toUrlName x)
123-
(tag "h3" ((tagWithAttributes "a" ("href='#' class='toplink'") "[top]") <> x))
121+
(tag "h3" (tagWithAttributes "a" "href='#' class='toplink'" "[top]" <> x))
124122

125123

126124
internalNav [] = ""
@@ -142,7 +140,7 @@ generateIndexPage = do
142140
-- Generates the text of the primitives module
143141
generatePrimitivesPage :: (?globals::Globals) => IO Text
144142
generatePrimitivesPage = do
145-
generateModulePage' "Primitives" "Built-in primitives" (Primitives.builtinSrc) (appendPrimitiveTys $ fst . fromRight $ parseDefs "Primitives" Primitives.builtinSrc)
143+
generateModulePage' "Primitives" "Built-in primitives" Primitives.builtinSrc (appendPrimitiveTys $ fst . fromRight $ parseDefs "Primitives" Primitives.builtinSrc)
146144
where
147145
fromRight (Right x) = x
148146
fromRight (Left x) = error x
@@ -174,7 +172,7 @@ generatePrimitivesPage = do
174172
matches tyConName (id', (ty, _, _)) =
175173
case (tyConName, resultType ty) of
176174
(internalName -> "Type", Type 0) -> True
177-
(a, b) -> (TyCon a) == b
175+
(a, b) -> TyCon a == b
178176

179177
generateFromTemplate :: PageContext -> Text -> Text -> Text -> IO Text
180178
generateFromTemplate ctxt modName title content = do
@@ -197,7 +195,7 @@ generateNavigatorText ctxt = do
197195
files <- return $ sort (filter (\file -> takeExtension file == ".html" && takeBaseName file /= "Primitives") files)
198196
-- Build a list of these links
199197
let prefix = if ctxt == ModulePage then "" else "modules/"
200-
let toLi file = li $ link (pack $ takeBaseName file) (prefix <> (pack $ takeBaseName file) <> ".html")
198+
let toLi file = li $ link (pack $ takeBaseName file) (prefix <> pack (takeBaseName file) <> ".html")
201199
-- Link to index page
202200
let indexPrefix = if ctxt == ModulePage then "../" else ""
203201
let topLevelLink = link "<i>Top-level</i><br />" (indexPrefix <> "index.html")
@@ -251,7 +249,7 @@ parsePreamble inputLines =
251249
presentPrequelLine line =
252250
if name == "Module" -- drop duplicate module info
253251
then ""
254-
else li $ (tag "b" (pack name)) <> pack info
252+
else li $ tag "b" (pack name) <> pack info
255253
where
256254
(name, info) = break (== ':') (drop 4 line)
257255
prequelLines =
@@ -265,20 +263,20 @@ parsePreamble inputLines =
265263
-- HTML generation helpers
266264

267265
toUrlName :: Text -> Text
268-
toUrlName = (replace " " "-") . (Text.filter isAlpha) . Text.toLower
266+
toUrlName = replace " " "-" . Text.filter isAlpha . Text.toLower
269267

270268
section :: Text -> Text -> Text
271269
section "" contents = tag "section" contents
272270
section name contents = tag "section" (tag "h2" name <> contents)
273271

274272
codeDiv :: Text -> Text
275-
codeDiv = tagWithAttributes "div" ("class='code'") . tag "pre"
273+
codeDiv = tagWithAttributes "div" "class='code'" . tag "pre"
276274

277275
descDiv :: Text -> Text
278-
descDiv = tagWithAttributes "div" ("class='desc'")
276+
descDiv = tagWithAttributes "div" "class='desc'"
279277

280278
navDiv :: Text -> Text
281-
navDiv = tagWithAttributes "div" ("class='mininav'")
279+
navDiv = tagWithAttributes "div" "class='mininav'"
282280

283281
span :: Text -> Text -> Text
284282
span name = tagWithAttributes "span" ("class='" <> name <> "'")

interpreter/src/Language/Granule/Interpreter.hs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ main = do
8585
runGrOnFiles :: [FilePath] -> GrConfig -> IO ()
8686
runGrOnFiles globPatterns config = let ?globals = grGlobals config in do
8787
pwd <- getCurrentDirectory
88-
results <- forM globPatterns $ \pattern -> do
89-
paths <- glob pattern
88+
results <- forM globPatterns $ \pat -> do
89+
paths <- glob pat
9090
case paths of
9191
[] -> do
92-
let result = Left $ NoMatchingFiles pattern
92+
let result = Left $ NoMatchingFiles pat
9393
printResult result
9494
return [result]
9595
_ -> forM paths $ \path -> do
@@ -114,7 +114,7 @@ runGrOnFiles globPatterns config = let ?globals = grGlobals config in do
114114
result <- run config src
115115
printResult result
116116
return result
117-
if all isRight (concat results) then exitSuccess else exitFailure
117+
if all (all isRight) results then exitSuccess else exitFailure
118118

119119
runGrOnStdIn :: GrConfig -> IO ()
120120
runGrOnStdIn config@GrConfig{..}
@@ -142,7 +142,7 @@ run
142142
=> GrConfig
143143
-> String
144144
-> IO (Either InterpreterError InterpreterResult)
145-
run config input = let ?globals = fromMaybe mempty (grGlobals <$> getEmbeddedGrFlags input) <> ?globals in do
145+
run config input = let ?globals = maybe mempty grGlobals (getEmbeddedGrFlags input) <> ?globals in do
146146
if grDocMode config
147147
-- Generate docs mode
148148
then do
@@ -175,7 +175,7 @@ run config input = let ?globals = fromMaybe mempty (grGlobals <$> getEmbeddedGrF
175175
let holeErrors = getHoleMessages errs
176176
if ignoreHoles && length holeErrors == length errs && not (fromMaybe False $ globalsSynthesise ?globals)
177177
then do
178-
printSuccess $ "OK " ++ (blue $ "(but with " ++ show (length holeErrors) ++ " holes)")
178+
printSuccess $ "OK " ++ blue ("(but with " ++ show (length holeErrors) ++ " holes)")
179179
return $ Right NoEval
180180
else
181181
case (globalsRewriteHoles ?globals, holeErrors) of
@@ -209,8 +209,8 @@ run config input = let ?globals = fromMaybe mempty (grGlobals <$> getEmbeddedGrF
209209

210210
where
211211
getHoleMessages :: NonEmpty CheckerError -> [CheckerError]
212-
getHoleMessages es =
213-
NonEmpty.filter (\ e -> case e of HoleMessage{} -> True; _ -> False) es
212+
getHoleMessages =
213+
NonEmpty.filter (\case HoleMessage{} -> True; _ -> False)
214214

215215
runHoleSplitter :: (?globals :: Globals)
216216
=> String
@@ -245,7 +245,7 @@ run config input = let ?globals = fromMaybe mempty (grGlobals <$> getEmbeddedGrF
245245
res <- synthesiseHoles config ast holesWithEmptyMeasurements isGradedBase
246246
let (holes', measurements, _) = unzip3 res
247247
when benchmarkingRawData $ do
248-
forM_ measurements (\m -> case m of (Just m') -> putStrLn $ show m' ; _ -> return ())
248+
forM_ measurements (\case (Just m') -> print m' ; _ -> return ())
249249
return holes'
250250

251251

@@ -257,10 +257,10 @@ run config input = let ?globals = fromMaybe mempty (grGlobals <$> getEmbeddedGrF
257257
rest <- synthesiseHoles config astSrc holes isGradedBase
258258

259259
gradedExpr <- if cartSynth > 0 then getGradedExpr config defId else return Nothing
260-
let defs' = map (\(x, (Forall tSp con bind ty)) ->
260+
let defs' = map (\(x, Forall tSp con bind ty) ->
261261
if cartSynth > 0
262-
then (x, (Forall tSp con bind (toCart ty)))
263-
else (x, (Forall tSp con bind ty))
262+
then (x, Forall tSp con bind (toCart ty))
263+
else (x, Forall tSp con bind ty)
264264
) defs
265265
let (unrestricted, restricted) = case spec of
266266
Just (Spec _ _ _ comps) ->
@@ -290,7 +290,7 @@ run config input = let ?globals = fromMaybe mempty (grGlobals <$> getEmbeddedGrF
290290
Just (programs@(_:_), measurement) -> do
291291
when synthHtml $ do
292292
printSynthOutput $ uncurry synthTreeToHtml (last programs)
293-
return $ (HoleMessage sp goal ctxt tyVars hVars synthCtxt [([], fst $ last $ programs)], measurement, attemptNo) : rest
293+
return $ (HoleMessage sp goal ctxt tyVars hVars synthCtxt [([], fst $ last programs)], measurement, attemptNo) : rest
294294

295295

296296
synthesiseHoles config ast (hole:holes) isGradedBase = do
@@ -309,23 +309,21 @@ synEval ast = do
309309

310310
getGradedExpr :: (?globals :: Globals) => GrConfig -> Id -> IO (Maybe (Def () ()))
311311
getGradedExpr config def = do
312-
let file = (fromJust $ globalsSourceFilePath ?globals) <> ".output"
312+
let file = fromJust (globalsSourceFilePath ?globals) <> ".output"
313313
src <- preprocess
314314
(rewriter config)
315315
(keepBackup config)
316316
file
317317
(literateEnvName config)
318-
((AST _ defs _ _ _), _) <- parseDefsAndDoImports src
318+
(AST _ defs _ _ _, _) <- parseDefsAndDoImports src
319319
return $ find (\(Def _ id _ _ _ _) -> id == def) defs
320320

321321

322322
-- | Get the flags embedded in the first line of a file, e.g.
323323
-- "-- gr --no-eval"
324324
getEmbeddedGrFlags :: String -> Maybe GrConfig
325325
getEmbeddedGrFlags
326-
= foldr (<|>) Nothing
327-
. map getEmbeddedGrFlagsLine
328-
. take 3 -- only check for flags within the top 3 lines
326+
= foldr ((<|>) . getEmbeddedGrFlagsLine) Nothing . take 3 -- only check for flags within the top 3 lines
329327
. filter (not . all isSpace)
330328
. lines
331329
where

0 commit comments

Comments
 (0)