Artificial Intelligence and Prolog
Copyright © 2020 Jiri Kriz, www.nosco.ch

4    Knowledge Representation in Pure Prolog

Solution of Exercise 4

4.1    Means of Transport

transport( X) :- public( X).
transport( X) :- private( X).

public( bus).
public( train).
public( airplane).
public( ship).

private( bicycle).
private( motorcycle).
private( car).

vehicle( bicycle).
vehicle( motorcycle).
vehicle( car).
vehicle( bus).

motor_vehicle( X) :- has_motor( X).

slow( bicycle).
eco-friendly( bicycle).

fast( car, mittel).
dangerous( car).
has_petrolengine( car).

fast( bus, rather).
has_petrolengine( bus).

fast( train, medium).
has_electricmotor( train).

fast( airplane, very).
has_petrolengine( airplane).

slow( ship).
has_dieselmotor( ship).

eco_hostile( X) :- has_petrolengine( X).
eco_hostile( X) :- has_dieselmotor( X).
eco_friendly( X) :- has_electricmotor( X).

safe( X) :- public( X).

has_motor( X) :- has_petrolengine( X).
has_motor( X) :- has_dieselmotor( X).
has_motor( X) :- has_electricmotor( X).

fast( X) :- fast( X, _).

/* ?- transport( X), fast( X)     ... etc.
   ?- fast( airplane)          
   ?- fast( airplane, How)
   ?- vehicle( X), has_motor(X) 
   The knowledge base has to be replenish in order to answer
   "Which properties has the car?", e.g.:
      property( car, [fast, dangerous, has_petrolengine]).
   Another way is to change the representation, e.g.:
      object( car, fast).
*/

Back to example 4.1

4.2    Problems with Insulators

breakdown( flashover_arc).
breakdown( puncture_arc).
breakdown( surface_leakage).

cause( flashover_arc, high_voltage).
cause( high_voltage, lightning_strike).
cause( puncture_arc, faulty_material).
cause( puncture_arc, cracky_material).
cause( surface_leakage, 'surface is dirty and wet').
cause( surface_leakage, 'material is composite polymer').

consequence( Cause, Consequence) :- cause( Consequence, Cause).
consequence( puncture_arc, damage).
consequence( damage, 'replace insulator').
consequence( surface_leakage, creepage_path).
consequence( creepage_path, 'replace insulator').

Back to example 4.2

4.3    Resistor Network (Representation by Terms)

res( NW, R) :- 
    resistor( NW, R).
res( seq( NW1, NW2), R) :- 
    res( NW1, R1), res( NW2, R2), R is R1 + R2.
res( par( NW1, NW2), R) :- 
    res( NW1, R1), res( NW2, R2), 
    S is 1/R1 + 1/R2, R is 1/S.

Back to example 4.3

4.4    Digital Circuits (Representation by Clauses)

adder1( I1, I2, C_in, Out, C_out) :-
     xor( I1, I2, X), and( I1, I2, A1), and( X, C_in, A2),
     xor( X, C_in, Out), or( A1, A2, C_out).

adder_overflow( [], [], [], 0).
adder_overflow( [ I1| I1s], [ I2| I2s], [ O| Os], Overflow) :-
     adder_overflow( I1s, I2s, Os, Carry),
     adder1( I1, I2, Carry, O, Overflow).

adder( X, Y, [ OF| Z]) :-
	adder_overflow( X, Y, Z, OF).

or( 0, 0, 0).
or( 0, 1, 1).
or( 1, 0, 1).
or( 1, 1, 1).

xor( 0, 0, 0).
xor( 0, 1, 1).
xor( 1, 0, 1).
xor( 1, 1, 0).

and( 0, 0, 0).
and( 0, 1, 0).
and( 1, 0, 0).
and( 1, 1, 1).

/* Test: 6 + 5 = 11
?- adder([1, 1, 0], [1, 0, 1], Sum).
=> Sum = [1, 0, 1, 1]
*/

Back to example 4.4