From: ··········@bbs.ee.ncu.edu.tw
Subject: Treat program as data
Date: 
Message-ID: <1146913365.650843.33540@g10g2000cwb.googlegroups.com>
Is it possible for prolog to treat program as data .
It is very easy to treat program as data in lisp,
but how about in prolog ?

I ask this question for the following situation :
supose we have the following database :

db_txt('aaa').
db_txt('bbb').
db_txt('ccc').

if we want to destructively show all the contents of this database ,
we can use the following codes:

show_db:- repeat,
                db_txt(A),
                retract(db_txt(A)),
                write(';'),write(A),
                not(db_txt(A)).

But if we have another database :
db_txt_another('aaaa')
db_txt_another('bbbb')
db_txt_another('cccc')
db_txt_another('dddd')

and we want to show the content , is it necessary to
write the codes again ?

A better way is to pass the name of the database to the
show_db predicate. Thus show_db(db_txt) can show the
contents of db_txt(_), and the show_db(db_txt_another)
can show the contents of db_txt_another .

This idea can be easily done with lisp , but in prolog , is it
possible?

In lisp, "quote" can make program into data and "funcall" can make
data into program . Is there anything like this in prolog ?

The followings are  my humble opinion : if in prolog , there is a
procedure
called "eval" , thus
eval([funcName, arg1,arg2,...argN]) does the same thing as
funcName(arg1,arg2,...,argN).

and there is a procedure called "quote" , thus in
quote(funcName(arg1,,arg2,...argN), L), L is unified into
[funcName,arg1,arg2,...argN],

then everything will be settled.

The problem now is " Are there such  procedures ?"

Thank you , if somebody can show me the way .

From: Cesar Rabak
Subject: Re: Treat program as data
Date: 
Message-ID: <e3iap3$hh4$1@emma.aioe.org>
··········@bbs.ee.ncu.edu.tw escreveu:
> Is it possible for prolog to treat program as data .

Yes. See, for example, Clocksin and Mellish section 7.13.


> It is very easy to treat program as data in lisp,
> but how about in prolog ?

While it is good to have references on other languages, it is more 
productive to attempt to keep the mindset on the language paradigm 
you're working with, otherwise you may find yourself instead of 
mastering, Prolog in this case, attempting to augment it to make it 
similar to the other language(s) you start to miss features.

[snipped]
> 
> Thank you , if somebody can show me the way .

Look in your Prolog material for the predicate clause/2 and in the 
compiler documentation for related predicates (some may be non ISO though).

--
Cesar Rabak
From: ··········@bbs.ee.ncu.edu.tw
Subject: Re: Treat program as data
Date: 
Message-ID: <1146932618.772372.81760@e56g2000cwe.googlegroups.com>
Cesar Rabak wrote:
> Look in your Prolog material for the predicate clause/2 and in the
> compiler documentation for related predicates (some may be non ISO though).

Thank you for telling me that.
In the GNU prolog's manual , I found
call_with_args(+atom, +term,..., +term) , and
functor(+nonvar, ?atomic, ?integer) .
I think these are what I need.

Thank you again. 
> 
> --
> Cesar Rabak
From: Mauro Di Nuzzo
Subject: Re: Treat program as data
Date: 
Message-ID: <U9k7g.62$W24.33@nntpserver.swip.net>
The is another way based on prolog =.. operator. That is:

show_db(N) :-    % suppose N = db_txt
    P =.. [N, A],    % now P = db_txt(A)
    ...

Maybe one wants to be more general, so you can use functor/3, as you said:

show_db(N/J) :-    % you can call show_db(mydb_txt/5)
    functor(P, N, J),    % now P = mydb_txt(A, B, C, D, E)
    ...

Cheers - /\/\


<··········@bbs.ee.ncu.edu.tw> ha scritto nel messaggio
····························@g10g2000cwb.googlegroups.com...
> Is it possible for prolog to treat program as data .
> It is very easy to treat program as data in lisp,
> but how about in prolog ?
>
> I ask this question for the following situation :
> supose we have the following database :
>
> db_txt('aaa').
> db_txt('bbb').
> db_txt('ccc').
>
> if we want to destructively show all the contents of this database ,
> we can use the following codes:
>
> show_db:- repeat,
>                 db_txt(A),
>                 retract(db_txt(A)),
>                 write(';'),write(A),
>                 not(db_txt(A)).
>
> But if we have another database :
> db_txt_another('aaaa')
> db_txt_another('bbbb')
> db_txt_another('cccc')
> db_txt_another('dddd')
>
> and we want to show the content , is it necessary to
> write the codes again ?
>
> A better way is to pass the name of the database to the
> show_db predicate. Thus show_db(db_txt) can show the
> contents of db_txt(_), and the show_db(db_txt_another)
> can show the contents of db_txt_another .
>
> This idea can be easily done with lisp , but in prolog , is it
> possible?
>
> In lisp, "quote" can make program into data and "funcall" can make
> data into program . Is there anything like this in prolog ?
>
> The followings are  my humble opinion : if in prolog , there is a
> procedure
> called "eval" , thus
> eval([funcName, arg1,arg2,...argN]) does the same thing as
> funcName(arg1,arg2,...,argN).
>
> and there is a procedure called "quote" , thus in
> quote(funcName(arg1,,arg2,...argN), L), L is unified into
> [funcName,arg1,arg2,...argN],
>
> then everything will be settled.
>
> The problem now is " Are there such  procedures ?"
>
> Thank you , if somebody can show me the way .
>
From: ··········@bbs.ee.ncu.edu.tw
Subject: Re: Treat program as data
Date: 
Message-ID: <1147071976.091517.135900@u72g2000cwu.googlegroups.com>
Mauro Di Nuzzo wrote:

> The is another way based on prolog =.. operator. That is:
>
> show_db(N) :-    % suppose N = db_txt
>     P =.. [N, A],    % now P = db_txt(A)
>     ...
>
> Maybe one wants to be more general, so you can use functor/3, as you said:
>

Mauro , thank you for telling me the operator (=../2).
I think your way (=../2) is more general than my way ,functor/3, and
more
convenient than mine after I tried (=../2) in gprolog.

> show_db(N/J) :-    % you can call show_db(mydb_txt/5)
>     functor(P, N, J),    % now P = mydb_txt(A, B, C, D, E)
>     ...
> 
> Cheers - /\/\
>
From: ··········@bbs.ee.ncu.edu.tw
Subject: Re: Treat program as data
Date: 
Message-ID: <1147076639.353736.228640@j33g2000cwa.googlegroups.com>
I tried (=../) more , and find that it is really ideal for
metaprogramming.
It can be applied not only to predicates , but also clause, list , etc.
See the following :
----------------------------------------------------------------------------------------------------
GNU Prolog 1.2.16
By Daniel Diaz
Copyright (C) 1999-2002 Daniel Diaz
| ?- (this(is):-an,example)=..A.

A = [:-,this(is),(an,example)]

yes
| ?- (this(is):-an,example)=..A,


Prolog interruption (h for help) ? a
execution aborted
| ?- (this(is):-an,example)=..[_,H,T],
     H=..B1,T=..B2.

B1 = [this,is]
B2 = [',',an,example]
H = this(is)
T = an,example

yes
| ?- [another,example]=..C.

C = ['.',another,[example]]

yes
| ?-
---------------------------------------------------------------------------
I think with operator of this kind , prolog can be as
powerful as lisp in metaprogramming .
From: Joachim Schimpf
Subject: Re: Treat program as data
Date: 
Message-ID: <KtF7g.11313$gK2.3504@newsfe6-gui.ntli.net>
··········@bbs.ee.ncu.edu.tw wrote:
> I tried (=../) more , and find that it is really ideal for
> metaprogramming.

=../2 has absolutely nothing to do with metaprogramming.
It simply converts one data structure into another
(list to structure, or structure to list).


> In lisp, "quote" can make program into data and "funcall" can make
> data into program . Is there anything like this in prolog ?

Prolog doesn't need QUOTE because everything is quoted by default.
Take for example sqrt(2), which is just a data structure with one
argument.  You have to explicitly pass it to a predicate like is/2
if you want to evaluate it as an arithmetic expression.

The equivalent to FUNCALL is call/1, which takes a data structure
and interprets it as a goal to be proven.

-- Joachim