Mastering Unification and Pattern Matching Queries in Prolog Programming

21 Oct 2023 Balmiki Mandal 0 Prolog Programming language

Unification and Pattern Matching in Prolog Programming

Unification and pattern matching are two of the most fundamental concepts in Prolog programming. Unification is the process of making two terms identical by binding variables to values. Pattern matching is the process of comparing two terms to see if they match, given a set of variable bindings.

Unification

Unification is a recursive process that works by matching the corresponding subterms of two terms. If the two subterms are variables, then the variables are unified. If the two subterms are constants, then they must be identical in order for the unification to succeed. If the two subterms are compound terms, then they must have the same predicate name and their corresponding subterms must all unify.

For example, the following unification succeeds:

unify(color(X, Y), color(rose, red)).

This unification binds the variable X to the atom rose and the variable Y to the atom red.

The following unification fails:

unify(color(X, X), color(rose, red)).

This unification fails because the variable X cannot be bound to both the atom rose and the atom red at the same time.

Pattern Matching

Pattern matching is the process of comparing two terms to see if they match, given a set of variable bindings. Pattern matching is used in Prolog to match goals with the heads of clauses.

For example, the following pattern match succeeds:

pattern_match(color(X, Y), color(rose, red)).

This pattern match succeeds because the two terms have the same predicate name and their corresponding subterms all unify.

The following pattern match fails:

pattern_match(color(X, X), color(rose, red)).

This pattern match fails because the variable X cannot be bound to both the atom rose and the atom red at the same time.

Example

The following Prolog program uses unification and pattern matching to find the names of all the people who are parents of a child named mary:

Prolog
parent(mary, john).
parent(mary, jane).

parents(Child, [Parent1, Parent2]) :-
  parent(Child, Parent1),
  parent(Child, Parent2).

To use the program, we would simply type the following goal at the Prolog prompt:

?- parents(mary, Parents).

The Prolog interpreter would then unify the goal parents(mary, Parents) with the head of the clause parents(Child, [Parent1, Parent2]). This would bind the variable Child to the atom mary and the variable Parents to the list [john, jane].

The Prolog interpreter would then succeed in proving the goal, and the answer Parents = [john, jane] would be printed to the screen.

Conclusion

Unification and pattern matching are two of the most powerful features of Prolog. They allow Prolog programmers to write concise and expressive programs. Unification is also used in many other programming languages, such as Haskell and Mercury

Enroll Now: 

Prolog Programming language Scratch to advance 2023 - 2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.