Prolog Tutorial Logic Programming Prolog o o o



![Simple Prolog Program o sort. List(X, Y) : permutation(X, Y), sorted(Y). o sorted([]). o Simple Prolog Program o sort. List(X, Y) : permutation(X, Y), sorted(Y). o sorted([]). o](https://slidetodoc.com/presentation_image_h/846925f291883d564d15c562de8b512d/image-4.jpg)













- Slides: 17
Prolog Tutorial Logic Programming
Prolog o o o Logic Programming Language Developed in the 1970 In the context of linguistics and logic Was Popular in the context of artifical intelligence Domains n n Expert Systems (rule base systems) Logic Meta Programming o Used technologies are strongly related to the ones used in deductive databases (Datalog)
Prolog vs imperative languages o Write Factbase + rules o Write statements o No side effects (Pure prolog) o Declarative (“What”) o Side Effects o Imparative (“How”) o Once bound, variables o Values assigned to a variable may change are fixed to one value
Simple Prolog Program o sort. List(X, Y) : permutation(X, Y), sorted(Y). o sorted([]). o sorted([_]). o sorted([H|[M|T]]) : H =< M, sorted([M|T]).
Variables o Syntax: Varname or _varname or _ Semantics: n first and second syntax are equal except there is no singleton check for the second one n last variable is anonymous o not bound to a value o not unified with another occurance n Once bound, variables are fixed to one value
Facts o o o Always true Independent from other facts/rules May contain variables Syntax: predicate(arg, Arg 2). Semantics: predicate/2 returns true, if there is a match for the 1 st argument (arg).
Rules Represent implications May depend on other rules/facts May contain variables Syntax: predicate(arg, Arg) : p 2(Arg, H), p 3(H). o Syntax: natural(Arg) : Arg > 0. o Syntax: natural. Even(Arg) : natural(Arg), 0 is Arg mod 2. o o o sumall([], 0). sumall([Head|Tail], Sum): sumall(Tail, Tail. Sum), Sum is Tail. Sum + Head.
Unification o Syntactic equivalence of terms o Variables are bound one by one, based on the defined rules/predicates o Backtracking will resolve all bindings o Bindings are fixed, until they are removed in the backtracking process
Bindings: Example o Facts: n woman(petra). n woman(jessica). o Query: woman(X). o X will be bound: X = petra. o Backtracking will find the next solution: X = jessica
Bindings: Example (cont). o Bindings may be faulty o In this case: remaining conditions will not lead to a solution o Implication: Take back the faulty binding, search new for a new binding (backtracking)
Bindings: Example (cont). o Facts: n woman(petra). n woman(jessica). o Query: woman(X), X = petra. o o X will be bound: X = petra Check: petra = petra (unequal) Fail, look for new binding: X = jessica … true
Search Tree o All alternative bindings n Including bindings that do not lead to a solution o Backtracking: Deep search on the search tree to find solution(s)
Debugging o Important tool: trace o Enabled by the query “trace. ” o Shows stepwise the decisions made in the backtracking alg. o <enter> will show the next step, a aborts the query, h (help) will show you all other command
Important Commands o listing(p). Shows the listing for the predicate p. o spy(p/2). Will set a breakpoint on the predicate p with two (2) arguments. This will enable the tracing mode when the predicate is reached in the search tree
assert und retract o assert and retract change the fact base n NOT a pure logic language o assert(X). adds X to the fact base o retract(X). remove X o dynamic fact base => “self-modifying code”?
Cuts, Negation-as-failure o Cut: Freeze the current binding, “cut” the search tree o Done with “!”-operator o alternative negation implementation n neg(A) : - A, !, fail. n will NOT backtrack for another binding of bound variables in neg.
Arithmetic Unification does not apply arithmetic operators, evaluated syntactically X = Y + 1 binds X to the expression “Y+1” But: is-operator enables evaluation of arithmetic expressions o Problem: X is Y is only allowed for a bound variable Y o o