<commit_before>/*  $Id$

    Part of CPL(R) (Constraint Logic Programming over Reals)

    Author:        <NAME>
    E-mail:        <EMAIL>
    WWW:           http://www.swi-prolog.org
		   http://www.ai.univie.ac.at/cgi-bin/tr-online?number+95-09
    Copyright (C): 2004, <NAME> and
		   1992-1995, Austrian Research Institute for
		              Artificial Intelligence (OFAI),
			      Vienna, Austria

    This software is part of <NAME>'s master thesis, supervised
    by <NAME> and daily advisor <NAME>.  It is based on CLP(Q,R)
    by <NAME> for SICStus Prolog and distributed under the
    license details below with permission from all mentioned authors.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/


:- module(dump,
	[
		dump/3,
		projecting_assert/1
	]).
:- use_module(class,
	[
		class_allvars/2
	]).
:- use_module(geler,
	[
		collect_nonlin/3
	]).
:- use_module(library(assoc), 
	[
	    empty_assoc/1,
	    get_assoc/3,
	    put_assoc/4,
	    assoc_to_list/2
	]).
:- use_module(itf3,
	[
		dump_linear/4,
		dump_nonzero/4
	]).
:- use_module(project,
	[
		project_attributes/2
	]).
:- dynamic blackboard_copy/1.	% replacement of bb_put and bb_delete by dynamic predicate

this_linear_solver(clpr). 

% dump(Target,NewVars,Constraints)
% 
% Returns in Constraints, the constraints that currently hold on Target where all variables 
% in Target are copied to new variables in NewVars and the constraints are given on these new
% variables. In short, you can safely manipulate NewVars and Constraints without changing the
% constraints on Target.

dump(Target,NewVars,Constraints) :-
	( 
		(
		  proper_varlist(Target) -> 

			true 
		; 
			% Target is not a list of variables
			raise_exception(instantiation_error(dump(Target,NewVars,Constraints),1))
		),
		ordering(Target),
		related_linear_vars(Target,All),	% All contains all variables of the classes of Target variables.
		nonlin_crux(All,Nonlin),
		project_attributes(Target,All),
		related_linear_vars(Target,Again),	% project drops/adds vars
		all_attribute_goals(Again,Gs,Nonlin),
		empty_assoc(D0),
		mapping(Target,NewVars,D0,D1),		% late (AVL suffers from put_atts)
		copy(Gs,Copy,D1,_),			% strip constraints
		(
		  blackboard_copy(_) ->

			retract(blackboard_copy(_)),
			assert(blackboard_copy(NewVars/Copy))
		;
			assert(blackboard_copy(NewVars/Copy))
		),
		fail					% undo projection
	;
		retract(blackboard_copy(NewVars/Constraints))	% garbage collect
	).

:- meta_predicate projecting_assert(:).

projecting_assert(Module:Clause) :-
	copy_term(Clause,Copy,Constraints),
	l2c(Constraints,Conj),			% fails for []
	this_linear_solver(Sm),			% proper module for {}/1
	!,
	(
	  Copy = (H:-B) ->			% former rule

		Module:assert((H:-Sm:{Conj},B))
	;					% former fact
		Module:assert((Copy:-Sm:{Conj}))
	).
projecting_assert(Clause) :-			% not our business
	assert(Clause).

copy_term(Term,Copy,Constraints) :-
	(
		term_variables(Term,Target),		% get all variables in Term
		related_linear_vars(Target,All),	% get all variables of the classes of the variables in Term
		nonlin_crux(All,Nonlin),		% get a list of all the nonlinear goals of these variables
		project_attributes(Target,All),
		related_linear_vars(Target,Again),	% project drops/adds vars
		all_attribute_goals(Again,Gs,Nonlin),
		empty_assoc(D0),
		copy(Term/Gs,TmpCopy,D0,_),	  % strip constraints
		(
		  blackboard_copy(_) ->

			retract(blackboard_copy(_)),
			assert(blackboard_copy(TmpCopy))
		;
			assert(blackboard_copy(TmpCopy))
		),
		fail				% undo projection
	;
		retract(blackboard_copy(Copy/Constraints))	  % garbage collect
	).

% l2c(Lst,Conj)
%
% converts a list to a round list: [a,b,c] -> (a,b,c) and [a] becomes a

l2c([X|Xs],Conj) :-
	(
	  Xs = [] ->
		
		Conj = X
	;
		Conj = (X,Xc),
		l2c(Xs,Xc)
	).

% proper_varlist(Lst)
%
% Returns whether Lst is a list of variables. 
% First predicate is to avoid unification of a variable with a list.

proper_varlist(X) :- 
	var(X),
	!,
	fail.
proper_varlist([]).
proper_varlist([X|Xs]) :-
	var(X),<commit_msg>* Test write access<commit_after>/*  $Id$

    Part of CPL(R) (Constraint Logic Programming over Reals)

    Author:        <NAME>
    E-mail:        <EMAIL>
    WWW:           http://www.swi-prolog.org
		   http://www.ai.univie.ac.at/cgi-bin/tr-online?number+95-09
    Copyright (C): 2004, <NAME> and
		   1992-1995, Austrian Research Institute for
		              Artificial Intelligence (OFAI),
			      Vienna, Austria

    This software is part of <NAME>'s master thesis, supervised
    by <NAME> and daily advisor <NAME>.  It is based on CLP(Q,R)
    by <NAME> for SICStus Prolog and distributed under the
    license details below with permission from all mentioned authors.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/


:- module(dump,
	[
		dump/3,
		projecting_assert/1
	]).
:- use_module(class,
	[
		class_allvars/2
	]).
:- use_module(geler,
	[
		collect_nonlin/3
	]).
:- use_module(library(assoc), 
	[
	    empty_assoc/1,
	    get_assoc/3,
	    put_assoc/4,
	    assoc_to_list/2
	]).
:- use_module(itf3,
	[
		dump_linear/4,
		dump_nonzero/4
	]).
:- use_module(project,
	[
		project_attributes/2
	]).
:- dynamic blackboard_copy/1.	% replacement of bb_put and bb_delete by dynamic predicate

this_linear_solver(clpr). 

% dump(Target,NewVars,Constraints)
% 
% Returns in Constraints, the constraints that currently hold on Target where all variables 
% in Target are copied to new variables in NewVars and the constraints are given on these new
% variables. In short, you can safely manipulate NewVars and Constraints without changing the
% constraints on Target.

dump(Target,NewVars,Constraints) :-
	( 
		(
		  proper_varlist(Target) -> 

			true 
		; 
			% Target is not a list of variables
			raise_exception(instantiation_error(dump(Target,NewVars,Constraints),1))
		),
		ordering(Target),
		related_linear_vars(Target,All),	% All contains all variables of the classes of Target variables.
		nonlin_crux(All,Nonlin),
		project_attributes(Target,All),
		related_linear_vars(Target,Again),	% project drops/adds vars
		all_attribute_goals(Again,Gs,Nonlin),
		empty_assoc(D0),
		mapping(Target,NewVars,D0,D1),		% late (AVL suffers from put_atts)
		copy(Gs,Copy,D1,_),			% strip constraints
		(
		  blackboard_copy(_) ->

			retract(blackboard_copy(_)),
			assert(blackboard_copy(NewVars/Copy))
		;
			assert(blackboard_copy(NewVars/Copy))
		),
		fail					% undo projection
	;
		retract(blackboard_copy(NewVars/Constraints))	% garbage collect
	).

:- meta_predicate projecting_assert(:).

projecting_assert(Module:Clause) :-
	copy_term(Clause,Copy,Constraints),
	l2c(Constraints,Conj),			% fails for []
	this_linear_solver(Sm),			% proper module for {}/1
	!,
	(
	  Copy = (H:-B) ->			% former rule

		Module:assert((H:-Sm:{Conj},B))
	;					% former fact
		Module:assert((Copy:-Sm:{Conj}))
	).
projecting_assert(Clause) :-			% not our business
	assert(Clause).

copy_term(Term,Copy,Constraints) :-
	(
		term_variables(Term,Target),		% get all variables in Term
		related_linear_vars(Target,All),	% get all variables of the classes of the variables in Term
		nonlin_crux(All,Nonlin),		% get a list of all the nonlinear goals of these variables
		project_attributes(Target,All),
		related_linear_vars(Target,Again),	% project drops/adds vars
		all_attribute_goals(Again,Gs,Nonlin),
		empty_assoc(D0),
		copy(Term/Gs,TmpCopy,D0,_),	  % strip constraints
		(
		  blackboard_copy(_) ->

			retract(blackboard_copy(_)),
			assert(blackboard_copy(TmpCopy))
		;
			assert(blackboard_copy(TmpCopy))
		),
		fail				% undo projection
	;
		retract(blackboard_copy(Copy/Constraints))	  % garbage collect
	).

% l2c(Lst,Conj)
%
% converts a list to a round list: [a,b,c] -> (a,b,c) and [a] becomes a

l2c([X|Xs],Conj) :-
	(
	  Xs = [] ->
		
		Conj = X
	;
		Conj = (X,Xc),
		l2c(Xs,Xc)
	).

% proper_varlist(List)
%
% Returns whether Lst is a list of variables. 
% First predicate is to avoid unification of a variable with a list.

proper_varlist(X) :- 
	var(X),
	!,
	fail.
proper_varlist([]).
proper_varlist([X|Xs]) :-
	var(X),
{'fraction_non_alphanumeric': 0.10141809290953545, 'fraction_numerical': 0.01095354523227384, 'mean_word_length': 3.6397459165154267, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2236412', 'n_tokens_mistral': 3657, 'n_tokens_neox': 3326, 'n_words': 994}