Partial.hs (1089B)
1 module Lude.Haskell.Partial ( 2 Partial, 3 unsafe_partial, 4 ) where 5 6 import GHC.Exts qualified as GHC 7 8 import Lude.Haskell.Constraint 9 import Lude.Haskell.Core 10 import Lude.Haskell.Type 11 12 -- NOTE: We prevent writing an instance of Partial by only exporting a 13 -- constraint synonym, which GHC disallows in the head of an instance 14 -- declaration. 15 16 -- NOTE: We need a dummy method to use `GHC.withDict`, hence `capability`. 17 -- Keeping the constructor of that method private helps avoid users 18 -- "accidentally" calling `GHC.withDict` on partial computations themselves. 19 20 -- NOTE: This is inspired by PureScript's Partial constraint: 21 -- https://pursuit.purescript.org/packages/purescript-partial 22 23 -------------------------------------------------------------------------------- 24 25 type Capability :: Type 26 data Capability = Unsafe_MkCapability 27 28 type Partial_ :: Constraint 29 class Partial_ where capability :: Capability 30 31 type Partial :: Constraint 32 type Partial = Partial_ 33 34 unsafe_partial :: forall {r :: Repr} (a :: Type# r). ((Partial) => a) -> a 35 unsafe_partial = GHC.withDict Unsafe_MkCapability