non deterministic - Matching determinism of a lambda and a predicate in Mercury -
non deterministic - Matching determinism of a lambda and a predicate in Mercury -
in mercury, can declare lambda having same determinism mode of predicate contains lambda?
here's i'm trying do. wrote fold function (below) works on array2d type. fold
calls caller-supplied predicate each element in array. works fine, long accepts det predicate argument.
:- pred fold(array2d(t), pred(t, int, int, a, a), a, a). :- mode fold(in, pred(in, in, in, in, out) det, in, out) det. % uncommenting next line causes mode errors during compilation % :- mode fold(in, pred(in, in, in, in, out) semidet, in, out) semidet. fold(array, pred, !accumulator) :- bounds(array, numrows, numcols), foldrows = (pred(rownumber :: in, rowaccin :: in, rowaccout :: out) det :- foldcols = (pred(colnumber :: in, colaccin :: in, colaccout :: out) det :- value = array^elem(rownumber, colnumber), pred(value, rownumber, colnumber, colaccin, colaccout) ), int.fold_up(foldcols, 0, numcols - 1, rowaccin, rowaccout) ), int.fold_up(foldrows, 0, numrows - 1, !accumulator).
but want fold
take either det or semidet predicate (and fail if phone call predicate fails). uncommenting mode ... semidet
line give me compiler errors don't know how resolve. problem lambdas in fold
declared det, can't phone call semidet pred
. if alter lambdas semidet, fold
whole can't det.
how can resolve this? seems straightforward approach declare lambdas inherit determinism fold
predicate -- they're det when fold
beingness used det, likewise semidet -- don't know if that's possible.
another approach, of course, turn foldrows
, foldcols
named predicates (not lambdas) multiple modes. becomes inelegant, , i'm wondering if there's more straightforward solution.
mercury can infer modes , determinisms of predicates, tried omitting determinism declaration lambda expressions. mercury's lambda syntax doesn't allow me this, inference cannot used lambdas.
i'm afraid solution is, guessed, turn foldrows , foldcols multi moded named predicates.
lambda non-deterministic mercury
Comments
Post a Comment