singleton-bool-0.1.6/0000755000000000000000000000000007346545000012642 5ustar0000000000000000singleton-bool-0.1.6/CHANGELOG.md0000644000000000000000000000106307346545000014453 0ustar0000000000000000- 0.1.6 - Add `boring` instances - Add `some` (`GEq` etc) instances - Add `deepseq` instances - Require GHC-7.8+ - 0.1.5 - Add `discreteBool :: Dec (a :~: b)` (GHC-7.8+) - Add `Show`, `Eq`, `Ord` `SBool b` instances. - 0.1.4 - Add `fromSBool` and `withSomeSBool`. - 0.1.3.0 - Add `reifyBool` and `reflectBool`. - Drop GHC-7.4 support (broken `PolyKinds`) - 0.1.2.0 - Enable `PolyKinds` on GHC >= 7.6 - Add `sboolEqRefl :: SBoolI (a == b) => Maybe (a :~: b)` - 0.1.1.0 - Add `eqToRefl`, `eqCast`, `trivialRefl` singleton-bool-0.1.6/LICENSE0000644000000000000000000000276207346545000013656 0ustar0000000000000000Copyright (c) 2016, Oleg Grenrus All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Oleg Grenrus nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. singleton-bool-0.1.6/Setup.hs0000644000000000000000000000005607346545000014277 0ustar0000000000000000import Distribution.Simple main = defaultMain singleton-bool-0.1.6/singleton-bool.cabal0000644000000000000000000000224607346545000016565 0ustar0000000000000000cabal-version: >=1.10 name: singleton-bool version: 0.1.6 synopsis: Type level booleans description: Type level booleans. . @singletons@ package provides similar functionality, but it has tight dependency constraints. category: Web homepage: https://github.com/phadej/singleton-bool#readme bug-reports: https://github.com/phadej/singleton-bool/issues author: Oleg Grenrus maintainer: Oleg Grenrus license: BSD3 license-file: LICENSE build-type: Simple extra-source-files: CHANGELOG.md tested-with: GHC ==7.8.4 || ==7.10.3 || ==8.0.2 || ==8.2.2 || ==8.4.4 || ==8.6.5 || ==8.8.4 || ==8.10.4 || ==9.0.1 source-repository head type: git location: https://github.com/phadej/singleton-bool library hs-source-dirs: src ghc-options: -Wall build-depends: base >=4.7 && <4.16 , boring >=0.2 && <0.3 , dec >=0.0.3 && <0.1 , deepseq >=1.3 && <1.5 , some >=1.0.3 && <1.1 exposed-modules: Data.Singletons.Bool default-language: Haskell2010 singleton-bool-0.1.6/src/Data/Singletons/0000755000000000000000000000000007346545000016427 5ustar0000000000000000singleton-bool-0.1.6/src/Data/Singletons/Bool.hs0000644000000000000000000001554407346545000017667 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE EmptyCase #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeOperators #-} #if __GLASGOW_HASKELL__ >= 800 {-# OPTIONS_GHC -Wno-redundant-constraints #-} #endif -- | Additions to "Data.Type.Bool". module Data.Singletons.Bool ( SBool(..), SBoolI(..), fromSBool, withSomeSBool, reflectBool, reifyBool, -- * Data.Type.Dec -- | 'discreteBool' is available with @base >= 4.7@ (GHC-7.8) discreteBool, -- * Data.Type.Bool and .Equality -- | These are only defined with @base >= 4.7@ sboolAnd, sboolOr, sboolNot, eqToRefl, eqCast, sboolEqRefl, trivialRefl, ) where import Control.DeepSeq (NFData (..)) import Data.Boring (Boring (..)) import Data.GADT.Compare (GCompare (..), GEq (..), GOrdering (..)) import Data.GADT.DeepSeq (GNFData (..)) import Data.GADT.Show (GRead (..), GShow (..)) import Data.Proxy (Proxy (..)) import Data.Type.Bool import Data.Type.Dec (Dec (..)) import Data.Type.Equality import Unsafe.Coerce (unsafeCoerce) import qualified Data.Some.Church as Church -- $setup -- >>> :set -XDataKinds -XTypeOperators -- >>> import Data.Proxy (Proxy (..)) -- >>> import Data.Type.Dec -- >>> import Data.Some -- >>> import Data.GADT.Compare -- >>> import Data.GADT.Show -- >>> import Data.Type.Equality data SBool (b :: Bool) where STrue :: SBool 'True SFalse :: SBool 'False class SBoolI (b :: Bool) where sbool :: SBool b instance SBoolI 'True where sbool = STrue instance SBoolI 'False where sbool = SFalse -- | @since 0.1.5 instance Show (SBool b) where showsPrec _ STrue = showString "STrue" showsPrec _ SFalse = showString "SFalse" -- | @since 0.1.5 instance Eq (SBool b) where _ == _ = True -- | @since 0.1.5 instance Ord (SBool b) where compare _ _ = EQ -- | @since 0.1.6 instance NFData (SBool b) where rnf STrue = () rnf SFalse = () ------------------------------------------------------------------------------- -- conversion to and from explicit SBool values ------------------------------------------------------------------------------- -- | Convert an 'SBool' to the corresponding 'Bool'. -- -- @since 0.1.4 fromSBool :: SBool b -> Bool fromSBool STrue = True fromSBool SFalse = False -- | Convert a normal 'Bool' to an 'SBool', passing it into a continuation. -- -- >>> withSomeSBool True fromSBool -- True -- -- @since 0.1.4 withSomeSBool :: Bool -> (forall b. SBool b -> r) -> r withSomeSBool True f = f STrue withSomeSBool False f = f SFalse ------------------------------------------------------------------------------- -- reify & reflect ------------------------------------------------------------------------------- -- | Reify 'Bool' to type-level. -- -- >>> reifyBool True reflectBool -- True -- reifyBool :: forall r. Bool -> (forall b. SBoolI b => Proxy b -> r) -> r reifyBool True f = f (Proxy :: Proxy 'True) reifyBool False f = f (Proxy :: Proxy 'False) -- | Reflect to term-level. -- -- >>> reflectBool (Proxy :: Proxy 'True) -- True reflectBool :: forall b proxy. SBoolI b => proxy b -> Bool reflectBool _ = fromSBool (sbool :: SBool b) ------------------------------------------------------------------------------- -- Boring ------------------------------------------------------------------------------- -- | @since 0.1.6 instance SBoolI b => Boring (SBool b) where boring = sbool ------------------------------------------------------------------------------- -- Data.GADT (some) ------------------------------------------------------------------------------- -- | -- -- >>> geq STrue STrue -- Just Refl -- -- >>> geq STrue SFalse -- Nothing -- -- @since 0.1.6 instance GEq SBool where geq STrue STrue = Just Refl geq SFalse SFalse = Just Refl geq _ _ = Nothing -- | -- -- @since 0.1.6 instance GCompare SBool where gcompare SFalse SFalse = GEQ gcompare SFalse STrue = GLT gcompare STrue SFalse = GGT gcompare STrue STrue = GEQ -- | @since 0.1.6 instance GNFData SBool where grnf STrue = () grnf SFalse = () -- | -- -- >>> showsPrec 0 STrue "" -- "STrue" -- -- @since 0.1.6 instance GShow SBool where gshowsPrec = showsPrec -- | -- -- >>> readsPrec 0 "Some STrue" :: [(Some SBool, String)] -- [(Some STrue,"")] -- -- >>> readsPrec 0 "Some SFalse" :: [(Some SBool, String)] -- [(Some SFalse,"")] -- -- >>> readsPrec 0 "Some Else" :: [(Some SBool, String)] -- [] -- -- @since 0.1.6 instance GRead SBool where greadsPrec _ s = [ (Church.mkSome STrue, t) | ("STrue", t) <- lex s ] ++ [ (Church.mkSome SFalse, t) | ("SFalse", t) <- lex s ] ------------------------------------------------------------------------------- -- Discrete ------------------------------------------------------------------------------- -- | Decidable equality. -- -- >>> decShow (discreteBool :: Dec ('True :~: 'True)) -- "Yes Refl" -- -- @since 0.1.5 discreteBool :: forall a b. (SBoolI a, SBoolI b) => Dec (a :~: b) discreteBool = case (sbool :: SBool a, sbool :: SBool b) of (STrue, STrue) -> Yes Refl (STrue, SFalse) -> No $ \p -> case p of {} (SFalse, STrue) -> No $ \p -> case p of {} (SFalse, SFalse) -> Yes Refl ------------------------------------------------------------------------------- -- Witnesses ------------------------------------------------------------------------------- -- | >>> sboolAnd STrue SFalse -- SFalse sboolAnd :: SBool a -> SBool b -> SBool (a && b) sboolAnd SFalse _ = SFalse sboolAnd STrue b = b sboolOr :: SBool a -> SBool b -> SBool (a || b) sboolOr STrue _ = STrue sboolOr SFalse b = b sboolNot :: SBool a -> SBool (Not a) sboolNot STrue = SFalse sboolNot SFalse = STrue -- | @since 0.1.1.0 eqToRefl :: (a == b) ~ 'True => a :~: b eqToRefl = unsafeCoerce trivialRefl -- | @since 0.1.1.0 eqCast :: (a == b) ~ 'True => a -> b eqCast = unsafeCoerce -- | @since 0.1.1.0 trivialRefl :: () :~: () trivialRefl = Refl -- GHC 8.10+ requires that all kind variables be explicitly quantified after -- a `forall`. Technically, GHC has had the ability to do this since GHC 8.0, -- but GHC 8.0-8.4 require enabling TypeInType to do. To avoid having to faff -- around with CPP to enable TypeInType on certain GHC versions, we only -- explicitly quantify kind variables on GHC 8.6 or later, since those versions -- do not require TypeInType, only PolyKinds. # if __GLASGOW_HASKELL__ >= 806 # define KVS(kvs) kvs # else # define KVS(kvs) # endif -- | Useful combination of 'sbool' and 'eqToRefl' -- -- @since 0.1.2.0 sboolEqRefl :: forall KVS(k) (a :: k) (b :: k). SBoolI (a == b) => Maybe (a :~: b) sboolEqRefl = case sbool :: SBool (a == b) of STrue -> Just eqToRefl SFalse -> Nothing