Page 1 of 1

Dynamic runtime predicate pointer binding

Posted: 28 Apr 2014 9:38
by kingchris
Pseudo Code.

Code: Select all

facts     predicate_pointer : pointer(string).   predicates     doNothing:(string).     writeToConsole:(string).     app_init().     doLotsOfWork().   clauses     app_init():-         if (debug_condition=true())             predicate_pointer := writeToConsole()         else             predicate_pointer := doNothing()     end if.       doLotsOfWork():-         repeat(),         LineVal = InStream:readLine(),           predicate_pointer(LineVal),           fail.  
This is a contrived example of the kind of thing that I wish to do.

Can I set up a fact or property or domain to allow what could be called runtime binding so that either the doNothing predicate gets called or writeToConsole gets called based on a runtime test.

Thanks for your trouble

Posted: 28 Apr 2014 10:19
by Thomas Linder Puls
You must have gone to a wrong school before ;-).

You should not use a "predicate pointer" you should use a predicate:

Code: Select all

domains     writer = (string String). facts     myWriter : writer.   predicates     doNothing:(string).     writeToConsole:(string).     app_init().     doLotsOfWork().   clauses     app_init():-         if debug_condition=true() then             myWriter := writeToConsole         else             myWriter := doNothing         end if.       doLotsOfWork():-         repeat(),         LineVal = InStream:readLine(),             myWriter(LineVal),         fail.
Also notice (other influence from your "wrong school") that you do not have to put parentheses around the test in an i-then-else, but you do have to write "then".

Posted: 28 Apr 2014 10:41
by kingchris
You are correct in your "wrong school" statements.

But please note that I did say Psuedo Code at the top. I and many other people on this forum, I am sure, program in multiple languages from F#, C#, C, C++, assembler, VB, Pascal etc. so its all gets very confusing. The latest PDC Prolog has introduced lots of things that you don't find in traditional Prolog. Hence I get very mixed up about what belongs in which language.

I was writing the essence of my problem. I personally get upset with people who post all of their code when asking a question. It becomes too much effort to read thorough and try and help them. So when I post a question I try keep it simple.

Thanks for your prompt answer.
I will code it up now into my program.
Cheers

Posted: 28 Apr 2014 14:26
by Thomas Linder Puls
No problem :-).

I hope it solves your problem.