• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

msakai / toysolver / 496

10 Nov 2024 11:05AM UTC coverage: 69.994% (-1.1%) from 71.113%
496

push

github

web-flow
Merge pull request #117 from msakai/update-coveralls-and-haddock

GitHub Actions: Update coveralls and haddock configuration

9872 of 14104 relevant lines covered (69.99%)

0.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

46.99
/src/ToySolver/Data/BoolExpr.hs
1
{-# LANGUAGE DeriveDataTypeable #-}
2
{-# LANGUAGE FlexibleContexts #-}
3
{-# LANGUAGE FlexibleInstances #-}
4
{-# LANGUAGE MultiParamTypeClasses #-}
5
{-# OPTIONS_GHC -Wall #-}
6
{-# OPTIONS_HADDOCK show-extensions #-}
7
-----------------------------------------------------------------------------
8
-- |
9
-- Module      :  ToySolver.Data.BoolExpr
10
-- Copyright   :  (c) Masahiro Sakai 2014-2015
11
-- License     :  BSD-style
12
--
13
-- Maintainer  :  masahiro.sakai@gmail.com
14
-- Stability   :  provisional
15
-- Portability :  non-portable
16
--
17
-- Boolean expression over a given type of atoms
18
--
19
-----------------------------------------------------------------------------
20
module ToySolver.Data.BoolExpr
21
  (
22
  -- * BoolExpr type
23
    BoolExpr (..)
24

25
  -- * Operations
26
  , fold
27
  , simplify
28
  ) where
29

30
import Control.DeepSeq
31
import Control.Monad
32
import Data.Data
33
import Data.Hashable
34
import Data.Traversable
35
import ToySolver.Data.Boolean
36
import ToySolver.Data.IntVar
37

38
-- | Boolean expression over a given type of atoms
39
data BoolExpr a
40
  = Atom a
41
  | And [BoolExpr a]
42
  | Or [BoolExpr a]
43
  | Not (BoolExpr a)
44
  | Imply (BoolExpr a) (BoolExpr a)
45
  | Equiv (BoolExpr a) (BoolExpr a)
46
  | ITE (BoolExpr a) (BoolExpr a) (BoolExpr a)
47
  deriving (Eq, Ord, Show, Read, Typeable, Data)
×
48

49
instance Functor BoolExpr where
×
50
  fmap = fmapDefault
1✔
51

52
instance Applicative BoolExpr where
×
53
  pure = Atom
1✔
54
  (<*>) = ap
1✔
55

56
instance Monad BoolExpr where
×
57
  return = pure
1✔
58
  m >>= f = fold f m
1✔
59

60
instance Foldable BoolExpr where
×
61
  foldMap = foldMapDefault
1✔
62

63
instance Traversable BoolExpr where
×
64
  traverse f (Atom x) = Atom <$> f x
1✔
65
  traverse f (And xs) = And <$> sequenceA (fmap (traverse f) xs)
1✔
66
  traverse f (Or xs) = Or <$> sequenceA (fmap (traverse f) xs)
1✔
67
  traverse f (Not x) = Not <$> traverse f x
1✔
68
  traverse f (Imply x y) = Imply <$> traverse f x <*> traverse f y
1✔
69
  traverse f (Equiv x y) = Equiv <$> traverse f x <*> traverse f y
1✔
70
  traverse f (ITE c t e) = ITE <$> traverse f c <*> traverse f t <*> traverse f e
1✔
71

72
instance NFData a => NFData (BoolExpr a) where
73
  rnf (Atom a) = rnf a
×
74
  rnf (And xs) = rnf xs
×
75
  rnf (Or xs) = rnf xs
×
76
  rnf (Not x) = rnf x
×
77
  rnf (Imply x y) = rnf x `seq` rnf y
×
78
  rnf (Equiv x y) = rnf x `seq` rnf y
×
79
  rnf (ITE c t e) = rnf c `seq` rnf t `seq` rnf e
×
80

81
instance Hashable a => Hashable (BoolExpr a) where
×
82
  hashWithSalt s (Atom a) = s `hashWithSalt` (0::Int) `hashWithSalt` a
×
83
  hashWithSalt s (And xs) = s `hashWithSalt` (1::Int) `hashWithSalt` xs
×
84
  hashWithSalt s (Or xs)  = s `hashWithSalt` (2::Int) `hashWithSalt` xs
×
85
  hashWithSalt s (Not x)  = s `hashWithSalt` (3::Int) `hashWithSalt` x
×
86
  hashWithSalt s (Imply x y) = s `hashWithSalt` (4::Int) `hashWithSalt` x `hashWithSalt` y
×
87
  hashWithSalt s (Equiv x y) = s `hashWithSalt` (5::Int) `hashWithSalt` x `hashWithSalt` y
×
88
  hashWithSalt s (ITE c t e) = s `hashWithSalt` (6::Int) `hashWithSalt` c `hashWithSalt` t `hashWithSalt` e
×
89

90
instance Complement (BoolExpr a) where
91
  notB = Not
1✔
92

93
instance MonotoneBoolean (BoolExpr a) where
1✔
94
  andB = And
1✔
95
  orB  = Or
1✔
96

97
instance IfThenElse (BoolExpr a) (BoolExpr a) where
98
  ite = ITE
1✔
99

100
instance Boolean (BoolExpr a) where
101
  (.=>.) = Imply
1✔
102
  (.<=>.) = Equiv
1✔
103

104
instance Variables a => Variables (BoolExpr a) where
105
  vars = foldMap vars
×
106

107

108
fold :: Boolean b => (atom -> b) -> BoolExpr atom -> b
109
fold f = g
1✔
110
  where
111
    g (Atom a) = f a
1✔
112
    g (Or xs) = orB (map g xs)
1✔
113
    g (And xs) = andB (map g xs)
1✔
114
    g (Not x) = notB (g x)
1✔
115
    g (Imply x y) = g x .=>. g y
1✔
116
    g (Equiv x y) = g x .<=>. g y
1✔
117
    g (ITE c t e) = ite (g c) (g t) (g e)
1✔
118

119
{-# RULES
120
  "fold/fmap"    forall f g e.  fold f (fmap g e) = fold (f.g) e
121
 #-}
122

123
instance Eval m a Bool => Eval m (BoolExpr a) Bool where
124
  eval m = fold (eval m)
1✔
125

126
simplify :: BoolExpr a -> BoolExpr a
127
simplify = runSimplify . fold (Simplify . Atom)
1✔
128

129
newtype Simplify a = Simplify{ runSimplify :: BoolExpr a }
1✔
130

131
instance Complement (Simplify a) where
132
  notB (Simplify (Not x)) = Simplify x
×
133
  notB (Simplify x) = Simplify (Not x)
×
134

135
instance MonotoneBoolean (Simplify a) where
×
136
  orB xs
1✔
137
    | any isTrue ys = Simplify true
×
138
    | otherwise = Simplify $ Or ys
×
139
    where
140
      ys = concat [f x | Simplify x <- xs]
×
141
      f (Or zs) = zs
×
142
      f z = [z]
×
143
  andB xs
1✔
144
    | any isFalse ys = Simplify false
1✔
145
    | otherwise = Simplify $ And ys
×
146
    where
147
      ys = concat [f x | Simplify x <- xs]
1✔
148
      f (And zs) = zs
1✔
149
      f z = [z]
1✔
150

151
instance IfThenElse (Simplify a) (Simplify a) where
152
  ite (Simplify c) (Simplify t) (Simplify e)
×
153
    | isTrue c  = Simplify t
×
154
    | isFalse c = Simplify e
×
155
    | otherwise = Simplify (ITE c t e)
×
156

157
instance Boolean (Simplify a) where
×
158
  Simplify x .=>. Simplify y
×
159
    | isFalse x = true
×
160
    | isTrue y  = true
×
161
    | isTrue x  = Simplify y
×
162
    | isFalse y = notB (Simplify x)
×
163
    | otherwise = Simplify (x .=>. y)
×
164

165
isTrue :: BoolExpr a -> Bool
166
isTrue (And []) = True
×
167
isTrue _ = False
×
168

169
isFalse :: BoolExpr a -> Bool
170
isFalse (Or []) = True
1✔
171
isFalse _ = False
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc