lude

Haskell prelude alternative
git clone git://git.rr3.xyz/lude | git clone gits://git.rr3.xyz/lude
Log | Files | Refs | README | LICENSE

Error.hs (1724B)


      1 module Lude.Error (
      2     module X,
      3     --
      4     ErrorMessage,
      5     ErrText,
      6     ErrType,
      7     type (!||),
      8     type (!//),
      9     TypeError,
     10     --
     11     panic,
     12     impossible,
     13 ) where
     14 
     15 -- NOTE: `assert` is special (GHC can compile assertions to no-ops based on
     16 -- a CLI flag), so we export it directly.
     17 import Control.Exception as X (assert)
     18 
     19 import GHC.TypeError qualified as GHC
     20 import Prelude qualified as Base
     21 
     22 import Lude.Haskell
     23 
     24 --------------------------------------------------------------------------------
     25 
     26 -- | An error message for custom type errors (see `TypeError`)
     27 type ErrorMessage :: Kind
     28 type ErrorMessage = GHC.ErrorMessage
     29 
     30 -- | Make an `ErrorMessage` showing text
     31 type ErrText :: Symbol -> ErrorMessage
     32 type ErrText = GHC.Text
     33 
     34 -- | Make an `ErrorMessage` showing a type
     35 type ErrType :: forall k. k -> ErrorMessage
     36 type ErrType = GHC.ShowType
     37 
     38 -- | Horizontally concatenate `ErrorMessage`s
     39 type (!||) :: Biendo ErrorMessage
     40 type (!||) = (GHC.:<>:)
     41 
     42 -- | Vertically concatenate `ErrorMessage`s
     43 type (!//) :: Biendo ErrorMessage
     44 type (!//) = (GHC.:$$:)
     45 
     46 -- | Produce a custom type error
     47 type TypeError :: forall k. ErrorMessage -> k
     48 type TypeError msg = GHC.TypeError msg
     49 
     50 --------------------------------------------------------------------------------
     51 
     52 type PanicExn :: Type
     53 newtype PanicExn = MkPanicExn Base.String
     54     deriving stock (Base.Show)
     55 instance Exception PanicExn where
     56     displayException (MkPanicExn msg) = "panic: " Base.++ msg
     57 
     58 panic :: forall {r :: Repr} (a :: Type# r). (HasCallStack) => Base.String -> a
     59 panic msg = withFrozenCallStack (unsafe_raise (MkPanicExn msg))
     60 
     61 impossible :: forall {r :: Repr} (a :: Type# r). (HasCallStack) => a
     62 impossible = withFrozenCallStack (panic "impossible")