Skip to content

Commit 886e9cf

Browse files
committedSep 5, 2019
Make singling preserve type variable order in type signatures
This patch tweaks `singType` (and its sister function `singCtor`) to produce type signatures that preserve the order in which type variables appear. For instance, if one writes this: ```hs const2 :: forall b a. a -> b -> a ``` Then its singled counterpart will now quantify `b` and `a` in the same order: ```hs sConst2 :: forall b a (x :: a) (y :: b). Sing x -> Sing y -> Sing (Const2 x y) ``` This is important if one wants to combine `sConst2` with visible type applications, as this means that the meaning of `@T1 @T2` in `const @t1 @T2` will be preserved when one writes `sConst2 @t1 @T2`. This also paves the way for #378, which seeks to fully support promoting/singling type applications in the TH machinery. Most of the interesting parts of this patch are described in `Note [Preserve the order of type variables during singling]` in `D.S.Single.Type`, so start that is a good starting point when looking at this patch for the first time. Fixes chunk (1) in #378 (comment), but there are still other parts of #378 that remain to be fixed.
1 parent 9abd345 commit 886e9cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+574
-197
lines changed
 

Diff for: ‎CHANGES.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Changelog for singletons project
44
next
55
----
66
* Require GHC 8.10.
7+
* `singletons` now does a much better job of preserving the order of type
8+
variables when singling the type signatures of top-level functions and data
9+
constructors. See the `Support for TypeApplications` section of the `README`
10+
for more details.
711
* `singletons` now does a more much thorough job of rejecting higher-rank types
812
during promotion or singling, as `singletons` cannot support them.
913
(Previously, `singletons` would sometimes accept them, often changing rank-2

Diff for: ‎README.md

+36
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,42 @@ mechanism will not promote `TypeRep` to `*`.
712712
of types with which to work. See the Haddock documentation for the function
713713
`singletonStar` for more info.
714714

715+
Support for `TypeApplications`
716+
------------------------------
717+
718+
`singletons` currently cannot handle promoting or singling code that uses
719+
`TypeApplications` syntax, so `singletons` will simply drop any visible type
720+
applications. For example, `id @Bool True` will be promoted to `Id True` and
721+
singled to `sId STrue`. See
722+
[#378](https://github.com/goldfirere/singletons/issues/378) for a discussion
723+
of how `singletons` may support `TypeApplications` in the future.
724+
725+
On the other hand, `singletons` does make an effort to preserve the order of
726+
type variables when singling type signatures. For example, this type signature:
727+
728+
```haskell
729+
const2 :: forall b a. a -> b -> a
730+
```
731+
732+
Will single to the following:
733+
734+
```haskell
735+
sConst2 :: forall b a (x :: a) (y :: a). Sing x -> Sing y -> Sing (Const x y)
736+
```
737+
738+
Therefore, writing `const2 @T1 @T2` works just as well as writing
739+
`sConst2 @T1 @T2`, since the type signatures for `const2` and `sConst2` both
740+
begin with `forall b a.`, in that order. Again, it is worth emphasizing that
741+
the TH machinery does not support singling `const2 @T1 @T2` directly, but you
742+
can write the type applications by hand if you so choose.
743+
744+
It is not yet guaranteed that promotion preserves the order of type variables.
745+
For instance, if one writes `const @T1 @T2`, then one would have to write
746+
`Const @T2 @T1` at the kind level (and similarly for `Const`'s
747+
defunctionalization symbols). See
748+
[#378](https://github.com/goldfirere/singletons/issues/378) for a discussion
749+
of how this may be fixed in the future.
750+
715751
Known bugs
716752
----------
717753

0 commit comments

Comments
 (0)