mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
merge the states for reasoning and evaluation into a single structure
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
#include "pgf/pgf.h"
|
||||
#include "pgf/data.h"
|
||||
#include "pgf/evaluator.h"
|
||||
#include "pgf/reasoner.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#define PGF_ARGS_DELTA 5
|
||||
|
||||
static inline PgfClosure*
|
||||
pgf_mk_pap(PgfEvalState* state, PgfClosure* fun,
|
||||
pgf_mk_pap(PgfReasoner* rs, PgfClosure* fun,
|
||||
size_t n_args, PgfClosure** args)
|
||||
{
|
||||
if (n_args > 0) {
|
||||
PgfValuePAP* val = gu_new_flex(state->pool, PgfValuePAP, args, n_args);
|
||||
val->header.code = state->eval_gates->evaluate_value_pap;
|
||||
PgfValuePAP* val = gu_new_flex(rs->pool, PgfValuePAP, args, n_args);
|
||||
val->header.code = rs->eval_gates->evaluate_value_pap;
|
||||
val->fun = fun;
|
||||
val->n_args = n_args*sizeof(PgfClosure*);
|
||||
for (size_t i = 0; i < n_args; i++) {
|
||||
@@ -23,7 +23,7 @@ pgf_mk_pap(PgfEvalState* state, PgfClosure* fun,
|
||||
}
|
||||
|
||||
PgfClosure*
|
||||
pgf_evaluate_expr_thunk(PgfEvalState* state, PgfExprThunk* thunk)
|
||||
pgf_evaluate_expr_thunk(PgfReasoner* rs, PgfExprThunk* thunk)
|
||||
{
|
||||
PgfEnv* env = thunk->env;
|
||||
PgfExpr expr = thunk->expr;
|
||||
@@ -39,7 +39,7 @@ repeat:;
|
||||
PgfExprAbs* eabs = ei.data;
|
||||
|
||||
if (n_args > 0) {
|
||||
PgfEnv* new_env = gu_new(PgfEnv, state->pool);
|
||||
PgfEnv* new_env = gu_new(PgfEnv, rs->pool);
|
||||
new_env->next = env;
|
||||
new_env->closure = args[--n_args];
|
||||
|
||||
@@ -47,7 +47,7 @@ repeat:;
|
||||
expr = eabs->body;
|
||||
goto repeat;
|
||||
} else {
|
||||
thunk->header.code = state->eval_gates->evaluate_value_lambda;
|
||||
thunk->header.code = rs->eval_gates->evaluate_value_lambda;
|
||||
thunk->expr = eabs->body;
|
||||
res = &thunk->header;
|
||||
}
|
||||
@@ -56,8 +56,8 @@ repeat:;
|
||||
case PGF_EXPR_APP: {
|
||||
PgfExprApp* eapp = ei.data;
|
||||
PgfExprThunk* thunk =
|
||||
gu_new(PgfExprThunk, state->pool);
|
||||
thunk->header.code = state->eval_gates->evaluate_expr_thunk;
|
||||
gu_new(PgfExprThunk, rs->pool);
|
||||
thunk->header.code = rs->eval_gates->evaluate_expr_thunk;
|
||||
thunk->env = env;
|
||||
thunk->expr = eapp->arg;
|
||||
|
||||
@@ -72,7 +72,7 @@ repeat:;
|
||||
case PGF_EXPR_LIT: {
|
||||
PgfExprLit* elit = ei.data;
|
||||
PgfValueLit* val = (PgfValueLit*) thunk;
|
||||
val->header.code = state->eval_gates->evaluate_value_lit;
|
||||
val->header.code = rs->eval_gates->evaluate_value_lit;
|
||||
val->lit = elit->lit;
|
||||
res = &val->header;
|
||||
break;
|
||||
@@ -81,28 +81,28 @@ repeat:;
|
||||
PgfExprMeta* emeta = ei.data;
|
||||
|
||||
PgfValueMeta* val =
|
||||
gu_new(PgfValueMeta, state->pool);
|
||||
val->header.code = state->eval_gates->evaluate_meta;
|
||||
gu_new(PgfValueMeta, rs->pool);
|
||||
val->header.code = rs->eval_gates->evaluate_meta;
|
||||
val->env = env;
|
||||
val->id = emeta->id;
|
||||
res = pgf_mk_pap(state, &val->header, n_args, args);
|
||||
res = pgf_mk_pap(rs, &val->header, n_args, args);
|
||||
break;
|
||||
}
|
||||
case PGF_EXPR_FUN: {
|
||||
PgfExprFun* efun = ei.data;
|
||||
|
||||
PgfAbsFun* absfun =
|
||||
gu_seq_binsearch(state->pgf->abstract.funs, pgf_absfun_order, PgfAbsFun, efun->fun);
|
||||
gu_seq_binsearch(rs->abstract->funs, pgf_absfun_order, PgfAbsFun, efun->fun);
|
||||
gu_assert(absfun != NULL);
|
||||
|
||||
if (absfun->closure.code != NULL) {
|
||||
res = pgf_mk_pap(state, (PgfClosure*) &absfun->closure, n_args, args);
|
||||
res = pgf_mk_pap(rs, (PgfClosure*) &absfun->closure, n_args, args);
|
||||
} else {
|
||||
size_t arity = absfun->arity;
|
||||
|
||||
if (n_args == arity) {
|
||||
PgfValue* val = gu_new_flex(state->pool, PgfValue, args, arity);
|
||||
val->header.code = state->eval_gates->evaluate_value;
|
||||
PgfValue* val = gu_new_flex(rs->pool, PgfValue, args, arity);
|
||||
val->header.code = rs->eval_gates->evaluate_value;
|
||||
val->con = (PgfClosure*) &absfun->closure;
|
||||
|
||||
for (size_t i = 0; i < arity; i++) {
|
||||
@@ -112,10 +112,10 @@ repeat:;
|
||||
} else {
|
||||
gu_assert(n_args < arity);
|
||||
|
||||
PgfExprThunk* lambda = gu_new(PgfExprThunk, state->pool);
|
||||
lambda->header.code = state->eval_gates->evaluate_value_lambda;
|
||||
PgfExprThunk* lambda = gu_new(PgfExprThunk, rs->pool);
|
||||
lambda->header.code = rs->eval_gates->evaluate_value_lambda;
|
||||
lambda->env = NULL;
|
||||
res = pgf_mk_pap(state, &lambda->header, n_args, args);
|
||||
res = pgf_mk_pap(rs, &lambda->header, n_args, args);
|
||||
|
||||
for (size_t i = 0; i < arity; i++) {
|
||||
PgfExpr new_expr, arg;
|
||||
@@ -123,13 +123,13 @@ repeat:;
|
||||
PgfExprVar *evar =
|
||||
gu_new_variant(PGF_EXPR_VAR,
|
||||
PgfExprVar,
|
||||
&arg, state->pool);
|
||||
&arg, rs->pool);
|
||||
evar->var = arity-i-1;
|
||||
|
||||
PgfExprApp *eapp =
|
||||
gu_new_variant(PGF_EXPR_APP,
|
||||
PgfExprApp,
|
||||
&new_expr, state->pool);
|
||||
&new_expr, rs->pool);
|
||||
eapp->fun = expr;
|
||||
eapp->arg = arg;
|
||||
|
||||
@@ -142,7 +142,7 @@ repeat:;
|
||||
PgfExprAbs *eabs =
|
||||
gu_new_variant(PGF_EXPR_ABS,
|
||||
PgfExprAbs,
|
||||
&new_expr, state->pool);
|
||||
&new_expr, rs->pool);
|
||||
eabs->bind_type = PGF_BIND_TYPE_EXPLICIT;
|
||||
eabs->id = "_";
|
||||
eabs->body = expr;
|
||||
@@ -162,7 +162,7 @@ repeat:;
|
||||
while (i > 0) {
|
||||
tmp_env = tmp_env->next;
|
||||
if (tmp_env == NULL) {
|
||||
GuExnData* err_data = gu_raise(state->err, PgfExn);
|
||||
GuExnData* err_data = gu_raise(rs->err, PgfExn);
|
||||
if (err_data) {
|
||||
err_data->data = "invalid de Bruijn index";
|
||||
}
|
||||
@@ -171,7 +171,7 @@ repeat:;
|
||||
i--;
|
||||
}
|
||||
|
||||
res = pgf_mk_pap(state, tmp_env->closure, n_args, args);
|
||||
res = pgf_mk_pap(rs, tmp_env->closure, n_args, args);
|
||||
break;
|
||||
}
|
||||
case PGF_EXPR_TYPED: {
|
||||
@@ -193,24 +193,24 @@ repeat:;
|
||||
}
|
||||
|
||||
PgfClosure*
|
||||
pgf_evaluate_lambda_application(PgfEvalState* state, PgfExprThunk* lambda,
|
||||
PgfClosure* arg)
|
||||
pgf_evaluate_lambda_application(PgfReasoner* rs, PgfExprThunk* lambda,
|
||||
PgfClosure* arg)
|
||||
{
|
||||
PgfEnv* new_env = gu_new(PgfEnv, state->pool);
|
||||
PgfEnv* new_env = gu_new(PgfEnv, rs->pool);
|
||||
new_env->next = lambda->env;
|
||||
new_env->closure = arg;
|
||||
|
||||
PgfExprThunk* thunk = gu_new(PgfExprThunk, state->pool);
|
||||
thunk->header.code = state->eval_gates->evaluate_expr_thunk;
|
||||
PgfExprThunk* thunk = gu_new(PgfExprThunk, rs->pool);
|
||||
thunk->header.code = rs->eval_gates->evaluate_expr_thunk;
|
||||
thunk->env = new_env;
|
||||
thunk->expr = lambda->expr;
|
||||
return pgf_evaluate_expr_thunk(state, thunk);
|
||||
return pgf_evaluate_expr_thunk(rs, thunk);
|
||||
}
|
||||
|
||||
static PgfExpr
|
||||
pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
||||
pgf_value2expr(PgfReasoner* rs, int level, PgfClosure* clos)
|
||||
{
|
||||
clos = state->eval_gates->enter(state, clos);
|
||||
clos = rs->eval_gates->enter(rs, clos);
|
||||
if (clos == NULL)
|
||||
return gu_null_variant;
|
||||
|
||||
@@ -218,20 +218,20 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
||||
size_t n_args = 0;
|
||||
PgfClosure** args;
|
||||
|
||||
if (clos->code == state->eval_gates->evaluate_value) {
|
||||
if (clos->code == rs->eval_gates->evaluate_value) {
|
||||
PgfValue* val = (PgfValue*) clos;
|
||||
PgfAbsFun* absfun = gu_container(val->con, PgfAbsFun, closure);
|
||||
|
||||
expr = absfun->ep.expr;
|
||||
n_args = absfun->arity;
|
||||
args = val->args;
|
||||
} else if (clos->code == state->eval_gates->evaluate_value_lit) {
|
||||
} else if (clos->code == rs->eval_gates->evaluate_value_lit) {
|
||||
PgfValueLit* val = (PgfValueLit*) clos;
|
||||
|
||||
PgfExprLit *elit =
|
||||
gu_new_variant(PGF_EXPR_LIT,
|
||||
PgfExprLit,
|
||||
&expr, pool);
|
||||
&expr, rs->out_pool);
|
||||
|
||||
GuVariantInfo i = gu_variant_open(val->lit);
|
||||
switch (i.tag) {
|
||||
@@ -242,7 +242,7 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
||||
gu_new_flex_variant(PGF_LITERAL_STR,
|
||||
PgfLiteralStr,
|
||||
val, strlen(lstr->val)+1,
|
||||
&elit->lit, pool);
|
||||
&elit->lit, rs->out_pool);
|
||||
strcpy(new_lstr->val, lstr->val);
|
||||
break;
|
||||
}
|
||||
@@ -252,7 +252,7 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
||||
PgfLiteralInt* new_lint =
|
||||
gu_new_variant(PGF_LITERAL_INT,
|
||||
PgfLiteralInt,
|
||||
&elit->lit, pool);
|
||||
&elit->lit, rs->out_pool);
|
||||
new_lint->val = lint->val;
|
||||
break;
|
||||
}
|
||||
@@ -262,24 +262,24 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
||||
PgfLiteralFlt* new_lflt =
|
||||
gu_new_variant(PGF_LITERAL_FLT,
|
||||
PgfLiteralFlt,
|
||||
&elit->lit, pool);
|
||||
&elit->lit, rs->out_pool);
|
||||
new_lflt->val = lflt->val;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
gu_impossible();
|
||||
}
|
||||
} else if (clos->code == state->eval_gates->evaluate_value_pap) {
|
||||
} else if (clos->code == rs->eval_gates->evaluate_value_pap) {
|
||||
PgfValuePAP *pap = (PgfValuePAP*) clos;
|
||||
|
||||
PgfValueGen* gen =
|
||||
gu_new(PgfValueGen, state->pool);
|
||||
gen->header.code = state->eval_gates->evaluate_gen;
|
||||
gu_new(PgfValueGen, rs->pool);
|
||||
gen->header.code = rs->eval_gates->evaluate_gen;
|
||||
gen->level = level;
|
||||
|
||||
size_t n_args = pap->n_args/sizeof(PgfClosure*);
|
||||
PgfValuePAP* new_pap = gu_new_flex(state->pool, PgfValuePAP, args, n_args+1);
|
||||
new_pap->header.code = state->eval_gates->evaluate_value_pap;
|
||||
PgfValuePAP* new_pap = gu_new_flex(rs->pool, PgfValuePAP, args, n_args+1);
|
||||
new_pap->header.code = rs->eval_gates->evaluate_value_pap;
|
||||
new_pap->fun = pap->fun;
|
||||
new_pap->n_args = pap->n_args+sizeof(PgfClosure*);
|
||||
new_pap->args[0] = &gen->header;
|
||||
@@ -290,30 +290,30 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
||||
PgfExprAbs *eabs =
|
||||
gu_new_variant(PGF_EXPR_ABS,
|
||||
PgfExprAbs,
|
||||
&expr, pool);
|
||||
&expr, rs->out_pool);
|
||||
eabs->bind_type = PGF_BIND_TYPE_EXPLICIT;
|
||||
eabs->id = gu_format_string(pool, "v%d", level);
|
||||
eabs->body = pgf_value2expr(state, level+1, &new_pap->header, pool);
|
||||
} else if (clos->code == state->eval_gates->evaluate_value_const) {
|
||||
eabs->id = gu_format_string(rs->out_pool, "v%d", level);
|
||||
eabs->body = pgf_value2expr(rs, level+1, &new_pap->header);
|
||||
} else if (clos->code == rs->eval_gates->evaluate_value_const) {
|
||||
PgfValuePAP* val = (PgfValuePAP*) clos;
|
||||
|
||||
if (val->fun->code == state->eval_gates->evaluate_meta) {
|
||||
if (val->fun->code == rs->eval_gates->evaluate_meta) {
|
||||
PgfValueMeta* fun = (PgfValueMeta*) val->fun;
|
||||
|
||||
PgfExprMeta *emeta =
|
||||
gu_new_variant(PGF_EXPR_META,
|
||||
PgfExprMeta,
|
||||
&expr, pool);
|
||||
&expr, rs->out_pool);
|
||||
emeta->id = fun->id;
|
||||
} else if (val->fun->code == state->eval_gates->evaluate_gen) {
|
||||
} else if (val->fun->code == rs->eval_gates->evaluate_gen) {
|
||||
PgfValueGen* fun = (PgfValueGen*) val->fun;
|
||||
|
||||
PgfExprVar *evar =
|
||||
gu_new_variant(PGF_EXPR_VAR,
|
||||
PgfExprVar,
|
||||
&expr, pool);
|
||||
&expr, rs->out_pool);
|
||||
evar->var = level - fun->level - 1;
|
||||
} else if (val->fun->code == state->eval_gates->evaluate_sum) {
|
||||
} else if (val->fun->code == rs->eval_gates->evaluate_sum) {
|
||||
PgfValueSum* sum = (PgfValueSum*) val->fun;
|
||||
|
||||
PgfExpr e1,e2;
|
||||
@@ -321,19 +321,19 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
||||
gu_new_flex_variant(PGF_EXPR_FUN,
|
||||
PgfExprFun,
|
||||
fun, 2,
|
||||
&e1, pool);
|
||||
&e1, rs->out_pool);
|
||||
strcpy(efun->fun, "+");
|
||||
|
||||
PgfExprLit *elit =
|
||||
gu_new_variant(PGF_EXPR_LIT,
|
||||
PgfExprLit,
|
||||
&e2, pool);
|
||||
&e2, rs->out_pool);
|
||||
elit->lit = sum->lit;
|
||||
|
||||
PgfExprApp* eapp =
|
||||
gu_new_variant(PGF_EXPR_APP,
|
||||
PgfExprApp,
|
||||
&expr, pool);
|
||||
&expr, rs->out_pool);
|
||||
eapp->fun = e1;
|
||||
eapp->arg = e2;
|
||||
|
||||
@@ -344,14 +344,14 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
||||
|
||||
PgfExpr fun = expr;
|
||||
PgfExpr arg =
|
||||
pgf_value2expr(state, level, con, pool);
|
||||
pgf_value2expr(rs, level, con);
|
||||
if (gu_variant_is_null(arg))
|
||||
return gu_null_variant;
|
||||
|
||||
PgfExprApp* e =
|
||||
gu_new_variant(PGF_EXPR_APP,
|
||||
PgfExprApp,
|
||||
&expr, pool);
|
||||
&expr, rs->out_pool);
|
||||
e->fun = fun;
|
||||
e->arg = arg;
|
||||
}
|
||||
@@ -369,14 +369,14 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
||||
for (size_t i = 0; i < n_args; i++) {
|
||||
PgfExpr fun = expr;
|
||||
PgfExpr arg =
|
||||
pgf_value2expr(state, level, args[i], pool);
|
||||
pgf_value2expr(rs, level, args[i]);
|
||||
if (gu_variant_is_null(arg))
|
||||
return gu_null_variant;
|
||||
|
||||
PgfExprApp* e =
|
||||
gu_new_variant(PGF_EXPR_APP,
|
||||
PgfExprApp,
|
||||
&expr, pool);
|
||||
&expr, rs->out_pool);
|
||||
e->fun = fun;
|
||||
e->arg = arg;
|
||||
}
|
||||
@@ -387,47 +387,33 @@ pgf_value2expr(PgfEvalState* state, int level, PgfClosure* clos, GuPool* pool)
|
||||
PgfExpr
|
||||
pgf_compute(PgfPGF* pgf, PgfExpr expr, GuExn* err, GuPool* pool, GuPool* out_pool)
|
||||
{
|
||||
size_t n_cafs =
|
||||
(pgf->abstract.eval_gates->cafs == NULL)
|
||||
? 0 : gu_seq_length(pgf->abstract.eval_gates->cafs);
|
||||
|
||||
PgfEvalState* state =
|
||||
gu_new_flex(pool, PgfEvalState, cafs, n_cafs);
|
||||
state->pgf = pgf;
|
||||
state->eval_gates = pgf->abstract.eval_gates;
|
||||
state->pool = pool;
|
||||
state->err = err;
|
||||
|
||||
PgfFunction* cafs = gu_seq_data(state->eval_gates->cafs);
|
||||
for (size_t i = 0; i < n_cafs; i++) {
|
||||
state->cafs[i].header.code = cafs[i];
|
||||
state->cafs[i].val = NULL;
|
||||
}
|
||||
PgfReasoner* rs =
|
||||
pgf_new_reasoner(pgf, err, pool, out_pool);
|
||||
|
||||
PgfExprThunk* thunk =
|
||||
gu_new(PgfExprThunk, pool);
|
||||
thunk->header.code = state->eval_gates->evaluate_expr_thunk;
|
||||
thunk->header.code = rs->eval_gates->evaluate_expr_thunk;
|
||||
thunk->env = NULL;
|
||||
thunk->expr = expr;
|
||||
|
||||
return pgf_value2expr(state, 0, &thunk->header, out_pool);
|
||||
return pgf_value2expr(rs, 0, &thunk->header);
|
||||
}
|
||||
|
||||
void
|
||||
pgf_evaluate_accum_init_int(PgfEvalState* state,
|
||||
pgf_evaluate_accum_init_int(PgfReasoner* rs,
|
||||
PgfEvalAccum* accum, int val)
|
||||
{
|
||||
PgfLiteralInt *lit_int =
|
||||
gu_new_variant(PGF_LITERAL_INT,
|
||||
PgfLiteralInt,
|
||||
&accum->lit,
|
||||
state->pool);
|
||||
rs->pool);
|
||||
lit_int->val = val;
|
||||
accum->consts = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
pgf_evaluate_accum_init_str(PgfEvalState* state,
|
||||
pgf_evaluate_accum_init_str(PgfReasoner* rs,
|
||||
PgfEvalAccum* accum, GuString val)
|
||||
{
|
||||
if (val == NULL)
|
||||
@@ -437,23 +423,23 @@ pgf_evaluate_accum_init_str(PgfEvalState* state,
|
||||
gu_new_flex_variant(PGF_LITERAL_STR,
|
||||
PgfLiteralStr,
|
||||
val, strlen(val)+1,
|
||||
&accum->lit, state->pool);
|
||||
&accum->lit, rs->pool);
|
||||
strcpy((char*) lit_str->val, (char*) val);
|
||||
accum->consts = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
pgf_evaluate_accum_init_flt(PgfEvalState* state,
|
||||
pgf_evaluate_accum_init_flt(PgfReasoner* rs,
|
||||
PgfEvalAccum* accum, float val)
|
||||
{
|
||||
PgfLiteralFlt *lit_flt =
|
||||
gu_new_variant(PGF_LITERAL_FLT,
|
||||
PgfLiteralFlt,
|
||||
&accum->lit,
|
||||
state->pool);
|
||||
rs->pool);
|
||||
lit_flt->val = val;
|
||||
accum->enter_stack_ptr = state->enter_stack_ptr;
|
||||
state->enter_stack_ptr = ((void*)accum)-sizeof(void*)*2;
|
||||
accum->enter_stack_ptr = rs->enter_stack_ptr;
|
||||
rs->enter_stack_ptr = ((void*)accum)-sizeof(void*)*2;
|
||||
accum->consts = NULL;
|
||||
}
|
||||
|
||||
@@ -480,19 +466,19 @@ pgf_evaluate_accum_add_helper(PgfEvalAccum* accum, PgfLiteral lit)
|
||||
}
|
||||
|
||||
void
|
||||
pgf_evaluate_accum_add(PgfEvalState* state,
|
||||
pgf_evaluate_accum_add(PgfReasoner* rs,
|
||||
PgfEvalAccum* accum, PgfClosure* closure)
|
||||
{
|
||||
if (closure->code == state->eval_gates->evaluate_value_lit) {
|
||||
if (closure->code == rs->eval_gates->evaluate_value_lit) {
|
||||
PgfValueLit* val = (PgfValueLit*) closure;
|
||||
pgf_evaluate_accum_add_helper(accum, val->lit);
|
||||
} else if (closure->code == state->eval_gates->evaluate_value_const) {
|
||||
} else if (closure->code == rs->eval_gates->evaluate_value_const) {
|
||||
if (accum->consts == NULL)
|
||||
accum->consts = gu_new_buf(PgfClosure*, state->pool);
|
||||
accum->consts = gu_new_buf(PgfClosure*, rs->pool);
|
||||
|
||||
PgfValuePAP* pap = (PgfValuePAP*) closure;
|
||||
|
||||
if (pap->fun->code == state->eval_gates->evaluate_sum) {
|
||||
if (pap->fun->code == rs->eval_gates->evaluate_sum) {
|
||||
PgfValueSum* val = (PgfValueSum*) ((PgfValuePAP*) closure)->fun;
|
||||
pgf_evaluate_accum_add_helper(accum, val->lit);
|
||||
|
||||
@@ -510,18 +496,18 @@ pgf_evaluate_accum_add(PgfEvalState* state,
|
||||
}
|
||||
|
||||
PgfClosure*
|
||||
pgf_evaluate_accum_done(PgfEvalState* state, PgfEvalAccum* accum)
|
||||
pgf_evaluate_accum_done(PgfReasoner* rs, PgfEvalAccum* accum)
|
||||
{
|
||||
state->enter_stack_ptr = accum->enter_stack_ptr;
|
||||
rs->enter_stack_ptr = accum->enter_stack_ptr;
|
||||
|
||||
if (accum->consts == NULL) {
|
||||
PgfValueLit* val = gu_new(PgfValueLit, state->pool);
|
||||
val->header.code = state->eval_gates->evaluate_value_lit;
|
||||
PgfValueLit* val = gu_new(PgfValueLit, rs->pool);
|
||||
val->header.code = rs->eval_gates->evaluate_value_lit;
|
||||
val->lit = accum->lit;
|
||||
return &val->header;
|
||||
} else {
|
||||
PgfValueSum* val = gu_new(PgfValueSum, state->pool);
|
||||
val->header.code = state->eval_gates->evaluate_sum;
|
||||
PgfValueSum* val = gu_new(PgfValueSum, rs->pool);
|
||||
val->header.code = rs->eval_gates->evaluate_sum;
|
||||
val->lit = accum->lit;
|
||||
val->consts = accum->consts;
|
||||
return &val->header;
|
||||
|
||||
@@ -1,127 +0,0 @@
|
||||
#ifndef PGF_EVALUATOR_H_
|
||||
#define PGF_EVALUATOR_H_
|
||||
|
||||
typedef struct {
|
||||
PgfFunction code;
|
||||
} PgfClosure;
|
||||
|
||||
typedef struct {
|
||||
PgfClosure header;
|
||||
PgfClosure* val;
|
||||
} PgfIndirection;
|
||||
|
||||
typedef struct {
|
||||
PgfPGF* pgf;
|
||||
PgfEvalGates* eval_gates; // cached from pgf->abstr->eval_gates
|
||||
GuPool* pool;
|
||||
GuExn* err;
|
||||
void* enter_stack_ptr;
|
||||
void* tmp; // for temporary register spills
|
||||
PgfIndirection cafs[]; // derived from gu_seq_data(pgf->abstr->eval_gates->cafs)
|
||||
} PgfEvalState;
|
||||
|
||||
typedef struct {
|
||||
PgfLiteral lit;
|
||||
GuBuf* consts;
|
||||
void* enter_stack_ptr;
|
||||
} PgfEvalAccum;
|
||||
|
||||
typedef struct PgfEnv PgfEnv;
|
||||
|
||||
struct PgfEnv {
|
||||
PgfEnv* next;
|
||||
PgfClosure* closure;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
PgfClosure header;
|
||||
PgfEnv* env;
|
||||
PgfExpr expr;
|
||||
} PgfExprThunk;
|
||||
|
||||
typedef struct {
|
||||
PgfClosure header;
|
||||
PgfClosure* con;
|
||||
PgfClosure* args[];
|
||||
} PgfValue;
|
||||
|
||||
typedef struct {
|
||||
PgfClosure header;
|
||||
int level;
|
||||
} PgfValueGen;
|
||||
|
||||
typedef struct {
|
||||
PgfClosure header;
|
||||
PgfEnv* env;
|
||||
PgfMetaId id;
|
||||
} PgfValueMeta;
|
||||
|
||||
typedef struct {
|
||||
PgfClosure header;
|
||||
PgfLiteral lit;
|
||||
} PgfValueLit;
|
||||
|
||||
typedef struct {
|
||||
PgfClosure header;
|
||||
PgfLiteral lit;
|
||||
GuBuf* consts;
|
||||
} PgfValueSum;
|
||||
|
||||
typedef struct {
|
||||
PgfClosure header;
|
||||
PgfClosure* fun;
|
||||
size_t n_args;
|
||||
PgfClosure* args[];
|
||||
} PgfValuePAP;
|
||||
|
||||
struct PgfEvalGates {
|
||||
PgfFunction evaluate_expr_thunk;
|
||||
PgfFunction evaluate_indirection;
|
||||
PgfFunction evaluate_value;
|
||||
PgfFunction evaluate_value_lit;
|
||||
PgfFunction evaluate_value_pap;
|
||||
PgfFunction evaluate_value_lambda;
|
||||
PgfFunction evaluate_value_const;
|
||||
PgfFunction evaluate_meta;
|
||||
PgfFunction evaluate_gen;
|
||||
PgfFunction evaluate_sum;
|
||||
PgfFunction evaluate_caf;
|
||||
|
||||
PgfFunction update_closure;
|
||||
PgfFunction update_pap;
|
||||
|
||||
PgfFunction mk_const;
|
||||
|
||||
PgfClosure* (*enter)(PgfEvalState* state, PgfClosure* closure);
|
||||
|
||||
GuFinalizer fin;
|
||||
GuSeq* cafs;
|
||||
};
|
||||
|
||||
PgfClosure*
|
||||
pgf_evaluate_expr_thunk(PgfEvalState* state, PgfExprThunk* thunk);
|
||||
|
||||
PgfClosure*
|
||||
pgf_evaluate_lambda_application(PgfEvalState* state, PgfExprThunk* lambda,
|
||||
PgfClosure* arg);
|
||||
|
||||
void
|
||||
pgf_evaluate_accum_init_int(PgfEvalState* state,
|
||||
PgfEvalAccum* accum, int val);
|
||||
|
||||
void
|
||||
pgf_evaluate_accum_init_str(PgfEvalState* state,
|
||||
PgfEvalAccum* accum, GuString val);
|
||||
|
||||
void
|
||||
pgf_evaluate_accum_init_flt(PgfEvalState* state,
|
||||
PgfEvalAccum* accum, float val);
|
||||
|
||||
void
|
||||
pgf_evaluate_accum_add(PgfEvalState* state,
|
||||
PgfEvalAccum* accum, PgfClosure* closure);
|
||||
|
||||
PgfClosure*
|
||||
pgf_evaluate_accum_done(PgfEvalState* state, PgfEvalAccum* accum);
|
||||
|
||||
#endif
|
||||
@@ -1,10 +1,10 @@
|
||||
#include "data.h"
|
||||
#include "evaluator.h"
|
||||
#include "reasoner.h"
|
||||
#include "hopu.h"
|
||||
|
||||
void pgf_pattern_unify(PgfEvalState* state, PgfClosure* c1, PgfClosure* c2)
|
||||
void pgf_pattern_unify(PgfReasoner* rs, PgfClosure* c1, PgfClosure* c2)
|
||||
{
|
||||
c1 = state->eval_gates->enter(state, c1);
|
||||
c2 = state->eval_gates->enter(state, c2);
|
||||
c1 = rs->eval_gates->enter(rs, c1);
|
||||
c2 = rs->eval_gates->enter(rs, c2);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef HOPU_H
|
||||
#define HOPU_H
|
||||
|
||||
void pgf_pattern_unify(PgfEvalState* state, PgfClosure* c1, PgfClosure* c2);
|
||||
void pgf_pattern_unify(PgfReasoner* rs, PgfClosure* c1, PgfClosure* c2);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <gu/utf8.h>
|
||||
#include <pgf/data.h>
|
||||
#include <pgf/reasoner.h>
|
||||
#include <pgf/evaluator.h>
|
||||
#include <pgf/reader.h>
|
||||
#include "lightning.h"
|
||||
#ifdef __MINGW32__
|
||||
@@ -387,7 +386,7 @@ pgf_jit_gates(PgfReader* rdr)
|
||||
int closure_arg = jit_arg_p();
|
||||
jit_getarg_p(JIT_VSTATE, es_arg);
|
||||
jit_getarg_p(JIT_VCLOS, closure_arg);
|
||||
jit_stxi_p(offsetof(PgfEvalState, enter_stack_ptr), JIT_VSTATE, JIT_SP);
|
||||
jit_stxi_p(offsetof(PgfReasoner, enter_stack_ptr), JIT_VSTATE, JIT_SP);
|
||||
jit_ldr_p(JIT_R0, JIT_VCLOS);
|
||||
jit_callr(JIT_R0);
|
||||
jit_movr_p(JIT_RET, JIT_VHEAP);
|
||||
@@ -430,7 +429,7 @@ pgf_jit_gates(PgfReader* rdr)
|
||||
jit_prepare(2);
|
||||
jit_addi_i(JIT_R0, JIT_R0, sizeof(PgfValuePAP));
|
||||
jit_pusharg_ui(JIT_R0);
|
||||
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfEvalState,pool));
|
||||
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfReasoner,pool));
|
||||
jit_pusharg_p(JIT_R0);
|
||||
jit_finish(gu_malloc);
|
||||
jit_popr_i(JIT_R1);
|
||||
@@ -531,7 +530,7 @@ pgf_jit_gates(PgfReader* rdr)
|
||||
jit_prepare(2);
|
||||
jit_addi_i(JIT_R0, JIT_R0, sizeof(PgfValuePAP));
|
||||
jit_pusharg_ui(JIT_R0);
|
||||
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfEvalState,pool));
|
||||
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfReasoner,pool));
|
||||
jit_pusharg_p(JIT_R0);
|
||||
jit_finish(gu_malloc);
|
||||
jit_popr_i(JIT_R1);
|
||||
@@ -563,15 +562,15 @@ pgf_jit_gates(PgfReader* rdr)
|
||||
jit_pushr_p(JIT_R0);
|
||||
jit_jmpi(gates->mk_const);
|
||||
jit_patch(ref2);
|
||||
jit_ldxi_p(JIT_R1, JIT_VSTATE, offsetof(PgfEvalState,enter_stack_ptr));
|
||||
jit_ldxi_p(JIT_R1, JIT_VSTATE, offsetof(PgfReasoner,enter_stack_ptr));
|
||||
ref2 = jit_bner_p(jit_forward(), JIT_FP, JIT_R1);
|
||||
jit_stxi_p(offsetof(PgfEvalState,tmp), JIT_VSTATE, JIT_R0);
|
||||
jit_stxi_p(offsetof(PgfReasoner,tmp), JIT_VSTATE, JIT_R0);
|
||||
jit_subr_p(JIT_R0, JIT_FP, JIT_SP);
|
||||
jit_pushr_i(JIT_R0);
|
||||
jit_prepare(2);
|
||||
jit_addi_i(JIT_R0, JIT_R0, sizeof(PgfValuePAP));
|
||||
jit_pusharg_ui(JIT_R0);
|
||||
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfEvalState,pool));
|
||||
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfReasoner,pool));
|
||||
jit_pusharg_p(JIT_R0);
|
||||
jit_finish(gu_malloc);
|
||||
jit_movr_p(JIT_VHEAP, JIT_RET);
|
||||
@@ -588,7 +587,7 @@ pgf_jit_gates(PgfReader* rdr)
|
||||
jit_subi_i(JIT_R1, JIT_R1, sizeof(void*));
|
||||
jit_jmpi(next);
|
||||
jit_patch(ref);
|
||||
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfEvalState,tmp));
|
||||
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfReasoner,tmp));
|
||||
jit_jmpr(JIT_R0);
|
||||
jit_patch(ref2);
|
||||
jit_ldxi_p(JIT_VCLOS, JIT_FP, sizeof(void*));
|
||||
@@ -662,7 +661,7 @@ pgf_jit_function(PgfReader* rdr, PgfAbstr* abstr,
|
||||
absfun->closure.code = abstr->eval_gates->evaluate_caf;
|
||||
size_t caf_id = rdr->jit_state->n_cafs++;
|
||||
absfun->closure.caf_offset =
|
||||
offsetof(PgfEvalState,cafs)+
|
||||
offsetof(PgfReasoner,cafs)+
|
||||
caf_id*sizeof(PgfIndirection);
|
||||
gu_seq_set(abstr->eval_gates->cafs,
|
||||
PgfFunction,
|
||||
@@ -807,7 +806,7 @@ pgf_jit_function(PgfReader* rdr, PgfAbstr* abstr,
|
||||
jit_prepare(2);
|
||||
jit_movi_ui(JIT_R0, size*sizeof(void*));
|
||||
jit_pusharg_ui(JIT_R0);
|
||||
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfEvalState,pool));
|
||||
jit_ldxi_p(JIT_R0, JIT_VSTATE, offsetof(PgfReasoner,pool));
|
||||
jit_pusharg_p(JIT_R0);
|
||||
jit_finish(gu_malloc);
|
||||
jit_retval_p(JIT_VHEAP);
|
||||
|
||||
@@ -90,16 +90,16 @@ pgf_parseval(PgfConcr* concr, PgfExpr expr, PgfCId cat,
|
||||
double *precision, double *recall, double *exact);
|
||||
|
||||
PgfExpr
|
||||
pgf_compute(PgfPGF* pgf, PgfExpr expr, GuExn* err,
|
||||
pgf_compute(PgfPGF* pgf, PgfExpr expr, GuExn* err,
|
||||
GuPool* pool, GuPool* out_pool);
|
||||
|
||||
PgfExprEnum*
|
||||
pgf_generate_all(PgfPGF* pgf, PgfCId cat, GuPool* pool);
|
||||
pgf_generate_all(PgfPGF* pgf, PgfCId cat,
|
||||
GuExn* err, GuPool* pool, GuPool* out_pool);
|
||||
|
||||
PgfExprEnum*
|
||||
pgf_parse(PgfConcr* concr, PgfCId cat, GuString sentence,
|
||||
GuExn* err,
|
||||
GuPool* pool, GuPool* out_pool);
|
||||
GuExn* err, GuPool* pool, GuPool* out_pool);
|
||||
|
||||
typedef struct PgfMorphoCallback PgfMorphoCallback;
|
||||
struct PgfMorphoCallback {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "data.h"
|
||||
#include "expr.h"
|
||||
#include "evaluator.h"
|
||||
#include "reasoner.h"
|
||||
#include "reader.h"
|
||||
|
||||
#include <gu/defs.h>
|
||||
|
||||
@@ -48,7 +48,7 @@ pgf_reader_done(PgfReader* rdr, PgfPGF* pgf);
|
||||
|
||||
|
||||
// JIT specific interface
|
||||
#ifdef PGF_EVALUATOR_H_
|
||||
#ifdef PGF_REASONER_H_
|
||||
|
||||
typedef struct PgfJitState PgfJitState;
|
||||
|
||||
|
||||
@@ -58,18 +58,6 @@ typedef struct {
|
||||
size_t choice;
|
||||
} PgfCombine2State;
|
||||
|
||||
typedef GuStringMap PgfAbswersMap;
|
||||
|
||||
struct PgfReasoner {
|
||||
GuPool* pool;
|
||||
GuPool* tmp_pool;
|
||||
PgfAbstr* abstract;
|
||||
PgfAbswersMap* table;
|
||||
GuBuf* pqueue;
|
||||
GuBuf* exprs;
|
||||
PgfExprEnum en;
|
||||
};
|
||||
|
||||
static int
|
||||
cmp_expr_state(GuOrder* self, const void* a, const void* b)
|
||||
{
|
||||
@@ -136,16 +124,16 @@ pgf_print_expr_state(PgfExprState* st,
|
||||
#endif
|
||||
|
||||
static PgfExprState*
|
||||
pgf_combine1_to_expr(PgfCombine1State* st, GuPool* tmp_pool) {
|
||||
pgf_combine1_to_expr(PgfCombine1State* st, GuPool* pool, GuPool* out_pool) {
|
||||
PgfExprProb* ep =
|
||||
gu_buf_get(st->exprs, PgfExprProb*, st->choice);
|
||||
|
||||
PgfExprState* nst = gu_new(PgfExprState, tmp_pool);
|
||||
PgfExprState* nst = gu_new(PgfExprState, pool);
|
||||
nst->base.continuation = st->parent->base.continuation;
|
||||
nst->base.prob = st->base.prob;
|
||||
nst->answers = st->parent->answers;
|
||||
nst->expr =
|
||||
gu_new_variant_i(tmp_pool, PGF_EXPR_APP,
|
||||
gu_new_variant_i(out_pool, PGF_EXPR_APP,
|
||||
PgfExprApp,
|
||||
.fun = st->parent->expr,
|
||||
.arg = ep->expr);
|
||||
@@ -159,7 +147,7 @@ pgf_combine1_to_expr(PgfCombine1State* st, GuPool* tmp_pool) {
|
||||
}
|
||||
|
||||
static PgfExprState*
|
||||
pgf_combine2_to_expr(PgfCombine2State* st, GuPool* tmp_pool)
|
||||
pgf_combine2_to_expr(PgfCombine2State* st, GuPool* pool, GuPool* out_pool)
|
||||
{
|
||||
PgfExprState* parent =
|
||||
gu_buf_get(st->parents, PgfExprState*, st->choice);
|
||||
@@ -167,12 +155,12 @@ pgf_combine2_to_expr(PgfCombine2State* st, GuPool* tmp_pool)
|
||||
return NULL;
|
||||
|
||||
PgfExprState* nst =
|
||||
gu_new(PgfExprState, tmp_pool);
|
||||
gu_new(PgfExprState, pool);
|
||||
nst->base.continuation = parent->base.continuation;
|
||||
nst->base.prob = st->base.prob;
|
||||
nst->answers = parent->answers;
|
||||
nst->expr =
|
||||
gu_new_variant_i(tmp_pool, PGF_EXPR_APP,
|
||||
gu_new_variant_i(out_pool, PGF_EXPR_APP,
|
||||
PgfExprApp,
|
||||
.fun = parent->expr,
|
||||
.arg = st->ep->expr);
|
||||
@@ -190,7 +178,7 @@ static void
|
||||
pgf_print_combine1_state(PgfCombine1State* st,
|
||||
GuOut* out, GuExn* err, GuPool* tmp_pool)
|
||||
{
|
||||
PgfExprState* nst = pgf_combine1_to_expr(st, tmp_pool);
|
||||
PgfExprState* nst = pgf_combine1_to_expr(st, tmp_pool, tmp_pool);
|
||||
pgf_print_expr_state(nst, out, err, tmp_pool);
|
||||
}
|
||||
|
||||
@@ -207,7 +195,7 @@ pgf_print_combine2_state(PgfCombine2State* st,
|
||||
static void
|
||||
pgf_combine1(PgfReasoner* rs, PgfCombine1State* st)
|
||||
{
|
||||
PgfExprState* nst = pgf_combine1_to_expr(st, rs->tmp_pool);
|
||||
PgfExprState* nst = pgf_combine1_to_expr(st, rs->pool, rs->out_pool);
|
||||
nst->base.continuation(rs, &nst->base);
|
||||
|
||||
st->choice++;
|
||||
@@ -228,9 +216,9 @@ pgf_reasoner_try_first(PgfReasoner* rs, PgfExprState* parent, PgfAbsFun* absfun)
|
||||
|
||||
PgfAnswers* answers = gu_map_get(rs->table, cat, PgfAnswers*);
|
||||
if (answers == NULL) {
|
||||
answers = gu_new(PgfAnswers, rs->tmp_pool);
|
||||
answers->parents = gu_new_buf(PgfExprState*, rs->tmp_pool);
|
||||
answers->exprs = gu_new_buf(PgfExprProb*, rs->tmp_pool);
|
||||
answers = gu_new(PgfAnswers, rs->pool);
|
||||
answers->parents = gu_new_buf(PgfExprState*, rs->pool);
|
||||
answers->exprs = gu_new_buf(PgfExprProb*, rs->pool);
|
||||
answers->outside_prob = parent->base.prob;
|
||||
|
||||
gu_map_put(rs->table, cat, PgfAnswers*, answers);
|
||||
@@ -239,7 +227,7 @@ pgf_reasoner_try_first(PgfReasoner* rs, PgfExprState* parent, PgfAbsFun* absfun)
|
||||
gu_buf_push(answers->parents, PgfExprState*, parent);
|
||||
|
||||
if (gu_buf_length(answers->parents) == 1) {
|
||||
PgfExprState* st = gu_new(PgfExprState, rs->tmp_pool);
|
||||
PgfExprState* st = gu_new(PgfExprState, rs->pool);
|
||||
st->base.continuation = (PgfPredicate) absfun->predicate;
|
||||
st->base.prob = answers->outside_prob + absfun->ep.prob;
|
||||
st->answers = answers;
|
||||
@@ -256,7 +244,7 @@ pgf_reasoner_try_first(PgfReasoner* rs, PgfExprState* parent, PgfAbsFun* absfun)
|
||||
PgfExprProb* ep =
|
||||
gu_buf_get(answers->exprs, PgfExprProb*, 0);
|
||||
|
||||
PgfCombine1State* nst = gu_new(PgfCombine1State, rs->tmp_pool);
|
||||
PgfCombine1State* nst = gu_new(PgfCombine1State, rs->pool);
|
||||
nst->base.continuation = (PgfPredicate) pgf_combine1;
|
||||
nst->base.prob = parent->base.prob + ep->prob;
|
||||
nst->exprs = answers->exprs;
|
||||
@@ -274,7 +262,7 @@ pgf_reasoner_try_first(PgfReasoner* rs, PgfExprState* parent, PgfAbsFun* absfun)
|
||||
void
|
||||
pgf_reasoner_try_else(PgfReasoner* rs, PgfExprState* prev, PgfAbsFun* absfun)
|
||||
{
|
||||
PgfExprState *st = gu_new(PgfExprState, rs->tmp_pool);
|
||||
PgfExprState *st = gu_new(PgfExprState, rs->pool);
|
||||
st->base.continuation = (PgfPredicate) absfun->predicate;
|
||||
st->base.prob = prev->answers->outside_prob + absfun->ep.prob;
|
||||
st->answers = prev->answers;
|
||||
@@ -290,7 +278,7 @@ pgf_reasoner_try_else(PgfReasoner* rs, PgfExprState* prev, PgfAbsFun* absfun)
|
||||
static void
|
||||
pgf_combine2(PgfReasoner* rs, PgfCombine2State* st)
|
||||
{
|
||||
PgfExprState* nst = pgf_combine2_to_expr(st, rs->tmp_pool);
|
||||
PgfExprState* nst = pgf_combine2_to_expr(st, rs->pool, rs->out_pool);
|
||||
if (nst != NULL) {
|
||||
nst->base.continuation(rs, &nst->base);
|
||||
}
|
||||
@@ -309,12 +297,12 @@ pgf_combine2(PgfReasoner* rs, PgfCombine2State* st)
|
||||
void
|
||||
pgf_reasoner_complete(PgfReasoner* rs, PgfExprState* st)
|
||||
{
|
||||
PgfExprProb* ep = gu_new(PgfExprProb, rs->pool);
|
||||
PgfExprProb* ep = gu_new(PgfExprProb, rs->out_pool);
|
||||
ep->prob = st->base.prob - st->answers->outside_prob;
|
||||
ep->expr = st->expr;
|
||||
gu_buf_push(st->answers->exprs, PgfExprProb*, ep);
|
||||
|
||||
PgfCombine2State* nst = gu_new(PgfCombine2State, rs->tmp_pool);
|
||||
PgfCombine2State* nst = gu_new(PgfCombine2State, rs->pool);
|
||||
nst->base.continuation = (PgfPredicate) pgf_combine2;
|
||||
nst->base.prob = st->base.prob;
|
||||
nst->parents = st->answers->parents;
|
||||
@@ -336,10 +324,7 @@ pgf_reasoner_try_constant(PgfReasoner* rs, PgfExprState* prev, PgfAbsFun* absfun
|
||||
|
||||
static PgfExprProb*
|
||||
pgf_reasoner_next(PgfReasoner* rs)
|
||||
{
|
||||
if (rs->tmp_pool == NULL)
|
||||
return NULL;
|
||||
|
||||
{
|
||||
size_t n_exprs = gu_buf_length(rs->exprs);
|
||||
|
||||
while (gu_buf_length(rs->pqueue) > 0) {
|
||||
@@ -363,9 +348,6 @@ pgf_reasoner_next(PgfReasoner* rs)
|
||||
}
|
||||
}
|
||||
|
||||
gu_pool_free(rs->tmp_pool);
|
||||
rs->tmp_pool = NULL;
|
||||
rs->pqueue = NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -376,21 +358,42 @@ pgf_reasoner_enum_next(GuEnum* self, void* to, GuPool* pool)
|
||||
*(PgfExprProb**)to = pgf_reasoner_next(pr);
|
||||
}
|
||||
|
||||
PgfExprEnum*
|
||||
pgf_generate_all(PgfPGF* pgf, PgfCId cat, GuPool* pool)
|
||||
PgfReasoner*
|
||||
pgf_new_reasoner(PgfPGF* pgf, GuExn* err, GuPool* pool, GuPool* out_pool)
|
||||
{
|
||||
PgfReasoner* rs = gu_new(PgfReasoner, pool);
|
||||
rs->pool = pool;
|
||||
rs->tmp_pool = gu_new_pool(),
|
||||
rs->abstract = &pgf->abstract,
|
||||
rs->table = gu_new_string_map(PgfAnswers*, &gu_null_struct, rs->tmp_pool),
|
||||
size_t n_cafs =
|
||||
(pgf->abstract.eval_gates->cafs == NULL)
|
||||
? 0 : gu_seq_length(pgf->abstract.eval_gates->cafs);
|
||||
|
||||
rs->pqueue = gu_new_buf(PgfReasonerState*, rs->tmp_pool);
|
||||
rs->exprs = gu_new_buf(PgfExprProb*, rs->tmp_pool);
|
||||
PgfReasoner* rs = gu_new_flex(pool, PgfReasoner, cafs, n_cafs);
|
||||
rs->pool = pool,
|
||||
rs->out_pool = out_pool;
|
||||
rs->err = err;
|
||||
rs->abstract = &pgf->abstract,
|
||||
rs->table = gu_new_string_map(PgfAnswers*, &gu_null_struct, rs->pool),
|
||||
|
||||
rs->eval_gates = pgf->abstract.eval_gates;
|
||||
|
||||
rs->pqueue = gu_new_buf(PgfReasonerState*, rs->pool);
|
||||
rs->exprs = gu_new_buf(PgfExprProb*, rs->pool);
|
||||
rs->en.next = pgf_reasoner_enum_next;
|
||||
|
||||
PgfAnswers* answers = gu_new(PgfAnswers, rs->tmp_pool);
|
||||
answers->parents = gu_new_buf(PgfExprState*, rs->tmp_pool);
|
||||
PgfFunction* cafs = gu_seq_data(rs->eval_gates->cafs);
|
||||
for (size_t i = 0; i < n_cafs; i++) {
|
||||
rs->cafs[i].header.code = cafs[i];
|
||||
rs->cafs[i].val = NULL;
|
||||
}
|
||||
|
||||
return rs;
|
||||
}
|
||||
|
||||
PgfExprEnum*
|
||||
pgf_generate_all(PgfPGF* pgf, PgfCId cat, GuExn* err, GuPool* pool, GuPool* out_pool)
|
||||
{
|
||||
PgfReasoner* rs = pgf_new_reasoner(pgf, err, pool, out_pool);
|
||||
|
||||
PgfAnswers* answers = gu_new(PgfAnswers, rs->pool);
|
||||
answers->parents = gu_new_buf(PgfExprState*, rs->pool);
|
||||
answers->exprs = rs->exprs;
|
||||
answers->outside_prob = 0;
|
||||
gu_map_put(rs->table, cat, PgfAnswers*, answers);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef PGF_REASONER_H_
|
||||
#define PGF_REASONER_H_
|
||||
|
||||
typedef GuStringMap PgfAbswersMap;
|
||||
|
||||
typedef struct PgfReasoner PgfReasoner;
|
||||
typedef struct PgfReasonerState PgfReasonerState;
|
||||
typedef struct PgfExprState PgfExprState;
|
||||
@@ -19,4 +21,135 @@ pgf_reasoner_complete(PgfReasoner* rs, PgfExprState* st);
|
||||
void
|
||||
pgf_reasoner_try_constant(PgfReasoner* rs, PgfExprState* prev, PgfAbsFun* absfun);
|
||||
|
||||
typedef struct {
|
||||
PgfFunction code;
|
||||
} PgfClosure;
|
||||
|
||||
typedef struct {
|
||||
PgfClosure header;
|
||||
PgfClosure* val;
|
||||
} PgfIndirection;
|
||||
|
||||
typedef struct {
|
||||
PgfLiteral lit;
|
||||
GuBuf* consts;
|
||||
void* enter_stack_ptr;
|
||||
} PgfEvalAccum;
|
||||
|
||||
struct PgfReasoner {
|
||||
GuPool* pool;
|
||||
GuPool* out_pool;
|
||||
PgfAbstr* abstract;
|
||||
PgfAbswersMap* table;
|
||||
GuBuf* pqueue;
|
||||
GuBuf* exprs;
|
||||
PgfEvalGates* eval_gates; // cached from pgf->abstr->eval_gates
|
||||
GuExn* err;
|
||||
void* enter_stack_ptr;
|
||||
void* tmp; // for temporary register spills
|
||||
PgfExprEnum en;
|
||||
PgfIndirection cafs[]; // derived from gu_seq_data(pgf->abstr->eval_gates->cafs)
|
||||
};
|
||||
|
||||
typedef struct PgfEnv PgfEnv;
|
||||
|
||||
struct PgfEnv {
|
||||
PgfEnv* next;
|
||||
PgfClosure* closure;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
PgfClosure header;
|
||||
PgfEnv* env;
|
||||
PgfExpr expr;
|
||||
} PgfExprThunk;
|
||||
|
||||
typedef struct {
|
||||
PgfClosure header;
|
||||
PgfClosure* con;
|
||||
PgfClosure* args[];
|
||||
} PgfValue;
|
||||
|
||||
typedef struct {
|
||||
PgfClosure header;
|
||||
int level;
|
||||
} PgfValueGen;
|
||||
|
||||
typedef struct {
|
||||
PgfClosure header;
|
||||
PgfEnv* env;
|
||||
PgfMetaId id;
|
||||
} PgfValueMeta;
|
||||
|
||||
typedef struct {
|
||||
PgfClosure header;
|
||||
PgfLiteral lit;
|
||||
} PgfValueLit;
|
||||
|
||||
typedef struct {
|
||||
PgfClosure header;
|
||||
PgfLiteral lit;
|
||||
GuBuf* consts;
|
||||
} PgfValueSum;
|
||||
|
||||
typedef struct {
|
||||
PgfClosure header;
|
||||
PgfClosure* fun;
|
||||
size_t n_args;
|
||||
PgfClosure* args[];
|
||||
} PgfValuePAP;
|
||||
|
||||
struct PgfEvalGates {
|
||||
PgfFunction evaluate_expr_thunk;
|
||||
PgfFunction evaluate_indirection;
|
||||
PgfFunction evaluate_value;
|
||||
PgfFunction evaluate_value_lit;
|
||||
PgfFunction evaluate_value_pap;
|
||||
PgfFunction evaluate_value_lambda;
|
||||
PgfFunction evaluate_value_const;
|
||||
PgfFunction evaluate_meta;
|
||||
PgfFunction evaluate_gen;
|
||||
PgfFunction evaluate_sum;
|
||||
PgfFunction evaluate_caf;
|
||||
|
||||
PgfFunction update_closure;
|
||||
PgfFunction update_pap;
|
||||
|
||||
PgfFunction mk_const;
|
||||
|
||||
PgfClosure* (*enter)(PgfReasoner* rs, PgfClosure* closure);
|
||||
|
||||
GuFinalizer fin;
|
||||
GuSeq* cafs;
|
||||
};
|
||||
|
||||
PgfReasoner*
|
||||
pgf_new_reasoner(PgfPGF* pgf, GuExn* err, GuPool* pool, GuPool* out_pool);
|
||||
|
||||
PgfClosure*
|
||||
pgf_evaluate_expr_thunk(PgfReasoner* rs, PgfExprThunk* thunk);
|
||||
|
||||
PgfClosure*
|
||||
pgf_evaluate_lambda_application(PgfReasoner* rs, PgfExprThunk* lambda,
|
||||
PgfClosure* arg);
|
||||
|
||||
void
|
||||
pgf_evaluate_accum_init_int(PgfReasoner* rs,
|
||||
PgfEvalAccum* accum, int val);
|
||||
|
||||
void
|
||||
pgf_evaluate_accum_init_str(PgfReasoner* rs,
|
||||
PgfEvalAccum* accum, GuString val);
|
||||
|
||||
void
|
||||
pgf_evaluate_accum_init_flt(PgfReasoner* rs,
|
||||
PgfEvalAccum* accum, float val);
|
||||
|
||||
void
|
||||
pgf_evaluate_accum_add(PgfReasoner* rs,
|
||||
PgfEvalAccum* accum, PgfClosure* closure);
|
||||
|
||||
PgfClosure*
|
||||
pgf_evaluate_accum_done(PgfReasoner* rs, PgfEvalAccum* accum);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -113,7 +113,8 @@ generateAll p cat =
|
||||
do genPl <- gu_new_pool
|
||||
exprPl <- gu_new_pool
|
||||
enum <- withCString cat $ \cat ->
|
||||
pgf_generate_all (pgf p) cat genPl
|
||||
exn <- gu_new_exn genPl
|
||||
pgf_generate_all (pgf p) cat exn genPl exprPl
|
||||
genFPl <- newForeignPtr gu_pool_finalizer genPl
|
||||
exprFPl <- newForeignPtr gu_pool_finalizer exprPl
|
||||
fromPgfExprEnum enum genFPl (p,exprFPl)
|
||||
|
||||
@@ -209,7 +209,7 @@ foreign import ccall "pgf/expr.h pgf_print_expr"
|
||||
pgf_print_expr :: PgfExpr -> Ptr PgfPrintContext -> CInt -> Ptr GuOut -> Ptr GuExn -> IO ()
|
||||
|
||||
foreign import ccall "pgf/pgf.h pgf_generate_all"
|
||||
pgf_generate_all :: Ptr PgfPGF -> CString -> Ptr GuPool -> IO (Ptr GuEnum)
|
||||
pgf_generate_all :: Ptr PgfPGF -> CString -> Ptr GuExn -> Ptr GuPool -> Ptr GuPool -> IO (Ptr GuEnum)
|
||||
|
||||
foreign import ccall "pgf/pgf.h pgf_print"
|
||||
pgf_print :: Ptr PgfPGF -> Ptr GuOut -> Ptr GuExn -> IO ()
|
||||
|
||||
@@ -1154,11 +1154,12 @@ JNIEXPORT jobject JNICALL
|
||||
Java_org_grammaticalframework_pgf_Generator_generateAll(JNIEnv* env, jclass clazz, jobject jpgf, jstring jstartCat)
|
||||
{
|
||||
GuPool* pool = gu_new_pool();
|
||||
|
||||
GuPool* out_pool = gu_new_pool();
|
||||
GuString startCat = j2gu_string(env, jstartCat, pool);
|
||||
GuExn* err = gu_exn(pool);
|
||||
|
||||
GuEnum* res =
|
||||
pgf_generate_all(get_ref(env, jpgf), startCat, pool);
|
||||
pgf_generate_all(get_ref(env, jpgf), startCat, err, pool, out_pool);
|
||||
if (res == NULL) {
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The generation failed");
|
||||
gu_pool_free(pool);
|
||||
@@ -1167,7 +1168,7 @@ Java_org_grammaticalframework_pgf_Generator_generateAll(JNIEnv* env, jclass claz
|
||||
|
||||
jclass expiter_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/ExprIterator");
|
||||
jmethodID constrId = (*env)->GetMethodID(env, expiter_class, "<init>", "(Lorg/grammaticalframework/pgf/PGF;JJJ)V");
|
||||
jobject jexpiter = (*env)->NewObject(env, expiter_class, constrId, jpgf, p2l(pool), p2l(pool), p2l(res));
|
||||
jobject jexpiter = (*env)->NewObject(env, expiter_class, constrId, jpgf, p2l(pool), p2l(out_pool), p2l(res));
|
||||
|
||||
return jexpiter;
|
||||
}
|
||||
|
||||
@@ -2484,24 +2484,26 @@ PGF_generateAll(PGFObject* self, PyObject *args, PyObject *keywds)
|
||||
pyres->source = (PyObject*) self;
|
||||
Py_INCREF(self);
|
||||
|
||||
GuPool* out_pool = gu_new_pool();
|
||||
|
||||
PyObject* py_pool = PyPool_New(out_pool);
|
||||
pyres->container = PyTuple_Pack(2, pyres->source, py_pool);
|
||||
Py_DECREF(py_pool);
|
||||
|
||||
pyres->pool = gu_new_pool();
|
||||
pyres->max_count = max_count;
|
||||
pyres->counter = 0;
|
||||
pyres->fetch = Iter_fetch_expr;
|
||||
pyres->container = (PyObject*) pyres;
|
||||
|
||||
GuPool *tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_exn(pyres->pool);
|
||||
|
||||
pyres->res =
|
||||
pgf_generate_all(self->pgf, catname, pyres->pool);
|
||||
pgf_generate_all(self->pgf, catname, err, pyres->pool, out_pool);
|
||||
if (pyres->res == NULL) {
|
||||
Py_DECREF(pyres);
|
||||
gu_pool_free(tmp_pool);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
gu_pool_free(tmp_pool);
|
||||
|
||||
return pyres;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user