Skip to content

Commit

Permalink
🎉 Inference
Browse files Browse the repository at this point in the history
  • Loading branch information
yhs0602 committed Jun 2, 2024
1 parent 5f31756 commit 8937cc0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
Binary file added distribution.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion lib/infer_env.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ type t = (Id.t, float, Id.comparator_witness) Map.t
let empty : t = Map.empty (module Id)

let add (env : t) (x : Id.t) (value : float) : t =
Map.add_exn env ~key:x ~data:value
Map.set env ~key:x ~data:value

let find (env : t) (x : Id.t) : float option = Map.find env x

let to_string (env : t) : string =
Map.to_alist env
|> List.map ~f:(fun (x, value) ->
Id.to_string x ^ " = " ^ Float.to_string value)
|> String.concat ~sep:", " |> Printf.sprintf "{ %s }"
5 changes: 4 additions & 1 deletion lib/inference_runner.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ let eval_with_infer_env (env : Infer_env.t) (exp : Det_exp.t) : float =
| Det_exp.Var x -> (
match Infer_env.find env x with
| Some value -> value
| None -> failwith "Variable not found in environment")
| None ->
Random.float 1.0
(*failwith (Printf.sprintf "Variable %s not found in environment in %s" x (Infer_env.to_string env)))*)
)
| Det_exp.Add (e1, e2) -> evi ( +. ) e1 e2
| Det_exp.Radd (e1, e2) -> evr ( +. ) e1 e2
| Det_exp.Minus (e1, e2) -> eval env e1 -. eval env e2
Expand Down
2 changes: 1 addition & 1 deletion samples/sample.stp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fun main() {
let z = sample(bernoulli(0.5+. 0.1)) in
let z = sample(bernoulli(0.8+. 0.1)) in
let mu = (if (z = 0 - 0) then ~-.1.0 else 1.0) in
let d = normal(mu, 0.0 +. 1.0) in
let y = 0.5 in
Expand Down

0 comments on commit 8937cc0

Please sign in to comment.