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.Fixed (Fixed (..), HasResolution)
32 import Data.Foldable (foldr)
33 import Data.Function (id, ($), (.))
34 import Data.Functor (Functor, (<$>))
37 import Data.List qualified as List
38 import Data.Ratio (Ratio, Rational)
39 import Data.Semigroup (Semigroup (..))
40 import Data.String (String)
41 import Data.String qualified as String
42 import Data.Text (Text)
43 import Data.Time qualified as Time
45 import Data.Word (Word64)
46 import GHC.Exts qualified as GHC
47 import GHC.IsList (IsList (..))
49 import GHC.TypeLits (ErrorMessage (..), Symbol)
50 import Numeric.Natural (Natural)
51 import Text.Show (Show (..))
52 import Prelude (Double, Integer, Integral, error)
53 import Prelude qualified
55 ifThenElse :: Bool -> a -> a -> a
56 ifThenElse True x _ = x
57 ifThenElse False _ y = y
61 -- | Like 'String.IsString' but with an 'HasCallStack' constraint
62 -- to report the location of the 'String'.
63 -- This is to be used with the @OverloadedStrings@ and @RebindableSyntax@ extensions
64 -- to replace literal strings by this 'fromString'.
65 class IsString a where
66 fromString :: HasCallStack => String -> a
72 fromString = String.fromString
74 instance IsString String
75 instance IsString Text
77 -- * Class 'FromInteger'
78 class FromInteger a where
79 fromInteger :: HasCallStack => Prelude.Integer -> a
80 default fromInteger :: Prelude.Num a => Prelude.Integer -> a
81 fromInteger = Prelude.fromInteger
82 instance FromInteger (Ratio Natural)
83 instance FromInteger Double
84 instance FromInteger Int
85 instance FromInteger Integer
86 instance FromInteger Natural
87 instance FromInteger Rational
88 instance FromInteger Time.NominalDiffTime
89 instance FromInteger Word64
90 instance HasResolution a => FromInteger (Fixed a)
92 -- instance FromInteger Decimal
94 -- * Class 'FromRational'
95 class FromRational a where
96 fromRational :: HasCallStack => Prelude.Rational -> a
97 default fromRational :: Prelude.Fractional a => Prelude.Rational -> a
98 fromRational = Prelude.fromRational
100 -- instance FromRational Decimal
101 instance FromRational Double
102 instance Integral a => FromRational (Ratio a)
109 type Item a = GHC.Item a
110 fromList :: HasCallStack => [Item a] -> a
111 fromListN :: HasCallStack => Prelude.Int -> [Item a] -> a
112 toList :: HasCallStack => a -> [Item a]
113 default fromList :: GHC.Item a ~ Item a => GHC.IsList a => [Item a] -> a
114 default fromListN :: GHC.Item a ~ Item a => GHC.IsList a => Prelude.Int -> [Item a] -> a
115 default toList :: GHC.Item a ~ Item a => GHC.IsList a => a -> [Item a]
116 fromList = GHC.fromList
117 fromListN = GHC.fromListN
121 -- class Listable repr where
122 -- cons :: repr a -> repr [a] -> repr [a]
124 -- concat :: repr [a] -> repr [a] -> repr [a]
125 -- class IsList repr where
126 -- fromList :: HasCallStack => [repr a] -> repr [a]
127 -- fromListN :: HasCallStack => Int -> [repr a] -> repr [a]
129 -- toList :: HasCallStack => repr [a] -> [repr a]
131 default fromList :: GHC.Item a ~ Item a => GHC.IsList a => [Item a] -> a
132 default fromListN :: GHC.Item a ~ Item a => GHC.IsList a => Prelude.Int -> [Item a] -> a
133 default toList :: GHC.Item a ~ Item a => GHC.IsList a => a -> [Item a]
134 fromList = GHC.fromList
135 fromListN = GHC.fromListN
138 -- instance Listable repr => IsList repr where
139 -- fromList = foldr cons nil
140 -- fromListN _n = foldr cons nil
142 -- toList = error "toList"
145 class Applicative repr where
146 fmap :: (a->b) -> repr a -> repr b
148 (<*>) :: repr (a->b) -> repr a -> repr b
149 join :: repr (repr a) -> repr a