Skip to content

Compare compile-to targets with alpha equivalence - #507

Draft
jespercockx wants to merge 3 commits into
masterfrom
issue479
Draft

jespercockx wants to merge 3 commits into
masterfrom
issue479

Conversation

@jespercockx

Copy link
Copy Markdown
Member

This fixes #479 by comparing the compiled Haskell code up to alpha equivalence for both bound variables and names of local helper functions.

Incidentally, it also updates the test runner so Succeed tests cannot produce any warnings, only Warn tests are allowed to do so.

@jespercockx jespercockx added this to the 1.5 milestone Nov 24, 2026
Comment thread src/Agda2Hs/Language/Haskell/Utils.hs Outdated
Comment on lines +165 to +168
eqName :: AlphaRenaming -> Name () -> Name () -> Bool
eqName ren x y = case lookup x ren of
Just y' -> y == y'
Nothing -> x == y && y `notElem` map snd ren

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.

The current code does not consider shadowing

Suggested change
eqName :: AlphaRenaming -> Name () -> Name () -> Bool
eqName ren x y = case lookup x ren of
Just y' -> y == y'
Nothing -> x == y && y `notElem` map snd ren
eqName :: AlphaRenaming -> Name () -> Name () -> Bool
eqName ren x y = case find (\(x', y') -> x' == x || y' == y) ren of
Just (x', y') -> x' == x && y' == y
Nothing -> x == y

@wasabi315 wasabi315 Nov 25, 2026 •

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.

\a b -> a and \b b -> b are not alpha equivalent but eqName [(b, b), (a, b)] a b computes to True currently

Comment thread src/Agda2Hs/Compile/Data.hs Outdated
Comment on lines +203 to +204
forM_ (zip args1 args2) $ \(b1, b2) ->
unless (b1 == b2) $ fail $
unless (alphaEq b1 b2) $ fail $

@wasabi315 wasabi315 Nov 25, 2026 •

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.

I believe alpha equivalence check here should be done under the identity renaming of type parameters, rather than the empty renaming.

@wasabi315 wasabi315 Nov 25, 2026 •

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.

Testcase:

data Foo (a b : Type) : Type where
  C : b → a → Foo a b
{-# COMPILE AGDA2HS Foo #-}

data Bar (a b : Type) : Type where
  C : a → b → Bar a b -- swapped!
{-# COMPILE AGDA2HS Bar to Foo #-}

Comment thread src/Agda2Hs/Language/Haskell/Utils.hs Outdated
eqAlpha ren' mvs mvs2
&& eqAlpha ren' mctx mctx2
&& eqAlpha ren' ih ih2
where ren' = zip (tyVarBinders mvs) (tyVarBinders mvs2) ++ ren

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.

ren' does not contain implicitly forall-quantified variables. mvs only contains vars bound by foralls we explicitly generate (for module parameters, for example).

@wasabi315 wasabi315 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.

Thank you for the fix! I found some places where the check is too permissive.

@jespercockx

Copy link
Copy Markdown
Member Author

Thank you for the review. I'm really sorry that this was such a sloppy PR. In the future, I will be more diligent to double-check the implementation myself before requesting a review.

For now, I can definitely fix the issues you identified but I'm not very confident in the correctness of this implementation. So perhaps it is better to postpone this until after the release of 1.5.

@jespercockx jespercockx modified the milestones: 1.5, 1.6 Nov 26, 2026
Succeed tests now run agda2hs with warnings enabled and fail if any are emitted, so warning-producing tests must live in the Warn directory. Fix the incidental warnings in the existing Succeed tests: remove a stale hiding clause and suppress the intentional unreachable-clause warnings in the indexed Vec instance tests.
The checks for a `to` pragma now compare the translated Haskell code up to
alpha-equivalence, so the names of local variables (pattern variables, type
variables, and lambda and do bound variables) and of auxiliary definitions from
a where block may differ between the source and the target.
- `eqName` now accounts for shadowing by resolving both names to their
  innermost binder instead of only checking the left name.
- The compile-to checks for datatypes compare the datatype parameters by
  identity rather than treating them as implicitly quantified, so swapped
  constructor argument types are rejected.
- `eqInstRule` includes the implicitly quantified variables of the whole
  instance rule in the renaming, so they are renamed consistently across
  the context and the head.

Add a regression test for each issue: a shadowing case in the function
clause warning test, a swapped-parameter datatype in a new fail test, and
an inconsistently reordered instance in the instance warning test.
@jespercockx
jespercockx marked this pull request as draft November 26, 2026 16:08

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.

Compare things with α-equivalence

2 participants