Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
105 commits
Select commit Hold shift + click to select a range
71bb265
suggest replacing if statements with maybe checks with case-of expres…
nimec01 Oct 30, 2026
777310f
allow words
nimec01 Oct 30, 2026
5a00050
remove trailing whitespace
nimec01 Oct 30, 2026
24e8a15
use ubuntu-24.04
nimec01 Oct 30, 2026
727cce0
Merge branch 'master' into maybe
nimec01 Nov 5, 2026
66a6cd0
Merge branch 'master' into maybe
nimec01 Nov 6, 2026
17119ad
Fix typo in comment for suggestMaybeCaseOfUsage
jvoigtlaender Nov 6, 2026
ee67b5d
add HLint config
nimec01 Nov 6, 2026
547c16d
use maybeToList
nimec01 Nov 6, 2026
02ca68b
move position of config setting
nimec01 Nov 6, 2026
4befb13
provide suggestion earlier
nimec01 Nov 6, 2026
af54885
add more tests
nimec01 Nov 6, 2026
c19880e
only suggest when fromJust is used and make newly introduced variable…
nimec01 Nov 6, 2026
dd6d280
import Data.Maybe in test cases
nimec01 Nov 6, 2026
4cca77a
make checks dependent on name scopes
nimec01 Nov 6, 2026
ba0c4bb
share parsed AST of submission
nimec01 Nov 6, 2026
35300d0
Merge branch 'master' into maybe
nimec01 Nov 6, 2026
552be15
directly access value
nimec01 Nov 6, 2026
15b9ed9
add document start
nimec01 Nov 6, 2026
62aa9a1
use errorP
nimec01 Nov 7, 2026
44b38e1
reduce diff
nimec01 Nov 7, 2026
5a47f3b
put detection into separate module
nimec01 Nov 7, 2026
6244293
add more configuration options
nimec01 Nov 7, 2026
ce5bbba
reject with hint
nimec01 Nov 7, 2026
bd7e420
allow word
nimec01 Nov 7, 2026
e3d63bd
allow pattern
nimec01 Nov 7, 2026
42968c5
don't run check again when check was run before
nimec01 Nov 7, 2026
d219ef4
add more test cases
nimec01 Nov 7, 2026
d6c5a9c
update documentation of config settings
nimec01 Nov 7, 2026
f6601c2
Fix comments in Task.hs for clarity
jvoigtlaender Nov 8, 2026
0e3fd0b
only suggest if scrutinee is variable
nimec01 Nov 8, 2026
a06535e
update tests
nimec01 Nov 8, 2026
0236838
update handling of redundant brackets
nimec01 Nov 8, 2026
90ceaa9
refactor config options
nimec01 Nov 8, 2026
cef905a
update documentation for default config
nimec01 Nov 8, 2026
2d2c1a6
allow word
nimec01 Nov 8, 2026
50614b5
trailing whitespace
nimec01 Nov 8, 2026
79f8e78
allow pattern
nimec01 Nov 8, 2026
f39f54d
Fix typo in comments for hlint configuration
jvoigtlaender Nov 8, 2026
ce021f7
make asSuggestion required
nimec01 Nov 8, 2026
5d21071
more config checks
nimec01 Nov 8, 2026
2018097
update comment
nimec01 Nov 8, 2026
3f88a48
add new status datatype and replace asSuggestion and asError
nimec01 Nov 8, 2026
c147db0
re-add information regarding dependence on hlint
nimec01 Nov 8, 2026
9573627
fix error message
nimec01 Nov 8, 2026
a03422c
Clarify comments regarding hlint and maybe checks
jvoigtlaender Nov 8, 2026
e8c2f63
Fix typo
jvoigtlaender Nov 8, 2026
a55238d
overhaul implementation of new config type
nimec01 Nov 8, 2026
8e14146
allow FMaybe
nimec01 Nov 8, 2026
ecbfa47
Merge branch 'master' into maybe
nimec01 Nov 9, 2026
94ce8cf
rename provideSuggestion and update documentation
nimec01 Nov 9, 2026
53acb28
make status required
nimec01 Nov 9, 2026
1126e73
add config spec
nimec01 Nov 9, 2026
2d994d5
allow Utf
nimec01 Nov 9, 2026
d35a838
reject unknown fields
nimec01 Nov 9, 2026
66806a2
Revert "make status required"
nimec01 Nov 9, 2026
e0dd66a
fix test cases
nimec01 Nov 9, 2026
36f1410
don't set a default value for status (which also makes status a requi…
nimec01 Nov 9, 2026
9ddeb09
disable spell checking for two lines
nimec01 Nov 9, 2026
9370869
also make "# no-spell-check" ignore line
nimec01 Nov 9, 2026
8440355
Refactor comments in Task.hs for better clarity
jvoigtlaender Nov 9, 2026
63e45c0
make defaultCode complete again
nimec01 Nov 9, 2026
cb324dd
make (==) and (/=) need to be default implementations
nimec01 Nov 9, 2026
68663c1
don't replace unrelated fromJust expressions
nimec01 Nov 9, 2026
5b78ce8
directly use derived value name instead of replacing it afterwards
nimec01 Nov 9, 2026
5ed122b
minimize diff?
nimec01 Nov 9, 2026
5aed49e
only try to parse submission when syntaxCutoff is not CodeWidth
nimec01 Nov 9, 2026
5698cad
only overwrite what really was Nothing
jvoigtlaender Nov 9, 2026
bc3f5ec
import fromJust
jvoigtlaender Nov 9, 2026
3d10097
line length
jvoigtlaender Nov 9, 2026
50c78f5
Merge branch 'master' into maybe
jvoigtlaender Nov 9, 2026
ab06338
fix condition
nimec01 Nov 9, 2026
da6daca
add GradingSpec
nimec01 Nov 9, 2026
16d1549
Update comments in Task.hs for clarity
jvoigtlaender Nov 10, 2026
6770af1
add parsing test under normal conditions
nimec01 Nov 10, 2026
42e565f
rename config name and related functions
nimec01 Nov 10, 2026
2ed36d0
don't reveal potentially confusing error message from haskell-src-exts
nimec01 Nov 10, 2026
eeae76b
document parsing behavior
nimec01 Nov 10, 2026
b818c80
update allowed words
nimec01 Nov 10, 2026
3f37720
refactor handling of parse error to embed withSyntax
nimec01 Nov 10, 2026
613592c
remove dummy method
nimec01 Nov 10, 2026
6683b5c
shorten line
nimec01 Nov 10, 2026
3ca7396
revert changes
nimec01 Nov 10, 2026
711c624
simpler way to embed withSyntax
nimec01 Nov 10, 2026
66428e1
spacing
jvoigtlaender Nov 28, 2026
f4cf4f4
Merge branch 'master' into maybe
jvoigtlaender Nov 28, 2026
eaed8d6
fixup after merge
jvoigtlaender Nov 28, 2026
f4a2638
list layout like in the rest of this function
jvoigtlaender Nov 28, 2026
04cf3ac
don't replace shadowed fromJust call
nimec01 Nov 29, 2026
abd60c9
fix test case names
nimec01 Nov 29, 2026
5dafd72
fix test case using fromMaybe instead of fromJust
nimec01 Nov 29, 2026
bb4c033
fix invalid programs in test cases
nimec01 Nov 29, 2026
034d318
test interaction with grade
nimec01 Nov 29, 2026
d4251d4
take record fields into account
nimec01 Nov 29, 2026
c38a16e
make getMaybeFeedback respect default language extensions
nimec01 Nov 29, 2026
9f2ea20
shorten line
nimec01 Nov 29, 2026
b094c5a
throw error on unexpected module type
nimec01 Nov 29, 2026
8314392
add notice
nimec01 Nov 29, 2026
af054c6
Clarify comment about module exports in Detection.hs
jvoigtlaender Nov 29, 2026
84cd120
Fix typo
jvoigtlaender Nov 29, 2026
0282b19
move module
nimec01 Nov 30, 2026
95c0aa7
add test cases (for edge cases)
nimec01 Nov 30, 2026
7edc81c
rename module in test submissions
nimec01 Nov 30, 2026
f35afc3
format new files
nimec01 Nov 30, 2026
0d8e21b
Clarify description of edge cases in TaskSpec
jvoigtlaender Nov 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/actions/spelling/allow.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ autotool
Bifunctor
concat
elems
FCase
finalise
fmap
fmidue
Expand All @@ -21,8 +22,14 @@ mempty
mplus
msum
mzero
PApp
QName
QOp
QVar
scrutinee
snd
thd
uncurry
unlines
Utf
yml
3 changes: 3 additions & 0 deletions .github/actions/spelling/patterns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
\b[Ll]og in to\b

# acceptable duplicates
linebreak linebreak
False False

# ls directory listings
[-bcdlpsw](?:[-r][-w][-Ssx]){3}\s+\d+\s+\S+\s+\S+\s+\d+\s+
Expand Down Expand Up @@ -39,3 +41,4 @@ https?://[-+0-9a-zA-Z?&=_\/%.]*

# marker to ignore all code on line
^.*-- no-spell-check$
^.*# no-spell-check$
6 changes: 6 additions & 0 deletions .hlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
# HLint configuration file
# https://github.com/ndmitchell/hlint
##########################

- hint: {lhs: "maybe [] singleton", rhs: "maybeToList"}
7 changes: 7 additions & 0 deletions haskell-template-task.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ source-repository head

library
exposed-modules:
Haskell.Template.Detection.Maybe
Haskell.Template.Match
Haskell.Template.Task
other-modules:
Expand All @@ -39,9 +40,11 @@ library
, aeson
, base >=4.7 && <5
, bytestring
, containers
, extra
, file-embed
, filepath
, haskell-names
, haskell-src-exts
, haskell-template-task-raw
, hint
Expand All @@ -60,6 +63,8 @@ test-suite haskell-template-task-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Haskell.Template.ConfigSpec
Haskell.Template.GradingSpec
Haskell.Template.MatchSpec
Haskell.Template.TaskSpec
Paths_haskell_template_task
Expand All @@ -73,12 +78,14 @@ test-suite haskell-template-task-test
, aeson
, base >=4.7 && <5
, bytestring
, containers
, deepseq
, directory
, exceptions
, extra
, file-embed
, filepath
, haskell-names
, haskell-src-exts
, haskell-template-task
, haskell-template-task-raw
Expand Down
2 changes: 2 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ dependencies:
- wl-pprint-text
- yaml
- aeson
- containers
- haskell-names
Comment thread
jvoigtlaender marked this conversation as resolved.
Resolved
- transformers

ghc-options:
Expand Down
155 changes: 155 additions & 0 deletions src/Haskell/Template/Detection/Maybe.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
module Haskell.Template.Detection.Maybe (collectIfsWithMaybeChecks) where

import Control.Monad (void)
import Data.Data (Data)
import Data.Generics (everything, everywhere', listify, mkQ, mkT)
import Data.List.Extra (notNull)
import Data.Maybe (listToMaybe, mapMaybe, maybeToList)
import Data.Set (Set)
import qualified Data.Set as Set (empty, fromList, member, singleton, union)
import Language.Haskell.Exts (
Alt (..),
Decl (..),
Exp (..),
FieldDecl (..),
Match (..),
Module (..),
ModuleName (..),
Name (..),
Pat (..),
QName (..),
QOp (..),
Rhs (..),
SrcSpanInfo,
)
import Language.Haskell.Names (NameInfo (..), Scoped (..), symbolModule)

collectIfsWithMaybeChecks :: Module (Scoped SrcSpanInfo) -> [(Exp (Scoped SrcSpanInfo), Exp ())]
collectIfsWithMaybeChecks (Module _ _ _ _ decls) = concat $ mapMaybe process decls
where
-- Imports are disregarded as no module (available to the students) exports a 'value'-named symbol.
-- This also assumes that the modules provided by the lecturer don't expose such a symbol.
moduleNames =
Set.fromList (mapMaybe declName decls)
`Set.union` recordFieldNames decls

process d = do
let
allSymbols = moduleNames `Set.union` allNames d
freeName n
| Set.member n allSymbols = freeName (n ++ "'")
| otherwise = n

pure $ everything (++) (mkQ [] (maybeToList . exprToIfInfo (freeName "value"))) d
collectIfsWithMaybeChecks _ = []

declName :: Decl a -> Maybe String
declName (FunBind _ ms) = listToMaybe $ map fromMatch ms
where
fromMatch (Match _ (Ident _ n) _ _ _) = n
fromMatch (Match _ (Symbol _ n) _ _ _) = n
fromMatch (InfixMatch _ _ (Ident _ n) _ _ _) = n
fromMatch (InfixMatch _ _ (Symbol _ n) _ _ _) = n
declName (PatBind _ (PVar _ pn) _ _) = Just $ fromName pn
where
fromName (Ident _ n) = n
fromName (Symbol _ n) = n
declName _ = Nothing

exprToIfInfo :: String -> Exp (Scoped SrcSpanInfo) -> Maybe (Exp (Scoped SrcSpanInfo), Exp ())
exprToIfInfo valueName expr@(If _ cond t e) = case removeParens cond of
(App _ (Var _ vn) maybeVal)
| hasName maybeMod "isJust" vn -> transform maybeVal t e
| hasName maybeMod "isNothing" vn -> transform maybeVal e t
(InfixApp _ l (QVarOp _ opName) r) -> case getMaybeVal l r of
Just maybeVal
| hasName eqMod "==" opName -> transform maybeVal e t
| hasName eqMod "/=" opName -> transform maybeVal t e
_ -> Nothing
_ -> Nothing
where
eqMod = Just "GHC.Classes"
maybeMod = Just "Data.Maybe"
isNothingExpression (Con _ vName) = hasName maybeMod "Nothing" vName
isNothingExpression _ = False

getMaybeVal l r
| isNothingExpression l && isNothingExpression r = Nothing
| isNothingExpression l = Just r
| isNothingExpression r = Just l
| otherwise = Nothing

transform mValExpr j n = case removeParens mValExpr of
mVal@(Var _ _)
| hasFromJust mVal j -> Just (expr, asCaseExpression valueName mVal j n)
| otherwise -> Nothing
_ -> Nothing
exprToIfInfo _ _ = Nothing

allNames :: Data a => a -> Set String
allNames = everything Set.union $ mkQ Set.empty extract
where
extract :: Name (Scoped SrcSpanInfo) -> Set String
extract (Ident _ n) = Set.singleton n
extract (Symbol _ n) = Set.singleton n

recordFieldNames :: Data a => a -> Set String
recordFieldNames = everything Set.union $ mkQ Set.empty extract
where
extract :: FieldDecl (Scoped SrcSpanInfo) -> Set String
extract (FieldDecl _ names _) = Set.fromList $ map nameToString names

nameToString (Ident _ n) = n
nameToString (Symbol _ n) = n

hasFromJust :: Exp (Scoped SrcSpanInfo) -> Exp (Scoped SrcSpanInfo) -> Bool
hasFromJust mExpr expr = notNull $ listify isFromJustApp expr
where
isFromJustApp :: Exp (Scoped SrcSpanInfo) -> Bool
isFromJustApp (App _ (Var _ vn) maybeVal) =
hasName (Just "Data.Maybe") "fromJust" vn && fmap void (removeParens maybeVal) == fmap void mExpr
Comment thread
nimec01 marked this conversation as resolved.
Resolved
isFromJustApp _ = False

asCaseExpression
:: String
-> Exp (Scoped SrcSpanInfo)
-> Exp (Scoped SrcSpanInfo)
-> Exp (Scoped SrcSpanInfo)
-> Exp ()
asCaseExpression valueName maybeValue justCase nothingCase =
Case
()
(void maybeValue)
[ Alt () (PApp () (UnQual () (Ident () "Nothing")) []) (UnGuardedRhs () $ void nothingCase) Nothing
Comment thread
jvoigtlaender marked this conversation as resolved.
Resolved
, Alt
()
(PApp () (UnQual () (Ident () "Just")) [PVar () (Ident () valueName)])
(UnGuardedRhs () $ replaceFromJustWithValue valueName maybeValue justCase)
Nothing
]

replaceFromJustWithValue :: String -> Exp (Scoped SrcSpanInfo) -> Exp (Scoped SrcSpanInfo) -> Exp ()
replaceFromJustWithValue valueName maybeValue = void . everywhere' (mkT replace)
where
replace :: Exp (Scoped SrcSpanInfo) -> Exp (Scoped SrcSpanInfo)
replace expr = case removeParens expr of
App l (Var _ vn) fnBody
| hasName (Just "Data.Maybe") "fromJust" vn && fmap void (removeParens fnBody) == fmap void maybeValue ->
Var l (UnQual l (Ident l valueName))
_ -> expr

removeParens :: Exp a -> Exp a
removeParens (Paren _ e) = removeParens e
removeParens e = e

hasName :: Maybe String -> String -> QName (Scoped l) -> Bool
hasName mModuleName qName (UnQual scope (Ident _ n)) = n == qName && isGloballyScopedInModule mModuleName scope
hasName mModuleName qName (Qual scope _ (Ident _ n)) = n == qName && isGloballyScopedInModule mModuleName scope
hasName mModuleName qName (UnQual scope (Symbol _ n)) = n == qName && isGloballyScopedInModule mModuleName scope
hasName mModuleName qName (Qual scope _ (Symbol _ n)) = n == qName && isGloballyScopedInModule mModuleName scope
hasName _ _ _ = False

isGloballyScopedInModule :: Maybe String -> Scoped l -> Bool
isGloballyScopedInModule Nothing _ = True
isGloballyScopedInModule (Just modName) (Scoped (GlobalSymbol sym _) _) = symbolModule sym == ModuleName () modName
isGloballyScopedInModule (Just _) _ = False
Loading
Loading