Skip to content

Commit

Permalink
Eliminate the dollar sign hack (#203)
Browse files Browse the repository at this point in the history
* Suffix symbol defun symbols with @#$%^%0@

* Eliminate the dollar-sign hack

* ConsSym0 and ConsSym1 aren't actually things, update them

* Use the suffix @#@ instead
  • Loading branch information
RyanGlScott authored Jul 7, 2017
1 parent 349a566 commit 74c9997
Show file tree
Hide file tree
Showing 58 changed files with 733 additions and 696 deletions.
16 changes: 16 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@ Changelog for singletons project

next
----
* The suffix for defunctionalized names of symbolic functions (e.g., `(+)`) has
changed. Before, the promoted type name would be suffixed with some number of
dollar signs (e.g., `(:+$)` and `(:+$$)`) to indicate defunctionalization
symbols. Now, the promoted type name is first suffixed with `@#@` and
_then_ followed by dollar signs (e.g., `(:+@#@$)` and `(:+@#@$$)`).
Adopting this conventional eliminates naming conflicts that could arise for
functions that consisted of solely `$` symbols.

* The promoted/singled/defunctionalized symbols for `($)` have changed:

* `($)` is now `(:$)`
* `(%$)` is now `(%:$)`
* `($$)` is now `($@#@$)`, and `($$$)` is now `($@#@$$)`

This makes the treatment of `($)` consistent with other symbolic functions.

* Add promoted and singled versions of `Show`, including `deriving` support.

* Permit derived `Ord` instances for empty datatypes.
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ generates. Here are some examples showing how this is done:

singleton constructor: `:%+:`

symbols: `:+:$`, `:+:$$`, `:+:$$$`
symbols: `:+:@#@$`, `:+:@#@$$`, `:+:@#@$$$`


5. original value: `pred`
Expand All @@ -392,7 +392,7 @@ generates. Here are some examples showing how this is done:

singleton value: `%:+`

symbols: `:+$`, `:+$$`, `:+$$$`
symbols: `:+@#@$`, `:+@#@$$`, `:+@#@$$$`


7. original class: `Num`
Expand All @@ -412,7 +412,8 @@ generates. Here are some examples showing how this is done:
Special names
-------------

There are some special cases, listed below (with asterisks\* denoting special treatment):
There are some special cases, listed below (with asterisks\* denoting special
treatment):

1. original datatype: `[]`

Expand All @@ -421,13 +422,13 @@ There are some special cases, listed below (with asterisks\* denoting special tr
singleton type\*: `SList`


2. original constructor: `[]`
2. original constructor: `[]`

promoted type: `'[]`
promoted type: `'[]`

singleton constructor\*: `SNil`
singleton constructor\*: `SNil`

symbols\*: `NilSym0`
symbols\*: `NilSym0`


3. original constructor: `:`
Expand All @@ -436,7 +437,7 @@ There are some special cases, listed below (with asterisks\* denoting special tr

singleton constructor\*: `SCons`

symbols\*: `ConsSym0`, `ConsSym1`
symbols: `:@#@$`, `:@#@$`


4. original datatype: `(,)`
Expand Down
22 changes: 12 additions & 10 deletions src/Data/Promotion/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module Data.Promotion.Prelude (
PShow(..), ShowS, SChar, show_, (:<>), Shows, ShowChar, ShowString, ShowParen,

-- ** Miscellaneous functions
Id, Const, (:.), type ($), type ($!), Flip, AsTypeOf, Until, Seq,
Id, Const, (:.), (:$), (:$!), Flip, AsTypeOf, Until, Seq,

-- * List operations
Map, (:++), Filter,
Expand Down Expand Up @@ -75,7 +75,9 @@ module Data.Promotion.Prelude (

-- * Defunctionalization symbols
FalseSym0, TrueSym0,
NotSym0, NotSym1, (:&&$), (:&&$$), (:&&$$$), (:||$), (:||$$), (:||$$$),
NotSym0, NotSym1,
(:&&@#@$), (:&&@#@$$), (:&&@#@$$$),
(:||@#@$), (:||@#@$$), (:||@#@$$$),
OtherwiseSym0,

NothingSym0, JustSym0, JustSym1,
Expand All @@ -95,27 +97,27 @@ module Data.Promotion.Prelude (
CurrySym0, CurrySym1, CurrySym2, CurrySym3,
UncurrySym0, UncurrySym1, UncurrySym2,

(:^$), (:^$$),
(:^@#@$), (:^@#@$$),

ShowsPrecSym0, ShowsPrecSym1, ShowsPrecSym2, ShowsPrecSym3,
Show_Sym0, Show_Sym1,
ShowListSym0, ShowListSym1, ShowListSym2,
(:<>$), (:<>$$), (:<>$$$),
(:<>@#@$), (:<>@#@$$), (:<>@#@$$$),
ShowsSym0, ShowsSym1, ShowsSym2,
ShowCharSym0, ShowCharSym1, ShowCharSym2,
ShowStringSym0, ShowStringSym1, ShowStringSym2,
ShowParenSym0, ShowParenSym1, ShowParenSym2,

IdSym0, IdSym1, ConstSym0, ConstSym1, ConstSym2,
(:.$), (:.$$), (:.$$$),
type ($$), type ($$$), type ($$$$),
type ($!$), type ($!$$), type ($!$$$),
(:.@#@$), (:.@#@$$), (:.@#@$$$),
(:$@#@$), (:$@#@$$), (:$@#@$$$),
(:$!@#@$), (:$!@#@$$), (:$!@#@$$$),
FlipSym0, FlipSym1, FlipSym2,
AsTypeOfSym0, AsTypeOfSym1, AsTypeOfSym2, SeqSym0, SeqSym1, SeqSym2,

(:$), (:$$), (:$$$), NilSym0,
(:@#@$), (:@#@$$), (:@#@$$$), NilSym0,
MapSym0, MapSym1, MapSym2, ReverseSym0, ReverseSym1,
(:++$$), (:++$), HeadSym0, HeadSym1, LastSym0, LastSym1,
(:++@#@$$), (:++@#@$), HeadSym0, HeadSym1, LastSym0, LastSym1,
TailSym0, TailSym1, InitSym0, InitSym1, NullSym0, NullSym1,

FoldlSym0, FoldlSym1, FoldlSym2, FoldlSym3,
Expand Down Expand Up @@ -159,7 +161,7 @@ module Data.Promotion.Prelude (
BreakSym0, BreakSym1, BreakSym2,
LookupSym0, LookupSym1, LookupSym2,
FilterSym0, FilterSym1, FilterSym2,
(:!!$), (:!!$$), (:!!$$$),
(:!!@#@$), (:!!@#@$$), (:!!@#@$$$),
) where

import Data.Promotion.Prelude.Base
Expand Down
10 changes: 5 additions & 5 deletions src/Data/Promotion/Prelude/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@

module Data.Promotion.Prelude.Base (
-- * Promoted functions from @GHC.Base@
Foldr, Map, (:++), Otherwise, Id, Const, (:.), type ($), type ($!),
Foldr, Map, (:++), Otherwise, Id, Const, (:.), (:$), (:$!),
Flip, Until, AsTypeOf, Seq,

-- * Defunctionalization symbols
FoldrSym0, FoldrSym1, FoldrSym2, FoldrSym3,
MapSym0, MapSym1, MapSym2,
(:++$), (:++$$), (:++$$$),
(:++@#@$), (:++@#@$$), (:++@#@$$$),
OtherwiseSym0,
IdSym0, IdSym1,
ConstSym0, ConstSym1, ConstSym2,
(:.$), (:.$$), (:.$$$), (:.$$$$),
type ($$), type ($$$), type ($$$$),
type ($!$), type ($!$$), type ($!$$$),
(:.@#@$), (:.@#@$$), (:.@#@$$$), (:.@#@$$$$),
(:$@#@$), (:$@#@$$), (:$@#@$$$),
(:$!@#@$), (:$!@#@$$), (:$!@#@$$$),
FlipSym0, FlipSym1, FlipSym2, FlipSym3,
UntilSym0, UntilSym1, UntilSym2, UntilSym3,
AsTypeOfSym0, AsTypeOfSym1, AsTypeOfSym2,
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Promotion/Prelude/Bool.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ module Data.Promotion.Prelude.Bool (
TrueSym0, FalseSym0,

NotSym0, NotSym1,
(:&&$), (:&&$$), (:&&$$$),
(:||$), (:||$$), (:||$$$),
(:&&@#@$), (:&&@#@$$), (:&&@#@$$$),
(:||@#@$), (:||@#@$$), (:||@#@$$$),
Bool_Sym0, Bool_Sym1, Bool_Sym2, Bool_Sym3,
OtherwiseSym0
) where
Expand Down
4 changes: 3 additions & 1 deletion src/Data/Promotion/Prelude/Eq.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

{-# LANGUAGE ExplicitNamespaces #-}
module Data.Promotion.Prelude.Eq (
PEq(..), (:==$), (:==$$), (:==$$$), (:/=$), (:/=$$), (:/=$$$)
PEq(..),
(:==@#@$), (:==@#@$$), (:==@#@$$$),
(:/=@#@$), (:/=@#@$$), (:/=@#@$$$)
) where

import Data.Singletons.Prelude.Eq
8 changes: 4 additions & 4 deletions src/Data/Promotion/Prelude/Function.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@

module Data.Promotion.Prelude.Function (
-- * "Prelude" re-exports
Id, Const, (:.), Flip, type ($)
Id, Const, (:.), Flip, (:$)
-- * Other combinators
, (:&), On

-- * Defunctionalization symbols
, IdSym0, IdSym1
, ConstSym0, ConstSym1, ConstSym2
, (:.$), (:.$$), (:.$$$), (:.$$$$)
, (:.@#@$), (:.@#@$$), (:.@#@$$$), (:.@#@$$$$)
, FlipSym0, FlipSym1, FlipSym2, FlipSym3
, type ($$), type ($$$), type ($$$$)
, (:&$), (:&$$), (:&$$$)
, (:$@#@$), (:$@#@$$), (:$@#@$$$)
, (:&@#@$), (:&@#@$$), (:&@#@$$$)
, OnSym0, OnSym1, OnSym2, OnSym3, OnSym4
) where

Expand Down
9 changes: 5 additions & 4 deletions src/Data/Promotion/Prelude/List.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ module Data.Promotion.Prelude.List (

-- * Defunctionalization symbols
NilSym0,
(:$), (:$$), (:$$$),
(:@#@$), (:@#@$$), (:@#@$$$),

(:++$$$), (:++$$), (:++$), HeadSym0, HeadSym1, LastSym0, LastSym1,
(:++@#@$$$), (:++@#@$$), (:++@#@$),
HeadSym0, HeadSym1, LastSym0, LastSym1,
TailSym0, TailSym1, InitSym0, InitSym1, NullSym0, NullSym1,

MapSym0, MapSym1, MapSym2, ReverseSym0, ReverseSym1,
Expand Down Expand Up @@ -157,7 +158,7 @@ module Data.Promotion.Prelude.List (
Unzip7Sym0, Unzip7Sym1,

DeleteSym0, DeleteSym1, DeleteSym2,
(:\\$), (:\\$$), (:\\$$$),
(:\\@#@$), (:\\@#@$$), (:\\@#@$$$),
IntersectSym0, IntersectSym1, IntersectSym2,

InsertSym0, InsertSym1, InsertSym2,
Expand Down Expand Up @@ -193,7 +194,7 @@ module Data.Promotion.Prelude.List (
FilterSym0, FilterSym1, FilterSym2,
PartitionSym0, PartitionSym1, PartitionSym2,

(:!!$), (:!!$$), (:!!$$$),
(:!!@#@$), (:!!@#@$$), (:!!@#@$$$),

ElemIndexSym0, ElemIndexSym1, ElemIndexSym2,
ElemIndicesSym0, ElemIndicesSym1, ElemIndicesSym2,
Expand Down
6 changes: 3 additions & 3 deletions src/Data/Promotion/Prelude/List/NonEmpty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module Data.Promotion.Prelude.List.NonEmpty (
Xor,

-- * Defunctionalization symbols
(:|$), (:|$$), (:|$$$),
(:|@#@$), (:|@#@$$), (:|@#@$$$),
MapSym0, MapSym1, MapSym2,
IntersperseSym0, IntersperseSym1, IntersperseSym2,
ScanlSym0, ScanlSym1, ScanlSym2, ScanlSym3,
Expand All @@ -84,7 +84,7 @@ module Data.Promotion.Prelude.List.NonEmpty (
TailSym0, TailSym1,
LastSym0, LastSym1,
InitSym0, InitSym1,
(:<|$), (:<|$$), (:<|$$$),
(:<|@#@$), (:<|@#@$$), (:<|@#@$$$),
ConsSym0, ConsSym1, ConsSym2,
UnconsSym0, UnconsSym1,
UnfoldrSym0, UnfoldrSym1, UnfoldrSym2,
Expand Down Expand Up @@ -114,7 +114,7 @@ module Data.Promotion.Prelude.List.NonEmpty (
IsPrefixOfSym0, IsPrefixOfSym1, IsPrefixOfSym2,
NubSym0, NubSym1,
NubBySym0, NubBySym1, NubBySym2,
(:!!$), (:!!$$), (:!!$$$),
(:!!@#@$), (:!!@#@$$), (:!!@#@$$$),
ZipSym0, ZipSym1, ZipSym2,
ZipWithSym0, ZipWithSym1, ZipWithSym2, ZipWithSym3,
UnzipSym0, UnzipSym1,
Expand Down
6 changes: 3 additions & 3 deletions src/Data/Promotion/Prelude/Num.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ module Data.Promotion.Prelude.Num (
PNum(..), Subtract,

-- ** Defunctionalization symbols
(:+$), (:+$$), (:+$$$),
(:-$), (:-$$), (:-$$$),
(:*$), (:*$$), (:*$$$),
(:+@#@$), (:+@#@$$), (:+@#@$$$),
(:-@#@$), (:-@#@$$), (:-@#@$$$),
(:*@#@$), (:*@#@$$), (:*@#@$$$),
NegateSym0, NegateSym1,
AbsSym0, AbsSym1,
SignumSym0, SignumSym1,
Expand Down
8 changes: 4 additions & 4 deletions src/Data/Promotion/Prelude/Ord.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ module Data.Promotion.Prelude.Ord (
POrd(..),
LTSym0, EQSym0, GTSym0,
CompareSym0, CompareSym1, CompareSym2,
(:<$), (:<$$), (:<$$$),
(:<=$), (:<=$$), (:<=$$$),
(:>$), (:>$$), (:>$$$),
(:>=$), (:>=$$), (:>=$$$),
(:<@#@$), (:<@#@$$), (:<@#@$$$),
(:<=@#@$), (:<=@#@$$), (:<=@#@$$$),
(:>@#@$), (:>@#@$$), (:>@#@$$$),
(:>=@#@$), (:>=@#@$$), (:>=@#@$$$),
MaxSym0, MaxSym1, MaxSym2,
MinSym0, MinSym1, MinSym2
) where
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Promotion/Prelude/Show.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Data.Promotion.Prelude.Show (
ShowsPrecSym0, ShowsPrecSym1, ShowsPrecSym2, ShowsPrecSym3,
Show_Sym0, Show_Sym1,
ShowListSym0, ShowListSym1, ShowListSym2,
(:<>$), (:<>$$), (:<>$$$),
(:<>@#@$), (:<>@#@$$), (:<>@#@$$$),
ShowsSym0, ShowsSym1, ShowsSym2,
ShowListWithSym0, ShowListWithSym1, ShowListWithSym2, ShowListWithSym3,
ShowCharSym0, ShowCharSym1, ShowCharSym2,
Expand Down
30 changes: 29 additions & 1 deletion src/Data/Singletons/Names.hs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ promoteTySym name sat
= let capped = toUpcaseStr noPrefix name in
if isHsLetter (head capped)
then mkName (capped ++ "Sym" ++ (show sat))
else mkName (capped ++ (replicate (sat + 1) '$'))
else mkName (capped ++ "@#@" -- See Note [Defunctionalization symbol suffixes]
++ (replicate (sat + 1) '$'))

promoteClassName :: Name -> Name
promoteClassName = prefixUCName "P" "#"
Expand Down Expand Up @@ -249,3 +250,30 @@ foldApply = foldl apply
-- make and equality predicate
mkEqPred :: DType -> DType -> DPred
mkEqPred ty1 ty2 = foldl DAppPr (DConPr equalityName) [ty1, ty2]

{-
Note [Defunctionalization symbol suffixes]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Before, we used to denote defunctionalization symbols by simply appending dollar
signs at the end (e.g., (:+$) and (:+$$)). But this can lead to ambiguity when you
have function names that consist of solely $ characters. For instance, if you
tried to promote ($) and ($$) simultaneously, you'd get these promoted types:
:$
:$$
And these defunctionalization symbols:
:$$
:$$$
But now there's a name clash between the promoted type for ($) and the
defunctionalization symbol for ($$)! The solution is to use a precede these
defunctionalization dollar signs with another string (we choose @#@).
So now the new defunctionalization symbols would be:
:$@#@$
:$@#@$$
And there is no conflict.
-}
18 changes: 10 additions & 8 deletions src/Data/Singletons/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module Data.Singletons.Prelude (
Shows, sShows, ShowChar, sShowChar, ShowString, sShowString, ShowParen, sShowParen,

-- ** Miscellaneous functions
Id, sId, Const, sConst, (:.), (%:.), type ($), (%$), type ($!), (%$!),
Id, sId, Const, sConst, (:.), (%:.), (:$), (%:$), (:$!), (%:$!),
Flip, sFlip, AsTypeOf, sAsTypeOf,
Seq, sSeq,

Expand Down Expand Up @@ -94,7 +94,9 @@ module Data.Singletons.Prelude (

-- * Defunctionalization symbols
FalseSym0, TrueSym0,
NotSym0, NotSym1, (:&&$), (:&&$$), (:&&$$$), (:||$), (:||$$), (:||$$$),
NotSym0, NotSym1,
(:&&@#@$), (:&&@#@$$), (:&&@#@$$$),
(:||@#@$), (:||@#@$$), (:||@#@$$$),
OtherwiseSym0,

NothingSym0, JustSym0, JustSym1,
Expand All @@ -117,22 +119,22 @@ module Data.Singletons.Prelude (
ShowsPrecSym0, ShowsPrecSym1, ShowsPrecSym2, ShowsPrecSym3,
Show_Sym0, Show_Sym1,
ShowListSym0, ShowListSym1, ShowListSym2,
(:<>$), (:<>$$), (:<>$$$),
(:<>@#@$), (:<>@#@$$), (:<>@#@$$$),
ShowsSym0, ShowsSym1, ShowsSym2,
ShowCharSym0, ShowCharSym1, ShowCharSym2,
ShowStringSym0, ShowStringSym1, ShowStringSym2,
ShowParenSym0, ShowParenSym1, ShowParenSym2,

IdSym0, IdSym1, ConstSym0, ConstSym1, ConstSym2,
(:.$), (:.$$), (:.$$$),
type ($$), type ($$$), type ($$$$),
type ($!$), type ($!$$), type ($!$$$),
(:.@#@$), (:.@#@$$), (:.@#@$$$),
(:$@#@$), (:$@#@$$), (:$@#@$$$),
(:$!@#@$), (:$!@#@$$), (:$!@#@$$$),
FlipSym0, FlipSym1, FlipSym2,
AsTypeOfSym0, AsTypeOfSym1, AsTypeOfSym2, SeqSym0, SeqSym1, SeqSym2,

(:$), (:$$), (:$$$), NilSym0,
(:@#@$), (:@#@$$), (:@#@$$$), NilSym0,
MapSym0, MapSym1, MapSym2, ReverseSym0, ReverseSym1,
(:++$$), (:++$), HeadSym0, HeadSym1, LastSym0, LastSym1,
(:++@#@$$), (:++@#@$), HeadSym0, HeadSym1, LastSym0, LastSym1,
TailSym0, TailSym1, InitSym0, InitSym1, NullSym0, NullSym1,

FoldlSym0, FoldlSym1, FoldlSym2, FoldlSym3,
Expand Down
Loading

0 comments on commit 74c9997

Please sign in to comment.