%Smart home automation
% Define the state of the light (on/off)
light_state(off).

% Define the state of the thermostat (temperature in Celsius)
thermostat_state(21).

% Define the state of the door (open/closed)
door_state(closed).

% Define the state of the lock (locked/unlocked)
lock_state(locked).

% Define the state of the motion sensor (active/inactive)
motion_sensor_state(inactive).

% Define the state of the fire sensor (alert/no_alert)
fire_sensor_state(no_alert).

% Turn on the light
turn_on_light :-
    retract(light_state(off)),
    asserta(light_state(on)).

% Turn off the light
turn_off_light :-
    retract(light_state(on)),
    asserta(light_state(off)).

% Set the thermostat temperature
set_thermostat(Temp) :-
    retract(thermostat_state(_)),
    asserta(thermostat_state(Temp)).

% Open the door
open_door :-
    retract(door_state(closed)),
    asserta(door_state(open)).

% Close the door
close_door :-
    retract(door_state(open)),
    asserta(door_state(closed)).

% Lock the door
lock_door :-
    retract(lock_state(unlocked)),
    asserta(lock_state(locked)).

% Unlock the door
unlock_door :-
    retract(lock_state(locked)),
    asserta(lock_state(unlocked)).

% Automatically turn on the light when motion is detected
automate_light :-
    motion_sensor_state(active),
    turn_on_light,
   !.

% Automatically adjust the thermostat based on the current temperature
automate_thermostat :-
    thermostat_state(CurrentTemp),
    (   CurrentTemp < 18 -> set_thermostat(18)
    ;   CurrentTemp > 22 -> set_thermostat(22)
    ;   true
    ),
   !.

% Automatically unlock the door when a fire is detected
automate_door_unlock :-
    fire_sensor_state(alert),
    unlock_door,
   !.

% Automatically lock the door when the fire alert is cleared
automate_door_lock :-
    fire_sensor_state(no_alert),
    lock_door,
   !.

% Main loop
main :-
    % Check the state of the motion sensor
    motion_sensor_state(State),
    (   State = active -> automate_light
    ;   true
    ),

    % Check the state of the fire sensor
    fire_sensor_state(State),
    (   State = alert -> automate_door_unlock
    ;   State = no_alert -> automate_door_lock
    ;   true
    ),

    % Check the state of the thermostat
    automate_thermostat,

    % Repeat the loop
    main.

% Initialize the system
init :-
    retractall(_),
    asserta(light_state(off)),
    asserta(thermostat_state(21)),
    asserta(door_state(closed)),
    asserta(lock_state(locked)),
    asserta(motion_sensor_state(inactive)),
    asserta(fire_sensor_state(no_alert)),
    main.

% Start the system
start :-
    init,
    main. 
by

Prolog online compiler

Write, Run & Share Prolog code online using OneCompiler’s Prolog online compiler for free. It’s a simple and intuitive platform to experiment with logic programming in Prolog. OneCompiler supports standard Prolog syntax, great for learning, prototyping, and practicing logic-based problems.

About Prolog

Prolog (Programming in Logic) is a logic programming language associated with artificial intelligence and computational linguistics. It works through facts, rules, and queries, using a form of symbolic reasoning known as backward chaining. Prolog is declarative, meaning you describe what you want instead of how to compute it.

Sample Code

The following is a simple Prolog program that prints a greeting:

:- initialization(main).

main :-
    write('Hello, World!').

Syntax Basics

Facts

Facts represent basic assertions about the world.

likes(alice, pizza).
likes(bob, pasta).

Rules

Rules define logical relationships using facts.

friends(X, Y) :- likes(X, Z), likes(Y, Z).

Queries

Queries are used to find information based on facts and rules.

?- likes(alice, What).

Operators

OperatorDescription
:-Rule definition
,Logical AND
;Logical OR
=Unification

Lists

member(X, [X|_]).
member(X, [_|T]) :- member(X, T).

Recursion

Prolog heavily relies on recursion.

factorial(0, 1).
factorial(N, F) :-
  N > 0,
  N1 is N - 1,
  factorial(N1, F1),
  F is N * F1.

This guide provides a quick reference to Prolog programming syntax and features. Start writing Prolog code using OneCompiler’s Prolog online compiler today!