{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
module NumHask.FreeAlgebra
(
FreeAlgebra (..),
NoLaws,
Tree (..),
toTreeL,
toTreeR,
Exp (..),
parseExp,
freeExp,
MagmaOnly,
UnitalOnly,
TreeU (..),
AssociativeOnly,
TreeA (..),
CommutativeOnly,
InvertibleOnly,
IdempotentOnly,
AbsorbingOnly,
FreeMonoid (..),
MultMonoid,
Bag (..),
mapBag,
AddCommGroup,
RingLaws,
FreeRing (..),
Example (..),
InformalTests,
calate,
)
where
import qualified Data.Attoparsec.Text as A
import qualified Data.Map as Map hiding (fromList)
import qualified Data.Sequence as Seq
import Data.Sequence ((<|), Seq (..), (|>))
import qualified Data.Text as Text
import GHC.Exts (IsList (..), coerce, toList)
import qualified GHC.Show
import NumHask.Algebra.Group ()
import NumHask.Prelude hiding (lift, reduce, toList)
class FreeAlgebra initial free a | free -> initial where
forget :: initial a -> free a
lift :: a -> free a
algebra :: free a -> a
printf :: free a -> Text
data NoLaws
data Tree laws a
= Leaf a
| Branch (Tree laws a) (Tree laws a)
deriving (Tree laws a -> Tree laws a -> Bool
(Tree laws a -> Tree laws a -> Bool)
-> (Tree laws a -> Tree laws a -> Bool) -> Eq (Tree laws a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall laws a. Eq a => Tree laws a -> Tree laws a -> Bool
/= :: Tree laws a -> Tree laws a -> Bool
$c/= :: forall laws a. Eq a => Tree laws a -> Tree laws a -> Bool
== :: Tree laws a -> Tree laws a -> Bool
$c== :: forall laws a. Eq a => Tree laws a -> Tree laws a -> Bool
Eq, Eq (Tree laws a)
Eq (Tree laws a)
-> (Tree laws a -> Tree laws a -> Ordering)
-> (Tree laws a -> Tree laws a -> Bool)
-> (Tree laws a -> Tree laws a -> Bool)
-> (Tree laws a -> Tree laws a -> Bool)
-> (Tree laws a -> Tree laws a -> Bool)
-> (Tree laws a -> Tree laws a -> Tree laws a)
-> (Tree laws a -> Tree laws a -> Tree laws a)
-> Ord (Tree laws a)
Tree laws a -> Tree laws a -> Bool
Tree laws a -> Tree laws a -> Ordering
Tree laws a -> Tree laws a -> Tree laws a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall laws a. Ord a => Eq (Tree laws a)
forall laws a. Ord a => Tree laws a -> Tree laws a -> Bool
forall laws a. Ord a => Tree laws a -> Tree laws a -> Ordering
forall laws a. Ord a => Tree laws a -> Tree laws a -> Tree laws a
min :: Tree laws a -> Tree laws a -> Tree laws a
$cmin :: forall laws a. Ord a => Tree laws a -> Tree laws a -> Tree laws a
max :: Tree laws a -> Tree laws a -> Tree laws a
$cmax :: forall laws a. Ord a => Tree laws a -> Tree laws a -> Tree laws a
>= :: Tree laws a -> Tree laws a -> Bool
$c>= :: forall laws a. Ord a => Tree laws a -> Tree laws a -> Bool
> :: Tree laws a -> Tree laws a -> Bool
$c> :: forall laws a. Ord a => Tree laws a -> Tree laws a -> Bool
<= :: Tree laws a -> Tree laws a -> Bool
$c<= :: forall laws a. Ord a => Tree laws a -> Tree laws a -> Bool
< :: Tree laws a -> Tree laws a -> Bool
$c< :: forall laws a. Ord a => Tree laws a -> Tree laws a -> Bool
compare :: Tree laws a -> Tree laws a -> Ordering
$ccompare :: forall laws a. Ord a => Tree laws a -> Tree laws a -> Ordering
$cp1Ord :: forall laws a. Ord a => Eq (Tree laws a)
Ord, Int -> Tree laws a -> ShowS
[Tree laws a] -> ShowS
Tree laws a -> String
(Int -> Tree laws a -> ShowS)
-> (Tree laws a -> String)
-> ([Tree laws a] -> ShowS)
-> Show (Tree laws a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall laws a. Show a => Int -> Tree laws a -> ShowS
forall laws a. Show a => [Tree laws a] -> ShowS
forall laws a. Show a => Tree laws a -> String
showList :: [Tree laws a] -> ShowS
$cshowList :: forall laws a. Show a => [Tree laws a] -> ShowS
show :: Tree laws a -> String
$cshow :: forall laws a. Show a => Tree laws a -> String
showsPrec :: Int -> Tree laws a -> ShowS
$cshowsPrec :: forall laws a. Show a => Int -> Tree laws a -> ShowS
Show, a -> Tree laws b -> Tree laws a
(a -> b) -> Tree laws a -> Tree laws b
(forall a b. (a -> b) -> Tree laws a -> Tree laws b)
-> (forall a b. a -> Tree laws b -> Tree laws a)
-> Functor (Tree laws)
forall a b. a -> Tree laws b -> Tree laws a
forall a b. (a -> b) -> Tree laws a -> Tree laws b
forall laws a b. a -> Tree laws b -> Tree laws a
forall laws a b. (a -> b) -> Tree laws a -> Tree laws b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Tree laws b -> Tree laws a
$c<$ :: forall laws a b. a -> Tree laws b -> Tree laws a
fmap :: (a -> b) -> Tree laws a -> Tree laws b
$cfmap :: forall laws a b. (a -> b) -> Tree laws a -> Tree laws b
Functor)
toTreeL :: NonEmpty a -> Tree NoLaws a
toTreeL :: NonEmpty a -> Tree NoLaws a
toTreeL (a
x :| [a]
xs) = (Tree NoLaws a -> a -> Tree NoLaws a)
-> Tree NoLaws a -> [a] -> Tree NoLaws a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\Tree NoLaws a
s a
a -> Tree NoLaws a -> Tree NoLaws a -> Tree NoLaws a
forall laws a. Tree laws a -> Tree laws a -> Tree laws a
Branch Tree NoLaws a
s (a -> Tree NoLaws a
forall laws a. a -> Tree laws a
Leaf a
a)) (a -> Tree NoLaws a
forall laws a. a -> Tree laws a
Leaf a
x) [a]
xs
toTreeR :: NonEmpty a -> Tree NoLaws a
toTreeR :: NonEmpty a -> Tree NoLaws a
toTreeR NonEmpty a
l =
let (a
x :| [a]
xs) = ([a] -> NonEmpty a
forall l. IsList l => [Item l] -> l
fromList ([a] -> NonEmpty a)
-> (NonEmpty a -> [a]) -> NonEmpty a -> NonEmpty a
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [a] -> [a]
forall a. [a] -> [a]
reverse ([a] -> [a]) -> (NonEmpty a -> [a]) -> NonEmpty a -> [a]
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. NonEmpty a -> [a]
forall l. IsList l => l -> [Item l]
toList (NonEmpty a -> NonEmpty a) -> NonEmpty a -> NonEmpty a
forall a b. (a -> b) -> a -> b
$ NonEmpty a
l)
in (Tree NoLaws a -> a -> Tree NoLaws a)
-> Tree NoLaws a -> [a] -> Tree NoLaws a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\Tree NoLaws a
s a
a -> Tree NoLaws a -> Tree NoLaws a -> Tree NoLaws a
forall laws a. Tree laws a -> Tree laws a -> Tree laws a
Branch (a -> Tree NoLaws a
forall laws a. a -> Tree laws a
Leaf a
a) Tree NoLaws a
s) (a -> Tree NoLaws a
forall laws a. a -> Tree laws a
Leaf a
x) [a]
xs
newtype Example = Example Int deriving (Example -> Example -> Bool
(Example -> Example -> Bool)
-> (Example -> Example -> Bool) -> Eq Example
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Example -> Example -> Bool
$c/= :: Example -> Example -> Bool
== :: Example -> Example -> Bool
$c== :: Example -> Example -> Bool
Eq, Eq Example
Eq Example
-> (Example -> Example -> Ordering)
-> (Example -> Example -> Bool)
-> (Example -> Example -> Bool)
-> (Example -> Example -> Bool)
-> (Example -> Example -> Bool)
-> (Example -> Example -> Example)
-> (Example -> Example -> Example)
-> Ord Example
Example -> Example -> Bool
Example -> Example -> Ordering
Example -> Example -> Example
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Example -> Example -> Example
$cmin :: Example -> Example -> Example
max :: Example -> Example -> Example
$cmax :: Example -> Example -> Example
>= :: Example -> Example -> Bool
$c>= :: Example -> Example -> Bool
> :: Example -> Example -> Bool
$c> :: Example -> Example -> Bool
<= :: Example -> Example -> Bool
$c<= :: Example -> Example -> Bool
< :: Example -> Example -> Bool
$c< :: Example -> Example -> Bool
compare :: Example -> Example -> Ordering
$ccompare :: Example -> Example -> Ordering
$cp1Ord :: Eq Example
Ord, Magma Example
Magma Example -> Associative Example
forall a. Magma a -> Associative a
Associative, Magma Example
Magma Example -> Commutative Example
forall a. Magma a -> Commutative a
Commutative, Magma Example
Magma Example -> Idempotent Example
forall a. Magma a -> Idempotent a
Idempotent)
instance Magma Example where
(Example Int
a) ⊕ :: Example -> Example -> Example
⊕ (Example Int
b) = Int -> Example
Example (Int -> Example) -> Int -> Example
forall a b. (a -> b) -> a -> b
$ Int
a Int -> Int -> Int
forall a. Additive a => a -> a -> a
+ Int
b
instance Unital Example where
unit :: Example
unit = Int -> Example
Example Int
forall a. Additive a => a
zero
instance Absorbing Example where
absorb :: Example
absorb = Int -> Example
Example Int
forall a. Additive a => a
zero
instance Invertible Example where
inv :: Example -> Example
inv (Example Int
a) = Int -> Example
Example (Int -> Int
forall a. Subtractive a => a -> a
negate Int
a)
instance Show Example where
show :: Example -> String
show (Example Int
a) = Int -> String
forall a b. (Show a, ConvertText String b) => a -> b
show Int
a
data MagmaOnly
instance
(Show a, Magma a) =>
FreeAlgebra (Tree NoLaws) (Tree MagmaOnly) a
where
forget :: Tree NoLaws a -> Tree MagmaOnly a
forget = Tree NoLaws a -> Tree MagmaOnly a
coerce
lift :: a -> Tree MagmaOnly a
lift = a -> Tree MagmaOnly a
forall laws a. a -> Tree laws a
Leaf
algebra :: Tree MagmaOnly a -> a
algebra (Leaf a
a) = a
a
algebra (Branch Tree MagmaOnly a
a Tree MagmaOnly a
b) = Tree MagmaOnly a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra Tree MagmaOnly a
a a -> a -> a
forall a. Magma a => a -> a -> a
⊕ Tree MagmaOnly a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra Tree MagmaOnly a
b
printf :: Tree MagmaOnly a -> Text
printf (Leaf a
a) = a -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show a
a
printf (Branch Tree MagmaOnly a
a Tree MagmaOnly a
b) = [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat [Item [Text]
"(", Tree MagmaOnly a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf Tree MagmaOnly a
a, Item [Text]
"⊕", Tree MagmaOnly a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf Tree MagmaOnly a
b, Item [Text]
")"]
data UnitalOnly
data TreeU laws a = EmptyTree | NonEmptyTree (Tree MagmaOnly a)
deriving (TreeU laws a -> TreeU laws a -> Bool
(TreeU laws a -> TreeU laws a -> Bool)
-> (TreeU laws a -> TreeU laws a -> Bool) -> Eq (TreeU laws a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall laws a. Eq a => TreeU laws a -> TreeU laws a -> Bool
/= :: TreeU laws a -> TreeU laws a -> Bool
$c/= :: forall laws a. Eq a => TreeU laws a -> TreeU laws a -> Bool
== :: TreeU laws a -> TreeU laws a -> Bool
$c== :: forall laws a. Eq a => TreeU laws a -> TreeU laws a -> Bool
Eq, Eq (TreeU laws a)
Eq (TreeU laws a)
-> (TreeU laws a -> TreeU laws a -> Ordering)
-> (TreeU laws a -> TreeU laws a -> Bool)
-> (TreeU laws a -> TreeU laws a -> Bool)
-> (TreeU laws a -> TreeU laws a -> Bool)
-> (TreeU laws a -> TreeU laws a -> Bool)
-> (TreeU laws a -> TreeU laws a -> TreeU laws a)
-> (TreeU laws a -> TreeU laws a -> TreeU laws a)
-> Ord (TreeU laws a)
TreeU laws a -> TreeU laws a -> Bool
TreeU laws a -> TreeU laws a -> Ordering
TreeU laws a -> TreeU laws a -> TreeU laws a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall laws a. Ord a => Eq (TreeU laws a)
forall laws a. Ord a => TreeU laws a -> TreeU laws a -> Bool
forall laws a. Ord a => TreeU laws a -> TreeU laws a -> Ordering
forall laws a.
Ord a =>
TreeU laws a -> TreeU laws a -> TreeU laws a
min :: TreeU laws a -> TreeU laws a -> TreeU laws a
$cmin :: forall laws a.
Ord a =>
TreeU laws a -> TreeU laws a -> TreeU laws a
max :: TreeU laws a -> TreeU laws a -> TreeU laws a
$cmax :: forall laws a.
Ord a =>
TreeU laws a -> TreeU laws a -> TreeU laws a
>= :: TreeU laws a -> TreeU laws a -> Bool
$c>= :: forall laws a. Ord a => TreeU laws a -> TreeU laws a -> Bool
> :: TreeU laws a -> TreeU laws a -> Bool
$c> :: forall laws a. Ord a => TreeU laws a -> TreeU laws a -> Bool
<= :: TreeU laws a -> TreeU laws a -> Bool
$c<= :: forall laws a. Ord a => TreeU laws a -> TreeU laws a -> Bool
< :: TreeU laws a -> TreeU laws a -> Bool
$c< :: forall laws a. Ord a => TreeU laws a -> TreeU laws a -> Bool
compare :: TreeU laws a -> TreeU laws a -> Ordering
$ccompare :: forall laws a. Ord a => TreeU laws a -> TreeU laws a -> Ordering
$cp1Ord :: forall laws a. Ord a => Eq (TreeU laws a)
Ord, Int -> TreeU laws a -> ShowS
[TreeU laws a] -> ShowS
TreeU laws a -> String
(Int -> TreeU laws a -> ShowS)
-> (TreeU laws a -> String)
-> ([TreeU laws a] -> ShowS)
-> Show (TreeU laws a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall laws a. Show a => Int -> TreeU laws a -> ShowS
forall laws a. Show a => [TreeU laws a] -> ShowS
forall laws a. Show a => TreeU laws a -> String
showList :: [TreeU laws a] -> ShowS
$cshowList :: forall laws a. Show a => [TreeU laws a] -> ShowS
show :: TreeU laws a -> String
$cshow :: forall laws a. Show a => TreeU laws a -> String
showsPrec :: Int -> TreeU laws a -> ShowS
$cshowsPrec :: forall laws a. Show a => Int -> TreeU laws a -> ShowS
Show, a -> TreeU laws b -> TreeU laws a
(a -> b) -> TreeU laws a -> TreeU laws b
(forall a b. (a -> b) -> TreeU laws a -> TreeU laws b)
-> (forall a b. a -> TreeU laws b -> TreeU laws a)
-> Functor (TreeU laws)
forall a b. a -> TreeU laws b -> TreeU laws a
forall a b. (a -> b) -> TreeU laws a -> TreeU laws b
forall laws a b. a -> TreeU laws b -> TreeU laws a
forall laws a b. (a -> b) -> TreeU laws a -> TreeU laws b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> TreeU laws b -> TreeU laws a
$c<$ :: forall laws a b. a -> TreeU laws b -> TreeU laws a
fmap :: (a -> b) -> TreeU laws a -> TreeU laws b
$cfmap :: forall laws a b. (a -> b) -> TreeU laws a -> TreeU laws b
Functor)
instance
(Eq a, Show a, Unital a) =>
FreeAlgebra (Tree NoLaws) (TreeU UnitalOnly) a
where
forget :: Tree NoLaws a -> TreeU UnitalOnly a
forget (Leaf a
a) = a -> TreeU UnitalOnly a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
a -> free a
lift a
a
forget (Branch Tree NoLaws a
a Tree NoLaws a
b) = TreeU UnitalOnly a -> TreeU UnitalOnly a -> TreeU UnitalOnly a
forall laws a. TreeU laws a -> TreeU laws a -> TreeU laws a
opU (Tree NoLaws a -> TreeU UnitalOnly a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
a) (Tree NoLaws a -> TreeU UnitalOnly a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
b)
where
opU :: TreeU laws a -> TreeU laws a -> TreeU laws a
opU TreeU laws a
EmptyTree TreeU laws a
t = TreeU laws a
t
opU TreeU laws a
t TreeU laws a
EmptyTree = TreeU laws a
t
opU (NonEmptyTree Tree MagmaOnly a
t) (NonEmptyTree Tree MagmaOnly a
t') = Tree MagmaOnly a -> TreeU laws a
forall laws a. Tree MagmaOnly a -> TreeU laws a
NonEmptyTree (Tree MagmaOnly a -> Tree MagmaOnly a -> Tree MagmaOnly a
forall laws a. Tree laws a -> Tree laws a -> Tree laws a
Branch Tree MagmaOnly a
t Tree MagmaOnly a
t')
lift :: a -> TreeU UnitalOnly a
lift a
a = TreeU UnitalOnly a
-> TreeU UnitalOnly a -> Bool -> TreeU UnitalOnly a
forall a. a -> a -> Bool -> a
bool (Tree MagmaOnly a -> TreeU UnitalOnly a
forall laws a. Tree MagmaOnly a -> TreeU laws a
NonEmptyTree (a -> Tree MagmaOnly a
forall laws a. a -> Tree laws a
Leaf a
a)) TreeU UnitalOnly a
forall laws a. TreeU laws a
EmptyTree (a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Unital a => a
unit)
algebra :: TreeU UnitalOnly a -> a
algebra TreeU UnitalOnly a
EmptyTree = a
forall a. Unital a => a
unit
algebra (NonEmptyTree Tree MagmaOnly a
t) = Tree MagmaOnly a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra Tree MagmaOnly a
t
printf :: TreeU UnitalOnly a -> Text
printf TreeU UnitalOnly a
EmptyTree = a -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show @a (Unital a => a
forall a. Unital a => a
unit @a)
printf (NonEmptyTree Tree MagmaOnly a
t) = Tree MagmaOnly a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf Tree MagmaOnly a
t
data AssociativeOnly
data TreeA laws a = LeafA a | BranchA a (TreeA laws a) deriving (TreeA laws a -> TreeA laws a -> Bool
(TreeA laws a -> TreeA laws a -> Bool)
-> (TreeA laws a -> TreeA laws a -> Bool) -> Eq (TreeA laws a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall laws a. Eq a => TreeA laws a -> TreeA laws a -> Bool
/= :: TreeA laws a -> TreeA laws a -> Bool
$c/= :: forall laws a. Eq a => TreeA laws a -> TreeA laws a -> Bool
== :: TreeA laws a -> TreeA laws a -> Bool
$c== :: forall laws a. Eq a => TreeA laws a -> TreeA laws a -> Bool
Eq, Int -> TreeA laws a -> ShowS
[TreeA laws a] -> ShowS
TreeA laws a -> String
(Int -> TreeA laws a -> ShowS)
-> (TreeA laws a -> String)
-> ([TreeA laws a] -> ShowS)
-> Show (TreeA laws a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall laws a. Show a => Int -> TreeA laws a -> ShowS
forall laws a. Show a => [TreeA laws a] -> ShowS
forall laws a. Show a => TreeA laws a -> String
showList :: [TreeA laws a] -> ShowS
$cshowList :: forall laws a. Show a => [TreeA laws a] -> ShowS
show :: TreeA laws a -> String
$cshow :: forall laws a. Show a => TreeA laws a -> String
showsPrec :: Int -> TreeA laws a -> ShowS
$cshowsPrec :: forall laws a. Show a => Int -> TreeA laws a -> ShowS
Show, a -> TreeA laws b -> TreeA laws a
(a -> b) -> TreeA laws a -> TreeA laws b
(forall a b. (a -> b) -> TreeA laws a -> TreeA laws b)
-> (forall a b. a -> TreeA laws b -> TreeA laws a)
-> Functor (TreeA laws)
forall a b. a -> TreeA laws b -> TreeA laws a
forall a b. (a -> b) -> TreeA laws a -> TreeA laws b
forall laws a b. a -> TreeA laws b -> TreeA laws a
forall laws a b. (a -> b) -> TreeA laws a -> TreeA laws b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> TreeA laws b -> TreeA laws a
$c<$ :: forall laws a b. a -> TreeA laws b -> TreeA laws a
fmap :: (a -> b) -> TreeA laws a -> TreeA laws b
$cfmap :: forall laws a b. (a -> b) -> TreeA laws a -> TreeA laws b
Functor)
instance (Show a, Associative a) => FreeAlgebra (Tree NoLaws) (TreeA AssociativeOnly) a where
forget :: Tree NoLaws a -> TreeA AssociativeOnly a
forget (Leaf a
a) = a -> TreeA AssociativeOnly a
forall laws a. a -> TreeA laws a
LeafA a
a
forget (Branch (Leaf a
a) Tree NoLaws a
b) = a -> TreeA AssociativeOnly a -> TreeA AssociativeOnly a
forall laws a. a -> TreeA laws a -> TreeA laws a
BranchA a
a (Tree NoLaws a -> TreeA AssociativeOnly a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
b)
forget (Branch Tree NoLaws a
a Tree NoLaws a
b) = TreeA AssociativeOnly a
-> TreeA AssociativeOnly a -> TreeA AssociativeOnly a
opA (Tree NoLaws a -> TreeA AssociativeOnly a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
a) (Tree NoLaws a -> TreeA AssociativeOnly a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
b)
where
opA :: TreeA AssociativeOnly a -> TreeA AssociativeOnly a -> TreeA AssociativeOnly a
opA :: TreeA AssociativeOnly a
-> TreeA AssociativeOnly a -> TreeA AssociativeOnly a
opA (LeafA a
a) TreeA AssociativeOnly a
b = a -> TreeA AssociativeOnly a -> TreeA AssociativeOnly a
forall laws a. a -> TreeA laws a -> TreeA laws a
BranchA a
a TreeA AssociativeOnly a
b
opA (BranchA a
a TreeA AssociativeOnly a
b) TreeA AssociativeOnly a
c = a -> TreeA AssociativeOnly a -> TreeA AssociativeOnly a
forall laws a. a -> TreeA laws a -> TreeA laws a
BranchA a
a (TreeA AssociativeOnly a
-> TreeA AssociativeOnly a -> TreeA AssociativeOnly a
opA TreeA AssociativeOnly a
b TreeA AssociativeOnly a
c)
lift :: a -> TreeA AssociativeOnly a
lift = a -> TreeA AssociativeOnly a
forall laws a. a -> TreeA laws a
LeafA
algebra :: TreeA AssociativeOnly a -> a
algebra (LeafA a
a) = a
a
algebra (BranchA a
a TreeA AssociativeOnly a
b) = a
a a -> a -> a
forall a. Magma a => a -> a -> a
⊕ TreeA AssociativeOnly a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra TreeA AssociativeOnly a
b
printf :: TreeA AssociativeOnly a -> Text
printf (LeafA a
a) = a -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show a
a
printf (BranchA a
a TreeA AssociativeOnly a
b) = a -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show a
a Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"⊕" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TreeA AssociativeOnly a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf TreeA AssociativeOnly a
b
data CommutativeOnly
instance (Show a, Ord a, Commutative a) => FreeAlgebra (Tree NoLaws) (Tree CommutativeOnly) a where
forget :: Tree NoLaws a -> Tree CommutativeOnly a
forget (Leaf a
a) = a -> Tree CommutativeOnly a
forall laws a. a -> Tree laws a
Leaf a
a
forget (Branch Tree NoLaws a
a Tree NoLaws a
b) = Tree CommutativeOnly a
-> Tree CommutativeOnly a -> Tree CommutativeOnly a
forall a laws. Ord a => Tree laws a -> Tree laws a -> Tree laws a
op (Tree NoLaws a -> Tree CommutativeOnly a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
a) (Tree NoLaws a -> Tree CommutativeOnly a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
b)
where
op :: Tree laws a -> Tree laws a -> Tree laws a
op Tree laws a
a Tree laws a
b = Tree laws a -> Tree laws a -> Bool -> Tree laws a
forall a. a -> a -> Bool -> a
bool (Tree laws a -> Tree laws a -> Tree laws a
forall laws a. Tree laws a -> Tree laws a -> Tree laws a
Branch Tree laws a
b Tree laws a
a) (Tree laws a -> Tree laws a -> Tree laws a
forall laws a. Tree laws a -> Tree laws a -> Tree laws a
Branch Tree laws a
a Tree laws a
b) (Tree laws a
a Tree laws a -> Tree laws a -> Bool
forall a. Ord a => a -> a -> Bool
< Tree laws a
b)
lift :: a -> Tree CommutativeOnly a
lift = a -> Tree CommutativeOnly a
forall laws a. a -> Tree laws a
Leaf
algebra :: Tree CommutativeOnly a -> a
algebra (Leaf a
a) = a
a
algebra (Branch Tree CommutativeOnly a
a Tree CommutativeOnly a
b) = Tree CommutativeOnly a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra Tree CommutativeOnly a
a a -> a -> a
forall a. Magma a => a -> a -> a
⊕ Tree CommutativeOnly a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra Tree CommutativeOnly a
b
printf :: Tree CommutativeOnly a -> Text
printf (Leaf a
a) = a -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show a
a
printf (Branch Tree CommutativeOnly a
a Tree CommutativeOnly a
b) = Text
"(" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Tree CommutativeOnly a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf Tree CommutativeOnly a
a Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"⊕" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Tree CommutativeOnly a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf Tree CommutativeOnly a
b Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
data InvertibleOnly
instance (Show a, Eq a, Invertible a) => FreeAlgebra (Tree NoLaws) (Tree InvertibleOnly) a where
forget :: Tree NoLaws a -> Tree InvertibleOnly a
forget (Leaf a
a) = a -> Tree InvertibleOnly a
forall laws a. a -> Tree laws a
Leaf a
a
forget (Branch Tree NoLaws a
a Tree NoLaws a
b) = Tree InvertibleOnly a
-> Tree InvertibleOnly a -> Tree InvertibleOnly a
forall a laws.
(Eq a, Invertible a) =>
Tree laws a -> Tree laws a -> Tree laws a
op (Tree NoLaws a -> Tree InvertibleOnly a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
a) (Tree NoLaws a -> Tree InvertibleOnly a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
b)
where
op :: Tree laws a -> Tree laws a -> Tree laws a
op a :: Tree laws a
a@(Branch Tree laws a
la (Leaf a
ra)) b :: Tree laws a
b@(Branch (Leaf a
lb) Tree laws a
rb) =
Tree laws a -> Tree laws a -> Bool -> Tree laws a
forall a. a -> a -> Bool -> a
bool (Tree laws a -> Tree laws a -> Tree laws a
forall laws a. Tree laws a -> Tree laws a -> Tree laws a
Branch Tree laws a
a Tree laws a
b) (Tree laws a -> Tree laws a -> Tree laws a
op Tree laws a
la Tree laws a
rb) (a
ra a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a -> a
forall a. Invertible a => a -> a
inv a
lb)
op l :: Tree laws a
l@(Leaf a
la) b :: Tree laws a
b@(Branch (Leaf a
lb) Tree laws a
bb) =
Tree laws a -> Tree laws a -> Bool -> Tree laws a
forall a. a -> a -> Bool -> a
bool (Tree laws a -> Tree laws a -> Tree laws a
forall laws a. Tree laws a -> Tree laws a -> Tree laws a
Branch Tree laws a
l Tree laws a
b) Tree laws a
bb (a
la a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a -> a
forall a. Invertible a => a -> a
inv a
lb)
op b :: Tree laws a
b@(Branch Tree laws a
bb (Leaf a
lb)) l :: Tree laws a
l@(Leaf a
la) =
Tree laws a -> Tree laws a -> Bool -> Tree laws a
forall a. a -> a -> Bool -> a
bool (Tree laws a -> Tree laws a -> Tree laws a
forall laws a. Tree laws a -> Tree laws a -> Tree laws a
Branch Tree laws a
b Tree laws a
l) Tree laws a
bb (a
lb a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a -> a
forall a. Invertible a => a -> a
inv a
la)
op Tree laws a
l Tree laws a
r = Tree laws a -> Tree laws a -> Tree laws a
forall laws a. Tree laws a -> Tree laws a -> Tree laws a
Branch Tree laws a
l Tree laws a
r
lift :: a -> Tree InvertibleOnly a
lift = a -> Tree InvertibleOnly a
forall laws a. a -> Tree laws a
Leaf
algebra :: Tree InvertibleOnly a -> a
algebra (Leaf a
a) = a
a
algebra (Branch Tree InvertibleOnly a
a Tree InvertibleOnly a
b) = Tree InvertibleOnly a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra Tree InvertibleOnly a
a a -> a -> a
forall a. Magma a => a -> a -> a
⊕ Tree InvertibleOnly a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra Tree InvertibleOnly a
b
printf :: Tree InvertibleOnly a -> Text
printf (Leaf a
a) = a -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show a
a
printf (Branch Tree InvertibleOnly a
a Tree InvertibleOnly a
b) = Text
"(" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Tree InvertibleOnly a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf Tree InvertibleOnly a
a Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"⊕" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Tree InvertibleOnly a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf Tree InvertibleOnly a
b Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
data IdempotentOnly
instance (Show a, Ord a) => FreeAlgebra (Tree NoLaws) (Tree IdempotentOnly) a where
forget :: Tree NoLaws a -> Tree IdempotentOnly a
forget (Leaf a
a) = a -> Tree IdempotentOnly a
forall laws a. a -> Tree laws a
Leaf a
a
forget (Branch Tree NoLaws a
a Tree NoLaws a
b) = Tree IdempotentOnly a
-> Tree IdempotentOnly a -> Tree IdempotentOnly a
forall a laws. Eq a => Tree laws a -> Tree laws a -> Tree laws a
op (Tree NoLaws a -> Tree IdempotentOnly a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
a) (Tree NoLaws a -> Tree IdempotentOnly a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
b)
where
op :: Tree laws a -> Tree laws a -> Tree laws a
op Tree laws a
a Tree laws a
b = Tree laws a -> Tree laws a -> Bool -> Tree laws a
forall a. a -> a -> Bool -> a
bool (Tree laws a -> Tree laws a -> Tree laws a
forall laws a. Tree laws a -> Tree laws a -> Tree laws a
Branch Tree laws a
a Tree laws a
b) Tree laws a
a (Tree laws a
a Tree laws a -> Tree laws a -> Bool
forall a. Eq a => a -> a -> Bool
== Tree laws a
b)
lift :: a -> Tree IdempotentOnly a
lift = a -> Tree IdempotentOnly a
forall laws a. a -> Tree laws a
Leaf
algebra :: Tree IdempotentOnly a -> a
algebra (Leaf a
a) = a
a
algebra (Branch Tree IdempotentOnly a
a Tree IdempotentOnly a
b) = Tree IdempotentOnly a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra Tree IdempotentOnly a
a a -> a -> a
forall a. Ord a => a -> a -> a
`max` Tree IdempotentOnly a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra Tree IdempotentOnly a
b
printf :: Tree IdempotentOnly a -> Text
printf (Leaf a
a) = a -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show a
a
printf (Branch Tree IdempotentOnly a
a Tree IdempotentOnly a
b) =
Text
"(" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Tree IdempotentOnly a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf Tree IdempotentOnly a
a Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" o " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Tree IdempotentOnly a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf Tree IdempotentOnly a
b Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
data AbsorbingOnly
instance
(Show a, Eq a, Absorbing a) =>
FreeAlgebra (Tree NoLaws) (Tree AbsorbingOnly) a
where
forget :: Tree NoLaws a -> Tree AbsorbingOnly a
forget (Leaf a
a) = a -> Tree AbsorbingOnly a
forall laws a. a -> Tree laws a
Leaf a
a
forget (Branch (Leaf a
a) (Leaf a
b)) =
Tree AbsorbingOnly a
-> Tree AbsorbingOnly a -> Bool -> Tree AbsorbingOnly a
forall a. a -> a -> Bool -> a
bool
(Tree AbsorbingOnly a
-> Tree AbsorbingOnly a -> Tree AbsorbingOnly a
forall laws a. Tree laws a -> Tree laws a -> Tree laws a
Branch (a -> Tree AbsorbingOnly a
forall laws a. a -> Tree laws a
Leaf a
a) (a -> Tree AbsorbingOnly a
forall laws a. a -> Tree laws a
Leaf a
b))
(a -> Tree AbsorbingOnly a
forall laws a. a -> Tree laws a
Leaf a
forall a. Absorbing a => a
absorb)
(a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Absorbing a => a
absorb Bool -> Bool -> Bool
|| a
b a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Absorbing a => a
absorb)
forget (Branch (Leaf a
a) Tree NoLaws a
r) =
Tree AbsorbingOnly a
-> Tree AbsorbingOnly a -> Bool -> Tree AbsorbingOnly a
forall a. a -> a -> Bool -> a
bool (Tree AbsorbingOnly a
-> Tree AbsorbingOnly a -> Tree AbsorbingOnly a
forall laws a. Tree laws a -> Tree laws a -> Tree laws a
Branch (a -> Tree AbsorbingOnly a
forall laws a. a -> Tree laws a
Leaf a
a) (Tree NoLaws a -> Tree AbsorbingOnly a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
r)) (a -> Tree AbsorbingOnly a
forall laws a. a -> Tree laws a
Leaf a
forall a. Absorbing a => a
absorb) (a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Absorbing a => a
absorb)
forget (Branch Tree NoLaws a
l (Leaf a
a)) =
Tree AbsorbingOnly a
-> Tree AbsorbingOnly a -> Bool -> Tree AbsorbingOnly a
forall a. a -> a -> Bool -> a
bool (Tree AbsorbingOnly a
-> Tree AbsorbingOnly a -> Tree AbsorbingOnly a
forall laws a. Tree laws a -> Tree laws a -> Tree laws a
Branch (Tree NoLaws a -> Tree AbsorbingOnly a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
l) (a -> Tree AbsorbingOnly a
forall laws a. a -> Tree laws a
Leaf a
a)) (a -> Tree AbsorbingOnly a
forall laws a. a -> Tree laws a
Leaf a
forall a. Absorbing a => a
absorb) (a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Absorbing a => a
absorb)
forget (Branch Tree NoLaws a
a Tree NoLaws a
b) = Tree AbsorbingOnly a
-> Tree AbsorbingOnly a -> Tree AbsorbingOnly a
forall a laws.
(Absorbing a, Eq a) =>
Tree laws a -> Tree laws a -> Tree laws a
op (Tree NoLaws a -> Tree AbsorbingOnly a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
a) (Tree NoLaws a -> Tree AbsorbingOnly a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
b)
where
op :: Tree laws a -> Tree laws a -> Tree laws a
op l :: Tree laws a
l@(Leaf a
a) Tree laws a
r =
Tree laws a -> Tree laws a -> Bool -> Tree laws a
forall a. a -> a -> Bool -> a
bool (Tree laws a -> Tree laws a -> Tree laws a
forall laws a. Tree laws a -> Tree laws a -> Tree laws a
Branch Tree laws a
l Tree laws a
r) (a -> Tree laws a
forall laws a. a -> Tree laws a
Leaf a
forall a. Absorbing a => a
absorb) (a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Absorbing a => a
absorb)
op Tree laws a
l r :: Tree laws a
r@(Leaf a
a) =
Tree laws a -> Tree laws a -> Bool -> Tree laws a
forall a. a -> a -> Bool -> a
bool (Tree laws a -> Tree laws a -> Tree laws a
forall laws a. Tree laws a -> Tree laws a -> Tree laws a
Branch Tree laws a
l Tree laws a
r) (a -> Tree laws a
forall laws a. a -> Tree laws a
Leaf a
forall a. Absorbing a => a
absorb) (a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Absorbing a => a
absorb)
op Tree laws a
a Tree laws a
b = Tree laws a -> Tree laws a -> Tree laws a
forall laws a. Tree laws a -> Tree laws a -> Tree laws a
Branch Tree laws a
a Tree laws a
b
lift :: a -> Tree AbsorbingOnly a
lift = a -> Tree AbsorbingOnly a
forall laws a. a -> Tree laws a
Leaf
algebra :: Tree AbsorbingOnly a -> a
algebra (Leaf a
a) = a
a
algebra (Branch Tree AbsorbingOnly a
a Tree AbsorbingOnly a
b) = Tree AbsorbingOnly a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra Tree AbsorbingOnly a
a a -> a -> a
forall a. Magma a => a -> a -> a
⊕ Tree AbsorbingOnly a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra Tree AbsorbingOnly a
b
printf :: Tree AbsorbingOnly a -> Text
printf (Leaf a
a) = a -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show a
a
printf (Branch Tree AbsorbingOnly a
a Tree AbsorbingOnly a
b) = Text
"(" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Tree AbsorbingOnly a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf Tree AbsorbingOnly a
a Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"*" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Tree AbsorbingOnly a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf Tree AbsorbingOnly a
b Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
newtype FreeMonoid laws a = FreeMonoid {FreeMonoid laws a -> [a]
leaves :: [a]} deriving (FreeMonoid laws a -> FreeMonoid laws a -> Bool
(FreeMonoid laws a -> FreeMonoid laws a -> Bool)
-> (FreeMonoid laws a -> FreeMonoid laws a -> Bool)
-> Eq (FreeMonoid laws a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall laws a.
Eq a =>
FreeMonoid laws a -> FreeMonoid laws a -> Bool
/= :: FreeMonoid laws a -> FreeMonoid laws a -> Bool
$c/= :: forall laws a.
Eq a =>
FreeMonoid laws a -> FreeMonoid laws a -> Bool
== :: FreeMonoid laws a -> FreeMonoid laws a -> Bool
$c== :: forall laws a.
Eq a =>
FreeMonoid laws a -> FreeMonoid laws a -> Bool
Eq, Eq (FreeMonoid laws a)
Eq (FreeMonoid laws a)
-> (FreeMonoid laws a -> FreeMonoid laws a -> Ordering)
-> (FreeMonoid laws a -> FreeMonoid laws a -> Bool)
-> (FreeMonoid laws a -> FreeMonoid laws a -> Bool)
-> (FreeMonoid laws a -> FreeMonoid laws a -> Bool)
-> (FreeMonoid laws a -> FreeMonoid laws a -> Bool)
-> (FreeMonoid laws a -> FreeMonoid laws a -> FreeMonoid laws a)
-> (FreeMonoid laws a -> FreeMonoid laws a -> FreeMonoid laws a)
-> Ord (FreeMonoid laws a)
FreeMonoid laws a -> FreeMonoid laws a -> Bool
FreeMonoid laws a -> FreeMonoid laws a -> Ordering
FreeMonoid laws a -> FreeMonoid laws a -> FreeMonoid laws a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall laws a. Ord a => Eq (FreeMonoid laws a)
forall laws a.
Ord a =>
FreeMonoid laws a -> FreeMonoid laws a -> Bool
forall laws a.
Ord a =>
FreeMonoid laws a -> FreeMonoid laws a -> Ordering
forall laws a.
Ord a =>
FreeMonoid laws a -> FreeMonoid laws a -> FreeMonoid laws a
min :: FreeMonoid laws a -> FreeMonoid laws a -> FreeMonoid laws a
$cmin :: forall laws a.
Ord a =>
FreeMonoid laws a -> FreeMonoid laws a -> FreeMonoid laws a
max :: FreeMonoid laws a -> FreeMonoid laws a -> FreeMonoid laws a
$cmax :: forall laws a.
Ord a =>
FreeMonoid laws a -> FreeMonoid laws a -> FreeMonoid laws a
>= :: FreeMonoid laws a -> FreeMonoid laws a -> Bool
$c>= :: forall laws a.
Ord a =>
FreeMonoid laws a -> FreeMonoid laws a -> Bool
> :: FreeMonoid laws a -> FreeMonoid laws a -> Bool
$c> :: forall laws a.
Ord a =>
FreeMonoid laws a -> FreeMonoid laws a -> Bool
<= :: FreeMonoid laws a -> FreeMonoid laws a -> Bool
$c<= :: forall laws a.
Ord a =>
FreeMonoid laws a -> FreeMonoid laws a -> Bool
< :: FreeMonoid laws a -> FreeMonoid laws a -> Bool
$c< :: forall laws a.
Ord a =>
FreeMonoid laws a -> FreeMonoid laws a -> Bool
compare :: FreeMonoid laws a -> FreeMonoid laws a -> Ordering
$ccompare :: forall laws a.
Ord a =>
FreeMonoid laws a -> FreeMonoid laws a -> Ordering
$cp1Ord :: forall laws a. Ord a => Eq (FreeMonoid laws a)
Ord, a -> FreeMonoid laws a -> Bool
FreeMonoid laws m -> m
FreeMonoid laws a -> [a]
FreeMonoid laws a -> Bool
FreeMonoid laws a -> Int
FreeMonoid laws a -> a
FreeMonoid laws a -> a
FreeMonoid laws a -> a
FreeMonoid laws a -> a
(a -> m) -> FreeMonoid laws a -> m
(a -> m) -> FreeMonoid laws a -> m
(a -> b -> b) -> b -> FreeMonoid laws a -> b
(a -> b -> b) -> b -> FreeMonoid laws a -> b
(b -> a -> b) -> b -> FreeMonoid laws a -> b
(b -> a -> b) -> b -> FreeMonoid laws a -> b
(a -> a -> a) -> FreeMonoid laws a -> a
(a -> a -> a) -> FreeMonoid laws a -> a
(forall m. Monoid m => FreeMonoid laws m -> m)
-> (forall m a. Monoid m => (a -> m) -> FreeMonoid laws a -> m)
-> (forall m a. Monoid m => (a -> m) -> FreeMonoid laws a -> m)
-> (forall a b. (a -> b -> b) -> b -> FreeMonoid laws a -> b)
-> (forall a b. (a -> b -> b) -> b -> FreeMonoid laws a -> b)
-> (forall b a. (b -> a -> b) -> b -> FreeMonoid laws a -> b)
-> (forall b a. (b -> a -> b) -> b -> FreeMonoid laws a -> b)
-> (forall a. (a -> a -> a) -> FreeMonoid laws a -> a)
-> (forall a. (a -> a -> a) -> FreeMonoid laws a -> a)
-> (forall a. FreeMonoid laws a -> [a])
-> (forall a. FreeMonoid laws a -> Bool)
-> (forall a. FreeMonoid laws a -> Int)
-> (forall a. Eq a => a -> FreeMonoid laws a -> Bool)
-> (forall a. Ord a => FreeMonoid laws a -> a)
-> (forall a. Ord a => FreeMonoid laws a -> a)
-> (forall a. Num a => FreeMonoid laws a -> a)
-> (forall a. Num a => FreeMonoid laws a -> a)
-> Foldable (FreeMonoid laws)
forall a. Eq a => a -> FreeMonoid laws a -> Bool
forall a. Num a => FreeMonoid laws a -> a
forall a. Ord a => FreeMonoid laws a -> a
forall m. Monoid m => FreeMonoid laws m -> m
forall a. FreeMonoid laws a -> Bool
forall a. FreeMonoid laws a -> Int
forall a. FreeMonoid laws a -> [a]
forall a. (a -> a -> a) -> FreeMonoid laws a -> a
forall laws a. Eq a => a -> FreeMonoid laws a -> Bool
forall laws a. Num a => FreeMonoid laws a -> a
forall laws a. Ord a => FreeMonoid laws a -> a
forall m a. Monoid m => (a -> m) -> FreeMonoid laws a -> m
forall laws m. Monoid m => FreeMonoid laws m -> m
forall laws a. FreeMonoid laws a -> Bool
forall laws a. FreeMonoid laws a -> Int
forall laws a. FreeMonoid laws a -> [a]
forall b a. (b -> a -> b) -> b -> FreeMonoid laws a -> b
forall a b. (a -> b -> b) -> b -> FreeMonoid laws a -> b
forall laws a. (a -> a -> a) -> FreeMonoid laws a -> a
forall laws m a. Monoid m => (a -> m) -> FreeMonoid laws a -> m
forall laws b a. (b -> a -> b) -> b -> FreeMonoid laws a -> b
forall laws a b. (a -> b -> b) -> b -> FreeMonoid laws a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: FreeMonoid laws a -> a
$cproduct :: forall laws a. Num a => FreeMonoid laws a -> a
sum :: FreeMonoid laws a -> a
$csum :: forall laws a. Num a => FreeMonoid laws a -> a
minimum :: FreeMonoid laws a -> a
$cminimum :: forall laws a. Ord a => FreeMonoid laws a -> a
maximum :: FreeMonoid laws a -> a
$cmaximum :: forall laws a. Ord a => FreeMonoid laws a -> a
elem :: a -> FreeMonoid laws a -> Bool
$celem :: forall laws a. Eq a => a -> FreeMonoid laws a -> Bool
length :: FreeMonoid laws a -> Int
$clength :: forall laws a. FreeMonoid laws a -> Int
null :: FreeMonoid laws a -> Bool
$cnull :: forall laws a. FreeMonoid laws a -> Bool
toList :: FreeMonoid laws a -> [a]
$ctoList :: forall laws a. FreeMonoid laws a -> [a]
foldl1 :: (a -> a -> a) -> FreeMonoid laws a -> a
$cfoldl1 :: forall laws a. (a -> a -> a) -> FreeMonoid laws a -> a
foldr1 :: (a -> a -> a) -> FreeMonoid laws a -> a
$cfoldr1 :: forall laws a. (a -> a -> a) -> FreeMonoid laws a -> a
foldl' :: (b -> a -> b) -> b -> FreeMonoid laws a -> b
$cfoldl' :: forall laws b a. (b -> a -> b) -> b -> FreeMonoid laws a -> b
foldl :: (b -> a -> b) -> b -> FreeMonoid laws a -> b
$cfoldl :: forall laws b a. (b -> a -> b) -> b -> FreeMonoid laws a -> b
foldr' :: (a -> b -> b) -> b -> FreeMonoid laws a -> b
$cfoldr' :: forall laws a b. (a -> b -> b) -> b -> FreeMonoid laws a -> b
foldr :: (a -> b -> b) -> b -> FreeMonoid laws a -> b
$cfoldr :: forall laws a b. (a -> b -> b) -> b -> FreeMonoid laws a -> b
foldMap' :: (a -> m) -> FreeMonoid laws a -> m
$cfoldMap' :: forall laws m a. Monoid m => (a -> m) -> FreeMonoid laws a -> m
foldMap :: (a -> m) -> FreeMonoid laws a -> m
$cfoldMap :: forall laws m a. Monoid m => (a -> m) -> FreeMonoid laws a -> m
fold :: FreeMonoid laws m -> m
$cfold :: forall laws m. Monoid m => FreeMonoid laws m -> m
Foldable, Int -> FreeMonoid laws a -> ShowS
[FreeMonoid laws a] -> ShowS
FreeMonoid laws a -> String
(Int -> FreeMonoid laws a -> ShowS)
-> (FreeMonoid laws a -> String)
-> ([FreeMonoid laws a] -> ShowS)
-> Show (FreeMonoid laws a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall laws a. Show a => Int -> FreeMonoid laws a -> ShowS
forall laws a. Show a => [FreeMonoid laws a] -> ShowS
forall laws a. Show a => FreeMonoid laws a -> String
showList :: [FreeMonoid laws a] -> ShowS
$cshowList :: forall laws a. Show a => [FreeMonoid laws a] -> ShowS
show :: FreeMonoid laws a -> String
$cshow :: forall laws a. Show a => FreeMonoid laws a -> String
showsPrec :: Int -> FreeMonoid laws a -> ShowS
$cshowsPrec :: forall laws a. Show a => Int -> FreeMonoid laws a -> ShowS
Show)
data MultMonoid
instance Multiplicative (FreeMonoid MultMonoid a) where
one :: FreeMonoid MultMonoid a
one = [a] -> FreeMonoid MultMonoid a
forall laws a. [a] -> FreeMonoid laws a
FreeMonoid []
* :: FreeMonoid MultMonoid a
-> FreeMonoid MultMonoid a -> FreeMonoid MultMonoid a
(*) (FreeMonoid [a]
as) (FreeMonoid [a]
bs) = [a] -> FreeMonoid MultMonoid a
forall laws a. [a] -> FreeMonoid laws a
FreeMonoid ([a] -> FreeMonoid MultMonoid a) -> [a] -> FreeMonoid MultMonoid a
forall a b. (a -> b) -> a -> b
$ [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
rz [a]
as [a]
bs
where
rz :: [a] -> [a] -> [a]
rz [] [a]
a = [a]
a
rz [a]
a [] = [a]
a
rz (a
x : [a]
xs) [a]
ys = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> [a] -> [a]
rz [a]
xs [a]
ys
instance (Show a, Eq a, Multiplicative a) => FreeAlgebra (Tree NoLaws) (FreeMonoid MultMonoid) a where
forget :: Tree NoLaws a -> FreeMonoid MultMonoid a
forget (Leaf a
a) = a -> FreeMonoid MultMonoid a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
a -> free a
lift a
a
forget (Branch Tree NoLaws a
a Tree NoLaws a
b) = Tree NoLaws a -> FreeMonoid MultMonoid a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
a FreeMonoid MultMonoid a
-> FreeMonoid MultMonoid a -> FreeMonoid MultMonoid a
forall a. Multiplicative a => a -> a -> a
* Tree NoLaws a -> FreeMonoid MultMonoid a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
b
lift :: a -> FreeMonoid MultMonoid a
lift a
a = FreeMonoid MultMonoid a
-> FreeMonoid MultMonoid a -> Bool -> FreeMonoid MultMonoid a
forall a. a -> a -> Bool -> a
bool ([a] -> FreeMonoid MultMonoid a
forall laws a. [a] -> FreeMonoid laws a
FreeMonoid [a
Item [a]
a]) FreeMonoid MultMonoid a
forall a. Multiplicative a => a
one (a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Multiplicative a => a
one)
algebra :: FreeMonoid MultMonoid a -> a
algebra = (a -> a -> a) -> a -> FreeMonoid MultMonoid a -> a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> a -> a
forall a. Multiplicative a => a -> a -> a
(*) a
forall a. Multiplicative a => a
one
printf :: FreeMonoid MultMonoid a -> Text
printf (FreeMonoid []) = a -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show @a a
forall a. Multiplicative a => a
one
printf (FreeMonoid [a]
ls) = Text -> [Text] -> Text
calate Text
"*" (a -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show (a -> Text) -> [a] -> [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a]
ls)
newtype Bag laws a = Bag {Bag laws a -> Map a Int
unbag :: Map.Map a Int} deriving (Bag laws a -> Bag laws a -> Bool
(Bag laws a -> Bag laws a -> Bool)
-> (Bag laws a -> Bag laws a -> Bool) -> Eq (Bag laws a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall laws a. Eq a => Bag laws a -> Bag laws a -> Bool
/= :: Bag laws a -> Bag laws a -> Bool
$c/= :: forall laws a. Eq a => Bag laws a -> Bag laws a -> Bool
== :: Bag laws a -> Bag laws a -> Bool
$c== :: forall laws a. Eq a => Bag laws a -> Bag laws a -> Bool
Eq, Eq (Bag laws a)
Eq (Bag laws a)
-> (Bag laws a -> Bag laws a -> Ordering)
-> (Bag laws a -> Bag laws a -> Bool)
-> (Bag laws a -> Bag laws a -> Bool)
-> (Bag laws a -> Bag laws a -> Bool)
-> (Bag laws a -> Bag laws a -> Bool)
-> (Bag laws a -> Bag laws a -> Bag laws a)
-> (Bag laws a -> Bag laws a -> Bag laws a)
-> Ord (Bag laws a)
Bag laws a -> Bag laws a -> Bool
Bag laws a -> Bag laws a -> Ordering
Bag laws a -> Bag laws a -> Bag laws a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall laws a. Ord a => Eq (Bag laws a)
forall laws a. Ord a => Bag laws a -> Bag laws a -> Bool
forall laws a. Ord a => Bag laws a -> Bag laws a -> Ordering
forall laws a. Ord a => Bag laws a -> Bag laws a -> Bag laws a
min :: Bag laws a -> Bag laws a -> Bag laws a
$cmin :: forall laws a. Ord a => Bag laws a -> Bag laws a -> Bag laws a
max :: Bag laws a -> Bag laws a -> Bag laws a
$cmax :: forall laws a. Ord a => Bag laws a -> Bag laws a -> Bag laws a
>= :: Bag laws a -> Bag laws a -> Bool
$c>= :: forall laws a. Ord a => Bag laws a -> Bag laws a -> Bool
> :: Bag laws a -> Bag laws a -> Bool
$c> :: forall laws a. Ord a => Bag laws a -> Bag laws a -> Bool
<= :: Bag laws a -> Bag laws a -> Bool
$c<= :: forall laws a. Ord a => Bag laws a -> Bag laws a -> Bool
< :: Bag laws a -> Bag laws a -> Bool
$c< :: forall laws a. Ord a => Bag laws a -> Bag laws a -> Bool
compare :: Bag laws a -> Bag laws a -> Ordering
$ccompare :: forall laws a. Ord a => Bag laws a -> Bag laws a -> Ordering
$cp1Ord :: forall laws a. Ord a => Eq (Bag laws a)
Ord, Int -> Bag laws a -> ShowS
[Bag laws a] -> ShowS
Bag laws a -> String
(Int -> Bag laws a -> ShowS)
-> (Bag laws a -> String)
-> ([Bag laws a] -> ShowS)
-> Show (Bag laws a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall laws a. Show a => Int -> Bag laws a -> ShowS
forall laws a. Show a => [Bag laws a] -> ShowS
forall laws a. Show a => Bag laws a -> String
showList :: [Bag laws a] -> ShowS
$cshowList :: forall laws a. Show a => [Bag laws a] -> ShowS
show :: Bag laws a -> String
$cshow :: forall laws a. Show a => Bag laws a -> String
showsPrec :: Int -> Bag laws a -> ShowS
$cshowsPrec :: forall laws a. Show a => Int -> Bag laws a -> ShowS
Show)
mapBag :: (Ord b) => (a -> b) -> Bag laws a -> Bag laws b
mapBag :: (a -> b) -> Bag laws a -> Bag laws b
mapBag a -> b
f (Bag Map a Int
m) = Map b Int -> Bag laws b
forall laws a. Map a Int -> Bag laws a
Bag (Map b Int -> Bag laws b) -> Map b Int -> Bag laws b
forall a b. (a -> b) -> a -> b
$ (Int -> Int -> Int) -> (a -> b) -> Map a Int -> Map b Int
forall k2 a k1.
Ord k2 =>
(a -> a -> a) -> (k1 -> k2) -> Map k1 a -> Map k2 a
Map.mapKeysWith Int -> Int -> Int
forall a. Additive a => a -> a -> a
(+) a -> b
f Map a Int
m
instance (Ord a, Subtractive a) => IsList (Bag AddCommGroup a) where
type Item (Bag AddCommGroup a) = a
toList :: Bag AddCommGroup a -> [Item (Bag AddCommGroup a)]
toList (Bag Map a Int
m) =
[[a]] -> [a]
forall a. Monoid a => [a] -> a
mconcat ([[a]] -> [a]) -> [[a]] -> [a]
forall a b. (a -> b) -> a -> b
$
( \(a
k, Int
v) ->
[a] -> [a] -> Bool -> [a]
forall a. a -> a -> Bool -> a
bool
(Int -> a -> [a]
forall a. Int -> a -> [a]
replicate Int
v a
k)
(Int -> a -> [a]
forall a. Int -> a -> [a]
replicate (Int -> Int
forall a. Subtractive a => a -> a
negate Int
v) (a -> a
forall a. Subtractive a => a -> a
negate a
k))
(Int
v Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
forall a. Additive a => a
zero)
)
((a, Int) -> [a]) -> [(a, Int)] -> [[a]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map a Int -> [(a, Int)]
forall k a. Map k a -> [(k, a)]
Map.toAscList Map a Int
m
fromList :: [Item (Bag AddCommGroup a)] -> Bag AddCommGroup a
fromList [Item (Bag AddCommGroup a)]
l =
Map a Int -> Bag AddCommGroup a
forall laws a. Map a Int -> Bag laws a
Bag
(Map a Int -> Bag AddCommGroup a)
-> Map a Int -> Bag AddCommGroup a
forall a b. (a -> b) -> a -> b
$ (Map a Int, Map a Int) -> Map a Int
forall a b. (a, b) -> b
snd
((Map a Int, Map a Int) -> Map a Int)
-> (Map a Int, Map a Int) -> Map a Int
forall a b. (a -> b) -> a -> b
$ (Int -> Bool) -> Map a Int -> (Map a Int, Map a Int)
forall a k. (a -> Bool) -> Map k a -> (Map k a, Map k a)
Map.partition (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
forall a. Additive a => a
zero)
(Map a Int -> (Map a Int, Map a Int))
-> Map a Int -> (Map a Int, Map a Int)
forall a b. (a -> b) -> a -> b
$ (Int -> Int -> Int) -> [(a, Int)] -> Map a Int
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith Int -> Int -> Int
forall a. Additive a => a -> a -> a
(+)
([(a, Int)] -> Map a Int) -> [(a, Int)] -> Map a Int
forall a b. (a -> b) -> a -> b
$ (\a
e -> (a, Int) -> (a, Int) -> Bool -> (a, Int)
forall a. a -> a -> Bool -> a
bool (a
e, Int
1) (a -> a
forall a. Subtractive a => a -> a
negate a
e, -Int
1) (a
e a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
forall a. Additive a => a
zero)) (a -> (a, Int)) -> [a] -> [(a, Int)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a]
[Item (Bag AddCommGroup a)]
l
data AddCommGroup
instance (Ord a) => Additive (Bag AddCommGroup a) where
zero :: Bag AddCommGroup a
zero = Map a Int -> Bag AddCommGroup a
forall laws a. Map a Int -> Bag laws a
Bag Map a Int
forall k a. Map k a
Map.empty
+ :: Bag AddCommGroup a -> Bag AddCommGroup a -> Bag AddCommGroup a
(+) (Bag Map a Int
a) (Bag Map a Int
b) =
Map a Int -> Bag AddCommGroup a
forall laws a. Map a Int -> Bag laws a
Bag
(Map a Int -> Bag AddCommGroup a)
-> Map a Int -> Bag AddCommGroup a
forall a b. (a -> b) -> a -> b
$ (Map a Int, Map a Int) -> Map a Int
forall a b. (a, b) -> b
snd
((Map a Int, Map a Int) -> Map a Int)
-> (Map a Int, Map a Int) -> Map a Int
forall a b. (a -> b) -> a -> b
$ (Int -> Bool) -> Map a Int -> (Map a Int, Map a Int)
forall a k. (a -> Bool) -> Map k a -> (Map k a, Map k a)
Map.partition (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
forall a. Additive a => a
zero)
(Map a Int -> (Map a Int, Map a Int))
-> Map a Int -> (Map a Int, Map a Int)
forall a b. (a -> b) -> a -> b
$ (Int -> Int -> Int) -> Map a Int -> Map a Int -> Map a Int
forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith Int -> Int -> Int
forall a. Additive a => a -> a -> a
(+) Map a Int
a Map a Int
b
instance (Ord a) => Subtractive (Bag AddCommGroup a) where
negate :: Bag AddCommGroup a -> Bag AddCommGroup a
negate (Bag Map a Int
m) = Map a Int -> Bag AddCommGroup a
forall laws a. Map a Int -> Bag laws a
Bag (Map a Int -> Bag AddCommGroup a)
-> Map a Int -> Bag AddCommGroup a
forall a b. (a -> b) -> a -> b
$ (Int -> Int) -> Map a Int -> Map a Int
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map Int -> Int
forall a. Subtractive a => a -> a
negate Map a Int
m
instance (Show a, Eq a, Ord a, Subtractive a) => FreeAlgebra (Tree NoLaws) (Bag AddCommGroup) a where
forget :: Tree NoLaws a -> Bag AddCommGroup a
forget (Leaf a
a) = a -> Bag AddCommGroup a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
a -> free a
lift a
a
forget (Branch Tree NoLaws a
a Tree NoLaws a
b) = Tree NoLaws a -> Bag AddCommGroup a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
a Bag AddCommGroup a -> Bag AddCommGroup a -> Bag AddCommGroup a
forall a. Additive a => a -> a -> a
+ Tree NoLaws a -> Bag AddCommGroup a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Tree NoLaws a
b
lift :: a -> Bag AddCommGroup a
lift a
a
| a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Additive a => a
zero = Bag AddCommGroup a
forall a. Additive a => a
zero
| a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
forall a. Additive a => a
zero = Map a Int -> Bag AddCommGroup a
forall laws a. Map a Int -> Bag laws a
Bag (a -> Int -> Map a Int
forall k a. k -> a -> Map k a
Map.singleton (a -> a
forall a. Subtractive a => a -> a
negate a
a) (-Int
1))
| Bool
otherwise = Map a Int -> Bag AddCommGroup a
forall laws a. Map a Int -> Bag laws a
Bag (Map a Int -> Bag AddCommGroup a)
-> Map a Int -> Bag AddCommGroup a
forall a b. (a -> b) -> a -> b
$ a -> Int -> Map a Int
forall k a. k -> a -> Map k a
Map.singleton a
a Int
1
algebra :: Bag AddCommGroup a -> a
algebra (Bag Map a Int
m) =
(a -> Int -> a -> a) -> a -> Map a Int -> a
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey
( \a
k Int
v a
acc ->
((a -> a) -> a -> a) -> a -> [a -> a] -> a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (a -> a) -> a -> a
forall a b. (a -> b) -> a -> b
($) a
acc (Int -> (a -> a) -> [a -> a]
forall a. Int -> a -> [a]
replicate Int
v (a
k a -> a -> a
forall a. Additive a => a -> a -> a
+))
)
a
forall a. Additive a => a
zero
Map a Int
m
printf :: Bag AddCommGroup a -> Text
printf Bag AddCommGroup a
b =
Text -> Text -> Bool -> Text
forall a. a -> a -> Bool -> a
bool
(Text -> [Text] -> Text
calate Text
"+" (a -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show (a -> Text) -> [a] -> [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bag AddCommGroup a -> [Item (Bag AddCommGroup a)]
forall l. IsList l => l -> [Item l]
toList Bag AddCommGroup a
b))
(a -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show @a a
forall a. Additive a => a
zero)
(Bag AddCommGroup a
b Bag AddCommGroup a -> Bag AddCommGroup a -> Bool
forall a. Eq a => a -> a -> Bool
== Bag AddCommGroup a
forall a. Additive a => a
zero)
data RingLaws
data Exp a
= Value a
| Add (Exp a) (Exp a)
| Mult (Exp a) (Exp a)
deriving (Exp a -> Exp a -> Bool
(Exp a -> Exp a -> Bool) -> (Exp a -> Exp a -> Bool) -> Eq (Exp a)
forall a. Eq a => Exp a -> Exp a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Exp a -> Exp a -> Bool
$c/= :: forall a. Eq a => Exp a -> Exp a -> Bool
== :: Exp a -> Exp a -> Bool
$c== :: forall a. Eq a => Exp a -> Exp a -> Bool
Eq, Eq (Exp a)
Eq (Exp a)
-> (Exp a -> Exp a -> Ordering)
-> (Exp a -> Exp a -> Bool)
-> (Exp a -> Exp a -> Bool)
-> (Exp a -> Exp a -> Bool)
-> (Exp a -> Exp a -> Bool)
-> (Exp a -> Exp a -> Exp a)
-> (Exp a -> Exp a -> Exp a)
-> Ord (Exp a)
Exp a -> Exp a -> Bool
Exp a -> Exp a -> Ordering
Exp a -> Exp a -> Exp a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (Exp a)
forall a. Ord a => Exp a -> Exp a -> Bool
forall a. Ord a => Exp a -> Exp a -> Ordering
forall a. Ord a => Exp a -> Exp a -> Exp a
min :: Exp a -> Exp a -> Exp a
$cmin :: forall a. Ord a => Exp a -> Exp a -> Exp a
max :: Exp a -> Exp a -> Exp a
$cmax :: forall a. Ord a => Exp a -> Exp a -> Exp a
>= :: Exp a -> Exp a -> Bool
$c>= :: forall a. Ord a => Exp a -> Exp a -> Bool
> :: Exp a -> Exp a -> Bool
$c> :: forall a. Ord a => Exp a -> Exp a -> Bool
<= :: Exp a -> Exp a -> Bool
$c<= :: forall a. Ord a => Exp a -> Exp a -> Bool
< :: Exp a -> Exp a -> Bool
$c< :: forall a. Ord a => Exp a -> Exp a -> Bool
compare :: Exp a -> Exp a -> Ordering
$ccompare :: forall a. Ord a => Exp a -> Exp a -> Ordering
$cp1Ord :: forall a. Ord a => Eq (Exp a)
Ord, Int -> Exp a -> ShowS
[Exp a] -> ShowS
Exp a -> String
(Int -> Exp a -> ShowS)
-> (Exp a -> String) -> ([Exp a] -> ShowS) -> Show (Exp a)
forall a. Show a => Int -> Exp a -> ShowS
forall a. Show a => [Exp a] -> ShowS
forall a. Show a => Exp a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Exp a] -> ShowS
$cshowList :: forall a. Show a => [Exp a] -> ShowS
show :: Exp a -> String
$cshow :: forall a. Show a => Exp a -> String
showsPrec :: Int -> Exp a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Exp a -> ShowS
Show, a -> Exp b -> Exp a
(a -> b) -> Exp a -> Exp b
(forall a b. (a -> b) -> Exp a -> Exp b)
-> (forall a b. a -> Exp b -> Exp a) -> Functor Exp
forall a b. a -> Exp b -> Exp a
forall a b. (a -> b) -> Exp a -> Exp b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Exp b -> Exp a
$c<$ :: forall a b. a -> Exp b -> Exp a
fmap :: (a -> b) -> Exp a -> Exp b
$cfmap :: forall a b. (a -> b) -> Exp a -> Exp b
Functor)
instance (Show a, Eq a, Ring a, Magma a) => FreeAlgebra Exp Exp a where
forget :: Exp a -> Exp a
forget = Exp a -> Exp a
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
lift :: a -> Exp a
lift = a -> Exp a
forall a. a -> Exp a
Value
algebra :: Exp a -> a
algebra (Value a
a) = a
a
algebra (Add Exp a
a Exp a
b) = Exp a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra Exp a
a a -> a -> a
forall a. Additive a => a -> a -> a
+ Exp a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra Exp a
b
algebra (Mult Exp a
a Exp a
b) = Exp a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra Exp a
a a -> a -> a
forall a. Multiplicative a => a -> a -> a
* Exp a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra Exp a
b
printf :: Exp a -> Text
printf (Value a
a) = a -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show a
a
printf (Mult Exp a
a Exp a
b) = Text
"(" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Exp a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf Exp a
a Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"*" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Exp a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf Exp a
b Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
printf (Add Exp a
a Exp a
b) = Text
"(" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Exp a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf Exp a
a Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"+" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Exp a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf Exp a
b Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"
data FreeRing laws a
= FreeV a
| FreeR (Seq (Bag AddCommGroup (FreeRing laws a)))
deriving (FreeRing laws a -> FreeRing laws a -> Bool
(FreeRing laws a -> FreeRing laws a -> Bool)
-> (FreeRing laws a -> FreeRing laws a -> Bool)
-> Eq (FreeRing laws a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall laws a. Eq a => FreeRing laws a -> FreeRing laws a -> Bool
/= :: FreeRing laws a -> FreeRing laws a -> Bool
$c/= :: forall laws a. Eq a => FreeRing laws a -> FreeRing laws a -> Bool
== :: FreeRing laws a -> FreeRing laws a -> Bool
$c== :: forall laws a. Eq a => FreeRing laws a -> FreeRing laws a -> Bool
Eq, Eq (FreeRing laws a)
Eq (FreeRing laws a)
-> (FreeRing laws a -> FreeRing laws a -> Ordering)
-> (FreeRing laws a -> FreeRing laws a -> Bool)
-> (FreeRing laws a -> FreeRing laws a -> Bool)
-> (FreeRing laws a -> FreeRing laws a -> Bool)
-> (FreeRing laws a -> FreeRing laws a -> Bool)
-> (FreeRing laws a -> FreeRing laws a -> FreeRing laws a)
-> (FreeRing laws a -> FreeRing laws a -> FreeRing laws a)
-> Ord (FreeRing laws a)
FreeRing laws a -> FreeRing laws a -> Bool
FreeRing laws a -> FreeRing laws a -> Ordering
FreeRing laws a -> FreeRing laws a -> FreeRing laws a
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall laws a. Ord a => Eq (FreeRing laws a)
forall laws a. Ord a => FreeRing laws a -> FreeRing laws a -> Bool
forall laws a.
Ord a =>
FreeRing laws a -> FreeRing laws a -> Ordering
forall laws a.
Ord a =>
FreeRing laws a -> FreeRing laws a -> FreeRing laws a
min :: FreeRing laws a -> FreeRing laws a -> FreeRing laws a
$cmin :: forall laws a.
Ord a =>
FreeRing laws a -> FreeRing laws a -> FreeRing laws a
max :: FreeRing laws a -> FreeRing laws a -> FreeRing laws a
$cmax :: forall laws a.
Ord a =>
FreeRing laws a -> FreeRing laws a -> FreeRing laws a
>= :: FreeRing laws a -> FreeRing laws a -> Bool
$c>= :: forall laws a. Ord a => FreeRing laws a -> FreeRing laws a -> Bool
> :: FreeRing laws a -> FreeRing laws a -> Bool
$c> :: forall laws a. Ord a => FreeRing laws a -> FreeRing laws a -> Bool
<= :: FreeRing laws a -> FreeRing laws a -> Bool
$c<= :: forall laws a. Ord a => FreeRing laws a -> FreeRing laws a -> Bool
< :: FreeRing laws a -> FreeRing laws a -> Bool
$c< :: forall laws a. Ord a => FreeRing laws a -> FreeRing laws a -> Bool
compare :: FreeRing laws a -> FreeRing laws a -> Ordering
$ccompare :: forall laws a.
Ord a =>
FreeRing laws a -> FreeRing laws a -> Ordering
$cp1Ord :: forall laws a. Ord a => Eq (FreeRing laws a)
Ord, Int -> FreeRing laws a -> ShowS
[FreeRing laws a] -> ShowS
FreeRing laws a -> String
(Int -> FreeRing laws a -> ShowS)
-> (FreeRing laws a -> String)
-> ([FreeRing laws a] -> ShowS)
-> Show (FreeRing laws a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall laws a. Show a => Int -> FreeRing laws a -> ShowS
forall laws a. Show a => [FreeRing laws a] -> ShowS
forall laws a. Show a => FreeRing laws a -> String
showList :: [FreeRing laws a] -> ShowS
$cshowList :: forall laws a. Show a => [FreeRing laws a] -> ShowS
show :: FreeRing laws a -> String
$cshow :: forall laws a. Show a => FreeRing laws a -> String
showsPrec :: Int -> FreeRing laws a -> ShowS
$cshowsPrec :: forall laws a. Show a => Int -> FreeRing laws a -> ShowS
Show)
freeExp :: Text -> Text
freeExp :: Text -> Text
freeExp Text
t = FreeRing RingLaws Int -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf (Exp Int -> FreeRing RingLaws Int
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget (Text -> Exp Int
parseExp Text
t) :: FreeRing RingLaws Int)
data InformalTests
bagV :: (Ord a, Subtractive a) => a -> Bag AddCommGroup (FreeRing RingLaws a)
bagV :: a -> Bag AddCommGroup (FreeRing RingLaws a)
bagV a
a
| a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Additive a => a
zero = Bag AddCommGroup (FreeRing RingLaws a)
forall a. Additive a => a
zero
| a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
forall a. Additive a => a
zero = Map (FreeRing RingLaws a) Int
-> Bag AddCommGroup (FreeRing RingLaws a)
forall laws a. Map a Int -> Bag laws a
Bag (FreeRing RingLaws a -> Int -> Map (FreeRing RingLaws a) Int
forall k a. k -> a -> Map k a
Map.singleton (a -> FreeRing RingLaws a
forall laws a. a -> FreeRing laws a
FreeV (a -> FreeRing RingLaws a) -> a -> FreeRing RingLaws a
forall a b. (a -> b) -> a -> b
$ a -> a
forall a. Subtractive a => a -> a
negate a
a) (-Int
1))
| Bool
otherwise = Map (FreeRing RingLaws a) Int
-> Bag AddCommGroup (FreeRing RingLaws a)
forall laws a. Map a Int -> Bag laws a
Bag (Map (FreeRing RingLaws a) Int
-> Bag AddCommGroup (FreeRing RingLaws a))
-> Map (FreeRing RingLaws a) Int
-> Bag AddCommGroup (FreeRing RingLaws a)
forall a b. (a -> b) -> a -> b
$ FreeRing RingLaws a -> Int -> Map (FreeRing RingLaws a) Int
forall k a. k -> a -> Map k a
Map.singleton (a -> FreeRing RingLaws a
forall laws a. a -> FreeRing laws a
FreeV a
a) Int
1
bagR :: (Ord a) => FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
bagR :: FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
bagR FreeRing RingLaws a
a
| FreeRing RingLaws a
a FreeRing RingLaws a -> FreeRing RingLaws a -> Bool
forall a. Eq a => a -> a -> Bool
== Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. Seq a
Empty = Bag AddCommGroup (FreeRing RingLaws a)
forall a. Additive a => a
zero
| Bool
otherwise = Map (FreeRing RingLaws a) Int
-> Bag AddCommGroup (FreeRing RingLaws a)
forall laws a. Map a Int -> Bag laws a
Bag (Map (FreeRing RingLaws a) Int
-> Bag AddCommGroup (FreeRing RingLaws a))
-> Map (FreeRing RingLaws a) Int
-> Bag AddCommGroup (FreeRing RingLaws a)
forall a b. (a -> b) -> a -> b
$ FreeRing RingLaws a -> Int -> Map (FreeRing RingLaws a) Int
forall k a. k -> a -> Map k a
Map.singleton FreeRing RingLaws a
a Int
1
instance (Eq a, Ord a, Subtractive a, Multiplicative a) => Multiplicative (FreeRing RingLaws a) where
one :: FreeRing RingLaws a
one = a -> FreeRing RingLaws a
forall laws a. a -> FreeRing laws a
FreeV a
forall a. Multiplicative a => a
one
* :: FreeRing RingLaws a -> FreeRing RingLaws a -> FreeRing RingLaws a
(*) FreeRing RingLaws a
_ (FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
Empty) = Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. Seq a
Empty
(*) (FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
Empty) FreeRing RingLaws a
_ = Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. Seq a
Empty
(*) (FreeV a
vl) (FreeV a
vr)
| a
vl a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Multiplicative a => a
one = a -> FreeRing RingLaws a
forall laws a. a -> FreeRing laws a
FreeV a
vr
| a
vr a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Multiplicative a => a
one = a -> FreeRing RingLaws a
forall laws a. a -> FreeRing laws a
FreeV a
vl
| Bool
otherwise = Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
(Ord a, Subtractive a) =>
a -> Bag AddCommGroup (FreeRing RingLaws a)
bagV a
vl Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a -> Seq a
<| a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
(Ord a, Subtractive a) =>
a -> Bag AddCommGroup (FreeRing RingLaws a)
bagV a
vr Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a -> Seq a
<| Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. Seq a
Empty)
(*) (FreeV a
v) (FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
bs) =
Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a
forall a b. (a -> b) -> a -> b
$ (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a)))
-> (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a)))
-> Bool
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> a -> Bool -> a
bool (a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
(Ord a, Subtractive a) =>
a -> Bag AddCommGroup (FreeRing RingLaws a)
bagV a
v Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a -> Seq a
<|) Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id (a
v a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Multiplicative a => a
one) Seq (Bag AddCommGroup (FreeRing RingLaws a))
bs
(*) (FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
bs) (FreeV a
v) =
Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a
forall a b. (a -> b) -> a -> b
$ (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a)))
-> (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a)))
-> Bool
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> a -> Bool -> a
bool (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. Seq a -> a -> Seq a
|> a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
(Ord a, Subtractive a) =>
a -> Bag AddCommGroup (FreeRing RingLaws a)
bagV a
v) Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id (a
v a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Multiplicative a => a
one) Seq (Bag AddCommGroup (FreeRing RingLaws a))
bs
(*) (FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
as) (FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
bs) = Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a
forall a b. (a -> b) -> a -> b
$ Seq (Bag AddCommGroup (FreeRing RingLaws a))
as Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. Semigroup a => a -> a -> a
<> Seq (Bag AddCommGroup (FreeRing RingLaws a))
bs
instance forall (a :: Type). (Ord a, Ring a) => Additive (FreeRing RingLaws a) where
zero :: FreeRing RingLaws a
zero = Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. Seq a
Empty
+ :: FreeRing RingLaws a -> FreeRing RingLaws a -> FreeRing RingLaws a
(+) (FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
Empty) FreeRing RingLaws a
a = FreeRing RingLaws a
a
(+) FreeRing RingLaws a
a (FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
Empty) = FreeRing RingLaws a
a
(+) (FreeV a
vl) (FreeV a
vr) =
FreeRing RingLaws a
-> FreeRing RingLaws a -> Bool -> FreeRing RingLaws a
forall a. a -> a -> Bool -> a
bool
(Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a
forall a b. (a -> b) -> a -> b
$ Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a
Seq.singleton (Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a)))
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a b. (a -> b) -> a -> b
$ a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
(Ord a, Subtractive a) =>
a -> Bag AddCommGroup (FreeRing RingLaws a)
bagV a
vl Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
forall a. Additive a => a -> a -> a
+ a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
(Ord a, Subtractive a) =>
a -> Bag AddCommGroup (FreeRing RingLaws a)
bagV a
vr)
(Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. Seq a
Empty)
(a
vl a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a -> a
forall a. Subtractive a => a -> a
negate a
vr)
(+) (FreeV a
v) (FreeR (Bag AddCommGroup (FreeRing RingLaws a)
b :<| Seq (Bag AddCommGroup (FreeRing RingLaws a))
Empty)) = Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a
forall a b. (a -> b) -> a -> b
$ Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a
Seq.singleton (Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a)))
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a b. (a -> b) -> a -> b
$ a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
(Ord a, Subtractive a) =>
a -> Bag AddCommGroup (FreeRing RingLaws a)
bagV a
v Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
forall a. Additive a => a -> a -> a
+ Bag AddCommGroup (FreeRing RingLaws a)
b
(+) (FreeR (Bag AddCommGroup (FreeRing RingLaws a)
b :<| Seq (Bag AddCommGroup (FreeRing RingLaws a))
Empty)) (FreeV a
v) = Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a
forall a b. (a -> b) -> a -> b
$ Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a
Seq.singleton (Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a)))
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a b. (a -> b) -> a -> b
$ a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
(Ord a, Subtractive a) =>
a -> Bag AddCommGroup (FreeRing RingLaws a)
bagV a
v Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
forall a. Additive a => a -> a -> a
+ Bag AddCommGroup (FreeRing RingLaws a)
b
(+) (FreeV a
v) (FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
bs) =
Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a
forall a b. (a -> b) -> a -> b
$ Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a
Seq.singleton (Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a)))
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a b. (a -> b) -> a -> b
$
a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
(Ord a, Subtractive a) =>
a -> Bag AddCommGroup (FreeRing RingLaws a)
bagV a
v Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
forall a. Additive a => a -> a -> a
+ FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
Ord a =>
FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
bagR (Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
bs)
(+) (FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
bs) (FreeV a
v) =
Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a
forall a b. (a -> b) -> a -> b
$ Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a
Seq.singleton (Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a)))
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a b. (a -> b) -> a -> b
$
a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
(Ord a, Subtractive a) =>
a -> Bag AddCommGroup (FreeRing RingLaws a)
bagV a
v Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
forall a. Additive a => a -> a -> a
+ FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
Ord a =>
FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
bagR (Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
bs)
(+) (FreeR (Bag AddCommGroup (FreeRing RingLaws a)
a :<| Seq (Bag AddCommGroup (FreeRing RingLaws a))
Empty)) (FreeR (Bag AddCommGroup (FreeRing RingLaws a)
b :<| Seq (Bag AddCommGroup (FreeRing RingLaws a))
Empty)) =
Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a
forall a b. (a -> b) -> a -> b
$ Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a
Seq.singleton (Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a)))
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a b. (a -> b) -> a -> b
$ Bag AddCommGroup (FreeRing RingLaws a)
a Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
forall a. Additive a => a -> a -> a
+ Bag AddCommGroup (FreeRing RingLaws a)
b
(+) FreeRing RingLaws a
as (FreeR (Bag AddCommGroup (FreeRing RingLaws a)
b :<| Seq (Bag AddCommGroup (FreeRing RingLaws a))
Empty)) =
Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a
forall a b. (a -> b) -> a -> b
$ Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a
Seq.singleton (Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a)))
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a b. (a -> b) -> a -> b
$ FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
Ord a =>
FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
bagR FreeRing RingLaws a
as Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
forall a. Additive a => a -> a -> a
+ Bag AddCommGroup (FreeRing RingLaws a)
b
(+) (FreeR (Bag AddCommGroup (FreeRing RingLaws a)
a :<| Seq (Bag AddCommGroup (FreeRing RingLaws a))
Empty)) FreeRing RingLaws a
bs =
Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a
forall a b. (a -> b) -> a -> b
$ Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a
Seq.singleton (Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a)))
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a b. (a -> b) -> a -> b
$ FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
Ord a =>
FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
bagR FreeRing RingLaws a
bs Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
forall a. Additive a => a -> a -> a
+ Bag AddCommGroup (FreeRing RingLaws a)
a
(+) f :: FreeRing RingLaws a
f@(FreeR ((Bag AddCommGroup (FreeRing RingLaws a)
al :<| Seq (Bag AddCommGroup (FreeRing RingLaws a))
as') :|> Bag AddCommGroup (FreeRing RingLaws a)
ar)) f' :: FreeRing RingLaws a
f'@(FreeR ((Bag AddCommGroup (FreeRing RingLaws a)
bl :<| Seq (Bag AddCommGroup (FreeRing RingLaws a))
bs') :|> Bag AddCommGroup (FreeRing RingLaws a)
br))
| Bag AddCommGroup (FreeRing RingLaws a)
al Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a) -> Bool
forall a. Eq a => a -> a -> Bool
== Bag AddCommGroup (FreeRing RingLaws a)
bl = Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a
Seq.singleton Bag AddCommGroup (FreeRing RingLaws a)
al) FreeRing RingLaws a -> FreeRing RingLaws a -> FreeRing RingLaws a
forall a. Multiplicative a => a -> a -> a
* (Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Seq (Bag AddCommGroup (FreeRing RingLaws a))
as' Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. Seq a -> a -> Seq a
:|> Bag AddCommGroup (FreeRing RingLaws a)
ar) FreeRing RingLaws a -> FreeRing RingLaws a -> FreeRing RingLaws a
forall a. Additive a => a -> a -> a
+ Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Seq (Bag AddCommGroup (FreeRing RingLaws a))
bs' Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. Seq a -> a -> Seq a
:|> Bag AddCommGroup (FreeRing RingLaws a)
br))
| Bag AddCommGroup (FreeRing RingLaws a)
ar Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a) -> Bool
forall a. Eq a => a -> a -> Bool
== Bag AddCommGroup (FreeRing RingLaws a)
br = (Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Bag AddCommGroup (FreeRing RingLaws a)
al Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a -> Seq a
:<| Seq (Bag AddCommGroup (FreeRing RingLaws a))
as') FreeRing RingLaws a -> FreeRing RingLaws a -> FreeRing RingLaws a
forall a. Additive a => a -> a -> a
+ Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Bag AddCommGroup (FreeRing RingLaws a)
bl Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a -> Seq a
:<| Seq (Bag AddCommGroup (FreeRing RingLaws a))
bs')) FreeRing RingLaws a -> FreeRing RingLaws a -> FreeRing RingLaws a
forall a. Multiplicative a => a -> a -> a
* Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a
Seq.singleton Bag AddCommGroup (FreeRing RingLaws a)
ar)
| Bool
otherwise =
Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a
forall a b. (a -> b) -> a -> b
$ Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a
Seq.singleton (Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a)))
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a b. (a -> b) -> a -> b
$ FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
Ord a =>
FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
bagR FreeRing RingLaws a
f Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
forall a. Additive a => a -> a -> a
+ FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
Ord a =>
FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
bagR FreeRing RingLaws a
f'
(+) FreeRing RingLaws a
a FreeRing RingLaws a
b = Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a
forall a b. (a -> b) -> a -> b
$ Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a
Seq.singleton (Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a)))
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a b. (a -> b) -> a -> b
$ FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
Ord a =>
FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
bagR FreeRing RingLaws a
a Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
forall a. Additive a => a -> a -> a
+ FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
forall a.
Ord a =>
FreeRing RingLaws a -> Bag AddCommGroup (FreeRing RingLaws a)
bagR FreeRing RingLaws a
b
instance (Show a, Ord a, Ring a) => Subtractive (FreeRing RingLaws a) where
negate :: FreeRing RingLaws a -> FreeRing RingLaws a
negate (FreeV a
a) = a -> FreeRing RingLaws a
forall laws a. a -> FreeRing laws a
FreeV (a -> a
forall a. Subtractive a => a -> a
negate a
a)
negate (FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
Empty) = Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. Seq a
Empty
negate (FreeR ((Bag Map (FreeRing RingLaws a) Int
m) :<| Seq (Bag AddCommGroup (FreeRing RingLaws a))
xs)) =
Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR (Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> FreeRing RingLaws a
forall a b. (a -> b) -> a -> b
$ Bag AddCommGroup (FreeRing RingLaws a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. a -> Seq a
Seq.singleton (Map (FreeRing RingLaws a) Int
-> Bag AddCommGroup (FreeRing RingLaws a)
forall laws a. Map a Int -> Bag laws a
Bag (Map (FreeRing RingLaws a) Int
-> Bag AddCommGroup (FreeRing RingLaws a))
-> Map (FreeRing RingLaws a) Int
-> Bag AddCommGroup (FreeRing RingLaws a)
forall a b. (a -> b) -> a -> b
$ (Int -> Int)
-> Map (FreeRing RingLaws a) Int -> Map (FreeRing RingLaws a) Int
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map Int -> Int
forall a. Subtractive a => a -> a
negate Map (FreeRing RingLaws a) Int
m) Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
-> Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. Semigroup a => a -> a -> a
<> Seq (Bag AddCommGroup (FreeRing RingLaws a))
xs
instance (Show a, Eq a, Ord a, Ring a) => FreeAlgebra Exp (FreeRing RingLaws) a where
forget :: Exp a -> FreeRing RingLaws a
forget (Value a
a) = a -> FreeRing RingLaws a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
a -> free a
lift a
a
forget (Add Exp a
a Exp a
b) = Exp a -> FreeRing RingLaws a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Exp a
a FreeRing RingLaws a -> FreeRing RingLaws a -> FreeRing RingLaws a
forall a. Additive a => a -> a -> a
+ Exp a -> FreeRing RingLaws a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Exp a
b
forget (Mult Exp a
a Exp a
b) = Exp a -> FreeRing RingLaws a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Exp a
a FreeRing RingLaws a -> FreeRing RingLaws a -> FreeRing RingLaws a
forall a. Multiplicative a => a -> a -> a
* Exp a -> FreeRing RingLaws a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
initial a -> free a
forget Exp a
b
lift :: a -> FreeRing RingLaws a
lift a
a = FreeRing RingLaws a
-> FreeRing RingLaws a -> Bool -> FreeRing RingLaws a
forall a. a -> a -> Bool -> a
bool (a -> FreeRing RingLaws a
forall laws a. a -> FreeRing laws a
FreeV a
a) (Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> FreeRing RingLaws a
forall laws a.
Seq (Bag AddCommGroup (FreeRing laws a)) -> FreeRing laws a
FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
forall a. Seq a
Empty) (a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. Additive a => a
zero)
algebra :: FreeRing RingLaws a -> a
algebra (FreeV a
a) = a
a
algebra (FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
Empty) = a
forall a. Additive a => a
zero
algebra (FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
xs) = (a -> a -> a) -> a -> Seq a -> a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> a -> a
forall a. Multiplicative a => a -> a -> a
(*) a
forall a. Multiplicative a => a
one (FreeRing RingLaws a -> a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra (FreeRing RingLaws a -> a)
-> (Bag AddCommGroup (FreeRing RingLaws a) -> FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a)
-> a
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Bag AddCommGroup (FreeRing RingLaws a) -> FreeRing RingLaws a
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> a
algebra (Bag AddCommGroup (FreeRing RingLaws a) -> a)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> Seq a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq (Bag AddCommGroup (FreeRing RingLaws a))
xs)
printf :: FreeRing RingLaws a -> Text
printf (FreeV a
v) = a -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show a
v
printf (FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
Empty) = a -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show @a (Additive a => a
forall a. Additive a => a
zero @a)
printf (FreeR Seq (Bag AddCommGroup (FreeRing RingLaws a))
bs) = Text -> [Text] -> Text
calate Text
"*" (Seq Text -> [Item (Seq Text)]
forall l. IsList l => l -> [Item l]
toList (Seq Text -> [Item (Seq Text)]) -> Seq Text -> [Item (Seq Text)]
forall a b. (a -> b) -> a -> b
$ Bag AddCommGroup (FreeRing RingLaws a) -> Text
printBagFreeR (Bag AddCommGroup (FreeRing RingLaws a) -> Text)
-> Seq (Bag AddCommGroup (FreeRing RingLaws a)) -> Seq Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq (Bag AddCommGroup (FreeRing RingLaws a))
bs)
where
printBagFreeR :: Bag AddCommGroup (FreeRing RingLaws a) -> Text
printBagFreeR Bag AddCommGroup (FreeRing RingLaws a)
b =
Text -> Text -> Bool -> Text
forall a. a -> a -> Bool -> a
bool
(Text -> [Text] -> Text
calate Text
"+" (FreeRing RingLaws a -> Text
forall (initial :: * -> *) (free :: * -> *) a.
FreeAlgebra initial free a =>
free a -> Text
printf (FreeRing RingLaws a -> Text) -> [FreeRing RingLaws a] -> [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bag AddCommGroup (FreeRing RingLaws a)
-> [Item (Bag AddCommGroup (FreeRing RingLaws a))]
forall l. IsList l => l -> [Item l]
toList Bag AddCommGroup (FreeRing RingLaws a)
b))
(a -> Text
forall a b. (Show a, ConvertText String b) => a -> b
show @a a
forall a. Additive a => a
zero)
(Bag AddCommGroup (FreeRing RingLaws a)
b Bag AddCommGroup (FreeRing RingLaws a)
-> Bag AddCommGroup (FreeRing RingLaws a) -> Bool
forall a. Eq a => a -> a -> Bool
== Bag AddCommGroup (FreeRing RingLaws a)
forall a. Additive a => a
zero)
data BadExpParse = BadExpParse deriving (Int -> BadExpParse -> ShowS
[BadExpParse] -> ShowS
BadExpParse -> String
(Int -> BadExpParse -> ShowS)
-> (BadExpParse -> String)
-> ([BadExpParse] -> ShowS)
-> Show BadExpParse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BadExpParse] -> ShowS
$cshowList :: [BadExpParse] -> ShowS
show :: BadExpParse -> String
$cshow :: BadExpParse -> String
showsPrec :: Int -> BadExpParse -> ShowS
$cshowsPrec :: Int -> BadExpParse -> ShowS
Show)
instance Exception BadExpParse
parseExp :: Text -> Exp Int
parseExp :: Text -> Exp Int
parseExp Text
t = (String -> Exp Int)
-> (Exp Int -> Exp Int) -> Either String (Exp Int) -> Exp Int
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (BadExpParse -> String -> Exp Int
forall a e. Exception e => e -> a
throw BadExpParse
BadExpParse) Exp Int -> Exp Int
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id (Either String (Exp Int) -> Exp Int)
-> Either String (Exp Int) -> Exp Int
forall a b. (a -> b) -> a -> b
$ Parser (Exp Int) -> Text -> Either String (Exp Int)
forall a. Parser a -> Text -> Either String a
A.parseOnly Parser (Exp Int)
expr Text
t
expr :: A.Parser (Exp Int)
expr :: Parser (Exp Int)
expr = Parser (Exp Int)
-> Parser (Exp Int -> Exp Int -> Exp Int) -> Parser (Exp Int)
forall a. Parser a -> Parser (a -> a -> a) -> Parser a
branch Parser (Exp Int)
term Parser (Exp Int -> Exp Int -> Exp Int)
forall a. Parser (Exp a -> Exp a -> Exp a)
add
factor :: A.Parser (Exp Int)
factor :: Parser (Exp Int)
factor = Parser (Exp Int)
val Parser (Exp Int) -> Parser (Exp Int) -> Parser (Exp Int)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser (Exp Int) -> Parser (Exp Int)
forall a. Parser a -> Parser a
paren Parser (Exp Int)
expr
term :: A.Parser (Exp Int)
term :: Parser (Exp Int)
term = Parser (Exp Int)
-> Parser (Exp Int -> Exp Int -> Exp Int) -> Parser (Exp Int)
forall a. Parser a -> Parser (a -> a -> a) -> Parser a
branch Parser (Exp Int)
factor Parser (Exp Int -> Exp Int -> Exp Int)
forall a. Parser (Exp a -> Exp a -> Exp a)
mult
val :: A.Parser (Exp Int)
val :: Parser (Exp Int)
val = Parser ()
A.skipSpace Parser () -> Parser (Exp Int) -> Parser (Exp Int)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (Int -> Exp Int
forall a. a -> Exp a
Value (Int -> Exp Int) -> Parser Text Int -> Parser (Exp Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Text Int -> Parser Text Int
forall a. Num a => Parser a -> Parser a
A.signed Parser Text Int
forall a. Integral a => Parser a
A.decimal)
add :: A.Parser (Exp a -> Exp a -> Exp a)
add :: Parser (Exp a -> Exp a -> Exp a)
add = (Parser ()
A.skipSpace Parser () -> Parser Text Char -> Parser Text Char
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Char -> Parser Text Char
A.char Char
'+') Parser Text Char
-> (Exp a -> Exp a -> Exp a) -> Parser (Exp a -> Exp a -> Exp a)
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Exp a -> Exp a -> Exp a
forall a. Exp a -> Exp a -> Exp a
Add Parser (Exp a -> Exp a -> Exp a)
-> Parser () -> Parser (Exp a -> Exp a -> Exp a)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser ()
A.skipSpace
mult :: A.Parser (Exp a -> Exp a -> Exp a)
mult :: Parser (Exp a -> Exp a -> Exp a)
mult = (Parser ()
A.skipSpace Parser () -> Parser Text Char -> Parser Text Char
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Char -> Parser Text Char
A.char Char
'*') Parser Text Char
-> (Exp a -> Exp a -> Exp a) -> Parser (Exp a -> Exp a -> Exp a)
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Exp a -> Exp a -> Exp a
forall a. Exp a -> Exp a -> Exp a
Mult Parser (Exp a -> Exp a -> Exp a)
-> Parser () -> Parser (Exp a -> Exp a -> Exp a)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser ()
A.skipSpace
paren :: A.Parser a -> A.Parser a
paren :: Parser a -> Parser a
paren Parser a
p = do
()
_ <- Parser ()
A.skipSpace
Char
_ <- Char -> Parser Text Char
A.char Char
'('
()
_ <- Parser ()
A.skipSpace
a
r <- Parser a
p
()
_ <- Parser ()
A.skipSpace
Char
_ <- Char -> Parser Text Char
A.char Char
')'
a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
r Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser a
p
branch :: A.Parser a -> A.Parser (a -> a -> a) -> A.Parser a
branch :: Parser a -> Parser (a -> a -> a) -> Parser a
branch Parser a
p Parser (a -> a -> a)
op = do
a
a <- Parser a
p
a -> Parser a
more a
a
where
more :: a -> Parser a
more a
a' = (a -> Parser a
more (a -> Parser a) -> Parser a -> Parser a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ((a -> a -> a) -> a -> a -> a
forall a b. (a -> b) -> a -> b
($) ((a -> a -> a) -> a -> a -> a)
-> Parser (a -> a -> a) -> Parser (a -> a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser (a -> a -> a)
op Parser (a -> a -> a) -> Parser a -> Parser Text (a -> a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a' Parser Text (a -> a) -> Parser a -> Parser a
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser a
p)) Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a'
calate :: Text -> [Text] -> Text
calate :: Text -> [Text] -> Text
calate Text
_ [] = Text
forall a. Monoid a => a
mempty
calate Text
_ [Item [Text]
x] = Item [Text]
Text
x
calate Text
op [Text]
xs = Text
"(" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
Text.intercalate Text
op [Text]
xs Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"