4

How can I express next 3 sentences in Prolog?

All summers are warm. If it not summer, then it is winter. Now it is winter. 
5
  • Is this homework? If so please tag as such. Commented Jun 7, 2012 at 8:55
  • 1
    I don't remember the syntax much but from what I understand you are looking at a way of defining universal/existential quantifiers. This text may be of some help to get you started. Commented Jun 7, 2012 at 8:56
  • There are myriad ways of representing this in Prolog. What have you tried? Commented Jun 7, 2012 at 8:59
  • larsman, can you show the simpliest way? It is not a homework. Commented Jun 7, 2012 at 9:00
  • @dirkgently: thanks for the link. Interesting ! Commented Oct 1, 2016 at 10:55

4 Answers 4

5

Nice question. As @larsman (well, @FredFoo now, I think) rightly said, can be a large thema. And his answer is very good indeed.

As your question could be driven by necessity of a custom language (one of main Prolog' uses), here I propose the syntax sugar for a dummy DSL (what that's means it's totally empty now...)

:- op(500, fx, all). :- op(500, fx, now). :- op(600, xfx, are). :- op(700, fx, if). :- op(399, fx, it). :- op(398, fx, is). :- op(397, fx, not). :- op(701, xfx, then). all summers are warm. if it is not summer then it is winter. now it is winter. 

SWI-Prolog is kind enough to make red those op that get stored, i.e. can be queried easily. These are the higher precedence words declared: i.e. are,then,now.

?- now X. X = it is winter. 
Sign up to request clarification or add additional context in comments.

Comments

2

How to represent this depends on what inferences you want to make. One of the simplest ways is

warm :- summer. winter. 

The "if not summer then winter" rule does not actually allow you to make any useful inferences, so you could just as well skip it. If you were to include it, it might be something like

winter :- \+ summer. 

but since negation in Prolog is negation as failure, this might not do what you think it does if you expect the semantics of vanilla propositional logic.

Comments

0
winter(now). warm(X) :- summer(X). summer(X) :- \+ winter(X). winter(X) :- \+ summer(X). 

Would be one of the ways to do this.

In action:

6 ?- summer(now). false. 7 ?- summer(tomorrow). ERROR: Out of local stack 8 ?- warm(now). false. 

Comments

0

Here a solution without using negation, instead the universe of seasons is specified.

season(summer). season(winter). now(winter). warm(S) :- season(S), S = summer. 

Some example queries:

?- now(S). S = winter ; false. ?- now(S), warm(S). false. ?- warm(S). S = summer ; false. 

Comments