Skip to content

Commit

Permalink
📝 Update README.md and print the empty predicate as true
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeta611 committed Jun 10, 2024
1 parent 1a1a9e7 commit 0f20467
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
64 changes: 33 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ Here's an example program and its components:
```ocaml
# student.stp
fun determine_grade(difficult, smart) {
if difficult = 1 & smart = 1 then 0.8
else if difficult = 1 & smart = 0 then 0.3
else if difficult = 0 & smart = 1 then 0.95
if difficult & smart then 0.8
else if difficult & !smart then 0.3
else if !difficult & smart then 0.95
else 0.5
}
let difficult = sample(bernoulli(0.4)) in
let smart = sample(bernoulli(0.3)) in
let grade = bernoulli(determine_grade(difficult, smart)) in
let sat = bernoulli(
if smart = 1 then 0.95
if smart then 0.94 +. 0.01 # partial evaluate to 0.95
else 0.2
) in
observe(grade, 0);
observe(sat, 1);
observe(grade, false);
observe(sat, true);
smart
```

Expand All @@ -51,45 +51,48 @@ smart
((funs
(((name determine_grade) (params (difficult smart))
(body
(If (And (Eq (Var difficult) (Int 1)) (Eq (Var smart) (Int 1)))
(Real 0.8)
(If (And (Eq (Var difficult) (Int 1)) (Eq (Var smart) (Int 0)))
(Real 0.3)
(If (And (Eq (Var difficult) (Int 0)) (Eq (Var smart) (Int 1)))
(Real 0.95) (Real 0.5))))))))
(If (And (Var difficult) (Var smart)) (Real 0.8)
(If (And (Var difficult) (Not (Var smart))) (Real 0.3)
(If (And (Not (Var difficult)) (Var smart)) (Real 0.95) (Real 0.5))))))))
(exp
(Assign difficult (Sample (Call bernoulli ((Real 0.4))))
(Assign smart (Sample (Call bernoulli ((Real 0.3))))
(Assign grade
(Call bernoulli ((Call determine_grade ((Var difficult) (Var smart)))))
(Assign sat
(Call bernoulli ((If (Eq (Var smart) (Int 1)) (Real 0.95) (Real 0.2))))
(Seq (Observe (Var grade) (Int 0))
(Seq (Observe (Var sat) (Int 1)) (Var smart)))))))))
(Call bernoulli
((If (Var smart) (Radd (Real 0.94) (Real 0.01)) (Real 0.2))))
(Seq (Observe (Var grade) (Bool false))
(Seq (Observe (Var sat) (Bool true)) (Var smart)))))))))
```

#### Graph Mode (-graph)

```scheme
((vertices (X1 X2 X3 X4)) (arcs ((X1 X3) (X2 X3) (X2 X4)))
(pmdf_map
((X1 (Dist_obj (dist bernoulli) (var X1) (args ((Real 0.4)))))
(X2 (Dist_obj (dist bernoulli) (var X2) (args ((Real 0.3)))))
((X1 (Call bernoulli ((Value 0.4)))) (X2 (Call bernoulli ((Value 0.3))))
(X3
(If_pred Empty
(Dist_obj (dist bernoulli) (var X3)
(args
((If (And (Eq (Var X1) (Int 1)) (Eq (Var X2) (Int 1))) (Real 0.8)
(If (And (Eq (Var X1) (Int 1)) (Eq (Var X2) (Int 0))) (Real 0.3)
(If (And (Eq (Var X1) (Int 0)) (Eq (Var X2) (Int 1))) (Real 0.95)
(Real 0.5)))))))
One))
(If (Value true)
(Call bernoulli
((If (Bop && (Value true) (Bop && (Var X1) (Var X2))) (Value 0.8)
(If
(Bop && (Bop && (Value true) (Uop not (Bop && (Var X1) (Var X2))))
(Bop && (Var X1) (Uop ! (Var X2))))
(Value 0.3)
(If
(Bop &&
(Bop && (Bop && (Value true) (Uop not (Bop && (Var X1) (Var X2))))
(Uop not (Bop && (Var X1) (Uop ! (Var X2)))))
(Bop && (Uop ! (Var X1)) (Var X2)))
(Value 0.95) (Value 0.5))))))
(Value 1)))
(X4
(If_pred Empty
(Dist_obj (dist bernoulli) (var X4)
(args ((If (Eq (Var X2) (Int 1)) (Real 0.95) (Real 0.2)))))
One))))
(obs_map ((X3 (Int 0)) (X4 (Int 1)))))
(If (Value true)
(Call bernoulli
((If (Bop && (Value true) (Var X2)) (Value 0.95) (Value 0.2))))
(Value 1)))))
(obs_map ((X3 (Value false)) (X4 (Value true)))))
```

#### Inference Mode
Expand All @@ -99,4 +102,3 @@ smart
## License

STAPPL is available under the MIT license. See the [LICENSE](LICENSE) file for more info.

3 changes: 1 addition & 2 deletions lib/typed_tree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,7 @@ module Erased = struct
| arg :: args -> of_exp arg :: of_args args

and of_pred : pred -> exp = function
| Empty -> Value ""
| True -> Value "true"
| Empty | True -> Value "true"
| False -> Value "false"
| And (pred, exp) -> Bop ("&&", of_pred pred, of_exp exp)
| And_not (pred, exp) -> Bop ("&&", of_pred pred, Uop ("not", of_exp exp))
Expand Down
Binary file modified samples/student.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0f20467

Please sign in to comment.