Search code examples
prologdcg

Semantic knowledge representation predicate in Prolog


I am studying DCG grammars and parse trees in Prolog using Ivan Bratko's Programming for Artificial Intelligence. In a program that uses a DCG grammar to extrapolate the meaning of a sentence, I find these two predicates that, I think, represent a kind of semantic knowledge:

properName(john) --> [john].
properName(mary) --> [mary].

How should I read these predicates? I am thinking that they mean: it is true that an element of a list represented by the string "john" is a proper name and this proper name is John (same thing for Mary).

Is it my reading correct or are there some other implications?


Solution

  • properName(X) is just an unary rule (in the context of DCG; it is a ternary predicate in Prolog - check it out with ?- listing(properName) ). you could've called it "socks", or "jam", it's totally up to you. So the semantic knowledge about it representing proper name "john" or "mary" is nowhere to be found in the code (it uses naming as self-documenting feature, but documentation is not code).

    The predicate allows for an atom john or mary to be present in the input stream, and nothing else; and demands that X unified with that atom.

    You could've defined it thus:

    name(X) --> [X], { member(X, [john, mary]) }.
    

    then,

    4 ?- phrase( name(X), [john,jack], Z).
    X = john,
    Z = [jack] ;
    false.
    
    5 ?- phrase( name(X), [jack,john], Z).
    false.
    
    8 ?- phrase( name(X), [john,mary], Z).
    X = john,
    Z = [mary] ;
    false.
    
    9 ?- phrase( name(X), [mary,john,jack], Z).
    X = mary,
    Z = [john, jack].
    
    11 ?- phrase( name(jack), [jack,mary,john], Z).
    false.