Skip to content

Commit

Permalink
Move max_, min_, all_, any_, sum_, and product_ to *.Disambiguation m…
Browse files Browse the repository at this point in the history
…odules

This moves `max_` and `min_` to `D.S.Prelude.Ord.Disambiguation`, and
`all_`, `any_`, `sum_`, and `product_` to
`D.S.Prelude.Semigroup.Internal.Disambiguation`. This has two
benefits:

1. This brings their treatment in line with existing precedent, as
   there is already a `D.S.Prelude.List.Internal.Disambiguation`
   module.
2. The defunctionalization symbols used on the right-hand sides of
   `Max_` and `Min_` no longer clash with other definitions in
   `D.S.Prelude.Semigroup.Internal`, so they can now be defined
   using Template Haskell, which cuts out quite a bit of boilerplate.
  • Loading branch information
RyanGlScott committed Dec 22, 2019
1 parent e24d655 commit d06c203
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 50 deletions.
2 changes: 2 additions & 0 deletions singletons.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ library
Data.Singletons.Prelude.List.Internal
Data.Singletons.Prelude.List.Internal.Disambiguation
Data.Singletons.Prelude.Monad.Internal
Data.Singletons.Prelude.Ord.Disambiguation
Data.Singletons.Prelude.Semigroup.Internal
Data.Singletons.Prelude.Semigroup.Internal.Disambiguation
Data.Singletons.Promote
Data.Singletons.Promote.Monad
Data.Singletons.Promote.Eq
Expand Down
1 change: 1 addition & 0 deletions src/Data/Singletons/Prelude/Foldable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ import Data.Singletons.Prelude.Semigroup.Internal
, LastSym0, LastSym1, SLast
, ProductSym0(..), ProductSym1, SProduct
, SumSym0(..), SumSym1, SSum )
import Data.Singletons.Prelude.Semigroup.Internal.Disambiguation
import Data.Singletons.Promote
import Data.Singletons.Single
import Data.Singletons.TypeLits.Internal
Expand Down
36 changes: 36 additions & 0 deletions src/Data/Singletons/Prelude/Ord/Disambiguation.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

-----------------------------------------------------------------------------
-- |
-- Module : Data.Singletons.Prelude.Ord.Disambiguation
-- Copyright : (C) 2019 Ryan Scott
-- License : BSD-style (see LICENSE)
-- Maintainer : Ryan Scott
-- Stability : experimental
-- Portability : non-portable
--
-- TODO RGS
--
----------------------------------------------------------------------------

module Data.Singletons.Prelude.Ord.Disambiguation where

import Data.Singletons.Prelude.Ord
import Data.Singletons.Single

-- We need these in Data.Singletons.Prelude.Semigroup, as we need to promote
-- code that simultaneously uses the Min/Max constructors and the min/max
-- functions, which have clashing defunctionalization symbol names. Our
-- workaround is to simply define synonyms for min/max and use those instead.
$(singletons [d|
min_, max_ :: Ord a => a -> a -> a
min_ = min
max_ = max
|])
1 change: 1 addition & 0 deletions src/Data/Singletons/Prelude/Semigroup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import Data.Singletons.Prelude.Monoid hiding
import Data.Singletons.Prelude.Num
import Data.Singletons.Prelude.Ord hiding
(MinSym0, MinSym1, MaxSym0, MaxSym1)
import Data.Singletons.Prelude.Ord.Disambiguation
import Data.Singletons.Prelude.Semigroup.Internal
import Data.Singletons.Prelude.Show
import Data.Singletons.Prelude.Traversable
Expand Down
50 changes: 0 additions & 50 deletions src/Data/Singletons/Prelude/Semigroup/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import Data.Singletons.Prelude.Instances
import Data.Singletons.Prelude.Monad.Internal
import Data.Singletons.Prelude.Num
import Data.Singletons.Prelude.Ord hiding (MinSym0, MinSym1, MaxSym0, MaxSym1)
import Data.Singletons.Promote
import Data.Singletons.Single
import Data.Singletons.TypeLits.Internal
import Data.Singletons.Util
Expand Down Expand Up @@ -231,52 +230,3 @@ instance SSemigroup Symbol where
ex = someSymbolVal $ T.unpack $ a <> b
in case ex of
SomeSymbol (_ :: Proxy ab) -> unsafeCoerce (SSym :: Sing ab)

-- We need these in Data.Singletons.Prelude.Semigroup, as we need to promote
-- code that simultaneously uses the Min/Max constructors and the min/max
-- functions, which have clashing defunctionalization symbol names. Our
-- workaround is to simply define synonyms for min/max and use those instead.
min_, max_ :: Ord a => a -> a -> a
min_ = min
max_ = max

type Min_ x y = Min x y
type Max_ x y = Max x y
$(genDefunSymbols [''Min_, ''Max_])

sMin_ :: forall a (x :: a) (y :: a). SOrd a => Sing x -> Sing y -> Sing (x `Min_` y)
sMin_ = sMin

sMax_ :: forall a (x :: a) (y :: a). SOrd a => Sing x -> Sing y -> Sing (x `Max_` y)
sMax_ = sMax

-- We need these in Data.Singletons.Prelude.Foldable.
all_ :: Bool -> All
all_ = All

any_ :: Bool -> Any
any_ = Any

sum_ :: a -> Sum a
sum_ = Sum

product_ :: a -> Product a
product_ = Product

type All_ a = 'All a
type Any_ a = 'Any a
type Sum_ a = 'Sum a
type Product_ a = 'Product a
$(genDefunSymbols [''All_, ''Any_, ''Sum_, ''Product_])

sAll_ :: forall (x :: Bool). Sing x -> Sing (All_ x)
sAll_ = SAll

sAny_ :: forall (x :: Bool). Sing x -> Sing (Any_ x)
sAny_ = SAny

sSum_ :: forall a (x :: a). Sing x -> Sing (Sum_ x)
sSum_ = SSum

sProduct_ :: forall a (x :: a). Sing x -> Sing (Product_ x)
sProduct_ = SProduct
42 changes: 42 additions & 0 deletions src/Data/Singletons/Prelude/Semigroup/Internal/Disambiguation.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

-----------------------------------------------------------------------------
-- |
-- Module : Data.Singletons.Prelude.Semigroup.Internal.Disambiguation
-- Copyright : (C) 2019 Ryan Scott
-- License : BSD-style (see LICENSE)
-- Maintainer : Ryan Scott
-- Stability : experimental
-- Portability : non-portable
--
-- TODO RGS
--
----------------------------------------------------------------------------

module Data.Singletons.Prelude.Semigroup.Internal.Disambiguation where

import Data.Semigroup
import Data.Singletons.Prelude.Semigroup.Internal
import Data.Singletons.Single

-- We need these in Data.Singletons.Prelude.Foldable.
$(singletons [d|
all_ :: Bool -> All
all_ = All

any_ :: Bool -> Any
any_ = Any

sum_ :: a -> Sum a
sum_ = Sum

product_ :: a -> Product a
product_ = Product
|])

0 comments on commit d06c203

Please sign in to comment.