% -----------------------------------------------------------------------------
%  (C) Altran Praxis Limited
% -----------------------------------------------------------------------------
% 
%  The SPARK toolset is free software; you can redistribute it and/or modify it
%  under terms of the GNU General Public License as published by the Free
%  Software Foundation; either version 3, or (at your option) any later
%  version. The SPARK toolset 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
%  General Public License distributed with the SPARK toolset; see file
%  COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy of
%  the license.
% 
% =============================================================================


%-------------------------------------------------------------------------------
% RULE FAMILY CONTAINED HEREIN :-
% array        : basic properties of FDL array manipulation functions
% mk__array    : rules specific to SPARK array aggregates
%-------------------------------------------------------------------------------
% MODEL DECLARATION FOR THIS FILE :-
%
% rule_family array:
%         element(X, Y)   requires [ X:any, Y:any        ] &
%         update(X, Y, Z) requires [ X:any, Y:any, Z:any ].
%
% rule_family mk__array:
%         element(X, Y)   requires [ X:any, Y:any ].
%-------------------------------------------------------------------------------


/*** Rules specific to ARRAY MANIPULATIONS ***/

array(1):  element(update(A,I,X),I) may_be_replaced_by X.
array(2):  update(A,I,element(A,I)) may_be_replaced_by A.
array(3):  element(update(A,J,X),K) & element(A,K) are_interchangeable if
                [ J<>K ].
array(4):  update(update(A,I,X),J,Y) & update(update(A,J,Y),I,X)
                are_interchangeable if [ I<>J ].
array(5):  update(update(A,I,X),I,Y) may_be_replaced_by update(A,I,Y).


/*** Rules specific to SPARK ARRAY AGGREGATES ***/

/* mk__array(1):
	element(mk__array(LARGS, Inds := X), [I]) may_be_replaced_by X if
		[ "satisfies_index_constraint(Inds, I)" ].
   In the above rule, the quoted side-condition can be met as follows:
	(1) if Inds is of the form "[J]", by satisfying I = J;
	(2) if Inds is of the form "[J .. K]", by satisfying J <= I <= K;
	(3) if Inds is of the form "Is & Js", by satisfying either Is or Js.
   Note that the last case makes the definition recursive.

   Equally, if we can prove that I does not satisfy the index constraints
   represented by Inds, we can instead consider whether it satisfies any of
   the index constraints in LARGS, and so on recursively until we either
   succeed or reach a point where neither satisfaction nor non-satisfaction can
   be proved (because of an insufficiently powerful inference engine, for
   instance, or because the index I is insufficiently constrained by the
   hypotheses of the VC).

   Given the above, the following is a general-purpose rule for fetching the
   value of an element of an array aggregate (where this is non-trivial): */

mk__array(1):
	element(MK__ARRAY_PART, [I]) may_be_replaced_by VALUE if
		[ goal(nonvar(MK__ARRAY_PART)),
		  goal(MK__ARRAY_PART =.. [mk__array|ARGUMENTS]),
		  goal(find_element(MK__ARRAY_PART, [I], VALUE)) ].


/* mk__array(2):
	element(mk__ARRAYTYPE(LARGS, Inds := X), [I]) may_be_replaced_by X if
		[ "satisfies_index_constraint(Inds, I)" ].
   In the above rule, the quoted side-condition can be met as follows:
	(1) if Inds is of the form "[J]", by satisfying I = J;
	(2) if Inds is of the form "[J .. K]", by satisfying J <= I <= K;
	(3) if Inds is of the form "Is & Js", by satisfying either Is or Js.
   Note that the last case makes the definition recursive.

   Given the above, the following is a general-purpose rule for fetching the
   value of an element of a typed array aggregate (where non-trivial): */

mk__array(2):
	element(MK__ARRAY_PART, [I]) may_be_replaced_by VALUE if
		[ goal(nonvar(MK__ARRAY_PART)),
		  goal(MK__ARRAY_PART =.. [MK__ARRAYTYPE|ARGUMENTS]),
		  goal(mk__function_name(MK__ARRAYTYPE, _, array)),
		  goal(find_element(MK__ARRAY_PART, [I], VALUE)) ].


/* It is recommended that the user should avoid reasoning about the
   equality (or otherwise) of two array aggregates. */
