This site provides some course material and tools for teaching Logic Programming, Prolog, CLP, and Computational Logic in general, covering both theory and practice. Different parts of this material come from undergraduate and graduate courses taught by us at different institutions, including the Technical University of Madrid (UPM -- CS / AI Dept.), the IMDEA Software Institute, UT Austin (CS Dept.), and the University of New Mexico (CS and ECE Depts).
- See Some Thoughts on How to Teach Prolog (in the book Prolog - The Next 50 Years) and this video, or
- Teaching Pure LP with Prolog and a Fair Search Rule and these slides/Active Logic Document
These courses have been developed and are taught by members of the CLIP group and other contributors. There is much more information available on the CLIP (Computational Logic, Languages, Implementation, and Parallelism) research group and our Computational Logic Software pages. Please note that these pages are all under permanent construction/change. Suggestions, comments, questions, raves, rants, etc., should be directed by email to <clip@cliplab.org>. You are welcome to use all or part of this course material, we only ask you to please quote the origin. We would also very much appreciate if you let us know that you are using it and also that you send us by email to the address above any error corrections or improvements you may make to this material. Thanks in advance.
Teaching tools
- We use the Ciao Prolog system for teaching these courses, since it allows covering within one system pure logic programming, different search rules, Prolog, CLP, analysis, verification, partial evaluation, tabling, etc.; but most of the examples can also be run on any modern system supporting ISO-Prolog, constraint libraries, etc..
-
We also use other tools that we have developed for
teaching Prolog, LP, CLP, etc., including:
- the Prolog Playground (see, e.g., the LaTeX links to executable code in the pdf slides below), and
-
Active
Logic Documents (ALDs). See, e.g.,
a simple
editable example, the ALD notebooks
embedded in the slides below, or
the ALD
version of the "99 Programs in Prolog"
suite, and
its source
code in markdown.
More details can be found in "Teaching Prolog with Active Logic Documents" (in the book "Prolog - The Next 50 Years") and in this poster.
Slides and Examples: The Basic Sequence
Motivational Introduction:
- Introduction and Motivation.
- [html slides (ALD) w/embedded runnable code and exercise notebooks]
- [pdf slides w/links to runnable code and exercise notebooks]
- [course notes (ALD) w/embedded runnable code and exercise notebooks]
- [code files]
- [accessible html]
- The code correctness problem
- Natural language vs. logic for describing problems
- Using logic to write specifications (running example: generating squares)
- From specification to computation: deduction as program execution
- Which logic? Expressive power vs. decidability: propositional, first-order and higher-order logic, lambda calculus
- SLD-resolution and Logic Programming
- Encoding and running the example in a practical system (Prolog)
- Applications of Logic Programming
- Why (Constraint) Logic Programming and Prolog?
- A (very) brief history of Logic Programming
- Some Textbooks and References.
[slides]
An open access reference book: The Art of Prolog (Open Access from MIT Press).
Pure Logic Programming / Pure Prolog
- A "hands-on" Introduction to Pure Logic Programming (Pure Prolog).
- [html slides (ALD) w/embedded runnable code and exercise notebooks]
- [pdf slides w/links to runnable code and exercise notebooks]
- [course notes (ALD) w/embedded runnable code and exercise notebooks]
- [code files]
- [accessible html]
- Syntax: terms (variables, constants, structures), facts and rules (clauses), predicates, programs, and queries
- Declarative meaning of facts, rules, predicates, and queries
- Running programs in a Prolog system
- Top-down operational meaning of programs
- Unification: definition, algorithm, examples, and its role in execution
- SLD-resolution: a schematic interpreter and the choices made in standard Prolog
- Comparison with imperative and functional languages
- Search: the search tree, depth-first (backtracking) and breadth-first search, control of search
- Modes: running programs in different directions
- Database programming, structured data, and data abstraction; terms as data structures with pointers
- Logic programs and the relational database model; deductive databases, Datalog, and ASP
- Recursive programming and types (including recursive types and defining "not equal")
- Arithmetic with Peano numbers; functional syntax, packages, and directives
- Lists: membership, length, sum, concatenation, and reversal
- Binary trees, polymorphism, and manipulating symbolic expressions
- Graphs, automata, and the Towers of Hanoi
- Learning to compose recursive programs
- Developing Programs with a Prolog System.
[slides] [accessible html]
Additional Features of Prolog and the ISO Standard
- Additional Features of Prolog and the ISO Standard.
- [html slides (ALD) w/embedded runnable code and exercise notebooks]
- [pdf slides w/links to runnable code and exercise notebooks]
- [course notes (ALD) w/embedded runnable code and exercise notebooks]
- [code files]
- [accessible html]
- The ISO Prolog standard; advantages and drawbacks of classical systems
- Programming interface: top level, interpreters, compilers, debuggers, modules
- Syntax and terminology; defining operators
- The execution mechanism of classical Prolog
- Built-in arithmetic and arithmetic programs
- Dynamic checking of basic types
- Structure inspection (functor/3, arg/3, =../2): arrays, subterms; strings and atoms
- Meta-logical predicates: choosing implementations by calling mode; comparing non-ground terms
- Input/output
- The cut: white, green, and red cuts
- Meta-calls and higher order: call/N, maplist, anonymous predicates
- Aggregation predicates: findall/3, bagof/3, setof/3
- Negation as failure and cut-fail
- Dynamic program modification: assert, retract, lemmas
- Meta-interpreters
- Incomplete data structures: difference lists; quick-sort with and without append
- Parsing: with lists, difference lists, and strings; Definite Clause Grammars (DCGs)
- Other issues, typical libraries, and extensions in Ciao
- 99 Programs in Prolog.
[interactive ALD version] [source code in markdown]
Efficiency Issues in Prolog
- Some efficiency Issues in Prolog.
- [html slides (ALD) w/embedded runnable code and exercise notebooks]
- [pdf slides w/links to runnable code and exercise notebooks]
- [course notes (ALD) w/embedded runnable code and exercise notebooks]
- [code files]
- [accessible html]
- What efficiency means (time and memory) and general advice
- Choosing data structures: lists vs. structures, advanced data structures
- Letting unification do the work
- The dynamic database: avoiding global variables, using lemmas
- Determinism: avoiding unneeded alternatives, sort-and-unify
- Search order: failing as early as possible, generate-and-test
- First-argument indexing
- Iteration vs. recursion: tail recursion and last call optimization
- Cuts
- Delaying work (arithmetic, head unification)
- When to apply these techniques: performance vs. readability and maintainability
Constraint Logic Programming
- Constraint Logic Programming (with reals/rationals and finite domains).
- [html slides (ALD) w/embedded runnable code and exercise notebooks]
- [pdf slides w/links to runnable code and exercise notebooks]
- [course notes (ALD) w/embedded runnable code and exercise notebooks]
- [code files]
- [accessible html]
- Constraints and comparison with classic Logic Programming
- Search space reduction: generate-and-test vs. constrain-and-generate
- Constraint systems and CLP(X): constraint domains and CLP programs
- CLP(R): linear and non-linear equations, Fibonacci revisited, mortgage calculation, analog RLC circuits
- The N-queens problem in Prolog and in CLP(R), and their search spaces
- Finite domains: SEND+MORE=MONEY, a project management problem, N-queens
- Other domains: finite trees, word equations (strings), words with arithmetic
- Complex constraints and other primitives
- Implementation issues: satisfiability, backtracking, extensibility; attributed variables and freeze
- Programming tips
- CLP systems: origins and current systems
Additional Topics
Additional CLP topics
- Additional slides on How To Solve Finite Domain Constraints. [slides]
- A longer, 5-day seminar on CLP. [html]
- Concurrent (Constraint) Logic Programming. [slides]
LP and CLP Theory
- Automated Deduction Fundamentals. [slides]
- First Order Logic and Logic Programming Theory. [slides]
- Same, but just the model and fixpoint semantics. [slides]
- CLP: operational, model, and fixpoint semantics. [slides]
Some Ciao Prolog tutorials
- Ciao tutorial at LOPSTR 2018. [slides] [Download, installation, manuals.]
- Some tutorials on the Ciao Preprocessor, CiaoPP. [html] [Download, installation, manuals.]
- The Ciao Programming System (2000). [slides] [Download, installation, manuals.]
- The LPdoc Automatic Documenter (2000). [slides] [Download, installation, manuals.]
- The Ciao Preprocessor, CiaoPP (1999). [slides] [Download, installation, manuals.]
Other topics
- Global Analysis by Abstract Interpretation. [slides]
- Abstract Specialization and its Applications. [slides]
- Parallel Execution of Logic Programs. [slides]
- Intro to Prolog Implementation:
WAM tutorial, by Hassan Ait-Kaci. [slides] [auth version] [older book website]
Manuel Hermenegildo's original 1989 tutorial. [slides pdf] - Distributed/Internet programming using LP/CLP systems. [slides]
- WWW programming using LP/CLP systems. [slides] [Code for examples]
- A very brief intro to Operational and Axiomatic Semantics [slides]
General Note: We used to start with basic theory (resolution, etc..), then Logic Programming, then Prolog, CLP(*), and Concurrent LP. Eventually, we decided to start directly with a quick introduction to Logic Programming and Prolog programming, presenting only afterwards theory, CLP, and Concurrent (C)LP. If you prefer to start with theory, just use the lp_theory slides first and skip some slides (first order languages and unification, mainly) from logic_programming.tex.
(One of the reasons for doing things this way is that our students take a semester of formal logic prior to this course and are therefore quite familiar with, e.g., resolution, but not with logic programming. We feel it is best for student motivation to get as quickly as possible to the practical use of the programming tools.)