How does f(s(s(s(s(s(s(1)))))),C) work on Prolog? -
i practicing textbook , cannot find reason when see result.
on prolog data base, shows
f(1,one). f(s(1),two). f(s(s(1)),three). f(s(s(s(x))),n) :- f(x,n).
when run program
f(s(s(s(s(s(s(1)))))),c).
the response of program "c = one."
how work?
prolog simple. programs consist of rules of form
to_prove_this :- must_prove_this, and_this. % , perhaps also, to_prove_this :- must_otherwise_prove_this, and_this_too.
so program means
1. prove `f( 1, one)` :- there's no need prove more. 2. prove `f( s(1), two)` :- there's no need prove more. 3. prove `f( s(s(1)), three)` :- there's no need prove more. 4. prove `f( s(s(s(x))), n)` :- must prove `f( x, n)`.
so start
to prove: f( s(s(s(s(s(s(1)))))), c).
can rule 1.
used?
| `f( s(s(s(s(s(s(1)))))), c)` similar `f(1,one)`? | | `f` similar `f`? | | -- yes. | | `s(s(s(s(s(s(1))))))` similar `1`? | | -- no. | -- no, `f( s(s(s(s(s(s(1)))))), c)` , `f(1,one)` not similar. -- no, rule 1. can't used.
can rule 2.
used?
| `f( s(s(s(s(s(s(1)))))), c)` similar `f(s(1),two)`? . . . . . . . . . . . . . . .
can rule 4.
used?
| `f( s(s(s(s(s(s(1)))))), c)` similar `f(s(s(s(x))),n)`? | | `f` similar `f`? | | -- yes. | | `s(s(s(s(s(s(1))))))` similar `s(s(s(x)))`? | | | `s(s(s(s(s(1)))))` similar `s(s(x))`? | | | | `s(s(s(s(1))))` similar `s(x)`? | | | | | `s(s(s(1)))` similar `x`? | | | | | -- yes, `x = s(s(s(1)))`. | | `c` similar `n`? | | -- yes, `c = n`. | -- yes, similar, `x = s(s(s(1)))` , `c = n`. -- yes, can used, `x = s(s(s(1)))` , `c = n`.
this means, need prove f(x,n)
now, x = s(s(s(1)))
, c = n
.
means, need prove f(x1,n1)
now, x1 = s(s(s(1)))
, c = n1
.
means, need prove f( s(s(s(1))), c )
now.
can rule 1.
used?
. . . . . . . . . . . .
this means, need prove f(x,n)
now, x = 1
, c = n
.
means, need prove f(x2,n2)
now, x2 = 1
, c = n2
.
means, need prove f( 1, c )
now.
can rule 1.
used now?
Comments
Post a Comment