Home > Archive > Prolog > August 2005 > Relations into functions?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Relations into functions?
|
|
| Daniel C. Wang 2005-08-20, 7:57 am |
| Consider the following prolog program.
add_rel(zero,N,N).
add_rel(succ(N1),N2,succ(N3)) :- add_rel(N1,N2,N3).
fib_rel(zero,succ(zero)).
fib_rel(succ(zero),succ(zero)).
fib_rel(succ(succ(N)),FibN) :-
fib_rel(N,T1),
fib_rel(succ(N),T2),
add_rel(T1,T2,FibN).
The fib_rel relation is of course the function.
I'm wondering if there are extensions to prolog that would allow me to
write the above as:
add_rel(zero,N,N).
add_rel(succ(N1),N2,succ(N3)) :- add_rel(N1,N2,N3).
add(N,M) = the X \ add_rel(N,M,X).
fib_rel(zero,succ(zero)).
fib_rel(succ(zero),succ(zero)).
fib_rel(succ(succ(N)),add(T1,T2)) :-
fib_rel(N,T1),
fib_rel(succ(N),T2).
or even better.
add(N,M) = the X \ add_rel(N,M,X).
fib(N) = the X \ fib_rel(N,X).
add(zero,N) = N.
add(succ(N1),N2) = succ(add(N1,N2)).
fib(zero) = succ(zero).
fib(succ(zero)) = succ(zero).
fib(succ(succ(N))) = add(fib(N),fib(succ(N)).
That is provide functional syntax for relational definitions. At the end
of the day this is just syntax, and the underlying system expands out
into pure prolog via an "obvious" translation.
I know there are various functional-logic systems out there but I'm not
familar with any that have this feature. The "the" operators is basicly
a choice operator that shows up in higher-order logics. You could even
insist on only allowing the use of the "the" operator on properly moded
relations that don't back track (aka represent real functions..).
So, is this a new idea, or something that people thought about in the
past but gave up on?
| |
| peter.ludemann@gmail.com 2005-08-20, 9:56 pm |
| IBM-Prolog had the "?" operator to turn a predicate into a function
(not to be with a functor). There were also many other
extensions to standard Prolog, such as a freeze/thaw, declarative SQL
interface, infinite trees, etc. The compiler generated very fast native
code.
IBM stopped selling the product about 10 years ago.
| |
| Oskar Bartenstein 2005-08-21, 2:56 am |
| For arithmetic functions MINERVA offers such a mapping. See
http://www.ifcomputer.co.jp/MINERVA...ic/home_en.html
Oskar Bartenstein oskar@ifcomputer.co.jp http://www.ifcomputer.co.jp
Daniel C. Wang
> I'm wondering if there are extensions to prolog that...
...
> That is provide functional syntax for relational definitions. At the end
> of the day this is just syntax, and the underlying system expands out
> into pure prolog via an "obvious" translation.
| |
| Daniel C. Wang 2005-08-21, 2:56 am |
| Any chance there might be some documentation I could get a hold of
online or at some library?
peter.ludemann@gmail.com wrote:
> IBM-Prolog had the "?" operator to turn a predicate into a function
> (not to be with a functor). There were also many other
> extensions to standard Prolog, such as a freeze/thaw, declarative SQL
> interface, infinite trees, etc. The compiler generated very fast native
> code.
> IBM stopped selling the product about 10 years ago.
>
| |
| Kristof Bastiaensen 2005-08-21, 6:57 pm |
| Hi,
At Sat, 20 Aug 2005 01:45:40 -0700,
Daniel C. Wang wrote:
>
> Consider the following prolog program.
>
> add_rel(zero,N,N).
> add_rel(succ(N1),N2,succ(N3)) :- add_rel(N1,N2,N3).
>
> fib_rel(zero,succ(zero)).
> fib_rel(succ(zero),succ(zero)).
> fib_rel(succ(succ(N)),FibN) :-
> fib_rel(N,T1),
> fib_rel(succ(N),T2),
> add_rel(T1,T2,FibN).
>
> The fib_rel relation is of course the function.
>
> I'm wondering if there are extensions to prolog that would allow me to
> write the above as:
>
> add_rel(zero,N,N).
> add_rel(succ(N1),N2,succ(N3)) :- add_rel(N1,N2,N3).
> add(N,M) = the X \ add_rel(N,M,X).
>
> fib_rel(zero,succ(zero)).
> fib_rel(succ(zero),succ(zero)).
> fib_rel(succ(succ(N)),add(T1,T2)) :-
> fib_rel(N,T1),
> fib_rel(succ(N),T2).
>
I don't know about prolog, but here is how it translates to curry:
data Nat = Zero | Succ Nat
add_rel Zero n1 n2 = n1 =:= n2 -- success if n1 and n2 unify
add_rel (Succ n1) n2 (Succ n3) = add_rel n1 n2 n3
add n m | (add_rel n m x) = x where x free
fib_rel Zero (Succ Zero) = success
fib_rel (Succ Zero) (Succ Zero) = success
fib_rel (Succ (Succ n)) n2 =
add t1 t2 =:= n2 &
fib_rel n t1 &
fib_rel (Succ n) t2 where t1, t2 free
> or even better.
>
> add(N,M) = the X \ add_rel(N,M,X).
> fib(N) = the X \ fib_rel(N,X).
>
> add(zero,N) = N.
> add(succ(N1),N2) = succ(add(N1,N2)).
> fib(zero) = succ(zero).
> fib(succ(zero)) = succ(zero).
> fib(succ(succ(N))) = add(fib(N),fib(succ(N)).
>
This would become:
add n m | add_rel n m x = x where x free
fib n | fib_rel n x = x where x free
add Zero n = n
add (Succ n1) n2 = Succ (add n1 n2)
fib Zero = Succ Zero
fib (Succ Zero) = (Succ Zero)
fib (Succ (Succ n)) = add (fib n) (fib (Succ n))
As you can see, this translates to purely functional code, so it is
just plain haskell!
> That is provide functional syntax for relational definitions. At the end
> of the day this is just syntax, and the underlying system expands out
> into pure prolog via an "obvious" translation.
>
There are curry implementations that work that way.
Regards,
Kristof Bastiaensen
> I know there are various functional-logic systems out there but I'm not
> familar with any that have this feature. The "the" operators is basicly
> a choice operator that shows up in higher-order logics. You could even
> insist on only allowing the use of the "the" operator on properly moded
> relations that don't back track (aka represent real functions..).
>
> So, is this a new idea, or something that people thought about in the
> past but gave up on?
>
>
>
| |
|
|
| Bart Demoen 2005-08-22, 7:02 pm |
| peter.ludemann@gmail.com wrote:
> IBM-Prolog had the "?" operator to turn a predicate into a function
> (not to be with a functor).
BIM had some compatibility agreement with IBM at those times, so we
also had the ?/1 "arithmetically evaluate me" operator - but it worked
only on arithmetic expressions, I mean
h(X) :- a(?(X+1)), ...
would do
h(X) :- Z is X + 1, a(Z), ...
but
h(X) :- a(?(append(X,[1,2,3])), ...
wouldn't do
h(X) :- append(X,[1,2,3],Z), a(Z), ...
[I am pretty sure about this, because I implemented it in the BIM
compiler]
> There were also many other
> extensions to standard Prolog, such as a freeze/thaw, declarative SQL
> interface, infinite trees, etc. The compiler generated very fast native
> code.
Really ? I am very surprised by what you write, because I always thought
(because I was told so) that IBM-Prolog was an extremely fast
interpreter - written in assembly code - and I heard that from Marc
Gillet who was (AFAIK) the main author of the interpreter.
Maybe it is a version/release misunderstanding ?
Just for historical reasons - and for correcting any misunderstandings I
seem to have about IBM-Prolog - I would be grateful if you commented on
this.
Cheers
Bart Demoen
> IBM stopped selling the product about 10 years ago.
>
| |
| Bart Demoen 2005-08-22, 7:02 pm |
| Daniel C. Wang wrote:
> That is provide functional syntax for relational definitions. At the end
> of the day this is just syntax, and the underlying system expands out
> into pure prolog via an "obvious" translation.
[...]
> So, is this a new idea, or something that people thought about in the
> past but gave up on?
Mercury has it, and also Ciao Prolog.
There is an extension/package/lib for SICStus that does some of it.
SWI has a function declaration (arithmetic_function) ...
There was at some point a proposal to have it in ISO Prolog.
Cheers
Bart Demoen
| |
| peter.ludemann@gmail.com 2005-08-22, 7:02 pm |
| Bart Demoen wrote:
[snip]
>
>
> Really ? I am very surprised by what you write, because I always thought
> (because I was told so) that IBM-Prolog was an extremely fast
> interpreter - written in assembly code - and I heard that from Marc
> Gillet who was (AFAIK) the main author of the interpreter.
> Maybe it is a version/release misunderstanding ?
> Just for historical reasons - and for correcting any misunderstandings I
> seem to have about IBM-Prolog - I would be grateful if you commented on
> this.
Well, I worked with Marc Gillet for two years.
The original VM/Prolog had a very fast interpreter.
The later AD/Cycle Prolog added a compiler and many other features
(http://publibz.boulder.ibm.com/cgi-...=19930429045400
http://publibz.boulder.ibm.com/cgi-...726060132&CASE=).
It was targetted at the IBM mainframes (Z/390); there was also a
product for PCs but marketeers decided it should support the 80286
chip, which was a nightmare.
|
|
|
|
|