## Logicmoo Utils Examples
from https://github.com/logicmoo/logicmoo_utils.git
Install locally with
````prolog
?- pack_install('https://github.com/logicmoo/logicmoo_utils.git').
````
# Control local side effects on each REDO
Example usages:
````prolog
% Scope *when* a prolog flag is set
with_prolog_flag(Flag,Value,Goal):-
current_prolog_flag(Flag,Was),
each_call_cleanup(
set_prolog_flag(Flag,Value),
Goal,
set_prolog_flag(Flag,Was)).
````
## no_repeats
New ways to avoid duplicate solutions
?- use_module(library(lm_utils/no_repeats)).
?- no_repeats( X , member(X-Y,[3-2,1-4,1-5,2-1])).
% X = 3, Y = 2 ;
% X = 1, Y = 4 ;
% X = 2, Y = 1.
?- no_repeats(member(X,[3,1,1,1,3,2])).
% X = 3 ;
% X = 1 ;
% X = 2.
## loop_check
New simple loop checking
Allows code to declare special locations that loop prevention will occur
?- use_module(library(lm_utils/no_loops)).
true.
?- TODO Doc this
## sub_clause_expansion
:- use_module(library(file_scope)).
:- set_prolog_flag_until_eof(access_level, system).
:- assert_until_eof(( term_expansion(.,.) :- .. )).
## hook_hybrid
Hook and/or override assert, retract, call, clause, erase, etc for specific predicates
?- use_module(library(hook_hybrid)).
true.
?- TODO Doc this
## clause_attvars
An alternate interface to the clause database to allow attributed variables to be asserted/read
?- use_module(library(hybrid_db/clause_attvars)).
true.
?- TODO Doc this
## attvar_reader
?- use_module(library(hybrid_db/attvar_reader)).
true.
?- TODO Doc this
# with_thread_local
Call a Goal with local assertions
locally_each( :Effect, :Call) is nondet.
Temporally have :Effect (see locally/2)
But Ensure Non-determism is respected (effect is undone between _each_ Redo)
uses each_call_cleanup/3 instead of setup_call_cleanup/3 (slightly slower?)
for example,
locally_each/2 works (Does not throw)
?- current_prolog_flag(xref,Was),
locally_each(set_prolog_flag(xref,true),
assertion(current_prolog_flag(xref,true));assertion(current_prolog_flag(xref,true))),
assertion(current_prolog_flag(xref,Was)),fail.
locally/2 is little less carefull so it should _not_ work (it throws instead)
?- current_prolog_flag(xref,Was),
locally(set_prolog_flag(xref,true),
assertion(current_prolog_flag(xref,true));assertion(current_prolog_flag(xref,true))),
assertion(current_prolog_flag(xref,Was)),fail.
locally( :Effect, :Call) is nondet.
Effect may be of type:
set_prolog_flag -
Temporarily change prolog flag
op/3 -
change op
$gvar=Value -
set a global variable
Temporally (thread_local) Assert some :Effect
use locally_each/3 if respecting Non-determism is important
(slightly slower?)
?- current_prolog_flag(xref,Was),
locally(set_prolog_flag(xref,true),
assertion(current_prolog_flag(xref,true))),
assertion(current_prolog_flag(xref,Was)).
:- set_prolog_flag_until_eof(access_level,system).
:- assert_until_eof(( term_expansion(.,.) :- .. )).
# Utilities to open various objects for read/write
:- use_module(library(lmu/filestreams)).
# mass wildcard expansions
:- use_module(library(lmu/filesystem)).
draw_goal(Tree).
Draw the tree and print the latex code:
draw_goal(Tree),format("~s",[Tree]).
:- use_module(library(sldnfdraw)).
:- if(current_predicate(use_rendering/1)).
:- use_rendering(sldnf).
:- endif.
:- sldnf.
:- begin_program.
member(X ,[X|_T]).
member(X ,[_H|T]):-
member(X,T).
:-end_program.
:-begin_query.
member(X,[1,2]), \+ member(X,[1,3]).
:-end_query.