Lude.hs.txt (12415B)
1 -- import Control.Applicative (Alternative (empty, (<|>)), Applicative (liftA2, pure, (<*>)), (<**>)) 2 -- import Control.Monad (Monad ((>>=))) 3 -- import Data.Bool (Bool (..), not, (&&), (||)) 4 -- import Data.Foldable (Foldable) 5 -- import Data.Function (const, fix, flip) 6 -- import Data.Functor (Functor (fmap), void) 7 -- import Data.Functor.Compose (Compose (Compose, getCompose)) 8 -- import Data.Functor.Const (Const (Const, getConst)) 9 -- import Data.Functor.Identity (Identity (Identity, runIdentity)) 10 -- import Data.Maybe (Maybe (..)) 11 -- import Data.Traversable (Traversable (sequenceA)) 12 13 -- TODO: SIMD: VecCount (..) and VecElem (..) 14 import GHC.Exts (Constraint, Levity (..), RuntimeRep (..)) 15 import GHC.Exts qualified as GHC 16 17 -------------------------------------------------------------------------------- 18 -- Types 19 20 -- | Types with an unlifted alternative. 21 -- 22 -- Laws: 23 -- - @force (lift x) = x@ 24 class Unlift a where 25 type U a :: UType 26 force :: a -> U a 27 lift :: U a -> a 28 29 -------------------------------------------------------------------------------- 30 31 -- type Functor :: forall r s. (Type r -> Type s) -> Constraint 32 -- class Functor @r f where 33 -- map :: forall (a :: Type r) (b :: Type r). (a -> b) -> f a -> f b 34 35 -- type SemiApplicative :: forall r s. (Type r -> Type s) -> Constraint 36 -- class (Functor f) => SemiApplicative @r f where 37 -- map2 :: forall (a :: Type r) (b :: Type r) (c :: Type r). (a -> b -> c) -> f a -> f b -> f c 38 39 -- type Applicative :: forall r s. (Type r -> Type s) -> Constraint 40 -- class (SemiApplicative f) => Applicative @r f where 41 -- pure :: forall (a :: Type r). a -> f a 42 43 -- type SemiMonad :: forall r s. (Type r -> Type s) -> Constraint 44 -- class (SemiApplicative f) => SemiMonad @r f where 45 -- bind :: forall (a :: Type r) (b :: Type r). (a -> f b) -> f a -> f b 46 47 -- type Monad :: forall r s. (Type r -> Type s) -> Constraint 48 -- class (Applicative f, SemiMonad f) => Monad @r f 49 50 -- type SemiComonad :: forall r s. 51 52 -------------------------------------------------------------------------------- 53 -- Function 54 55 -- Note how the associativities and (relative) precedences differ with the 56 -- infix operators in Data.Function. 57 -- infixl 0 &, &! 58 -- infixl 1 $, $! 59 60 -- | Function application. 61 -- ($) :: (a -> b) -> a -> b 62 -- f $ x = f x 63 64 -- | Strict function application. 65 -- ($!) :: (a -> b) -> a -> b 66 -- f $! !x = f x 67 68 -- Flipped function application, or "pipe". 69 -- (&) :: a -> (a -> b) -> b 70 -- x & f = f x 71 72 -- Strict flipped function application, or "pipe". 73 -- (&!) :: a -> (a -> b) -> b 74 -- !x &! f = f x 75 76 -------------------------------------------------------------------------------- 77 -- Bool 78 79 -- class IsBool (bool :: Type r) where 80 -- true :: (Thunked) => bool 81 -- false :: (Thunked) => bool 82 -- bool :: a -> a -> bool -> a 83 84 -- instance IsBool Bool where 85 -- true = True 86 -- false = False 87 -- bool t f = \case 88 -- True -> t 89 -- False -> f 90 91 -- data UBool :: UType where 92 -- UTrue :: UBool 93 -- UFalse :: UBool 94 95 -- instance IsBool UBool where 96 -- true = UTrue 97 -- false = UFalse 98 -- bool t f = \case 99 -- UTrue -> t 100 -- UFalse -> f 101 102 -- instance Unlift Bool where 103 -- type U Bool = UBool 104 105 -- force True = UTrue 106 -- force False = UFalse 107 108 -- lift UTrue = True 109 -- lift UFalse = False 110 111 -------------------------------------------------------------------------------- 112 -- Numeric types 113 114 -- TODO 115 116 -------------------------------------------------------------------------------- 117 -- Category 118 119 -- infixr 9 ∘ 120 121 -- infixr 2 <. 122 -- infixl 2 .> 123 124 -- (⊲⊳) 125 -- (⋖⋗) 126 -- (⩹⩺) 127 -- (⨟) 128 -- (⦂) 129 -- (⋄) 130 -- (∧∨) 131 -- (⩓⩔) 132 -- (∩∪) 133 -- (=≠≡≢≈≉) 134 -- (<>≤≥≮≯≰≱) 135 -- (≺≻≼≽⊀⊁⋠⋡) 136 -- (⊂⊃⊆⊇⊄⊅⊈⊉) 137 -- (⊏⊐⊑⊒) 138 -- (⊢⊣⊤⊥) 139 -- y = h ∘ g ∘ f ∙^ x 140 -- (f ⦾ g ⦿ x) 141 -- y = h ◁ g ◁ f ◀ x 142 -- y = x ▶ f ▷ g ▷ h 143 -- (⧑⧒⧓⧔⧕⧖⧗⋈⋉⋊) 144 -- (↑↓↔↕↖↗↘↙) 145 -- (⥼⥽⥾⥿) 146 -- (⊰⊱) 147 -- (⭘∘) 148 -- (⦁∙⋅) 149 -- (⊙) 150 -- (⊚) 151 -- (⬬⬭⬮⬯) 152 -- (⬝⬞) 153 -- (⋆) 154 -- (⬥⬦⬧⬨⬩⬪⬫) 155 -- (◖◗) 156 -- (◐◑) 157 -- (⫾) 158 -- (■▪▬▮▰▲▶▼◀◆) 159 -- (∙⋆⋄*≀⌑▫) 160 -- y = x ▪ f ▫ g ▫ h 161 -- y = x ⦂ f ⨟ g ⨟ h 162 163 -- TODO: Op, Products, Sums, Clown, Joker 164 165 {- 166 167 class (Bifunctor f, LDomOf f ~ Hask ρ, RDomOf f ~ Hask ρ) => Monoidal (f :: Type ρ -> Type ρ -> Type ρ) where 168 type UnitOf f :: Type ρ 169 unitl :: Iso (Hask ρ) (f (UnitOf f) a) a 170 unitr :: Iso (Hask ρ) (f a (UnitOf f)) a 171 assoc :: Iso (Hask ρ) (f (f a b) c) (f a (f b c)) 172 173 -- TODO: Having this only work for Hask sucks and is needlessly specialized. 174 -- We can generalize this and some above definitions to any category whose 175 -- objects have kind Type ρ, which isn't just Hask (e.g., Kleisli). 176 class (Monoidal f) => Cartesian (f :: Type ρ -> Type ρ -> Type ρ) where 177 fst :: forall (a :: Type ρ) (b :: Type ρ). f a b -> a 178 snd :: forall (a :: Type ρ) (b :: Type ρ). f a b -> b 179 180 pair :: forall (a :: Type ρ) (b :: Type ρ) (c :: Type ρ). (a -> b) -> (a -> c) -> a -> f b c 181 pair f g = bimap f g ∘ diag 182 183 diag :: forall (a :: Type ρ). a -> f a a 184 diag = pair identity identity 185 186 -} 187 188 -- | Right-to-left composition. 189 -- 190 -- This operator is right associative to exploit laziness in the case where 191 -- the outer function doesn't need its argument: 192 -- 193 -- > (const x <. (f <. g)) y ---> const x ((f <. g) y) ---> x 194 -- 195 -- Note that @f <. g@ is never evaluated. 196 -- (<.) :: (Category v) => v b c -> v a b -> v a c 197 -- (<.) = (.) 198 199 -- | Left-to-right composition. 200 -- 201 -- This operator is left associative to exploit laziness in the case where 202 -- the outer function doesn't need its argument: 203 -- 204 -- > ((g .> f) .> const x) y ---> const x ((g .> f) y) ---> x 205 -- 206 -- Note that @g .> f@ is never evaluated. 207 -- (.>) :: (Category v) => v a b -> v b c -> v a c 208 -- (.>) = flip (.) 209 210 -- TODO: (.:), (<.:), (.:>) 211 212 -------------------------------------------------------------------------------- 213 -- Functor 214 215 {- 216 infixl 0 ^&, &^, ~^&, ^&~, ~&^, &^~ 217 infixl 1 $^, ^$, $^~, ~$^, ^$~, ~^$ 218 infixr 2 <<^, ^>>, ^<<, >>^ 219 220 -- | The action of a functor on a function. 221 map, ($^) :: (Functor f) => (a -> b) -> f a -> f b 222 map = fmap 223 ($^) = map 224 225 -- | The action of a functor on a function, flipped. 226 -- 227 -- With the BlockArguments extension, expressions like "@for xs \\x -> ...@" 228 -- look like for-loops, hence the name "for". 229 for, (^&) :: (Functor f) => f a -> (a -> b) -> f b 230 for = flip map 231 (^&) = for 232 233 -- | Application of a function in a functor to a pure value. 234 flap, (^$) :: (Functor f) => f (a -> b) -> a -> f b 235 flap f a = f ^& ($ a) 236 (^$) = flap 237 238 -- | Flipped application of a function in a functor to a pure value. 239 flor, (&^) :: (Functor f) => a -> f (a -> b) -> f b 240 flor = flip flap 241 (&^) = flor 242 243 ($^~) :: (Functor f, Functor g) => (a -> b) -> g (f a) -> g (f b) 244 f $^~ a = (f $^) $^ a 245 246 (~^&) :: (Functor f, Functor g) => g (f a) -> (a -> b) -> g (f b) 247 (~^&) = flip ($^~) 248 249 (~$^) :: (Functor f, Functor g) => g (a -> b) -> f a -> g (f b) 250 f ~$^ a = f ^& ($^ a) 251 252 (^&~) :: (Functor f, Functor g) => f a -> g (a -> b) -> g (f b) 253 (^&~) = flip (~$^) 254 255 (^$~) :: (Functor f, Functor g) => f (a -> b) -> g a -> g (f b) 256 f ^$~ a = (f ^$) $^ a 257 258 (~&^) :: (Functor f, Functor g) => g a -> f (a -> b) -> g (f b) 259 (~&^) = flip (^$~) 260 261 (~^$) :: (Functor f, Functor g) => g (f (a -> b)) -> a -> g (f b) 262 f ~^$ a = f ^& (^$ a) 263 264 (&^~) :: (Functor f, Functor g) => a -> g (f (a -> b)) -> g (f b) 265 (&^~) = flip (~^$) 266 267 -- TODO 268 269 -- | Right-to-left composition with the first function in a functor. 270 (<<^) :: (Functor f, Category v) => v b c -> f (v a b) -> f (v a c) 271 g <<^ f = (g <.) $^ f 272 273 -- TODO 274 275 -- | Left-to-right composition with the first function in a functor. 276 (^>>) :: (Functor f, Category v) => f (v a b) -> v b c -> f (v a c) 277 f ^>> g = f ^& (.> g) 278 279 -- TODO 280 281 -- | Right-to-left composition with the second function in a functor. 282 (^<<) :: (Functor f, Category v) => f (v b c) -> v a b -> f (v a c) 283 g ^<< f = g ^& (<. f) 284 285 -- TODO 286 287 -- | Left-to-right composition with the second function in a functor. 288 (>>^) :: (Functor f, Category v) => v a b -> f (v b c) -> f (v a c) 289 f >>^ g = (f .>) $^ g 290 291 -- TODO: *^, ^* 292 293 -------------------------------------------------------------------------------- 294 -- Applicative 295 296 infixl 0 ^&^, ~^&^~ 297 infixl 1 ^$^, ~^$^~ 298 infixr 2 ^<<^, ^>>^ 299 300 -- | Functions with applicative effects. 301 newtype Appy f a b = Appy {runAppy :: f (a -> b)} 302 303 instance (Functor f) => Functor (Appy f a) where 304 fmap f (Appy a) = Appy (f $^~ a) 305 306 instance (Applicative f) => Applicative (Appy f a) where 307 pure = Appy . pure . const 308 Appy f <*> Appy a = Appy (f ~^$^~ a) 309 310 hom :: (a -> b) -> (b -> b -> c) -> a -> a -> c 311 hom f g x y = g (f x) (f y) 312 313 instance (Alternative f) => Alternative (Appy f a) where 314 empty = Appy empty 315 316 -- Appy x <|> Appy y = Appy (x <|> y) 317 -- (<|>) = hom runAppy (<|>) 318 319 instance (Applicative f) => Category (Appy f) where 320 id = Appy (pure id) 321 Appy f . Appy g = Appy ((.) $^ f ^$^ g) 322 323 -- TODO: Remove ap and plug? 324 -- TODO: liftA{,1,2,3,4} 325 326 -- | An alias for @pure ()@. 327 pass :: (Applicative f) => f () 328 pass = pure () 329 330 -- | Application in an applicative functor. 331 ap, (^$^) :: (Applicative f) => f (a -> b) -> f a -> f b 332 ap = (<*>) 333 (^$^) = ap 334 335 -- | Flipped application in an applicative functor (with effects resolved as 336 -- they appear, left-to-right). 337 plug, (^&^) :: (Applicative f) => f a -> f (a -> b) -> f b 338 plug = (<**>) 339 (^&^) = plug 340 341 (~^$^~) :: (Applicative f, Applicative g) => g (f (a -> b)) -> g (f a) -> g (f b) 342 f ~^$^~ a = (^$^) $^ f ^$^ a 343 344 (~^&^~) :: (Applicative f, Applicative g) => g (f a) -> g (f (a -> b)) -> g (f b) 345 f ~^&^~ a = (^&^) $^ f ^$^ a 346 347 -- TODO: document 348 (^<<^) :: (Applicative f, Category v) => f (v b c) -> f (v a b) -> f (v a c) 349 (^<<^) = liftA2 (<.) 350 351 -- TODO: document 352 (^>>^) :: (Applicative f, Category v) => f (v a b) -> f (v b c) -> f (v a c) 353 (^>>^) = liftA2 (.>) 354 355 -- TODO: "semicolons" 356 357 -------------------------------------------------------------------------------- 358 -- Monad 359 360 infixl 0 ^&!, &!^, ^&!^ 361 infixl 1 $!^, ^$!, ^$!^ 362 363 map', ($!^) :: (Monad m) => (a -> b) -> m a -> m b 364 map' f mx = mx >>= \x -> pure $! f x 365 ($!^) = map' 366 367 for', (^&!) :: (Monad m) => m a -> (a -> b) -> m b 368 for' = flip map' 369 (^&!) = for 370 371 flap', (^$!) :: (Monad m) => m (a -> b) -> a -> m b 372 flap' mf x = mf >>= \f -> pure $! f x 373 (^$!) = flap' 374 375 flor', (&!^) :: (Monad m) => a -> m (a -> b) -> m b 376 flor' = flip flap' 377 (&!^) = flor' 378 379 ap', (^$!^) :: (Monad m) => m (a -> b) -> m a -> m b 380 ap' mf mx = do 381 f <- mf 382 x <- mx 383 pure $! f x 384 (^$!^) = ap' 385 386 plug', (^&!^) :: (Monad m) => m a -> m (a -> b) -> m b 387 plug' mx mf = do 388 x <- mx 389 f <- mf 390 pure $! f x 391 (^&!^) = plug' 392 -} 393 394 -- (.) :: (b0 -> c) -> (a0 -> b0) -> (a0 -> c) 395 -- (g . f) a0 = g (f a0) 396 -- (^.) :: (b0 -> b1 -> c) -> (a0 -> b1) -> (b0 -> a0 -> c) 397 -- (g ^. f) b0 a0 = g b0 (f a0) 398 -- (^^.) :: (b0 -> b1 -> b2 -> c) -> (a0 -> b2) -> (b0 -> b1 -> a0 -> c) 399 -- (g ^^. f) b0 b1 a0 = g b0 b1 (f a0) 400 -- (^^^.) :: (b0 -> b1 -> b2 -> b3 -> c) -> (a0 -> b3) -> (b0 -> b1 -> b2 -> a0 -> c) 401 -- (g ^^^. f) b0 b1 b2 a0 = g b0 b1 b2 (f a0) 402 -- (.^) :: (b0 -> c) -> (a0 -> a1 -> b0) -> (a0 -> a1 -> c) 403 -- (g .^ f) a0 a1 = g (f a0 a1) 404 -- (^.^) :: (b0 -> b1 -> c) -> (a0 -> a1 -> b1) -> (b0 -> a0 -> a1 -> c) 405 -- (g ^.^ f) b0 a0 a1 = g b0 (f a0 a1) 406 -- (^^.^) :: (b0 -> b1 -> b2 -> c) -> (a0 -> a1 -> b2) -> (b0 -> b1 -> a0 -> a1 -> c) 407 -- (g ^^.^ f) b0 b1 a0 a1 = g b0 b1 (f a0 a1) 408 -- (^^^.^) :: (b0 -> b1 -> b2 -> b3 -> c) -> (a0 -> a1 -> b3) -> (b0 -> b1 -> b2 -> a0 -> a1 -> c) 409 -- (g ^^^.^ f) b0 b1 b2 a0 a1 = g b0 b1 b2 (f a0 a1) 410 -- (.^^) :: (b0 -> c) -> (a0 -> a1 -> a2 -> b0) -> (a0 -> a1 -> a2 -> c) 411 -- (^.^^) :: (b0 -> b1 -> c) -> (a0 -> a1 -> a2 -> b1) -> (b0 -> a0 -> a1 -> a2 -> c) 412 -- (^^.^^) :: (b0 -> b1 -> b2 -> c) -> (a0 -> a1 -> a2 -> b2) -> (b0 -> b1 -> a0 -> a1 -> a2 -> c) 413 -- (^^^.^^) :: (b0 -> b1 -> b2 -> b3 -> c) -> (a0 -> a1 -> a2 -> b3) -> (b0 -> b1 -> b2 -> a0 -> a1 -> a2 -> c) 414 -- (.^^^) :: (b0 -> c) -> (a0 -> a1 -> a2 -> a3 -> b0) -> (a0 -> a1 -> a2 -> a3 -> c) 415 -- (^.^^^) :: (b0 -> b1 -> c) -> (a0 -> a1 -> a2 -> a3 -> b1) -> (b0 -> a0 -> a1 -> a2 -> a3 -> c) 416 -- (^^.^^^) :: (b0 -> b1 -> b2 -> c) -> (a0 -> a1 -> a2 -> a3 -> b2) -> (b0 -> b1 -> a0 -> a1 -> a2 -> a3 -> c) 417 -- (^^^.^^^) :: (b0 -> b1 -> b2 -> b3 -> c) -> (a0 -> a1 -> a2 -> a3 -> b3) -> (b0 -> b1 -> b2 -> a0 -> a1 -> a2 -> a3 -> c)