Skip to content

Commit e0f2460

Browse files
committed
add rules file
1 parent 59d95a5 commit e0f2460

2 files changed

Lines changed: 121 additions & 5 deletions

File tree

EMSE2021/README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
- the `*.nix` files in the `nix` directory, pin the development environment
1313
used in the benchmarks and analysis to specific commits of `nixpkgs` which
1414
are dated for the time of publication. This allows researchers to create a
15-
development environment identical to the one used in this paper for both
16-
the `vsat` tool and for the statistics scripts, in a `nix-shell`; Use `>
17-
nix-shell R.nix` for the `R` environment. To reproduce the haskell
18-
environment we rely on the `stack` built tool
15+
development environment identical to the one used in this paper for the
16+
statistics scripts, in a `nix-shell`; Use `> nix-shell R.nix` for the `R`
17+
environment. To reproduce the haskell environment we rely on the `stack`
18+
build tool.
1919
- We provide the source code R scripts used in our evaluation in the
2020
`statisticsScripts` directory. The scripts are heavily commented.
2121
- The vsat tool can be built using either `cabal`, `stack`. Both allow a
@@ -24,11 +24,20 @@
2424
recommend `stack` for most cases because it pins the package set to a
2525
specific package set, whereas `cabal` uses the package set present on the
2626
user's computer.
27+
- We also provide a small haskell implementation of the inference rules in
28+
the `rules/VSAT.hs` file. If you are looking to build your own variational
29+
solver, and want to learn the inference rules, this would be the simplest
30+
place to start because it is just a pure, functional model, i.e., it
31+
doesn't actually solve anything it just uses haskell's type system to show
32+
the rules are sound. Once you are comfortable, you can view the rest of
33+
the architecture in the `vsat` repo linked above.
2734

2835

2936
### The Vsat tool
3037
You can find the version of Vsat for this paper
31-
[here](https://github.com/doyougnu/VSat). To run the tool:
38+
[here](https://github.com/doyougnu/VSat) and archived here:
39+
[![DOI](https://zenodo.org/badge/105307042.svg)](https://zenodo.org/badge/latestdoi/105307042)
40+
. To run the tool:
3241
1. Clone the repository, i.e., `git clone https://github.com/doyougnu/VSat`
3342
2. If you are using `nix` un-comment the following in `stack.yaml`:
3443
```

EMSE2021/rules/VSAT.hs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)