KIPA PROLOG samenvatting
PROLOG 1
Facts and rules
- A Prolog program consists of facts and rules
o color(chardonnay, white). % fact
o tannines(X, low) :- color(X, white). % rule
- Arguments can be complex
o date(day(15), month(march, num(3)), year(2023))
Variables
- Arguments can be a variable
- Variable is assigned a value through unification
o If the variable is used again after unification then the value is fixed
- Special variable _ matches always, no value assigned
Transform knowledge to prolog code
- Domain rule with or: two rules in Prolog
o If it rains or if you swim then you get wet
wet(_) :- rains.
wet(X) :- swim(X).
- Domain rule with and: one rule with two conditions
o If it rains and you are outside then you get wet
wet(X) :- rains, outside(X).
Query algorithm
, - Find the first fact or rule with the same name and arity as the query
- Check if arguments unify
- If yes:
o Assign values to variables
o For rules: create new query for each condition
- If no: try the next fact or rule
Unification
- A query and a fact or rule unify
o if Name is the same
o Number of arguments is the same
o Value of arguments is the same
o or: arguments are uninstantiated (free) variables
Mathematical operators
- Prolog uses standard operators + - * /
- Test for equality: =/2
o Note: same syntax and same value
- Assignment: is/2
o Math expression only on the right
Syntax of arguments and variables
- Constants: lower case, no spaces or punctuation
PROLOG 1
Facts and rules
- A Prolog program consists of facts and rules
o color(chardonnay, white). % fact
o tannines(X, low) :- color(X, white). % rule
- Arguments can be complex
o date(day(15), month(march, num(3)), year(2023))
Variables
- Arguments can be a variable
- Variable is assigned a value through unification
o If the variable is used again after unification then the value is fixed
- Special variable _ matches always, no value assigned
Transform knowledge to prolog code
- Domain rule with or: two rules in Prolog
o If it rains or if you swim then you get wet
wet(_) :- rains.
wet(X) :- swim(X).
- Domain rule with and: one rule with two conditions
o If it rains and you are outside then you get wet
wet(X) :- rains, outside(X).
Query algorithm
, - Find the first fact or rule with the same name and arity as the query
- Check if arguments unify
- If yes:
o Assign values to variables
o For rules: create new query for each condition
- If no: try the next fact or rule
Unification
- A query and a fact or rule unify
o if Name is the same
o Number of arguments is the same
o Value of arguments is the same
o or: arguments are uninstantiated (free) variables
Mathematical operators
- Prolog uses standard operators + - * /
- Test for equality: =/2
o Note: same syntax and same value
- Assignment: is/2
o Math expression only on the right
Syntax of arguments and variables
- Constants: lower case, no spaces or punctuation