Can I add predicates at runtime?

122 Views Asked by At

The context is a system of predicates: rules and facts, that together determine an amount to be paid to a client, depending on his situation. Most rules and facts are known at compile time, but facts about a specific situation are only known at runtime, when processing a pay out request. I know how to program a system in Mercury in the case that all predicates are known at compile time (i.e. are part of the source code), but I don't know how to introduce predicates at runtime. Is it possible to write a function that constructs a predicate with the given parameters and then be able to use the constructed predicate as part of the context against which amounts are calculated?

1

There are 1 best solutions below

0
Zoltan Somogyi On

I am not sure exactly what your question is.

If by "construct a predicate with the given parameters", you are asking whether you can construct a closure that contains a reference to a predicate in the originally compiled program and a list of values for its initial arguments that are constructed at runtime, then the answer is "yes, you can do that".

If you mean whether you can construct at runtime a predicate whose code is NOT part of the originally compiled program, then the answer is "no, you can't do that" in Mercury. However, it is possible to work around this, by

  • writing the code of the new predicate to a new Mercury module;
  • compiling that module to object code;
  • using dynamic linking to make the new object file part of the existing executable; and
  • using the foreign code interface to transfer a target-language C pointer, or its equivalents in other languages, to the original code, for it to call.

However, this method is not recommended for anyone who doesn't have an implementor's understanding of how the Mercury implementation works.