mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-22 11:19:32 -06:00
added function treeProbability in the Haskell binding
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
#include "pgf.h"
|
||||
#include "data.h"
|
||||
#include <gu/assert.h>
|
||||
#include <gu/utf8.h>
|
||||
#include <gu/seq.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <math.h>
|
||||
|
||||
static PgfExpr
|
||||
pgf_expr_unwrap(PgfExpr expr)
|
||||
@@ -1500,3 +1501,27 @@ pgf_type_eq(PgfType* t1, PgfType* t2)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
PGF_API prob_t
|
||||
pgf_compute_tree_probability(PgfPGF *gr, PgfExpr expr)
|
||||
{
|
||||
GuVariantInfo ei = gu_variant_open(expr);
|
||||
switch (ei.tag) {
|
||||
case PGF_EXPR_APP: {
|
||||
PgfExprApp* app = ei.data;
|
||||
return pgf_compute_tree_probability(gr, app->fun) +
|
||||
pgf_compute_tree_probability(gr, app->arg);
|
||||
}
|
||||
case PGF_EXPR_FUN: {
|
||||
PgfExprFun* fun = ei.data;
|
||||
PgfAbsFun* absfun =
|
||||
gu_seq_binsearch(gr->abstract.funs, pgf_absfun_order, PgfAbsFun, fun->fun);
|
||||
if (absfun == NULL)
|
||||
return INFINITY;
|
||||
else
|
||||
return absfun->ep.prob;
|
||||
}
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,4 +226,7 @@ PGF_API_DECL void
|
||||
pgf_print_expr_tuple(size_t n_exprs, PgfExpr exprs[], PgfPrintContext* ctxt,
|
||||
GuOut* out, GuExn* err);
|
||||
|
||||
PGF_API prob_t
|
||||
pgf_compute_tree_probability(PgfPGF *gr, PgfExpr expr);
|
||||
|
||||
#endif /* EXPR_H_ */
|
||||
|
||||
Reference in New Issue
Block a user