Serializing Prolog terms

View: New views
2 Messages — Rating Filter:   Alert me  

Serializing Prolog terms

by Kaarel Kaljurand :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I would like to serialize Prolog terms so that they could be read back by
atom_to_term/3 (with no idea about my custom operator declarations),
but also so that the result would not be too verbose,
e.g. I'd like to have nice variable names (A, B, ...), and built-in Prolog
operators (e.g. for lists) should not be forced into prefix notation.

What I have now and what seems to have the required properties is:

serialize_term(Stream, Term) :-
        copy_term(Term, TermCopy),
        numbervars(TermCopy, 0, _),
        op(0, fy, -),
        op(0, fy, ~),
        op(0, fy, <>),
        op(0, fy, []),
        op(0, xfx, =>),
        op(0, xfx, v),
        op(0, xfx, &),
        write_term(Stream, TermCopy, [numbervars(true), quoted(true)]).

This however looks kind of stupid and is maybe slow.
Is there a better way to do this in SWI-Prolog?
(I'm currently using version 5.6.46, if that should matter.)

--
kaarel

------------
For further info, please visit http://www.swi-prolog.org/

To unsubscribe, send a plaintext mail with "unsubscribe prolog <e-mail>"
in its body to majordomo@...

Re: Serializing Prolog terms

by Jan Wielemaker :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wednesday 23 April 2008 19:34:34 Kaarel Kaljurand wrote:

> Hi,
>
> I would like to serialize Prolog terms so that they could be read back by
> atom_to_term/3 (with no idea about my custom operator declarations),
> but also so that the result would not be too verbose,
> e.g. I'd like to have nice variable names (A, B, ...), and built-in Prolog
> operators (e.g. for lists) should not be forced into prefix notation.
>
> What I have now and what seems to have the required properties is:
>
> serialize_term(Stream, Term) :-
>         copy_term(Term, TermCopy),
>         numbervars(TermCopy, 0, _),
>         op(0, fy, -),
>         op(0, fy, ~),
>         op(0, fy, <>),
>         op(0, fy, []),
>         op(0, xfx, =>),
>         op(0, xfx, v),
>         op(0, xfx, &),
>         write_term(Stream, TermCopy, [numbervars(true), quoted(true)]).
>
> This however looks kind of stupid and is maybe slow.
> Is there a better way to do this in SWI-Prolog?
> (I'm currently using version 5.6.46, if that should matter.)

If you don't mind being limited to SWI-Prolog, you can use the
module(Module) argument of write_term/3 and ensure Module has the
operator declarations you want in effect.

        --- Jan


------------
For further info, please visit http://www.swi-prolog.org/

To unsubscribe, send a plaintext mail with "unsubscribe prolog <e-mail>"
in its body to majordomo@...