[Beginner Question] [dippl] Error in the PCFG example

43 views
Skip to first unread message

Jianlin Li

unread,
Jul 4, 2021, 11:15:31 AM7/4/21
to webppl-dev
Hi folks,

I am a beginner at the topic of PPL. I am reading the DIPPL book, playing around in my browser today, and encountered the following error:

```
All paths explored by Enumerate have probability zero.
Error: All paths explored by Enumerate have probability zero.
at Enumerate.error (http://cdn.webppl.org/webppl-v0.9.9.js:1:1384954)
at Enumerate.exit (http://cdn.webppl.org/webppl-v0.9.9.js:1:1387749)
at Enumerate.factor (http://cdn.webppl.org/webppl-v0.9.9.js:1:1386472)
at env.factor (http://cdn.webppl.org/webppl-v0.9.9.js:1:1356972)
at eval (eval at run (http://cdn.webppl.org/webppl-v0.9.9.js:1:1486411), <anonymous>:348:36)
at f (http://cdn.webppl.org/webppl-v0.9.9.js:1:1540543)
at http://cdn.webppl.org/webppl-v0.9.9.js:1:1485684
at http://cdn.webppl.org/webppl-v0.9.9.js:1:1485684
at http://cdn.webppl.org/webppl-v0.9.9.js:1:1485684
at runner (http://cdn.webppl.org/webppl-v0.9.9.js:1:1485846)
```

when I replace
 
```
var expand = function(symbols) {
  if(symbols.length==0) {
    return []
  } else {
    var f = pcfg(symbols[0])
    return f.concat(expand(symbols.slice(1)))
  }
}
```

with

```
var expand = function(symbols) {
  return reduce(function(s, acc) { return acc.concat(pcfg(s)) }, [], symbols)
}
```

in the [PCFG example](http://dippl.org/chapters/04-factorseq.html)

I was wondering
- How to fix this error, and where should I look into?
- Is it possible to refactor the `expand` function using some higher-order abstractions like `reduce`, `map`, `foldl`, or `flatmap` instead of doing the recursion manually?

Thanks for your help.

Best regards.

null-a

unread,
Jul 5, 2021, 7:09:42 AM7/5/21
to webppl-dev
Hi! I think you just need to switch `acc` and `pcfg(s)` around in your new `expand` function, so:

```
var expand = function(symbols) {
  return reduce(function(s, acc) { return pcfg(s).concat(acc) }, [], symbols)
}
```

Best,
Paul

Jianlin Li

unread,
Jul 5, 2021, 8:31:41 AM7/5/21
to null-a, webppl-dev
Hi Pual,

Thanks, it works. Then I looked up [it’s implementation](https://github.com/probmods/webppl/blob/daabd60361f72eef53f69005751facce34c7f916/src/header.wppl#L233) and realized `reduce` is actually  `foldr`.

Best,
Jianlin

--
You received this message because you are subscribed to a topic in the Google Groups "webppl-dev" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webppl-dev/wvZrMOMzXcA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webppl-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/webppl-dev/eb46a669-55c8-44f5-987f-f6019c74f8d6n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages