dec-0.0.6/0000755000000000000000000000000007346545000010441 5ustar0000000000000000dec-0.0.6/ChangeLog.md0000644000000000000000000000052407346545000012613 0ustar0000000000000000# Revision history for dec ## 0.0.6 - Support GHC-8.6.5...9.10.1 ## 0.0.5 - Add `boringYes` and `absurdNo`. - Add `Decidable a => Boring (Dec a)` instance. - Add `Decidable ()`, `Decidable Void`, `Decidable (a, b)` instances. ## 0.0.4 - Mark module as explicitly `Safe`. ## 0.0.3 - First version. Released on an unsuspecting world. dec-0.0.6/LICENSE0000644000000000000000000000276207346545000011455 0ustar0000000000000000Copyright (c) 2019, 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. dec-0.0.6/dec.cabal0000644000000000000000000000250307346545000012160 0ustar0000000000000000cabal-version: >=1.10 name: dec version: 0.0.6 synopsis: Decidable propositions. category: Data, Dependent Types description: This package provides a @Dec@ type. . @ type Neg a = a -> Void . data Dec a \ = Yes a \ | No (Neg a) @ homepage: https://github.com/phadej/dec bug-reports: https://github.com/phadej/dec/issues license: BSD3 license-file: LICENSE author: Oleg Grenrus maintainer: Oleg.Grenrus copyright: (c) 2019-2021 Oleg Grenrus build-type: Simple extra-source-files: ChangeLog.md tested-with: GHC ==8.6.5 || ==8.8.4 || ==8.10.7 || ==9.0.2 || ==9.2.8 || ==9.4.8 || ==9.6.5 || ==9.8.2 || ==9.10.1 source-repository head type: git location: https://github.com/phadej/dec.git library default-language: Haskell2010 hs-source-dirs: src ghc-options: -Wall -fprint-explicit-kinds exposed-modules: Data.Type.Dec build-depends: base >=4.12.0.0 && <4.21 , boring >=0.2.2 && <0.3 if impl(ghc >=9.0) -- these flags may abort compilation with GHC-8.10 -- https://gitlab.haskell.org/ghc/ghc/-/merge_requests/3295 ghc-options: -Winferred-safe-imports -Wmissing-safe-haskell-mode dec-0.0.6/src/Data/Type/0000755000000000000000000000000007346545000013022 5ustar0000000000000000dec-0.0.6/src/Data/Type/Dec.hs0000644000000000000000000001010707346545000014050 0ustar0000000000000000{-# LANGUAGE Safe #-} {-# LANGUAGE ScopedTypeVariables #-} module Data.Type.Dec ( -- * Types Neg, Dec (..), Decidable (..), -- * Neg combinators toNegNeg, tripleNeg, contradict, contraposition, -- * Dec combinators decNeg, decShow, decToMaybe, decToBool, -- * Boring -- | @'Dec' a@ can be 'Boring' in two ways: When 'a' is 'Boring' or 'Absurd'. boringYes, absurdNo, ) where import Data.Void (Void) import Data.Boring (Absurd (..), Boring (..)) -- | Intuitionistic negation. type Neg a = a -> Void -- | Decidable (nullary) relations. data Dec a = Yes a | No (Neg a) -- | Class of decidable types. -- -- == Law -- -- @a@ should be a Proposition, i.e. the 'Yes' answers should be unique. -- -- /Note:/ We'd want to have decidable equality @:~:@ here too, -- but that seems to be a deep dive into singletons. class Decidable a where decide :: Dec a ------------------------------------------------------------------------------- -- Instances ------------------------------------------------------------------------------- -- | @()@ is truth. -- -- @since 0.0.5 instance Decidable () where decide = Yes () -- | 'Void' is falsehood. -- -- @since 0.0.5 instance Decidable Void where decide = No id -- | Products of decidable propositions are decidable -- -- @since 0.0.5 instance (Decidable a, Decidable b) => Decidable (a, b) where decide = case decide :: Dec a of No nx -> No (\c -> nx (fst c)) Yes x -> case decide :: Dec b of No ny -> No (\c -> ny (snd c)) Yes y -> Yes (x, y) ------------------------------------------------------------------------------- -- Neg combinators ------------------------------------------------------------------------------- -- | We can negate anything twice. -- -- Double-negation elimination is inverse of 'toNegNeg' and generally -- impossible. toNegNeg :: a -> Neg (Neg a) toNegNeg x = ($ x) -- | Triple negation can be reduced to a single one. tripleNeg :: Neg (Neg (Neg a)) -> Neg a tripleNeg f a = f (toNegNeg a) -- | Weak contradiction. contradict :: (a -> Neg b) -> b -> Neg a contradict f b a = f a b -- | A variant of contraposition. contraposition :: (a -> b) -> Neg b -> Neg a contraposition f nb a = nb (f a) ------------------------------------------------------------------------------- -- Dec combinators ------------------------------------------------------------------------------- instance Eq a => Eq (Dec a) where Yes x == Yes y = x == y Yes _ == No _ = False No _ == Yes _ = False No _ == No _ = True -- @'Not a' is a /h-Prop/ so all values are equal. -- | 'decToBool' respects this ordering. -- -- /Note:/ yet if you have @p :: a@ and @p :: 'Neg' a@, something is wrong. -- instance Ord a => Ord (Dec a) where compare (Yes a) (Yes b) = compare a b compare (No _) (Yes _) = compare False True compare (Yes _) (No _) = compare True False compare (No _) (No _) = EQ -- | Flip 'Dec' branches. decNeg :: Dec a -> Dec (Neg a) decNeg (Yes p) = No (toNegNeg p) decNeg (No np) = Yes np -- | Show 'Dec'. -- -- >>> decShow $ Yes () -- "Yes ()" -- -- >>> decShow $ No id -- "No " -- decShow :: Show a => Dec a -> String decShow (Yes x) = "Yes " ++ showsPrec 11 x "" decShow (No _) = "No " -- | Convert @'Dec' a@ to @'Maybe' a@, forgetting the 'No' evidence. decToMaybe :: Dec a -> Maybe a decToMaybe (Yes p) = Just p decToMaybe (No _) = Nothing -- | Convert 'Dec' to 'Bool', forgetting the evidence. decToBool :: Dec a -> Bool decToBool (Yes _) = True decToBool (No _) = False ------------------------------------------------------------------------------- -- Boring ------------------------------------------------------------------------------- -- | This relies on the fact that @a@ is /proposition/ in h-Prop sense. -- -- @since 0.0.5 instance Decidable a => Boring (Dec a) where boring = decide -- | 'Yes', it's 'boring'. -- -- @since 0.0.5 boringYes :: Boring a => Dec a boringYes = Yes boring -- | 'No', it's 'absurd'. -- -- @since 0.0.5 absurdNo :: Absurd a => Dec a absurdNo = No absurd