% Facts about animals
animal(dog).
animal(cat).
animal(elephant).
animal(lion).
animal(tiger).
animal(snake).  
animal(crocodile).
animal(bird).
animal(fish).

% Facts about characteristics
has_fur(dog).
has_fur(cat).
has_fur(lion).
has_stripes(tiger).
has_trunk(elephant).
has_scales(snake).
has_scales(crocodile).
can_fly(bird).
lives_in_water(fish).

% Rules for classification
mammal(X) :- animal(X), has_fur(X).
mammal(X) :- animal(X), has_stripes(X).
mammal(X) :- animal(X), has_trunk(X).

reptile(X) :- animal(X), has_scales(X).

bird(X) :- animal(X), can_fly(X).

fish(X) :- animal(X), lives_in_water(X).

% Classification predicate
classify(X) :- mammal(X), write(X), write(' is a mammal.'), nl.
classify(X) :- reptile(X), write(X), write(' is a reptile.'), nl.
classify(X) :- bird(X), write(X), write(' is a bird.'), nl.
classify(X) :- fish(X), write(X), write(' is a fish.'), nl.
classify(X) :- write(X), write(' is an unknown animal.'), nl.

% Initialization goal
:- initialization(main).

% Main goal
main :- classify(elephant),
        classify(snake),
        classify(fish),
        classify(unicorn),
        halt. 

Prolog Online Compiler

Write, Run & Share Prolog code online using OneCompiler's Prolog online compiler for free. It's one of the robust, feature-rich online compilers for Prolog language. Getting started with the OneCompiler's Prolog editor is easy and fast. The editor shows sample boilerplate code when you choose language as Prolog and start coding.