Why do circular references and recursion make my program fail?

后端 未结 4 1237
耶瑟儿~
耶瑟儿~ 2021-01-14 10:28

I wrote this simple Prolog program.

man(socrates).
mortal(X) :- man(X).
immortal(X) :- immortal(X).

I asked it the usual questions, such as

4条回答
  •  终归单人心
    2021-01-14 11:16

    Your definitions - and how you interpret them:

    man(socrates).
    

    Socrates is a man.

    mortal(X) :- man(X).
    

    Every man is a mortal.

    immortal(X) :- immortal(X).
    

    Every immortal is immortal.


    Your definitions - and how Prolog interprets them:

    man(socrates).
    

    If you ask about the manhood of Socrates, I know it's true.

    mortal(X) :- man(X).
    

    If you ask me about the mortality of someone, I'll check his manhood (and if that's true, so is the mortality).

    immortal(X) :- immortal(X).
    

    If you ask me about the immortality of someone, I'll check his immortality. (Do you still wonder how that leads to an infinite loop?)


    If you want to state that someone is immortal if he can't be proven mortal, then you can use:

    immortal(X) :- not( mortal(X) ).
    

提交回复
热议问题