module Agda.Utils.SemiRing where

-- | Semirings (<https://en.wikipedia.org/wiki/Semiring>).

class SemiRing a where
  ozero  :: a
  oone   :: a
  oplus  :: a -> a -> a
  otimes :: a -> a -> a

instance SemiRing () where
  ozero :: ()
ozero      = ()
  oone :: ()
oone       = ()
  oplus :: () -> () -> ()
oplus  ()
_ ()
_ = ()
  otimes :: () -> () -> ()
otimes ()
_ ()
_ = ()

instance SemiRing a => SemiRing (Maybe a) where
  ozero :: Maybe a
ozero = forall a. Maybe a
Nothing
  oone :: Maybe a
oone  = forall a. a -> Maybe a
Just forall a. SemiRing a => a
oone

  oplus :: Maybe a -> Maybe a -> Maybe a
oplus Maybe a
Nothing Maybe a
y = Maybe a
y
  oplus Maybe a
x Maybe a
Nothing = Maybe a
x
  oplus (Just a
x) (Just a
y) = forall a. a -> Maybe a
Just (forall a. SemiRing a => a -> a -> a
oplus a
x a
y)

  otimes :: Maybe a -> Maybe a -> Maybe a
otimes Maybe a
Nothing Maybe a
_ = forall a. Maybe a
Nothing
  otimes Maybe a
_ Maybe a
Nothing = forall a. Maybe a
Nothing
  otimes (Just a
x) (Just a
y) = forall a. a -> Maybe a
Just (forall a. SemiRing a => a -> a -> a
otimes a
x a
y)

-- | Star semirings
-- (<https://en.wikipedia.org/wiki/Semiring#Star_semirings>).

class SemiRing a => StarSemiRing a where
  ostar :: a -> a

instance StarSemiRing () where
  ostar :: () -> ()
ostar ()
_ = ()

instance StarSemiRing a => StarSemiRing (Maybe a) where
  ostar :: Maybe a -> Maybe a
ostar Maybe a
Nothing  = forall a. SemiRing a => a
oone
  ostar (Just a
x) = forall a. a -> Maybe a
Just (forall a. StarSemiRing a => a -> a
ostar a
x)