BayesianLogic / blog

The BLOG programming language
http://bayesianlogic.github.io/
BSD 4-Clause "Original" or "Old" License
98 stars 31 forks source link

comprehension_expr for Categorical distributions #329

Open cronburg opened 9 years ago

cronburg commented 9 years ago

I would be interested in seeing a list-comprehension-like syntax for categorical distributions. For instance:

Categorical({c -> foo(c) for Card c : bar(c) < limit})

would create a distribution over cards c satisfying bar(c) < limit with probabilities foo(c).

The only other way I can think of to do this right now is with meta-programming in a general purpose language to generate the exhaustive list of categorical values and probabilities in the existing BLOG syntax. Is there an alternative I'm missing?

I tried adding a rule like expression_pair:ep FOR type_var_lst:vars opt_colon_expr:condexpr to the comprehension_expr grammar rule, where:

expression_pair ::=
    expression:e1 RIGHTARROW expression:e2
    {: RESULT = new ExprTupleList(e1, e2, null); :}
  ;

but I couldn't get the compile & stage to notice my changes - I've never used sbt or jflex before, and don't really have time anytime soon to figure it out myself.

lileicc commented 9 years ago

Very good suggestion!

It is not list-comprehension, but a dictionary-comprehension.

In blog, the list-comprehension is planned but not supported yet. Dictionary-comprehension is not planned yet.

If you plan to work on this, we would very welcome your contribute. Here are a few things you need to do.

  1. modify the parser, blog.cup file (you probably do not need to modify jflex)
  2. add/modify abstract syntax tree if needed.
  3. add appropriate semantic processing in blog.semant.Semant.java
  4. add/modify blog.model.MapSpec.java

Alternatively, you donot really need dictionary-comprehension if your type is in closed domain (i.e. no number statement). You may use Discrete combined with toInt etc.

Lei

On Tue, Nov 4, 2014 at 10:34 PM, Karl Cronburg notifications@github.com wrote:

I would be interested in seeing a list-comprehension-like syntax for categorical distributions. For instance:

Categorical({c -> foo(c) for Card c : bar(c) < limit})

would create a distribution over cards c satisfying bar(c) < limit with probabilities foo(c).

The only other way I can think of to do this right now is with meta-programming in a general purpose language to generate the exhaustive list of categorical values and probabilities in the existing BLOG syntax. Is there an alternative I'm missing?

I tried adding a rule like expression_pair:ep FOR type_var_lst:vars opt_colon_expr:condexpr to the comprehension_expr grammar rule, where:

expression_pair ::= expression:e1 RIGHTARROW expression:e2 {: RESULT = new ExprTupleList(e1, e2, null); :} ;

but I couldn't get the compile & stage to notice my changes - I've never used sbt or jflex before, and don't really have time anytime soon to figure it out myself.

— Reply to this email directly or view it on GitHub https://github.com/BayesianLogic/blog/issues/329.

cronburg commented 9 years ago

Thanks for the info!

The example I had in mind is definitely closed domain - foo(c) returns a fixed Integer in my example, so I'll look into Discrete first.