Docsity

Lecture Notes on Prolog: A Logic-Based Programming Language for CPSC 326 (Spring 2013), Study notes of Programming Languages

Partial preview of the text

Download Lecture Notes on Prolog: A Logic-Based Programming Language for CPSC 326 (Spring 2013) and more Study notes Programming Languages in PDF only on Docsity!

Today ...

  • Quiz 6
  • Exam 2 overview
  • Prolog intro

Exam 2

Basics:

  • about 5–6 multipart questions
  • closed book, notes, computer, etc.
  • worth about 6.67% of final grade

Probable Topics:

  • Haskell Pattern matching
  • Haskell Guards
  • Haskell User-Defined Data Types (and w/ type parameters)
  • Haskell Higher-Order Functions (filter, zipWith, foldl, foldr)
  • Expect more recursion, function type inference, etc.
  • Basic Prolog from today
  • Reading (scope, environments, arrays, types, etc.)

Some features of Prolog

  • Extremely simple syntax
  • Declarative (what not how)
    • Haskell leans declarative as well
  • A very different way to think about programming
    • Use “rules” or “constraints”
    • Hard to pick up at first ...
  • Efficient at certain problems
    • Prolog runtime is fairly small ...
    • Prolog itself is generally fast (wasn’t always)
  • No data types!

Prolog programming today

Not widely used today to build applications ...

  • However, many langauges that are used are “prolog” like
    • SQL (databases) ... and Datalog
    • Erlang (highly concurrent/parallel computing)

SWI Prolog

We’ll be using SWI Prolog ...

  • http://www.swi-prolog.org
  • freely available, open-source implementation
  • supports most operating systems (mac, linux, windows)
  • available on ada (run swipl at the command prompt)
  • lots of documentation online

Like with Haskell

  • you are encouraged to download, install, and run on your own machine
  • if you need help, come see me

Starting SWI prolog on ada

$ swipl Welcome to SWI-Prolog ...

For help, use ?- help(Topic) ...

?-

  • The ?- symbol is the query prompt

Examples of atoms

x washington a123BC_d

  • note these are different than in most other languages!!!
  • these are not variables!

’Washington’ ’this is a long atom with spaces’ ’\one back slash’ ’this statement isn’’t true’

  • the last one prints “isn’t”
  • ’isn\’t’ does the same thing

Variables

Variables begin with either ...

  • uppercase characters
  • an underscore (don’t care / wildcard)

Variables can contain letters, digits, underscores ...

X A _ My_Var Xs

Facts and Rules

A knowledge base (KB) consists of facts and rules

  • all statements end in periods ... /* facts */ mother(necessity, invention). mother(june, wally). father(experience, wisdom). father(ward, wally). human(june). human(wally). human(ward).

/* rules */ parent(X, Y) :- mother(X, Y). % parent if mother parent(X, Y) :- father(X, Y). % parent if father human_parent(X, Y) :- parent(X, Y), human(X). % "and"

  • :- is an infix functor read “if”
  • e.g., “X is the parent of Y if X is the mother of Y”
  • and “X is a human parent of Y if X is the parent of Y and X is human”

Load these into swi by “consulting” the file:

  • e.g.: ?- consult(‘the file.pl’).
  • or: ?- [‘the file.pl’].
  • or: ?- [the file].
  • To exit swi: ?- halt.

?- consult(‘family.pl’). % family compiled 0.00 sec, 1,816 bytes

?- [family] % alternative load % family compiled 0.00 sec, 1,816 bytes

?- parent(X, Y). X = necessity, Y = invention ; X = june, Y = wally ; ... etc ....

  • the ; tells prolog to search for more results
  • you can hit enter to stop searching

A common warning

  • SWI generally assumes your clauses are grouped together
  • For example:

mother(a, b). mother(a, c). father(d, b).

  • instead of:

mother(a, b). father(d, b). mother(a, c).

A fact is an assertion

mother(necessity, invention).

  • we’re asserting “necessity is the mother of invention” as true
  • if no variables, a “ground clause”

A rule can create new facts from existing facts

parent(X, Y) :- mother(X, Y). human_parent(X, Y) :- parent(X, Y), human(X).

  • The left-side of :- is called the rule Head
    • can contain only one relation
  • The right-side of :- is called the rule Body
    • can contain multiple relations conjoined (ANDed) together
  • A rule is also a clause, but with a head and body

Prolog is agnostic about inputs and outputs

?- parent(X, invention). % X is an "output" X = necessity.

?- parent(necessity, Y). % Y is an "output" Y = invention.

?- parent(X, Y). % X and Y are "outputs" X = invention, Y = wisdom.

  • Some rules require certain “binding patterns” ... more later