Product.hs 1.5 KB
Newer Older
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module MA.Functors.Product
  ( product
  , Product(..)
  ) where

import           Control.Monad (void)
import           Prelude hiding (product)

import           Data.Tuple.Extra (both)

import           MA.RefinementInterface
import qualified MA.Parser.Lexer as L
import           MA.FunctorExpression.Parser
import           MA.Coalgebra.RefinementTypes
import           MA.Coalgebra.Parser

data Product a = Product a a
  deriving (Show, Functor, Foldable, Traversable)

product :: FunctorParser Product
21
product = infixR $ do
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  void $ L.symbol "×"
  return Product

data Side = L | R
  deriving (Eq)

data Three = ToRest | ToCompound | ToSub
  deriving (Show, Eq, Ord, Enum)

type instance H1 Product = ()
type instance H3 Product = (Three, Three)
type instance Label Product = Side
type instance Weight Product = (Bool, Bool)

instance ParseMorphism Product where
  parseMorphismPoint (Product parseLeft parseRight) = L.parens $ do
    left <- parseLeft
    void L.comma
    right <- parseRight

    return ((), [(left, L), (right, R)])

instance RefinementInterface Product where
  init :: H1 Product -> [Label Product] -> Weight Product
  init _ _ = (True, True)

  update :: [Label Product] -> Weight Product -> (Weight Product, H3 Product, Weight Product)
  update labels (left, right) = val up
    where
      val h3 = (both (==ToSub) h3, h3, both (==ToCompound) h3)

      up = (left +? (L `elem` labels), right +? (R `elem` labels))

      (+?) :: Bool -> Bool -> Three
      a +? b = toEnum (fromEnum a + fromEnum b)