shithub: MicroHs

Download patch

ref: a92b259db04fb77944858dbc660bafbc30677a9e
parent: e17b0b42aa6ad6ea26c2eb044a404e80e807fd68
author: Lennart Augustsson <lennart.augustsson@epicgames.com>
date: Thu Nov 30 08:18:09 EST 2023

Add lots of instances.

--- a/lib/Control/Alternative.hs
+++ b/lib/Control/Alternative.hs
@@ -3,7 +3,7 @@
 import Control.Applicative
 import Data.Bool_Type
 import Data.Functor
-import Data.List
+import Data.List_Type
 
 infixl 3 <|>
 
@@ -19,3 +19,7 @@
 
 guard :: forall (f :: Type -> Type) a . Alternative f => Bool -> f ()
 guard b = if b then pure () else empty
+
+asum :: forall (f :: Type -> Type) a . Alternative f => [f a] -> f a
+asum [] = empty
+asum (a:as) = a <|> asum as
--- a/lib/Data/List.hs
+++ b/lib/Data/List.hs
@@ -5,6 +5,7 @@
   module Data.List_Type
   ) where
 import Primitives
+import Control.Alternative
 import Control.Applicative
 import Control.Error
 import Control.Monad
@@ -14,9 +15,11 @@
 import Data.Functor
 import Data.Int
 import Data.List_Type
+import Data.Maybe_Type
+import Data.Monoid
 import Data.Num
 import Data.Ord
-import Data.Maybe_Type
+import Data.Semigroup
 import Data.Tuple
 import Text.Show
 
@@ -40,6 +43,17 @@
 
 instance forall a . Show a => Show [a] where
   showsPrec _ = showList
+
+instance Alternative [] where
+  empty = []
+  (<|>) = (++)
+
+instance forall a . Semigroup [a] where
+  (<>) = (++)
+
+instance forall a . Monoid [a] where
+  mempty = []
+  mconcat = concat
 
 null :: forall a . [a] -> Bool
 null [] = True
--- a/lib/Data/Maybe.hs
+++ b/lib/Data/Maybe.hs
@@ -2,6 +2,7 @@
 -- See LICENSE file for full license.
 module Data.Maybe(module Data.Maybe, module Data.Maybe_Type) where
 import Primitives
+import Control.Alternative
 import Control.Applicative
 import Control.Monad
 import Data.Bool
@@ -12,7 +13,9 @@
 import Data.Int
 import Data.List
 import Data.Maybe_Type
+import Data.Monoid
 import Data.Ord
+import Data.Semigroup
 import Text.Show
 
 instance forall a . Eq a => Eq (Maybe a) where
@@ -40,6 +43,19 @@
 
 instance MonadFail Maybe where
   fail _ = Nothing
+
+instance Alternative Maybe where
+  empty = Nothing
+  Nothing <|> y = y
+  x       <|> _ = x
+
+instance forall a . Semigroup a => Semigroup (Maybe a) where
+    Nothing <> b       = b
+    a       <> Nothing = a
+    Just a  <> Just b  = Just (a <> b)
+
+instance forall a . Semigroup a => Monoid (Maybe a) where
+  mempty = Nothing
 
 maybe :: forall a r . r -> (a -> r) -> Maybe a -> r
 maybe r _ Nothing = r
--- a/lib/Data/Monoid.hs
+++ b/lib/Data/Monoid.hs
@@ -1,5 +1,6 @@
 module Data.Monoid(module Data.Monoid) where
 import Primitives
+import Data.List_Type
 import Data.Semigroup
 
 class Semigroup a => Monoid a where
@@ -6,3 +7,6 @@
   mempty :: a
   mappend :: a -> a -> a
   mappend = (<>)
+  mconcat :: [a] -> a
+  mconcat [] = mempty
+  mconcat (a:as) = a <> mconcat as
--- a/lib/Data/Tuple.hs
+++ b/lib/Data/Tuple.hs
@@ -9,6 +9,8 @@
 import Data.Bounded
 import Data.Eq
 import Data.Function
+import Data.Monoid
+import Data.Semigroup
 import Text.Show
 
 --data (a,b) = (a,b)  -- all tuples are built in
@@ -21,6 +23,11 @@
 snd :: forall a b . (a, b) -> b
 snd (_, b) = b
 
+-----------------------------------
+
+instance Eq () where
+  () == ()  =  True
+
 instance forall a b . (Eq a, Eq b) => Eq (a, b) where
   (a1, b1) == (a2, b2)  =  a1 == a2 && b1 == b2
 
@@ -30,6 +37,8 @@
 instance forall a b c d . (Eq a, Eq b, Eq c, Eq d) => Eq (a, b, c, d) where
   (a1, b1, c1, d1) == (a2, b2, c2, d2)  =  a1 == a2 && b1 == b2 && c1 == c2 && d1 == d2
 
+-----------------------------------
+
 instance Show () where
   showsPrec _ () = showString "()"
 
@@ -43,6 +52,8 @@
   showsPrec _ (a, b, c, d) = showParen True (showsPrec 0 a . showString "," . showsPrec 0 b . showString "," . showsPrec 0 c .
                                              showString "," . showsPrec 0 d)
 
+-----------------------------------
+
 instance Bounded () where
   minBound = ()
   maxBound = ()
@@ -58,3 +69,31 @@
 instance forall a b c d . (Bounded a, Bounded b, Bounded c, Bounded d) => Bounded (a, b, c, d) where
   minBound = (minBound, minBound, minBound, minBound)
   maxBound = (maxBound, maxBound, maxBound, maxBound)
+
+-----------------------------------
+
+instance Semigroup () where
+  _ <> _ = ()
+
+instance forall a b . (Semigroup a, Semigroup b) => Semigroup (a, b) where
+  (a, b) <> (a', b') = (a <> a', b <> b')
+
+instance forall a b c . (Semigroup a, Semigroup b, Semigroup c) => Semigroup (a, b, c) where
+  (a, b, c) <> (a', b', c') = (a <> a', b <> b', c <> c')
+
+instance forall a b c d . (Semigroup a, Semigroup b, Semigroup c, Semigroup d) => Semigroup (a, b, c, d) where
+  (a, b, c, d) <> (a', b', c', d') = (a <> a', b <> b', c <> c', d <> d')
+
+-----------------------------------
+
+instance Monoid () where
+  mempty = ()
+
+instance forall a b . (Monoid a, Monoid b) => Monoid (a, b) where
+  mempty = (mempty, mempty)
+
+instance forall a b c . (Monoid a, Monoid b, Monoid c) => Monoid (a, b, c) where
+  mempty = (mempty, mempty, mempty)
+
+instance forall a b c d . (Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (a, b, c, d) where
+  mempty = (mempty, mempty, mempty, mempty)
--