Why aren't these two types equivalent?

Given the following types:

type A = Ann ()

type Exp    = ExpA ()
type ExpA a = Ann a (IExpA a)
type IExp   = IExpA ()

type OptionalExpA a = Ann a (Maybe (IExpA a))
type OptionalExp    = OptionalExpA ()

r :: [a] -> [a]
r = reverse

argStmt :: (HasPositions b, HappyStatement s)
        => Token
        -> b
        -> [A (Maybe IExp)]
        -> ArgSpec
        -> ([Arg] -> s)
        -> P Stmt

I am getting an error message:

/home/ppelleti/prg/hs/language-intybasic/.stack-work/dist/x86_64-linux-tinfo6/Cabal-3.2.1.0/build/Language/IntyBasic/HappyParser.hs:514:44: error:
    ā€¢ Couldn't match type ā€˜Ann () (IExpA ())ā€™ with ā€˜IExpA ()ā€™
      Expected type: [A (Maybe IExp)]
        Actual type: [OptionalExp]
    ā€¢ In the third argument of ā€˜argStmtā€™, namely ā€˜(r happy_var_2)ā€™
      In the expression:
        (argStmt
           happy_var_1 happy_var_2 (r happy_var_2) borderArgs SBorder)
      In a case alternative:
          (HappyWrap13 happy_var_2)
            -> (argStmt
                  happy_var_1 happy_var_2 (r happy_var_2) borderArgs SBorder)
    |
514 |         ( argStmt happy_var_1 happy_var_2 (r happy_var_2) borderArgs SBorder)}})
    |                                            ^^^^^^^^^^^^^

However, if I simply change:

type OptionalExp    = OptionalExpA ()

to:

type OptionalExp    = A (Maybe IExp)

then my program compiles without error.

Why is there a difference between OptionalExpA () and A (Maybe IExp), given that both ought to expand to Ann () (Maybe (IExpA ())?

Iā€™m using LTS-18.22. (Therefore, GHC 8.10.7.)

Is there any chance you can simplify your example to something that demonstrates the same problem but without depending upon an external module? That would make it easier for someone to track down the error.

I can try, but it might be difficult to reproduce. It was actually compiling fine until I changed some unrelated code, and then it started compiling again once I changed the type that I mentioned in my post. So it seems like thereā€™s some sort of ā€œspooky action at a distanceā€ going on, but Iā€™ll see what I can do.

(And currently, some of the code in question is being generated by Happy, so Iā€™ll have to see if I can cut that out and reproduce it with plain Haskell code.)

Actually, I just now tried going back in my Git history to the point where it wasnā€™t compiling before. And now that same code is compiling. So now Iā€™m very confused. Either the failure is nondeterministic (or dependent on some sort of build state that is left behind) or else Iā€™m just losing my mind, and what I said in my original post isnā€™t actually what happened.

But whatever the reason, I donā€™t seem to be able to reproduce the problem, so I guess thereā€™s nothing to be done at this point.

1 Like

I wonder if a stray particle from outer space caused disturbance in the operation, resulting in this error.

1 Like