-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstraints.hs
63 lines (51 loc) · 1.98 KB
/
Constraints.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
57
58
59
60
61
62
63
-- Constraint definition and solving.
-- Author: Ben Blum <bblum@andrew.cmu.edu>
module Constraints where
import Data.List (intercalate)
import qualified Rules as R
-- Identifiers for symbolic (i.e., unsolved) function rules and effects.
data RV = RV Int String
data EV = EV Int String
type Unknown = (RV,EV)
instance Eq RV where
(RV a _) == (RV b _) = a == b
instance Eq EV where
(EV a _) == (EV b _) = a == b
-- instance Ord RV where -- in case I need to put them in maps, I guess?
-- (RV a _) <= (RV b _) = a <= b
-- instance Ord EV where
-- (EV a _) <= (EV b _) = a <= b
instance Show RV where
show (RV _ name) = "R{" ++ name ++ "}"
instance Show EV where
show (EV _ name) = "E{" ++ name ++ "}"
-- The elements of constraint expressions. Can be variables or fixed constants.
data R = RuleVar RV | RuleConst R.Rule deriving Eq
data E = EffectVar EV | EffectConst R.Effect deriving Eq
instance Show R where
show (RuleVar rv) = show rv
show (RuleConst rule) = show rule
instance Show E where
show (EffectVar ev) = show ev
show (EffectConst effect) = show effect
-- Note: the "<=" in the rule constraint is the subtyping comparison, not
-- the numeric comparison, so it's reversed (e.g., infinity <= 0).
data Constraint = EffectConstraint E [E] -- e = e1 + e2 + ... en
| RuleConstraint R R [E] -- r <= r1 + e1 + e2 + ... en
| InvariantConstraint R R -- r == r1; in fnptr assignment
deriving Eq
instance Show Constraint where
show (EffectConstraint e es) =
show e ++ " = " ++ (intercalate " + " $ map show es)
show (RuleConstraint r r1 []) =
show r ++ " <= " ++ show r1
show (RuleConstraint r r1 es) =
show r ++ " <= " ++ show r1 ++ " + " ++ (intercalate " + " $ map show es)
show (InvariantConstraint r r1) =
show r ++ " = " ++ show r1
--
-- Constaint processing.
--
-- Constant folding.
cfold :: Constraint -> Either (Maybe Constraint) (Constraint)
cfold x = Left $ Just x