0

We have just started to learn prolog in my classroom and our first exercise goes as follows:

Problem:

Note: assume that there are only two atoms such as a and b instead of infinitely many.

a) Write a prolog program that translates the inductive definition of Propositional Logic formula (i.e. that translates Definition 1). For this, you need to use:

1.) a unary predicate “at” to denote atomic formulas (so “at(f)” means “F is an atomic formula”, where f is a constant).

2.) a unary predicate “fmla” to denote formulas (so “fmla(F)” means “F is a formula”).

3.) a unary operation “neg” to denote negation (so “neg(F)” stands for ¬F).

4.) a binary operation “or” to denote disjunction of two formulas (so “or(F,G)” stands for (F∨G)).

Attempt:

    at(a). % Our first atom.
    at(b). % Our second atom.

    fmla(F):-
        at(F).

    neg(F):-
        fmla(F).

    or(F,G):-
        fmla(F), fmla(G).

    fmla(F):-
        or(F,G),
        neg(F),
        fmla(G).

Example of a valid formula: (~A v B) ---> or(neg(a), b).

I believe that the way that I've struct my program is correct but the recursive part doesn't work(I'm also getting a singleton error there). I've trying to figure this out for hours but to no avail. Any help would be appreciated.

Luis Averhoff
  • 887
  • 2
  • 11
  • 22

1 Answers1

2

You've already got the atoms right. Now simply walk through the inductive definition of a propositional logic formula:

at(a). % Our first atom.
at(b). % Our second atom.

fmla(F):-           % an atom is a formula
    at(F).

fmla(neg(F)) :-     % neg(F) is a formula if F is a formula
    fmla(F).

fmla(or(F,G)) :-    % or(F,G) is a formula if F and G are formulas
    fmla(F),
    fmla(G).

fmla(and(F,G)) :-   % and(F,G) is a formula if F and G are formulas
    fmla(F),
    fmla(G).

If you try to query this with your above example:

   ?- fmla(or(neg(a), b)).
yes

Whether you need the last rule for the conjunction or not depends on which inductive definition you are sticking to. Some textbooks only use negation and disjunction as conjunction can be expressed as and(A,B) = neg(or(neg(A),neg(B))).

tas
  • 8,100
  • 3
  • 14
  • 22