100 Helloslanguages
Home / Languages / Prolog

Prolog

1972fraglet
historicallogicdeclarative.pl.prolog
docker run --rm --platform="linux/amd64" 100hellos/prolog:latest

MCP + fragletc

MCPstdinargs
This language supports code execution via MCP and the fragletc CLI. Stdin piping and argument passing are both supported.
Install fragletc →

Prolog is a historical logic and declarative language first appearing in 1972.

Hello World

% BEGIN_FRAGLET
:- write("Hello World!"), nl, halt.
% END_FRAGLET

Coding Guide

Language Version

SWI-Prolog

Execution Model

  • Logic programming, runs goals/queries
  • Each statement executes immediately as a goal (like typing at the Prolog prompt)
  • Output goes to stdout
  • Runs in quiet mode

Key Characteristics

  • Statements must end with a period (.)
  • Case-sensitive: Variables start with uppercase, atoms/constants with lowercase
  • Predicates: predicate_name(arg1, arg2).
  • Rules: head :- body. (head is true if body is true)
  • Queries: ?- goal. (asks Prolog to find solutions)

Fragment Authoring

Write valid Prolog goals or statements. Your fragment becomes the script body. Code runs as if typed at the Prolog toplevel. Each statement executes as a goal.

Working with State

  • Use assertz/1 (or asserta/1) to add facts and rules before querying them
  • Remove facts with retract/1 or clear an entire predicate with retractall/1
  • Finish with halt. to exit explicitly once your goals are complete

Common Patterns

  • Output: write("Hello World!").
  • Facts: assertz(parent(john, mary)).
  • Rules: assertz((grandparent(X, Y) :- parent(X, Z), parent(Z, Y))).
  • Queries: parent(john, mary).
  • Lists: member(X, [X|_]).
  • Arithmetic: X is 5 + 3.

Examples

% Simple output
write("Hello, World!"), nl.

% Define facts
assertz(likes(alice, chocolate)).
assertz(likes(bob, ice_cream)).

% Query
likes(alice, What), write(What), nl.

% List processing
member(X, [1, 2, 3, 4, 5]), X > 3, write(X), nl.

Caveats

  • Prolog will stay in the toplevel after execution unless you call halt.
  • State persists during execution, so assertz facts remain available
  • Each run starts fresh—no state persists between runs

Fraglet Scripts

Echo Args

#!/usr/bin/env -S fragletc --vein=prolog
:- current_prolog_flag(argv, Args),
   atomic_list_concat(Args, ' ', Joined),
   format("Args: ~w~n", [Joined]).

Facts

#!/usr/bin/env -S fragletc --vein=prolog
assertz(likes(alice, chocolate)).
assertz(likes(bob, ice_cream)).
likes(alice, What), write("Alice likes: "), write(What), nl.
halt.

Stdin Upper

#!/usr/bin/env -S fragletc --vein=prolog
:- read_line_to_string(user_input, Line),
   upcase_atom(Line, Upper),
   write(Upper), nl.

Test

#!/usr/bin/env -S fragletc --vein=prolog
:- write("Hello World!"), nl, halt.

Connections

influences

Container Info

image100hellos/prolog:latest
build scheduleTuesday
fragletenabled