Skip to content

Commit

Permalink
Desugar x <~ uniform([c]) to x <- c This is solution #2 of issue #8.
Browse files Browse the repository at this point in the history
  • Loading branch information
gstew5 committed Sep 18, 2019
1 parent 91104fe commit f32989d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
5 changes: 5 additions & 0 deletions programs/issue8.zar
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
main:
p <~ uniform([1]:[int])
x <~ bernoulli(1/2)
y <~ bernoulli(1/2)
return x
2 changes: 1 addition & 1 deletion src/Distributions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ uniform lbl dom =
where
n = length dom
go :: [Tree a] -> Tree a
go [] = Hole n -- Shouldn't happen.
go [] = error "internal error in Distributions:uniform; please report"
go [x] = x
go xs = let m = length xs `div` 2 in
Split Nothing (go $ take m xs) (go $ drop m xs)
Expand Down
10 changes: 7 additions & 3 deletions src/TreeInterp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,15 @@ eval (ECond b e1 e2) st = do
eval (EPrim x) _ = return $ VPrim x

eval (EUniform e) st = do
lbl <- freshLbl
v <- eval e st
case v of
VNil -> error "eval: empty list argument to uniform distribution"
_ -> return $ VDist $ uniform lbl $ EVal <$> vlist_list v
--note(jgs): Don't generate fresh labels in special case of singleton
--lists. This is to address issue #8.
VCons v1 VNil -> return $ VDist $ Leaf (EVal v1)
_ -> do
lbl <- freshLbl
return $ VDist $ uniform lbl $ EVal <$> vlist_list v


-- | Interp. Commands are interpreted as functions from trees of
Expand Down Expand Up @@ -266,7 +270,7 @@ interp (While e c) t =
b' <- is_true e st'
return $ if b' then Hole fresh_lbl else Leaf st'
return $ set_label fresh_lbl t''
else
else
return $ Leaf st
else
-- Nothing in e depends on randomness so unfold the loop.
Expand Down

0 comments on commit f32989d

Please sign in to comment.