Re: about dynamic/1 and file format

Paulo Moura <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 06/01/2014, at 12:21, Jiun-Shiung Wu <[email protected]> wrote:

> Hi, SWI-Prolog users,
> 
> I have two questions concerning dynamic/1 and saved file format. I am
> wondering if someone can help me.
> 
> I used dynamic to declare that a predicate is dynamic. For example, I put
> :- dynamic(father/2) at the beginning of my file. I used listing(father/2)
> to list all instances of father/2. I found that the parentheses in dynamic
> is gone. That is, when I did listing(father/2), I got: dynamic father/2.
> When I used with_input_to to save all instances of father/2, I got :-
> dynamic father/2 at the beginning of the new file. The parentheses are
> missing. Is there a way not to remove the parentheses in this case?
> 
> Secondly, how can I save a file using the UTF-8 with the header BOM? I have
> a knowledge base containing facts written in traditional Chinese (UTF-8).
> When I used with_input_to to save those facts into a new file, the BOM head
> seems to be gone and my Prolog program cannot read it. Though it is easy to
> restore the BOM head using Notepadd++, I am still wondering whether
> SWI-Prolog can save a file using UTF-8 with the BOM head.

You can use something like (not tested but the code is simple):

save_facts(File) :-
	open(File, write, Stream, [bom(true), encoding(utf8)]),
	write_facts(Stream),
	close(Stream).

write_facts(Stream) :-
	write_canonical(Stream, (:- dynamic(father/2))), write(Stream, '.\n'),
	father(A, B),
	write_canonical(Stream, father(A,B)), write(Stream, '.\n'),
	fail.
write_facts(_).

Cheers,

Paulo

-----------------------------------------------------------------
Paulo Moura
Logtalk developer

Email: <mailto:[email protected]>
Web:   <http://logtalk.org/>
-----------------------------------------------------------------
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.