This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/library/gensym.yap

48 lines
905 B
Plaintext
Raw Normal View History

2015-11-18 15:06:25 +00:00
/**
* @file gensym.yap
* @author VITOR SANTOS COSTA <vsc@VITORs-MBP.lan>
* @date Tue Nov 17 18:37:13 2015
2018-11-06 22:47:44 +00:00
*
2015-11-18 15:06:25 +00:00
* @brief Generate a new atom.
2018-11-06 22:47:44 +00:00
*
*
2015-11-18 15:06:25 +00:00
*/
:- module(gensym, [
2018-06-28 12:48:29 +01:00
init_gensym/1,
gensym/2,
reset_gensym/1,
reset_gensym/0
2015-11-18 15:06:25 +00:00
]).
/**
* @defgroup gensym Generate a new symbol.
* @ingroup library
*
* Predicates to create new atoms based on the prefix _Atom_.
* They use a counter, stored as a
2018-11-06 22:47:44 +00:00
* dynamic predicate, to construct the atom's suffix.
2015-11-18 15:06:25 +00:00
*
*/
:- dynamic gensym_key/2.
2018-06-28 12:48:29 +01:00
init_gensym(Key) :-
2018-11-06 22:47:44 +00:00
retractall(gensym_key(Key,_)),
assert(gensym_key(Key,0) ).
2018-06-28 12:48:29 +01:00
2018-11-06 22:47:44 +00:00
gensym(Key, New) :-
retract(gensym_key(Key,Id)), !,
atomic_concat(Key,Id,New),
NId is Id+1,
2018-11-06 22:47:44 +00:00
assert(gensym_key(Key,NId)).
gensym(Atom, New) :-
2018-11-06 22:47:44 +00:00
atomic_concat(Atom,0,New),
assert(gensym_key(Atom,1)).
reset_gensym(Atom) :-
retract(gensym_key(Atom,_)).
reset_gensym :-
retractall(gensym_key(_,_)).