Skip to content

Naereen/Tiny-Prolog-in-OCaml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A tiny implementation of a small subset of the Prolog language, in OCaml. With small and fun examples.

WARNING: this project only has an educational purpose, for a real-world use of Prolog, please refer to GNU Prolog (gprolog).

This project contains the code and examples for a tiny Prolog implementation, written in the OCaml language.

  • See this folder for instructions to build the project (really easy: clone, run make, relax and enjoy 🎉). The implementation focuses on a very small subset of Prolog, see the examples for a good idea of what is supported.
  • See this other folder for examples (in English).

Example

  • A theory has this form, first with axioms (predicate):
cat(tom).
mouse(jerry).
fast(X) <-- mouse(X).
stupid(X) <-- cat(X).
  • If you save this file (see this example), you can then load it with the prolog binary:
$ ./prolog/prolog ./examples/tomandjerry.pl
?- stupid(tom).
  { }
continue ? (o/n) [o] :
  • This { } is an empty model, meaning that stupid(tom). evaluates to True in an empty model (no need for instanciation).
  • You can also ask your question directly in the command line:
$ ./prolog/prolog ./examples/tomandjerry.pl "fast(tom)."
?- fast(tom).
  • An empty response mean that the term is false, no matter the model.

  • You can add more rules, as you want.

ishuntedby(X, Y) <-- mouse(X), cat(Y).
$ ./prolog/prolog ./examples/tomandjerry.pl "ishuntedby(tom, jerry)."
?- ishuntedby(tom, jerry).
$ ./prolog/prolog ./examples/tomandjerry.pl "ishuntedby(jerry, tom)."
?- ishuntedby(jerry, tom).
  { }
  • You can also add comments, that are lines starting with one or more # character.
# the mouse always espace the cat!
istrickedby(X, Y) <-- cat(X), mouse(Y).

More example


📜 License ? GitHub license

This (small) repository is published under the terms of the MIT license (file LICENSE). © Lilian Besson, 2018.

Maintenance Ask Me Anything ! Analytics

made-with-OCaml made-for-teaching

ForTheBadge built-with-science ForTheBadge uses-badges ForTheBadge uses-git

About

A tiny implementation of a small subset of the Prolog language, 🐫 in OCaml. With small and fun examples.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published