Search code examples
prologpredicates

Prolog dict predicate matching


Given this program, why am I forced to define every atom in the predicate, even if they're anonymous. Why is it that undefined variables in a dict predicate aren't thought of as anonymous?

funt2(X) :-
    X = point{x:5, y:6}.

evalfunt(point{x:5, y : 6}) :-
    write('hello world!').

evalfunt(point{x:_, y : _} ) :-
    write('GoodBye world!').

Why can't I just say

evalfunt(point{x:5}) :-
        write('GoodBye world!').

^that won't match, by the way.

I may as well just use a structure if I have to define every possible value in the dict to use dicts.

What's the motivation here? Can I do something to make my predicate terse? I'm trying to define a dict with 30 variables and this is a huge roadblock. It's going to increase my program size by a magnitude if I'm forced to define each variables (anonymous or not).


Solution

  • I was able to accomplish what I needed by doing the following

    checkiffive(Y) :- 
            get_dict(x, Y, V), V=5.
    

    You need to use the built in methods for unifying values from a dict.

    Described in chapter 5.4 of the SWI prolog reference

    http://www.swi-prolog.org/download/devel/doc/SWI-Prolog-7.1.16.pdf