Slide 14.23: Rules (cont.) Slide 14.25: Rules (cont.) Home |
X
in the first fun
rule below) are constrained to have one and the same instantiation for each solution to a particular query.
Identical variable names in separate rules are totally independent, just as if different variable names had been used.
To give an example:
The program
fun( X ) :- red( X ), car( X ). fun( X ) :- blue( X ), bike( X ).is the same as the program
fun( X_1 ) :- red( X_1 ), car( X_1 ). fun( X_2 ) :- blue( X_2 ), bike( X_2 ).Thus variable name scoping is per-individual rule (often called a clause). The same variable name may appear in different clauses of a rule, or rules with different names. Each time it is treated as something specific to its context. A variable
X
may occur in the program many times with many different bindings.
It will only have the same bindings when you tell it to.
fun( X ) :- red( X ), car( X ). fun( X ) :- blue( X ), bike( X ). car( vw_beatle ). car( ford_escort ). bike( harley_davidson ). red( vw_beatle ). red( ford_escort ). blue( harley_davidson ).Above is both our previous program for finding fun items and facts describing some red and blue items and some cars and bikes.