ghc-plugin-non-empty
ghc-plugin-non-empty copied to clipboard
Handle arithmetic sequences
Another popular use case where using ordinary lists is easier than NonEmpty if arithmetic sequences like [1 .. 10]. Adding support for them would be much appreciated!
I anticipate quite a lot of difficulty with this, so I propose to do this in steps (probably in multiple PRs) with increasing difficulty:
- [ ] Implement increasing sequences for
Int - [ ] Support more types (ideally, now we should have a list of types):
Integer,Natural,Word,Word32, etc. - [ ] Support decreasing sequence
The [from .. to] sequence works for any enumeration but we don't know the implementation of enumerations in general so we can't solve this problem universally. However, handling some common cases would be great as well!
I already have some progress on the first task on the add-int-ranges on my fork.
The challenge here is that integers are wrapped on HsOverLit, which holds also an HsApp representing the inner fromInteger that converts them to HsLit. I have never done in my Haskell experience so nasty pattern matchings 💀
...and the lack of good debugging/documentation on the GHC.* packages are not helpful at all
Still, with a few extra hours I can have a PR 🎉
Welp... got it to work with successful test cases:
[1..1] -> 1 :| []
[1..10] -> 1 :| [2..10]
and also, the following example evaluates to [] as expected (i.e. plugin doesn't touch it)
overloadedListEmptySequence :: [Int]
overloadedListEmptySequence = [10..0]
however, when changing the data-type of the above example to NonEmpty Int, ghc goes bananas:
[4 of 4] Compiling Main
ghc-9.2.4: panic! (the 'impossible' happened)
(GHC version 9.2.4:
bind_args
[]
[a1_a4WO, a2_a4WP]
[]
scrut: [] @Int
Call stack:
CallStack (from HasCallStack):
callStackDoc, called at compiler/GHC/Utils/Panic.hs:181:37 in ghc:GHC.Utils.Panic
pprPanic, called at compiler/GHC/Core/Opt/Simplify.hs:3244:7 in ghc:GHC.Core.Opt.Simplify
Please report this as a GHC bug: https://www.haskell.org/ghc/reportabug
I'll give it another try tomorrow with a fresh mind.
@german1608 You can introduce some helper pattern synonyms to the make the implementation simple 🙂
On the GHC panic, I can't advice, unfortunately 😞 I'm not a GHC internals expert.