Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting to dot notation in prolog

I have to represent [[fruits],[ ] ] in dot notation form in Prolog, and below is how I did it ,but something tells me this is wrong because I have expanded [[ ]] too, is this incorrect ?

.([fruits],[[ ]] )

.(.(fruits, []),.([],[])
like image 449
Priceless Avatar asked May 22 '17 11:05

Priceless


2 Answers

In addition to what Willem wrote, you can always use write_canonical/1 to obtain the canonical representation of any term.

For example, in your case:

| ?- write_canonical([[fruits],[ ] ]).
'.'('.'(fruits,[]),'.'([],[]))

This solves the task, and shows that you have expanded the list [[]] correctly.

In particular, we have:

| ?- write_canonical([[]]).
'.'([],[])

That's right: This is a list with a single element, which is [] as indicated by the first argument of the '.'/2 term. Since it is the only element, the second argument is also [].

like image 57
mat Avatar answered Oct 11 '22 21:10

mat


Well, the ./2 is what is in Lisp known as the cons. It contains two parameters: a head the element, and a tail. The tail can be the empty list [], or another cons.

Let us first look at the term we have to convert:

X = [ [fruits] , [] ]

What we see is an outer list with two elements (we will ignore these elements for now). So that means that we have a structure like:

X = .( Item1, .( Item2, []) ).

Now of course we still need to fill in Item1 and Item2. Item2 is not hard: it is the empty list [] so:

Item2 = [].

Item1 on the other hand is a list with one element, so the structure is:

Item1 = .( Item11, [] ).

With Item11 the item of that sublist. That item is fruits, so that means that:

Item11 = fruits.

If we put these all together, we obtain:

X = .( .(fruits,[]), .([],[]) ).

If we enter this in GNU-Prolog (gprolog), we get:

$ gprolog
GNU Prolog 1.4.5 (64 bits)
Compiled Feb  5 2017, 10:30:08 with gcc
By Daniel Diaz
Copyright (C) 1999-2016 Daniel Diaz
| ?- X = .( .(fruits,[]), .([],[]) ).

X = [[fruits],[]]

yes

gprolog thus can be a tool to do verification, since it will convert the dot notation into the syntactical sugar for a list.

like image 22
Willem Van Onsem Avatar answered Oct 11 '22 22:10

Willem Van Onsem