|
| 1 | +module VSAT where |
| 2 | + |
| 3 | +import Prelude hiding (and,or) |
| 4 | + |
| 5 | + |
| 6 | +type Dim = String |
| 7 | +type Var = String |
| 8 | + |
| 9 | +data Op = And | Or |
| 10 | + deriving (Eq,Show) |
| 11 | + |
| 12 | +data Exp |
| 13 | + = Sym |
| 14 | + | Unit |
| 15 | + | Ref Var |
| 16 | + | Not Exp |
| 17 | + | Bin Op Exp Exp |
| 18 | + | Chc Dim Exp Exp |
| 19 | + deriving Eq |
| 20 | + |
| 21 | +instance Show Exp where |
| 22 | + show Sym = "s" |
| 23 | + show Unit = "o" |
| 24 | + show (Ref x) = x |
| 25 | + show (Chc d l r) = concat [d, "<", show l, ",", show r, ">"] |
| 26 | + show (Bin Or l r) = show l ++ " | " ++ show r |
| 27 | + show (Bin And l r) = help l ++ " & " ++ help r |
| 28 | + where |
| 29 | + help e@(Bin Or _ _) = "(" ++ show e ++ ")" |
| 30 | + help e = show e |
| 31 | + |
| 32 | +and = Bin And |
| 33 | +or = Bin Or |
| 34 | +a = Ref "a" |
| 35 | +b = Ref "b" |
| 36 | +c = Ref "c" |
| 37 | +chc = Chc "D" |
| 38 | +chc' = Chc "D'" |
| 39 | + |
| 40 | + |
| 41 | +-- | Structure of accumulation. |
| 42 | +acc :: Exp -> Exp |
| 43 | +acc (Ref _) = Sym |
| 44 | +acc (Not e) = case acc e of |
| 45 | + Sym -> Sym |
| 46 | + e' -> Not e' |
| 47 | +acc (Bin o l r) = case (acc l, acc r) of |
| 48 | + (Sym, Sym) -> Sym |
| 49 | + (l', r') -> Bin o l' r' |
| 50 | +acc e = e |
| 51 | + |
| 52 | +-- | Structure of evaluation. |
| 53 | +eval :: Exp -> Exp |
| 54 | +eval Sym = Unit |
| 55 | +eval (Bin And l r) = case (eval l, eval r) of |
| 56 | + (Unit, r') -> r' |
| 57 | + (l', Unit) -> l' |
| 58 | + (l', r') -> Bin And l' r' |
| 59 | +eval e = let e' = acc e |
| 60 | + in if e == e' then e' else eval e' |
| 61 | + |
| 62 | + |
| 63 | +data Ctx = InL Op Exp Ctx |
| 64 | + | InR Op Ctx -- there's a Sym in here! |
| 65 | + | InN Ctx -- there's a Sym in here! |
| 66 | + | Top |
| 67 | + |
| 68 | +type Cfg = [(Dim,Bool)] |
| 69 | + |
| 70 | +-- | Structure of choice removal. |
| 71 | +cr :: Cfg -> Ctx -> Exp -> (Exp,Int) |
| 72 | + |
| 73 | +cr c Top Sym = (eval Sym,1) |
| 74 | + |
| 75 | +cr c (InL o r z) Sym = cr c (InR o z) r |
| 76 | +cr c (InR o z) Sym = let s = acc (Bin o Sym Sym) |
| 77 | + in cr c z s |
| 78 | + |
| 79 | +cr c (InN z) Sym = let s = acc (Not Sym) |
| 80 | + in cr c z s |
| 81 | + |
| 82 | +-- recursive cases |
| 83 | +cr c z (Not e) = cr c (InN z) e |
| 84 | + |
| 85 | +cr c z (Bin And l r) = cr c (InL And r z) l |
| 86 | +cr c z (Bin Or l r) = cr c (InL Or r z) l |
| 87 | + |
| 88 | +cr c z (Chc d l r) = case lookup d c of |
| 89 | + Just True -> cr c z l |
| 90 | + Just False -> cr c z r |
| 91 | + Nothing -> let (Unit,i) = cr ((d,True):c) z l |
| 92 | + (Unit,j) = cr ((d,False):c) z r |
| 93 | + in (Unit,i+j) |
| 94 | + |
| 95 | +cr c z e = let e' = acc e |
| 96 | + in if e == e' then error "boom" else cr c z e' |
| 97 | + |
| 98 | + |
| 99 | +-- Examples from accumulation section. |
| 100 | +ex1 = or a (and a b) |
| 101 | +ex2 = or ex1 (and (chc a (and a b)) ex1) |
| 102 | + |
| 103 | + |
| 104 | +-- Example from evaluation section. |
| 105 | +ex3 = and (or a b) (chc a c) |
| 106 | + |
| 107 | +ex4 = and (chc' a c) (or a b) |
0 commit comments