0

In my program I have a number of rules, each "firing" based on a different precondition and current state. One such rule's purpose is to identify a special illegal condition, that is known to occur, and then explicitly invoke backtracking via a fail.

I now included a trace -- a list a visited rules -- as parameters, and noticed that for all, but this rule, i can include in this list. However, since that one rules behavior is to fail, i don't see a way to capture the firing of this rule in a trace.

This trace is important to me, since in I am writing test code, and in that code, i test each possible condition and whether it lead to the firing of a rule -- that the requisite rule fired, i can retrieve from the trace. All, but, as said, for this one rule.

Is there a way around this ... looks like i am trying to square the circle here :-)

thank you,

Dan

false
  • 10,264
  • 13
  • 101
  • 209
user2193970
  • 345
  • 2
  • 10
  • Use `assert/1`. – Tomas By Dec 18 '17 at 12:03
  • assert is verboten :-) -- but, i guess, no way around this – user2193970 Dec 18 '17 at 12:14
  • Open a file and print to it using Prolog syntax which you can then read in later? – Tomas By Dec 18 '17 at 12:17
  • 2
    I guess, I am trying to do this without side-effects -- i am trying to stay within the relational paradigm. – user2193970 Dec 18 '17 at 12:47
  • Then don't fail. Include a parameter with two possible values (ok and not ok) and instead of failing you return not ok. – Tomas By Dec 18 '17 at 12:51
  • Right, but, i think, i would loose the backtracking to search for a different solution, unless i introduce some cuts somewhere, which is also verboten, given the relational approach i am aiming for. – user2193970 Dec 18 '17 at 13:00
  • 2
    You need to provide more context and/or post some code I think. The rule identifies a condition and then fails. How is that useful? How do you know it found anything? You can make the rules not fail and use `findall/3` (but I guess that is also not allowed in this school project...) – Tomas By Dec 18 '17 at 13:11
  • Thank you Tomas. Its not for a school project, but my attempt to learn how to use a relational approach to prolog development, and still as much as possible to pure prolog. – user2193970 Dec 18 '17 at 14:43
  • Have you considered building a metainterpreter? It's not very much work, and then you have a lot more control over how Prolog is executed; the technique can be used to change Prolog to use breadth-first search. Alternatively, it sounds like you're trying to build a unit testing framework; maybe you should examine the source and approach used by [plUnit](http://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/plunit.html%27)). – Daniel Lyons Dec 18 '17 at 18:05
  • Hi Daniel, Yes, i tried to create a meta interpreter, but it got complicated, when dealing with cuts and \+, to ensure that these are applied correctly. In the end I moved away from meta interpretation. You are head-on with unit testing. This is what i am doing now. I am trying to write robust code where every predicate is tested, as i move up the abstraction levels; so as the code evolves i can know what still works as expected. I am using the plUnit test framework for that. I am now creating an internal trace structure, to relate expected states to the expected application of rules. – user2193970 Dec 18 '17 at 18:27
  • Hi Tomas, i think assert is probably the way i will eventually go for the test code. Since, i think, its the only way to record that a failure and backtracking has occurred, and keep this information intact for after the overall predicate completed its work. – user2193970 Dec 19 '17 at 01:35

0 Answers0