Commit d13f8db4 authored by Bastian Kauschke's avatar Bastian Kauschke
Browse files

impl Printable for FunctorExpressions

parent dc74bd5b
......@@ -13,4 +13,3 @@ transform
-> FunctorExpression g a
transform _ Variable = Variable
transform f (Functor a inner) = Functor a (f (fmap (transform f) inner))
......@@ -48,6 +48,7 @@ import qualified Copar.Parser.Lexer as L
import Copar.RefinementInterface
import Copar.Functors.Polynomial hiding ( PolyF1(..) )
import Copar.FunctorExpression.Type
import Copar.FunctorExpression.Printable
import Copar.Parser.Types
import qualified Data.Vector.Utils as V
import Data.Primitive.SmallArray
......@@ -62,6 +63,13 @@ $(deriveShow1 ''Inner)
newtype AbsorbingPolynomial f a = AbsorbingPolynomial (Polynomial (Inner f a))
deriving (Functor, Foldable, Traversable)
instance (Printable f) => Printable (AbsorbingPolynomial f) where
printMe (AbsorbingPolynomial poly) = "AbsorbingPolynomial "
<> printPolynomial ((\case
Direct a -> a
Absorbed inner -> "(" <> printMe inner <> ")"
) <$> poly)
$(deriveShow1 ''AbsorbingPolynomial)
......
......@@ -27,6 +27,7 @@ import Data.Text.Prettyprint ((<+>))
import Copar.RefinementInterface
import qualified Copar.Parser.Lexer as L
import Copar.FunctorExpression.Parser
import Copar.FunctorExpression.Printable
import Copar.Coalgebra.Parser
import Copar.Functors.GroupValued
import Copar.FunctorDescription
......@@ -34,6 +35,9 @@ import Copar.FunctorDescription
newtype Bag a = Bag a
deriving (Functor,Foldable,Traversable)
instance Printable Bag where
printMe (Bag x) = "Bag B" <> x
bag :: FunctorDescription Bag
bag = FunctorDescription
{ name = "Bag"
......
......@@ -22,6 +22,7 @@ import Data.Float.Utils (EqDouble)
import Copar.Coalgebra.Parser
import Copar.FunctorDescription
import Copar.FunctorExpression.Parser
import Copar.FunctorExpression.Printable
import Copar.Functors.GroupValued
import qualified Copar.Parser.Lexer as L
import Copar.RefinementInterface
......@@ -30,6 +31,9 @@ import Copar.RefinementInterface
newtype Distribution x = Distribution x
deriving (Functor, Foldable, Traversable)
instance Printable Distribution where
printMe (Distribution x) ="Distribution D" <> x
distribution :: FunctorDescription Distribution
distribution = FunctorDescription
{ name = "Distribution"
......
......@@ -37,6 +37,7 @@ import qualified Data.Vector.Utils as V
import Copar.RefinementInterface
import Copar.Coalgebra.Parser
import Copar.FunctorExpression.Parser
import Copar.FunctorExpression.Printable
import qualified Copar.Parser.Lexer as L
import Copar.Parser.Types
import Copar.FunctorDescription
......@@ -53,6 +54,20 @@ deriving instance Traversable (GroupValued m)
$(deriveShow1 ''GroupValued)
instance Printable (GroupValued Int) where
printMe (GroupValued x) = "Integer-valued Z^" <> x
instance Printable (GroupValued EqDouble) where
printMe (GroupValued x) = "Real-valued R^" <> x
instance Printable (GroupValued (Ratio Int)) where
printMe (GroupValued x) = "Rational-valued Q^" <> x
instance Printable (GroupValued OrderedComplex) where
printMe (GroupValued x) = "Complex-valued C^" <> x
intValued :: FunctorDescription (GroupValued Int)
intValued = FunctorDescription
{ name = "Integer-valued"
......
......@@ -37,6 +37,7 @@ import Copar.RefinementInterface
import Copar.FunctorDescription
import qualified Copar.Parser.Lexer as L
import Copar.FunctorExpression.Parser
import Copar.FunctorExpression.Printable
import Copar.Coalgebra.Parser
import Data.Float.Utils ( MaxDouble(..)
, MinDouble(..)
......@@ -49,6 +50,26 @@ import Data.Bits.Monoid
newtype SlowMonoidValued m a = SlowMonoidValued a
-- TODO We may want to change MonoidValuedDiscription to a type class,
-- this would allow us to simplify these instances.
instance Printable (SlowMonoidValued (Max Int)) where
printMe (SlowMonoidValued x) = "Max-valued (Z, max)^" <> x
instance Printable (SlowMonoidValued (Min Int)) where
printMe (SlowMonoidValued x) = "Min-valued (Z, min)^" <> x
instance Printable (SlowMonoidValued MaxDouble) where
printMe (SlowMonoidValued x) = "Max-valued (R, max)^" <> x
instance Printable (SlowMonoidValued MinDouble) where
printMe (SlowMonoidValued x) = "Min-valued (R, min)^" <> x
instance Printable (SlowMonoidValued (BitAnd Word64)) where
printMe (SlowMonoidValued x) = "BitAnd-valued (Word, and)^" <> x
instance Printable (SlowMonoidValued (BitOr Word64)) where
printMe (SlowMonoidValued x) = "BitOr-valued (Word, or)^" <> x
instance Eq1 (SlowMonoidValued m) where
liftEq f (SlowMonoidValued a1) (SlowMonoidValued a2) = f a1 a2
......@@ -142,7 +163,7 @@ maxRealValued = makeMonoidValued $ MonoidValued
}
-- | The @(ℝ, max)^X@ functor
-- | The @(ℝ, min)^X@ functor
minRealValued :: FunctorDescription (SlowMonoidValued MinDouble)
minRealValued = makeMonoidValued $ MonoidValued
{ mvName = "Min"
......
......@@ -11,6 +11,7 @@
module Copar.Functors.Polynomial
( -- * Functor expression parser
polynomial
, printPolynomial
-- * Types exported for easier testing
, Polynomial(..)
, Sum(..)
......@@ -23,6 +24,7 @@ module Copar.Functors.Polynomial
import Control.Monad
import Data.Bifunctor
import Data.Foldable (fold)
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as NonEmpty
import Data.Traversable
......@@ -45,6 +47,8 @@ import Data.Eq.Deriving (deriveEq1)
import Text.Show.Deriving (deriveShow1)
import Lens.Micro
import Control.DeepSeq
import Data.Text.Lazy.Builder as Build
import Data.Text.Lazy.Builder.Int as Build
import qualified Data.Text.Prettyprint as Doc
import Data.Text.Prettyprint ((<+>))
......@@ -53,8 +57,10 @@ import qualified Copar.Parser.Lexer as L
import Copar.Parser.Types
import Copar.RefinementInterface
import Copar.FunctorExpression.Parser
import Copar.FunctorExpression.Printable
import Copar.FunctorDescription
newtype Polynomial a = Polynomial (Sum a)
deriving (Functor, Foldable, Traversable)
......@@ -90,6 +96,34 @@ $(deriveEq1 ''Polynomial)
$(deriveShow1 ''Factor)
$(deriveShow1 ''Polynomial)
instance Printable Polynomial where
printMe poly = "Polynomial " <> printPolynomial poly
printPolynomial :: Polynomial Build.Builder -> Build.Builder
printPolynomial (Polynomial (Sum (prods))) = fold (NonEmpty.intersperse " + " (fmap printProduct prods))
printProduct :: Product Build.Builder -> Build.Builder
printProduct (Product (factors)) = fold (NonEmpty.intersperse "x" (fmap printFactor factors))
printFactor :: Factor Build.Builder -> Build.Builder
printFactor (Const IntSet) = "Z"
printFactor (Const NatSet) = "N"
printFactor (Const (FiniteNatSet v)) = Build.decimal v
printFactor (Const (ExplicitSet elems)) =
(V.foldl
(\s e -> s <> ", " <> Build.fromText e)
("{" <> Build.fromText (V.head elems)) (V.tail elems)
) <> "}"
printFactor (Identity a) = a
printFactor (Exponential a exp) = a <> "^" <> printExp exp
printExp :: Exponent -> Build.Builder
printExp (FiniteNatExp v) = Build.decimal v
printExp (ExplicitExp elems) =
(V.foldl (\s e -> s <> ", " <> Build.fromText e) ("{" <> Build.fromText (V.head elems)) (V.tail elems)) <> "}"
-- | Custom Show1 instance to show NonEmpty list like a normal list for easier
-- reading.
......
......@@ -25,6 +25,7 @@ import Data.Text.Prettyprint ((<+>))
import Copar.RefinementInterface
import qualified Copar.Parser.Lexer as L
import Copar.FunctorExpression.Parser
import Copar.FunctorExpression.Printable
import Copar.Coalgebra.Parser
import Copar.FunctorDescription
......@@ -33,6 +34,8 @@ newtype Powerset a = Powerset a
$(deriveShow1 ''Powerset)
instance Printable Powerset where
printMe (Powerset x) = "Powerset P" <> x
powerset :: FunctorDescription Powerset
powerset = FunctorDescription
......
......@@ -33,12 +33,13 @@ import qualified Data.Text as T
import Copar.Coalgebra.Parser.Class
import Copar.FunctorDescription
import Copar.FunctorExpression.Parser
import Copar.FunctorExpression.Printable
import Copar.PrettyShow
import Copar.RefinementInterface
type Suitable f
= ( RefinementInterface f
, Printable f
, Functor f
, Foldable f
, Traversable f
......@@ -63,6 +64,9 @@ instance Show1 SomeFunctor where
liftShowsPrec shows' showsList' pred (SomeFunctor f) =
liftShowsPrec shows' showsList' pred f
instance Printable SomeFunctor where
printMe (SomeFunctor x) = printMe x
-- | Apply a natural transformation under the 'SomeFunctor'.
--
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment