1. Home
  2. /
  3. Prolog
  4. /
  5. Counter Simulation

Counter Simulation - Prolog Typing CST Test

Loading…

Counter Simulation — Prolog Code

Demonstrates a simple counter simulation with theme toggle using Prolog facts and rules.

:- dynamic(count/1).
:- dynamic(isDark/1).

count(0).
isDark(false).

updateUI :-
	count(C),
	isDark(D),
	write('Counter: '), write(C), nl,
	write('Theme: '), (D = true -> write('Dark') ; write('Light')), nl.

increment :-
	count(C),
	C1 is C + 1,
	retract(count(C)),
	assert(count(C1)),
	updateUI.

decrement :-
	count(C),
	C1 is C - 1,
	retract(count(C)),
	assert(count(C1)),
	updateUI.

toggleTheme :-
	isDark(D),
	D1 is (D = false -> true ; false),
	retract(isDark(D)),
	assert(isDark(D1)),
	updateUI.

reset :-
	retract(count(_)),
	assert(count(0)),
	updateUI.

% Simulate actions
updateUI,
increment,
increment,
toggleTheme,
decrement,
reset.

Prolog Language Guide

Prolog (Programming in Logic) is a high-level, declarative programming language focused on logic programming and symbolic reasoning. It is widely used in artificial intelligence, natural language processing, and rule-based systems, enabling developers to express knowledge and relationships rather than step-by-step instructions.

Primary Use Cases

  • ▸Expert systems and rule-based AI
  • ▸Natural language processing
  • ▸Automated theorem proving
  • ▸Knowledge representation and reasoning
  • ▸Constraint logic programming

Notable Features

  • ▸Declarative syntax: describe what to solve, not how
  • ▸Built-in backtracking search
  • ▸Unification for pattern matching
  • ▸Logical inference engine
  • ▸Support for recursion and symbolic computation

Origin & Creator

Developed in 1972 by Alain Colmerauer and Robert Kowalski at the University of Marseille.

Industrial Note

Prolog underpins many AI applications, including expert systems, natural language understanding, and theorem proving frameworks.

More Prolog Typing Exercises

Prolog Simple AdditionProlog FactorialProlog Fibonacci SequenceProlog Max of Two NumbersProlog List SumProlog Even Numbers FilterProlog Conditional Counter IncrementProlog Resettable CounterProlog Theme Toggle Only

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher