1 {-# LANGUAGE AllowAmbiguousTypes #-}
2 {-# LANGUAGE BlockArguments #-}
3 {-# LANGUAGE ConstraintKinds #-}
4 {-# LANGUAGE DataKinds #-}
5 {-# LANGUAGE DefaultSignatures #-}
6 {-# LANGUAGE FlexibleContexts #-}
8 {-# LANGUAGE MultiParamTypeClasses #-}
9 {-# LANGUAGE PostfixOperators #-}
10 {-# LANGUAGE RebindableSyntax #-}
11 {-# LANGUAGE TypeFamilies #-}
12 {-# LANGUAGE UndecidableInstances #-}
13 {-# LANGUAGE UnicodeSyntax #-}
14 {-# LANGUAGE NoImplicitPrelude #-}
15 {-# OPTIONS_GHC -Wno-missing-signatures #-}
16 {-# OPTIONS_GHC -Wno-orphans #-}
17 {-# OPTIONS_GHC -Wno-unused-do-bind #-}
18 {-# OPTIONS_GHC -Wno-unused-imports #-}
20 module Literate.Rebindable (
21 module Literate.Rebindable,
28 import Control.Applicative (Applicative (..))
29 import Control.Monad (Monad (..), MonadFail (..))
30 import Data.Bool (Bool (..))
31 import Data.Foldable (foldr)
32 import Data.Function (id, ($), (.))
33 import Data.Functor (Functor, (<$>))
36 import Data.List qualified as List
37 import Data.Ratio (Ratio, Rational)
38 import Data.Semigroup (Semigroup (..))
39 import Data.String (String)
40 import Data.String qualified as String
41 import Data.Text (Text)
43 import Data.Word (Word64)
44 import GHC.Exts qualified as GHC
45 import GHC.IsList (IsList (..))
47 import GHC.TypeLits (ErrorMessage (..), Symbol)
48 import Numeric.Natural (Natural)
49 import Text.Show (Show (..))
50 import Prelude (Double, Integer, Integral, error)
51 import Prelude qualified
53 ifThenElse :: Bool -> a -> a -> a
54 ifThenElse True x _ = x
55 ifThenElse False _ y = y
59 -- | Like 'String.IsString' but with an 'HasCallStack' constraint
60 -- to report the location of the 'String'.
61 -- This is to be used with the @OverloadedStrings@ and @RebindableSyntax@ extensions
62 -- to replace literal strings by this 'fromString'.
63 class IsString a where
64 fromString :: HasCallStack => String -> a
70 fromString = String.fromString
72 instance IsString String
73 instance IsString Text
75 -- * Class 'FromInteger'
76 class FromInteger a where
77 fromInteger :: HasCallStack => Prelude.Integer -> a
78 default fromInteger :: Prelude.Num a => Prelude.Integer -> a
79 fromInteger = Prelude.fromInteger
80 instance FromInteger (Ratio Natural)
81 instance FromInteger Double
82 instance FromInteger Int
83 instance FromInteger Integer
84 instance FromInteger Natural
85 instance FromInteger Rational
86 instance FromInteger Word64
88 -- instance FromInteger Decimal
90 -- * Class 'FromRational'
91 class FromRational a where
92 fromRational :: HasCallStack => Prelude.Rational -> a
93 default fromRational :: Prelude.Fractional a => Prelude.Rational -> a
94 fromRational = Prelude.fromRational
96 -- instance FromRational Decimal
97 instance FromRational Double
98 instance Integral a => FromRational (Ratio a)
105 type Item a = GHC.Item a
106 fromList :: HasCallStack => [Item a] -> a
107 fromListN :: HasCallStack => Prelude.Int -> [Item a] -> a
108 toList :: HasCallStack => a -> [Item a]
109 default fromList :: GHC.Item a ~ Item a => GHC.IsList a => [Item a] -> a
110 default fromListN :: GHC.Item a ~ Item a => GHC.IsList a => Prelude.Int -> [Item a] -> a
111 default toList :: GHC.Item a ~ Item a => GHC.IsList a => a -> [Item a]
112 fromList = GHC.fromList
113 fromListN = GHC.fromListN
117 -- class Listable repr where
118 -- cons :: repr a -> repr [a] -> repr [a]
120 -- concat :: repr [a] -> repr [a] -> repr [a]
121 -- class IsList repr where
122 -- fromList :: HasCallStack => [repr a] -> repr [a]
123 -- fromListN :: HasCallStack => Int -> [repr a] -> repr [a]
125 -- toList :: HasCallStack => repr [a] -> [repr a]
127 default fromList :: GHC.Item a ~ Item a => GHC.IsList a => [Item a] -> a
128 default fromListN :: GHC.Item a ~ Item a => GHC.IsList a => Prelude.Int -> [Item a] -> a
129 default toList :: GHC.Item a ~ Item a => GHC.IsList a => a -> [Item a]
130 fromList = GHC.fromList
131 fromListN = GHC.fromListN
134 -- instance Listable repr => IsList repr where
135 -- fromList = foldr cons nil
136 -- fromListN _n = foldr cons nil
138 -- toList = error "toList"
141 class Applicative repr where
142 fmap :: (a->b) -> repr a -> repr b
144 (<*>) :: repr (a->b) -> repr a -> repr b
145 join :: repr (repr a) -> repr a