Hi, so I’m trying to implement a backward chaining in an expert system and I just can’t seem to figure out how to get a bool value which represents “are every elements in the list present in the other list”.
Also, is it possible to override parameters values? Basicaly, if I get true at the previous step, I need to add a value in the type’s list I received by parameters and then start using the version of the type that contains this new value.
…
if (List.for_all (fun value →
if (valueIsFact value facts) then true
else if (recFunc value system) then true
else false) (listOfValues))
then (begin system := (addFact newValue system); true; end) else false
…
Just a quick note that:
fun value ->
if (valueIsFact value facts) then true
else if (recFunc value system) then true
else false
Can be simplified to:
fun value -> valueIsFact value facts || recFunc value system
Unsure what you’re asking. If you want to know if every element of L1 is in L2:
l1 |> List.for_all (fun x -> List.mem x l2)
[just dashing this off, so it might be wrong, but I think that’s it]
Don’t understand what you mean by “override parameters values”, could you expand? I don’t see where “newValue” is defined.
1 Like
Basically I received “system” as a parameter but if I understand correctly, in functionnal programming, I have to recreate another and then return it or call my function again with it for the new values to be seen? (I can’t do this in this case)
I was asking:
- what do you add to “system”
- under what conditions do you add it?
- and what do you use it for?
None of these are obvious from your description. I realize that your -code- answers some of these questions, but since you’re asking for help with the code, I don’t think it’s a good idea to take direction from that as to your intentions.
Can you explain what system gets used for?
1 Like
system contains a list of rules and a list of facts
Each rule is a pair of premisses and conclusions, both are strings
Each fact is a string
I receive such rules and fact adn add/remove them from the system.
My problem is when trying to use a fact to infer an answer using backward chaining. meaning that I take the fact and seee which rules have this fact as a conclusion, then check the corresponding premisses and check if they are in the list of facts and if not I recursively do the proccess al over until I have explored all cases or confirmed that I can infer a fact from my current and infered knowledge.
So I think you mean that:
- each rule is a list of facts (premises) and a conclusion; these are all strings
- there is a set of ground facts (which we might think of as rules with no premises.
3, you start with a fact you wish to prove, and match it against the conclusions of rules; when you find a match, you try to prove the premises.
Perhaps
4. you keep track of which facts you are in-process of proving, so that you don’t end up in a cycle
5. you keep track of facts already-proven, in order to not prove facts repeatedly.
Is this accurate? Are there other things I’ve left out?
You are right.
When a premisse is proven, it is because this premisse is a conclusion of other premisses. So when all premisses are proven for an unproven conclusion, this conclusion is added to the facts
Knowing this, I can’t get it to work when coding it
OK, so:
type fact = string
type rule = fact list * fact
type ruleset = rule list
the prove function should have type:
prove : rules:ruleset -> inprogress:(fact list) -> goal:fact -> (fact * ruleset) option
yes? Then there are two cases:
- the
goal
is already in inprogress
: return None
- the
goal
is not in inprogress
, but does not match the head of any rule: return None
- the
goal
matches the head of some rules
- add the
goal
to the inprogress
stack
- for each such rule, try to prove all its premises, threading the
rules
thru
the calls
- if you get success, then return the resulting ruleset, but adding the goal to the ruleset
- if a premise fails, then proceed to the next matching rule
Does this make sense to you?
P.S. I made some assumptions that are not strictly necessary, and removing them might make things more efficient. But it seems like this might be a good starting-point for you.
Thanks!
I figured it out without using prove but I’ll keep it in mind for next time.
We have’nt been tought this yet (school course)