Skip to content

Look for redundant guard conditions - #84

Open
nimec01 wants to merge 38 commits into
fmidue:masterfrom
nimec01:guards-check
Open

nimec01 wants to merge 38 commits into
fmidue:masterfrom
nimec01:guards-check

Conversation

@nimec01

@nimec01 nimec01 commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

No description provided.

Comment thread haskell-template-task.cabal Resolved
@nimec01
nimec01 marked this pull request as ready for review December 2, 2026 17:29
let code =
[SI.i|module Main where
test t
| t < 0, t < -5 = 0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What about redundancy within one guard line? Isn't t < 0 redundant in t < 0, t < -5?

@jvoigtlaender

This comment was marked as resolved.

@jvoigtlaender

Copy link
Copy Markdown
Member

Is otherwise picked up as basically making everything after it "redundant" (as in: "no further guard lines should follow at all")?

Say, someone has

test t
  | t < 4 = 0
  | t >= 4 = 1
  | t >= 9 = 2

At least t >= 4 will be reported as redundant, maybe also t >= 9 (is it?), so the student may next submit

test t
  | t < 4 = 0
  | otherwise = 1
  | t >= 9 = 2

or

test t
  | t < 4 = 0
  | t >= 4 = 1
  | otherwise = 2

or

test t
  | t < 4 = 0
  | otherwise = 1
  | otherwise = 2

In each of these three cases, what would be the detector's next response be?

@nimec01

nimec01 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

The newly added tests answer most questions. Redundancy within one guard is currently not reported.

otherwise currently doesn't receive any special treatment.

[SI.i|module Main where
test t | True = 0
|]
detect code `shouldReturn` [("test", Right [(Redundant, "True")])]

@jvoigtlaender jvoigtlaender Dec 3, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If anything would become simpler in the implementation without detecting this particular case (also with otherwise), I would be fine with going that way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It would only make a difference if we were to completely drop the check on definitions with a single guard.

| t < 10 = 2
|]
-- this result is weird because the whole conjunction is unreachable and not the two arguments
detect code `shouldReturn` [("test", (False, [(Unreachable, "t < 0"), (Unreachable, "t >= 0")]))]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the feedback shouldn't be about the conditions at all. It should be about that whole line being removable.

combineMaybeArithExprs :: Show a => Bool -> (AST -> AST -> Z3 AST) -> ScopedExp a -> ScopedExp a -> Maybe (Z3 AST)
combineMaybeArithExprs onInt f l r = liftM2 (\(_, a) (_, b) -> join $ liftM2 f a b) (toZ3 l) (toZ3 r)
where
toZ3 v = second (if onInt then (mkReal2Int =<<) else id) <$> arithAstToZ3 v

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are you sure this will give a semantic interpretation on the SMT side that is equivalent to the Haskell semantics of mod etc.?

I remember that Oliver had problems with that. The SMT solver had a different interpretation of integer division in the negatives than what Haskell uses. With the consequence that relying on semantic information from the SMT solver could lead to wrong analysis results regarding the Haskell program under consideration.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I didn't investigate that yet.

This branch has not been deployed

No deployments
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.

2 participants