Skip to content

Suggest replacing if-then-else with case-of when working on Maybe values - #73

Merged
jvoigtlaender merged 105 commits into
fmidue:masterfrom
nimec01:maybe
Nov 30, 2026
Merged

jvoigtlaender merged 105 commits into
fmidue:masterfrom
nimec01:maybe

Conversation

@nimec01

@nimec01 nimec01 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@nimec01
nimec01 marked this pull request as ready for review November 4, 2026 16:25
Comment thread src/Haskell/Template/Match.hs Resolved
Comment thread src/Haskell/Template/Task.hs Resolved
Comment thread src/Haskell/Template/Task.hs Resolved
Comment thread src/Haskell/Template/Task.hs Resolved
@jvoigtlaender

Copy link
Copy Markdown
Member

I suspect the implementation here is way too naive, given that the provided test cases look very simple and do not contain the issues I would consider potentially problematic and needing special attention during the invention of variable names etc.

For example, what would currently happen to code like this:

f m = let value = 42 in if isJust m then value else 0

or this:

f m = let value = 42 in if isJust m then value + fromJust m else 0

or this:

f m = if isJust m then (m, fromJust m) else (Just 42, 0)

or this:

f value = if isJust value then fromJust value else 0

or this:

f m = if isJust m
      then let value = 100 in fromJust m + value
      else 0

etc. (also for stuff not introduced via let but instead via other binders like case or lambda-expressions, and probably other features like where, parameter names of local functions, binders in list comprehensions or do-notation and whatnot, all potentially occurring inside the if isJust ... then ... else ... scope)

Also nested occurrences of such patterns, where then the repeated use of always value as "new" variable name could become problematic.

Or cases where not the value variable is the (only) problem, but actually the variable that the scrutinee is taken from:

f m = if isJust m then let m = Just 99 in fromJust m else 0

(the fromJust m should be 99 here, not what comes from f's argument)

Even weirder but possibly not unthinkable:

f m = let fromJust _ = 99
      in if isJust m then fromJust m else 0

Many of these things might be so unrealistic/unexpected in student code that the checker could be conservative and simply not do anything on code that does such shadowing (e.g., of the fromJust function itself); but that conservativity still needs to be implemented (and tested to avoid regressions).

Maybe it is also reasonable to take a step back first and consider whether there are systematic alternatives to achieve the aim. Can hlint be used to achieve something similar as the hard-coded matching here? Or can functionality from hlint (using hlint as a library) be used somehow? After all, hlint also needs to somehow deal with variable names and possible scope conflicts. So either some lessons can be taken from how they do it, or some functionality be taken over outright? Or from other tooling (stan etc.) I mentioned elsewhere?

@nimec01

nimec01 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

HLint does actually allow me to transform if-expressions into case-of-expressions, but the problems you mentioned in the last comment still apply. It doesn't do any name resolution, so we can't really check if isJust or fromJust really are from Data.Maybe, and it also doesn't check if a variable newly introduced will clash. So we basically need to do our manual check but we of course need to reenforce it.

Comment thread src/Haskell/Template/Match.hs Resolved
Comment thread src/Haskell/Template/Task.hs Resolved
Comment thread src/Haskell/Template/Task.hs Resolved
Comment thread test/Haskell/Template/MatchSpec.hs Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Active critical and moderate issues remain, including a missing syb dependency that will break clean builds and unsound detection replacements involving shadowed bindings.

4 open issues

Review effort: Lite

Comment thread package.yaml Resolved
Comment thread src/Haskell/Template/Detection/Maybe.hs Resolved
Comment thread src/Haskell/Template/Detection.hs Resolved
Comment thread test/Haskell/Template/TaskSpec.hs Resolved
@jvoigtlaender

Copy link
Copy Markdown
Member

Separate ChatGPT usage resulted in the following claims about the test suite additions:

The non-variable-scrutinee test does not test its stated restriction

At TaskSpec.hs:287–295, the branch contains:

fromMaybe (f val)

instead of:

fromJust (f val)

The detector would return no opportunity even if its restriction to variable scrutinees were removed, because there is no matching fromJust call. The fixture also does not typecheck against its stated signature.

Changing this to fromJust (f val) would make the test exercise the intended boundary.

Two positive fixtures contain unbound variables

The nested-expression fixture at TaskSpec.hs:249–250 says:

def mVal1 mVal 2 = ...

while its body uses mVal2. It therefore has an unintended extra argument and an unresolved mVal2.

The mixed-shadowing fixture at TaskSpec.hs:326–327 binds mVal, while the body uses m.

The latter still tests some traversal behavior, but it compares an unresolved outer name with a resolved inner binding. It does not test the intended situation of two valid, differently scoped bindings.

These mistakes are possible because the helper parses the snippets and invokes feedback directly; it does not typecheck them.

Enabled detection is not tested through the grading pipeline

The new Maybe-specific tests call getMaybeFeedback directly. Every call uses maybeCheckIO False …; the helper’s asError path is unused. Meanwhile, the new GradingSpec configures the detector as Ignore and tests parsing behavior. See TaskSpec.hs and GradingSpec.hs.

The most useful integration additions would exercise an actual opportunity through grade and verify:

  • Ignore produces no Maybe feedback.
  • Hint reports the opportunity without rejecting.
  • Reject terminates at the documented phase and reports the first opportunity.
  • An HLint error takes precedence over Maybe rejection.
  • syntaxCutoff and disableSemantics correctly gate the new checks.
  • Configured extensions affect analysis consistently with compilation.

The current phase placement looks consistent with the documentation by inspection, but the tests do not establish those behaviors.

The first four test labels are rotated

The issue is broader than the one label mentioned by the existing automated review:

Label Actual condition
isJust isNothing mVal
isNothing mVal == Nothing
== Nothing mVal /= Nothing
/= Nothing isJust mVal

All four forms are present, so this is misleading labeling rather than missing coverage. The affected tests are at TaskSpec.hs:89–130.

@nimec01

nimec01 commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

The most useful integration additions would exercise an actual opportunity through grade and verify:

  • Ignore produces no Maybe feedback.
  • Hint reports the opportunity without rejecting.
  • Reject terminates at the documented phase and reports the first opportunity.
  • An HLint error takes precedence over Maybe rejection.
  • syntaxCutoff and disableSemantics correctly gate the new checks.
  • Configured extensions affect analysis consistently with compilation.

I only implemented the first three checks here. The rest can be added in another PR

The comments from the other repo are also mostly covered by the changes. I'm only delegating the "restrict allowed extensions" portion to another PR

Comment thread src/Haskell/Template/Detection.hs Resolved
Comment thread src/Haskell/Template/Task.hs Resolved
@jvoigtlaender jvoigtlaender changed the title Suggest replacing if with case-of when working with Maybe values Suggest replacing if-then-else with case-of when working on Maybe values Nov 29, 2026
@jvoigtlaender

This comment was marked as resolved.

@jvoigtlaender

Copy link
Copy Markdown
Member

Are there any cases left now (apart from syntax that will anyway not be available to students when the bans on certain extensions go into effect) where we know that something might go wrong (due to shadowing, or unexpected namings, function identification not taking qualified imports into account or similar; like the stuff you mentioned "hlint is also doing/ignoring), but we simply accept that and will not pursue more strict analysis?

Then representative cases for that could be added to the test suite, with explicit declaration "We know this is flagged, even though it is a false positive; or We know this will not be found, despite it actually being a positive." In order to not lose that knowledge.

Similarly to how for the inlining-checker there are cases in the test suite of "Actually, one could inline here, but we decided to not complicate the implementation to catch this case as well."

@jvoigtlaender
jvoigtlaender merged commit 4c1a7b6 into fmidue:master Nov 30, 2026
6 checks passed
@nimec01
nimec01 deleted the maybe branch November 30, 2026 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants