Prolog Cheat Sheet



  1. Prolog Syntax Cheat Sheet
  2. Prolog Cheat Sheet Pdf
PROLOG (PROgramming in LOGic) - Cheat Sheet
Under construction ...

View XML code cheat sheet.pptx from WEBD 341 at American Public University. Prolog/Declaration Root Element Enclosure Element. Prolog: Logic programming systems allow the programmer to state a collection of axioms from which theorems can be proven. The user of a logic program states a goal, and the language implementation attempts to find a collection of axioms and inference steps that together imply the goal. Axiomatic semantics each statement or expression is a predicate transformer (inference rule with conditions.

Prolog Cheat Sheet

Prolog originated in France [University of Marseilles(Prolog : Manuel de Reference et d'Utilisation by P. Roussel,Groupe d'Intelligence Artificielle, Marseille-Luminy, 1975]as a language for deductively analyzing logical arguments.

Its implementation makes it a relatively simple tool for parsingproblems which can be phrased in terms of relations which havea natural representation as tree structures.Two easily appreciated examples are - genealogy andEnglish grammar.

Prolog Cheat Sheet
  1. Literate Programming for Prolog with L a T e X. A L a T e X cheat sheet, in Japanese. Generate problem sheets and their solution sheets.
  2. Prolog and SICStus Prolog. The document is compiled using the reference manuals of SWI-Prolog 1 and SICStus Prolog 2, 3. The authors have taken the liberty to extract the relevant information and summarize into this single document for clarity and exhaustive presentation of the various tips and caveats. 1 Advanced CHR Programming 1.1 CHR.

It also has a powerful unification mechanism which allows quickprototyping of ``template' or ``pattern matching' systems.

Prolog

Prolog Syntax Cheat Sheet

List prolog

Here are three distinct ways of describing the form and actions of aProlog program. (Other interpretations are also useful.)

Prolog
  • Procedure view
  • Logic view
  • Data view

Like normal imperative programming languages, a Prolog programconsists of procedures, which are collections of statements.A program is executed by running a sequence of statements.Each of these statements executes by 'calling' a procedure, which callsother procedures, etc.

A Prolog program consists of a collection of theorems (or rules)and axioms (its assertions). Running a program consists of askinga query (a set of 'goals') to see if it is provable fromthe axioms by using the existing theorems.

Prolog Cheat Sheet Pdf

  • Assertions aka Unit Clauses

    Examples of some assertions:

  • Theorems
  • Queries

    To determine what can be 'proved' from the given assertions andtheorems, a user issues a query:

  • Nonground Queries (aka goals).

    The above are examples of ground queries, meaning there areno variables in the goal. Variables (anything which begins withan upper case letter) can be used instead.

    Because of its 'pattern matching/unification' mechanismfor binding actual arguments to formal arguments,Prolog provides a quick way to retrieve informationstored in the fact base.

    Consider the facts of before:

    These can be considered simply as either data structures,or access procedures to extract information from thesestructures.

    Unit clauses can be thought of as data structures andprocedures can be thought of as rules for extractingdata which is not hard-wired as a fact.

    Basic Definitions
    TermDefinition
    statementone of fact, rule, or query
    fact (cf.unit clause)record (cf.data model) of assumed true information
    rule (cf.predicate)deductive formulation whereby new facts can be inferred from existing facts
    querya statement asking about the existence of a fact(s)
    logic programcollection of rules
    meaning of a logic programcollection of facts that can be deduced from any set of initial facts

    Basic Lexical Elements
    EntityExamples
    atomhomer 1.5 'plan-9' 'My name is Nobody'
    operators (are also atoms)+ - :- < >
    symbol - atoms are a special case of symbol32
    string (is not a defined data type in PROLOG, but really a list of ASCII values)'Joe Friday' 'Son of Sam'
    variable - identifier beginning with uppercase letter or underscoreX Xs Joes_IQ _total

    Under construction ...

    Basic Syntactic Elements - have meaning according to position
    EntityExplanationExamples
    listseries of terms[Name1, Name2 | Names]

    Basic Control Elements
    Task to AccomplishCommandsComments
    starting PROLOG - at Unix $> prompt$> plOn ICC machines, your path variable must contain /usr/local/bin for this to work. Get help changing your .cshrc file if necessary.
    stopping PROLOG - at | ?- prompt| ?- halt.
    consultation mode from terminalconsult(user)User controls logical and data operation.
    consultation mode from fileconsult(<filename>)User controls logical operation, but data are in file.
    asserting factsassert(<term>)User controls data operation.
    retracting factsretract(<rule-head>)User controls data operation.

    PROLOG - Description Of Common Predicates
    Notation - parameters prefixed by:
    Loading files
    predicateexamples
    consult(+Files)
    reconsult(+Files)
    Control
    predicateexamples
    abortabort
    halthalt.
    Terms
    predicateexamples
    var(?Term)
    nonvar(?Term)
    ?Term1 = ?Term2
    functor(?Term, ?Name, ?Arity)
    ?Term =.. ?List
    length(?List, ?Length)
    name(?Atom, ?Chars)
    Term Comparison
    predicateexamples
    ?Term1 ?Term2
    ?Term1 ?Term2
    Arithmetic
    predicateexamples
    ?Varis+Expression
    Arithmetic Operators
    operator(s) (infix predicate name)meaning
    + - *overloaded floating point and integer operations
    / //floating point and integer division, respectively
    remmodremainder and modulo operations, respectively
    Arithmetic relational Operators
    operator(s) (infix predicate name)meaning
    < =< > >= =:=relationships between two numeric expressions
    Input/Output
    predicateexamples
    read(-Term)
    write(?Term)
    I/O of Sentences extensions necessary for parsing natural language constructs.
    predicatenotes
    read_sentence(-ListOfAtoms) May have to be modified to handle special lexical constraints.
    write_sentence(+ListOfAtoms)
    Clause Database
    predicateexamples
    assert(+Clause)
    asserta(+Clause) note: extra parentheses required for clause with a body. This will be the most usual version for our purposes.
    assertz(+Clause) Add assertion at bottom of search path - will be found last.
    clause(+Head,?Body)
    retract(+Clause) Retract the clause matching Clause
    retractall(+Head) Retract all clauses with head matching Head E.g., retractall(stuff(_)). erases all clauses of predicate stuff/1
    abolish(+Predicates) Abolish all clauses with head matching any in the list Predicates E.g., abolish(stuff/1). erases all clauses of predicate stuff/1 and makes predicate stuff/1 unknown
    Sets and Bags
    predicateexamples
    setof(?Template,+Generator,-Set)
    member(+Term,+List)
    nonmember(+Term,+List)
    make_set(+List1,-List2) Remove duplicates from List1.
    delete(+Term,+List1,-List2) Remove item Term from List1.
    insert(+Term,+List1,-List2) Add item Term to List1.
    subset(+List1,+List2) Is List1 a subset of List2?
    equal(+List1,+List2) Do List1 and List2 have same elements?
    difference(+List1,+List2,-List3) Remove items in List2 from List1 (set difference).
    union(+List1,+List2,-List3) Add items in List2 to List1 (set union).
    append(+List1,+List2,-List3) Add items in List2 to List1 (duplicates are presered).
    nth(+List,+Number,+Term) Get nth item in a list.
    Useful extensions to the built-in predicates. (source code)

    Useful Extensions.(Some Prolog implementations have most of these built-in.)

    Under construction ...

    Note, most of these are built-in to our versionof Prolog and you should NOT re-write them. Test first!