note
	description: "RELATION data structure for both specification and implementation."
	author: "JSO and JW"
	date: "$Date$"
	revision: "$Revision$"

class 
	REL [G -> attached ANY, H -> attached ANY]

create 
	make_empty,
	make_from_tuple_array

convert
	make_from_tuple_array ({attached ARRAY [TUPLE [G, H]]})

feature {NONE} -- Initialization

	default_create
			-- Process instances of classes with no creation clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end
	
feature -- Access

	generating_type: TYPE [detachable REL [G, H]]
			-- Type of current object
			-- (type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generating_type_not_void: Result /= Void
		end

	generator: STRING_8
			-- Name of current object's generating class
			-- (base class of the type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generator_not_void: Result /= Void
			generator_not_empty: not Result.is_empty
		end
	
feature -- Comparison

	frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void
			-- or attached to isomorphic object structures?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_deep_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			shallow_implies_deep: standard_equal (a, b) implies Result
			both_or_none_void: (a = Void) implies (Result = (b = Void))
			same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b))
			symmetric: Result implies deep_equal (b, a)
		end

	frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached
			-- to objects considered equal?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b))
		end

	frozen is_deep_equal (other: REL [G, H]): BOOLEAN
			-- Are Current and other attached to isomorphic object structures?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			shallow_implies_deep: standard_is_equal (other) implies Result
			same_type: Result implies same_type (other)
			symmetric: Result implies other.is_deep_equal (Current)
		end

	frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached to
			-- field-by-field identical objects of the same type?
			-- Always uses default object comparison criterion.
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.standard_is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b))
		end

	frozen standard_is_equal (other: REL [G, H]): BOOLEAN
			-- Is other attached to an object of the same type
			-- as current object, and field-by-field identical to it?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)
		end
	
feature -- Status report

	conforms_to (other: ANY): BOOLEAN
			-- Does type of current object conform to type
			-- of other (as per Eiffel: The Language, chapter 13)?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		end

	same_type (other: ANY): BOOLEAN
			-- Is type of current object identical to type of other?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			definition: Result = (conforms_to (other) and other.conforms_to (Current))
		end
	
feature -- Duplication

	frozen clone (other: detachable ANY): like other
		obsolete "Use `twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- equal to other
			--
			-- For non-void other, clone calls copy;
			-- to change copying/cloning semantics, redefine copy.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.twin
			end
		ensure -- from ANY
			instance_free: class
			equal: Result ~ other
		end

	copy (other: REL [G, H])
			-- Update current object using fields of object attached
			-- to other, so as to yield equal objects.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		ensure -- from ANY
			is_equal: Current ~ other
		end

	frozen deep_clone (other: detachable ANY): like other
		obsolete "Use `deep_twin' instead. [2017-05-31]"
			-- Void if other is void: otherwise, new object structure
			-- recursively duplicated from the one attached to other
			-- (from ANY)
		do
			if other /= Void then
				Result := other.deep_twin
			end
		ensure -- from ANY
			instance_free: class
			deep_equal: deep_equal (other, Result)
		end

	frozen deep_copy (other: REL [G, H])
			-- Effect equivalent to that of:
			--		copy (other . deep_twin)
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		do
			copy (other.deep_twin)
		ensure -- from ANY
			deep_equal: deep_equal (Current, other)
		end

	frozen deep_twin: REL [G, H]
			-- New object structure recursively duplicated from Current.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			deep_twin_not_void: Result /= Void
			deep_equal: deep_equal (Current, Result)
		end

	frozen standard_clone (other: detachable ANY): like other
		obsolete "Use `standard_twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.standard_twin
			end
		ensure -- from ANY
			instance_free: class
			equal: standard_equal (Result, other)
		end

	frozen standard_copy (other: REL [G, H])
			-- Copy every field of other onto corresponding field
			-- of current object.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		ensure -- from ANY
			is_standard_equal: standard_is_equal (other)
		end

	frozen standard_twin: REL [G, H]
			-- New object field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			standard_twin_not_void: Result /= Void
			equal: standard_equal (Result, Current)
		end

	frozen twin: REL [G, H]
			-- New object equal to Current
			-- twin calls copy; to change copying/twinning semantics, redefine copy.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			twin_not_void: Result /= Void
			is_equal: Result ~ Current
		end
	
feature -- Basic operations

	frozen as_attached: attached REL [G, H]
		obsolete "Remove calls to this feature. [2017-05-31]"
			-- Attached version of Current.
			-- (Can be used during transitional period to convert
			-- non-void-safe classes to void-safe ones.)
			-- (from ANY)
		do
			Result := Current
		end

	frozen default: detachable REL [G, H]
			-- Default value of object's type
			-- (from ANY)
		do
		end

	frozen default_pointer: POINTER
			-- Default value of type POINTER
			-- (Avoid the need to write p.default for
			-- some p of type POINTER.)
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end

	default_rescue
			-- Process exception for routines with no Rescue clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end

	frozen do_nothing
			-- Execute a null action.
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end
	
feature {SET} -- Implementation

	imp: ARRAYED_LIST [PAIR [G, H]]
			-- (from SET)

	item_imp: detachable CELL [PAIR [G, H]]
			-- (from SET)

	set_imp (other_imp: like imp)
			-- (from SET)
		do
			imp := other_imp
			if not imp.object_comparison then
				imp.compare_objects
			end
		end

	Void_item
			-- (from SET)
		once
			item_imp := Void
		end
	
feature -- Agent functions

	in_domain (g: G; dom: SET [G]): BOOLEAN
		do
			if dom.has (g) then
				Result := True
			end
		end

	in_range (h: H; ran: SET [H]): BOOLEAN
		do
			if ran.has (h) then
				Result := True
			end
		end

	singleton_g (g: G): SET [G]
		do
			create Result.make_from_array (<<g>>)
		ensure
				# Result = 1
				Result.has (g)
		end

	singleton_gh (g: G; h: H): REL [G, H]
		do
			create Result.make_from_tuple_array (<<[g, h]>>)
		ensure
				# Result = 1
		end

	singleton_h (h: H): SET [H]
		do
			create Result.make_from_array (<<h>>)
		ensure
				# Result = 1
				Result.has (h)
		end
	
feature -- Commands for implementation

	difference (other: REL [G, H])
			-- Subtract the current set by 'other'.
			-- (from SET)
		do
			across
				other as it
			loop
				subtract (it.item)
			end
			Void_item
		ensure -- from SET
			definition: old Current.deep_twin ~ Current |\/| (old Current.deep_twin |/\| other)
		end

	extend (g: PAIR [G, H])
			-- Extend the current set by 'g'.
			-- (from SET)
		do
			if not has (g) then
				imp.extend (g)
			end
			Void_item
		ensure -- from SET
			case_of_no_extension: (old Current.deep_twin).has (g) implies old Current.deep_twin ~ Current
			case_of_extension: not (old Current.deep_twin).has (g) implies old Current.deep_twin ~ Current - g
		end

	intersect (other: REL [G, H])
			-- Intersect current set with 'other'.
			-- (from SET)
		do
			from
				imp.start
			until
				imp.after
			loop
				if not other.has (imp.item) then
					imp.remove
				else
					imp.forth
				end
			end
			Void_item
		ensure -- from SET
				Current |<: old Current.deep_twin
				Current |<: other
			definition: hold_count (agent in_both_set (?, old Current.deep_twin, other)) = count
		end

	subtract (g: PAIR [G, H])
			-- Subtract the current set by 'g'.
			-- (from SET)
		do
			from
				imp.start
			until
				imp.after
			loop
				if imp.item ~ g then
					imp.remove
				else
					imp.forth
				end
			end
			Void_item
		ensure -- from SET
			case_of_subtraction: (old Current.deep_twin).has (g) implies old Current.deep_twin ~ Current + g
			case_of_no_subtraction: not (old Current.deep_twin).has (g) implies old Current.deep_twin ~ Current
		end

	union (other: REL [G, H])
			-- Extend the current set by 'other'.
			-- (from SET)
		do
			across
				other as it
			loop
				extend (it.item)
			end
			Void_item
		ensure -- from SET
				old Current.deep_twin |<: Current
				other |<: Current
			definition: hold_count (agent in_either_set (?, old Current.deep_twin, other)) = count
		end
	
feature -- Commands for relational operations

	domain_restrict (ds: SET [G])
			-- Keep all pairs whose first values are members of 'ds'.
		do
			domain_subtract (domain |\ ds)
		ensure
			new_rel_is_a_subset: Current |<: (old Current.deep_twin)
			only_ds_in_new_rel: ((old Current.deep_twin) |\ (Current)).domain ~ (old Current.deep_twin.domain |\ ds)
		end

	domain_restrict_by (g: G)
			-- Keep pairs whose first values are 'd'.
		do
			domain_subtract (domain - g)
		ensure
			new_rel_is_a_subset: Current |<: (old Current.deep_twin)
			only_g_in_new_rel: ((old Current.deep_twin) |\ (Current)).domain ~ (old Current.deep_twin.domain |\ singleton_g (g))
		end

	domain_subtract (ds: SET [G])
			-- Subtract all pairs whose first values are members of 'ds'.
		local
			g: G
		do
			from
				imp.start
			until
				imp.after
			loop
				g := imp.item.first
				if ds.has (g) then
					imp.remove
				else
					imp.forth
				end
			end
		ensure
			new_rel_is_a_subset: Current |<: (old Current.deep_twin)
			no_ds_in_new_rel: ((old Current.deep_twin) |\ (Current)).domain ~ ds.comprehension (agent in_domain (?, (old Current.deep_twin).domain))
		end

	domain_subtract_by (g: G)
			-- Subtract pairs whose first values are 'g'.
		do
			domain_subtract (create {SET [G]}.make_from_array (<<g>>))
		ensure
			new_rel_is_a_subset: Current |<: (old Current.deep_twin)
			no_g_in_new_rel: ((old Current.deep_twin) |\ (Current)).domain ~ singleton_g (g).comprehension (agent in_domain (?, (old Current.deep_twin).domain))
		end

	override (r: like Current)
			-- Update current relation such that it aggres on 'r'.
		do
			domain_subtract (r.domain)
			union (r)
		ensure
				Current ~ ((old Current.deep_twin |<< r.domain) |\/| r)
		end

	override_by (t: TUPLE [g: G; h: H])
			-- Update current relation such that it aggres on 'g |-> h'.
		do
			domain_subtract_by (t.g)
			extend (create {PAIR [G, H]}.make_from_tuple (t))
		ensure
				Current ~ ((old Current.deep_twin @<< t.g) |\/| singleton_gh (t.g, t.h))
		end

	range_restrict (rs: SET [H])
			-- Keep all pairs whose second values are members of 'rs'.
		do
			range_subtract (range |\ rs)
		ensure
			new_rel_is_a_subset: Current |<: (old Current.deep_twin)
			only_rs_in_new_rel: ((old Current.deep_twin) |\ (Current)).range ~ (old Current.deep_twin.range |\ rs)
		end

	range_restrict_by (h: H)
			-- Keep all pairs whose second values are 'h'.
		do
			range_subtract (range |\ singleton_h (h))
		ensure
			new_rel_is_a_subset: Current |<: (old Current.deep_twin)
			only_h_in_new_rel: ((old Current.deep_twin) |\ (Current)).range ~ (old Current.deep_twin.range |\ singleton_h (h))
		end

	range_subtract (rs: SET [H])
			-- Subtract all pairs whose second values are members of 'rs'.
		local
			h: H
		do
			from
				imp.start
			until
				imp.after
			loop
				h := imp.item.second
				if rs.has (h) then
					imp.remove
				else
					imp.forth
				end
			end
		ensure
			new_rel_is_a_subset: Current |<: (old Current.deep_twin)
			no_rs_in_new_rel: ((old Current.deep_twin) |\ (Current)).range ~ rs.comprehension (agent in_range (?, (old Current.deep_twin).range))
		end

	range_subtract_by (h: H)
			-- Subtract pairs whose second values are 'h'.
		do
			range_subtract (create {SET [H]}.make_from_array (<<h>>))
		ensure
			new_rel_is_a_subset: Current |<: (old Current.deep_twin)
			no_h_in_new_rel: ((old Current.deep_twin) |\ (Current)).range ~ singleton_h (h).comprehension (agent in_range (?, (old Current.deep_twin).range))
		end
	
feature -- Constructor

	make_empty
			-- Initialize an empty set.
			-- (from SET)
		do
			create imp.make (0)
			imp.compare_objects
		ensure -- from SET
				is_empty
		end

	make_from_array (a: ARRAY [PAIR [G, H]])
			-- (from SET)
		do
			make_empty
			across
				a as it
			loop
				extend (it.item)
			end
			imp.compare_objects
		ensure -- from SET
			all_items_are_intended: across
					Current as it
				all
					a.has (it.item)
				end
			all_intended_items_added: across
					a as it
				all
					Current.has (it.item)
				end
		end

	make_from_tuple_array (a: ARRAY [TUPLE [fst: G; snd: H]])
		do
			make_empty
			across
				a as tup
			loop
				extend (create {PAIR [G, H]}.make_from_tuple (tup.item))
			end
		end

	make_one (a_singleton: PAIR [G, H])
			-- make a singleton set
			-- (from SET)
		do
			make_empty
			extend (a_singleton)
		ensure -- from SET
				count = 1
				has (a_singleton)
		end
	
feature -- Conversion to array.

	as_array: ARRAY [TUPLE [G, H]]
		do
			create Result.make_empty
			across
				Current as c
			loop
				Result.force (c.item.as_tuple, Result.upper + 1)
			end
			Result.compare_objects
		ensure
				Current ~ create {REL [G, H]}.make_from_tuple_array (Result)
		end
	
feature -- Conversion to array.
--	as_array: ARRAY[G]
--		do
--			create Result.make_empty
--			across
--				Current as c
--			loop
--				Result.force (c.item, Result.upper + 1)
--			end
----			Result.compare_objects
--		ensure
--			Current ~ create {SET[G]}.make_from_array (Result)
--			-- why does this fail in {REL}.t8
--		end

	set_as_array: ARRAY [PAIR [G, H]]
			-- (from SET)
		do
			create Result.make_from_array (imp.to_array)
			Result.compare_objects
		end
	
feature -- Debug output

	debug_output: STRING_8
			-- String that should be displayed in debugger to represent Current.
			-- (from SET)
		do
			Result := out
		ensure -- from DEBUG_OUTPUT
			result_not_void: Result /= Void
		end

	out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from SET)
		local
			i: INTEGER_32
		do
			create Result.make_empty
			Result.append ("{ ")
			from
				i := imp.Lower
			until
				i > imp.upper
			loop
				check
						attached {PAIR [G, H]} imp [i] as e
				then
					Result.append (e.out)
				end
				if i < imp.upper then
					Result.append (", ")
				end
				i := i + 1
			end
			Result.append (" }")
		ensure -- from ANY
			out_not_void: Result /= Void
		end
	
feature -- Equality

	is_equal (other: REL [G, H]): BOOLEAN
			-- Is curren set equal to 'other'?
			-- (from SET)
		require -- from ANY
			other_not_void: other /= Void
		do
			Result := # Current = # other and then other |<: Current and then Current |<: other
		ensure -- from ANY
			symmetric: Result implies other ~ Current
			consistent: standard_is_equal (other) implies Result
		ensure then -- from SET
				Result = (Current |<: other and then other |<: Current)
		end
	
feature -- Iteration

	new_cursor: ITERATION_CURSOR [PAIR [G, H]]
			-- Fresh cursor associated with current structure
			-- (from SET)
		do
			Result := imp.new_cursor
		ensure -- from ITERABLE
			result_attached: Result /= Void
		end
	
feature -- Output

	Io: STD_FILES
			-- Handle to standard file setup
			-- (from ANY)
		once
			create Result
			Result.set_output_default
		ensure -- from ANY
			instance_free: class
			io_not_void: Result /= Void
		end

	print (o: detachable ANY)
			-- Write terse external representation of o
			-- on standard output.
			-- (from ANY)
		do
			if o /= Void then
				Io.put_string (o.out)
			end
		ensure -- from ANY
			instance_free: class
		end

	frozen tagged_out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			tagged_out_not_void: Result /= Void
		end
	
feature -- Platform

	Operating_environment: OPERATING_ENVIRONMENT
			-- Objects available from the operating system
			-- (from ANY)
		once
			create Result
		ensure -- from ANY
			instance_free: class
			operating_environment_not_void: Result /= Void
		end
	
feature -- Quantifiers

	comprehension alias "|" (exp: PREDICATE [PAIR [G, H]]): REL [G, H]
			-- Largest subset of the current set whose elements satisfy exp
			-- e.g. for set: MSL_SET[STRING],
			-- (set | agent {STRING}.has_substring ("xy")) = {g:set | g.has_substring("xy")}
			-- (from SET)
		local
			i: INTEGER_32
			imp_item: PAIR [G, H]
		do
			create Result.make_empty
			from
				i := imp.Lower
			until
				i > imp.upper
			loop
				imp_item := imp [i]
				if exp.item ([imp_item]) then
					Result.extend (imp_item)
				end
				i := i + 1
			end
			Result.Void_item
		ensure -- from SET
			current_set_unchanged: Current ~ old Current.deep_twin
			is_subset: Result |<: Current
			all_satisfying_exp: Result.hold_count (exp) = # Result
			consistent_satisfying_items: Current.hold_count (exp) = # Result
		end

	hold_count (exp: PREDICATE [PAIR [G, H]]): INTEGER_32
			-- How many items satisfying exp are in Current?
			-- (from SET)
		local
			i: INTEGER_32
		do
			from
				i := imp.Lower
			until
				i > imp.upper
			loop
				if exp.item ([imp [i]]) then
					Result := Result + 1
				end
				i := i + 1
			end
		ensure -- from SET
			current_set_unchanged: Current ~ old Current.deep_twin
			maximum_result: Result <= count
		end
	
feature -- Queries for relational operations

	domain_restricted alias "|<" (ds: SET [G]): like Current
			-- Return a copy of current relation with
			-- all pairs whose first values are members of 'ds' kept.
		do
			Result := Current.deep_twin
			Result.domain_restrict (ds)
		ensure
			current_rel_unchanged: Current ~ old Current.deep_twin
			new_rel_is_a_subset: Result |<: Current
			only_ds_in_new_rel: (Current |\ Result).domain ~ (domain |\ ds)
		end

	domain_restricted_by alias "@<" (g: G): like Current
			-- Return a copy of current relation with
			-- all pairs whose first values are 'g' kept.
		do
			Result := Current.deep_twin
			Result.domain_restrict_by (g)
		ensure
			current_rel_unchanged: Current ~ old Current.deep_twin
			new_rel_is_a_subset: Result |<: Current
			only_g_in_new_rel: (Current |\ Result).domain ~ (domain |\ singleton_g (g))
		end

	domain_subtracted alias "|<<" (ds: SET [G]): like Current
			-- Return a new copy of current relation with
			-- all pairs whose first values are members of 'ds' subtracted.
		do
			Result := Current.deep_twin
			Result.domain_subtract (ds)
		ensure
			current_rel_unchanged: Current ~ old Current.deep_twin
			new_rel_is_a_subset: Result |<: Current
			no_ds_in_new_rel: (Current |\ Result).domain ~ ds.comprehension (agent in_domain (?, domain))
		end

	domain_subtracted_by alias "@<<" (g: G): like Current
			-- Return a new copy of current relation with
			-- all pairs whose first values 'g' subtracted.
		do
			Result := Current.deep_twin
			Result.domain_subtract_by (g)
		ensure
			current_rel_unchanged: Current ~ old Current.deep_twin
			new_rel_is_a_subset: Result |<: Current
			no_ds_in_new_rel: (Current |\ Result).domain ~ singleton_g (g).comprehension (agent in_domain (?, domain))
		end

	image alias "[]" (g: G): SET [H]
			-- Retrieve the set of items that are associated with 'g'.
		do
			create Result.make_empty
			across
				Current as c
			loop
				if c.item.first ~ g then
					Result.extend (c.item.second)
				end
			end
		ensure
			current_rel_unchanged: Current ~ old Current.deep_twin
				Result.is_empty implies not domain.has (g)
				across
					Result as r
				all
					has (create {PAIR [G, H]}.make_from_tuple ([g, r.item]))
				end
				across
					range.differenced (Result) as r
				all
					not has (create {PAIR [G, H]}.make_from_tuple ([g, r.item]))
				end
		end

	inverse: REL [H, G]
		do
			create Result.make_empty
			across
				Current as pair
			loop
				Result.extend (create {PAIR [H, G]}.make_from_tuple ([pair.item.second, pair.item.first]))
			end
		ensure
			current_rel_unchanged: Current ~ old Current.deep_twin
				Result.inverse.is_equal (Current)
		end

	overriden alias "|<+" (r: like Current): like Current
			-- Return a copy of current relation that agrres on 'r'.
		do
			Result := Current.deep_twin
			Result.override (r)
		ensure
			current_rel_unchanged: Current ~ old Current.deep_twin
				Result ~ ((Current |<< r.domain) |\/| r)
		end

	overriden_by alias "@<+" (t: TUPLE [g: G; h: H]): like Current
			-- Return a copy of current relation that agrres on 'g |-> h'.
		do
			Result := Current.deep_twin
			Result.override_by (t.g, t.h)
		ensure
			current_rel_unchanged: Current ~ old Current.deep_twin
				Result ~ ((Current @<< t.g) |\/| singleton_gh (t.g, t.h))
		end

	range_restricted alias "|>" (rs: SET [H]): like Current
			-- Return a copy of current relation with
			-- all pairs whose second values are members of 'rs' kept.
		do
			Result := Current.deep_twin
			Result.range_restrict (rs)
		ensure
			current_rel_unchanged: Current ~ old Current.deep_twin
			new_rel_is_a_subset: Result |<: Current
			only_rs_in_new_rel: (Current |\ Result).range ~ (range |\ rs)
		end

	range_restricted_by alias "@>" (h: H): like Current
			-- Return a copy of current relation with
			-- all pairs whose second values are 'h' kept.
		do
			Result := Current.deep_twin
			Result.range_restrict_by (h)
		ensure
			current_rel_unchanged: Current ~ old Current.deep_twin
			new_rel_is_a_subset: Result |<: Current
			only_h_in_new_rel: (Current |\ Result).range ~ (range |\ singleton_h (h))
		end

	range_subtracted alias "|>>" (rs: SET [H]): like Current
			-- Return a new copy of current relation with
			-- all pairs whose second values are members of 'ds' subtracted.
		do
			Result := Current.deep_twin
			Result.range_subtract (rs)
		ensure
			current_rel_unchanged: Current ~ old Current.deep_twin
			new_rel_is_a_subset: Result |<: Current
			no_rs_in_new_rel: (Current |\ Result).range ~ rs.comprehension (agent in_range (?, range))
		end

	range_subtracted_by alias "@>>" (h: H): like Current
			-- Return a new copy of current relation with
			-- all pairs whose second values are 'h' subtracted.
		do
			Result := Current.deep_twin
			Result.range_subtract_by (h)
		ensure
			current_rel_unchanged: Current ~ old Current.deep_twin
			new_rel_is_a_subset: Result |<: Current
			no_rs_in_new_rel: (Current |\ Result).range ~ singleton_h (h).comprehension (agent in_range (?, range))
		end
	
feature -- Queries for specification.

	count alias "#": INTEGER_32
			-- Return the cardinality of the set.
			-- (from SET)
		do
			Result := imp.count
		ensure -- from SET
			current_set_unchanged: Current ~ old Current.deep_twin
			definition: Result = hold_count (agent g_to_true)
		end

	differenced alias "|\" (other: REL [G, H]): REL [G, H]
			-- Return a new set representing the difference between Current and 'other'.
			-- (from SET)
		local
			new_imp: ARRAYED_LIST [PAIR [G, H]]
		do
			create new_imp.make (0)
			across
				imp as src
			loop
				if not other.has (src.item) then
					new_imp.extend (src.item)
				end
			end
			create Result.make_empty
			Result.set_imp (new_imp)
			Result.Void_item
		ensure -- from SET
			current_set_unchanged: Current ~ old Current.deep_twin
			definition: Current ~ Result |\/| (Current |/\| other)
		end

	extended alias "+" (g: PAIR [G, H]): REL [G, H]
			-- Return a new set representing the addtion of current and 'g'.
			-- (from SET)
		do
			create Result.make_empty
			Result.set_imp (Current.imp.twin)
			Result.extend (g)
			Result.Void_item
		ensure -- from SET
			current_set_unchanged: Current ~ old Current.deep_twin
			case_of_no_extension: has (g) implies Current ~ Result
			case_of_extension: not has (g) implies Current ~ Result - g
		end

	has (g: PAIR [G, H]): BOOLEAN
			-- Does the set contain 'g'?
			-- (from SET)
		do
			Result := (imp.has (g))
		ensure -- from SET
			current_set_unchanged: Current ~ old Current.deep_twin
			definition_1: Result = (hold_count (agent eq (?, g)) = 1)
			definition_2: Result = across
					Current as it
				some
					it.item ~ g
				end
		end

	intersected alias "|/\|" (other: REL [G, H]): REL [G, H]
			-- Return a new set representing the intersection of current and 'other'.
			-- (from SET)
		do
			create Result.make_empty
			across
				other as it
			loop
				if has (it.item) then
					Result.extend (it.item)
				end
			end
			Result.Void_item
		ensure -- from SET
			current_set_unchanged: Current ~ old Current.deep_twin
			items_from_both_set: across
					Result as it
				all
					Current.has (it.item) and other.has (it.item)
				end
		end

	is_empty: BOOLEAN
			-- Is the set empty?
			-- (from SET)
		do
			Result := (count = 0)
		ensure -- from SET
			current_set_unchanged: Current ~ old Current.deep_twin
				Result = (# Current = 0)
		end

	is_subset_of alias "|<:" (other: REL [G, H]): BOOLEAN
			-- (from SET)
		local
			i: INTEGER_32
		do
			from
				Result := True
				i := imp.Lower
			until
				not Result or else i > imp.upper
			loop
				Result := other.has (imp [i])
				i := i + 1
			end
		ensure -- from SET
			current_set_unchanged: Current ~ old Current.deep_twin
				Result = across
					Current as cur
				all
					other.has (cur.item)
				end
		end

	subtracted alias "-" (g: PAIR [G, H]): REL [G, H]
			-- Return a new set representing the difference between Current and 'other'.
			-- (from SET)
		do
			create Result.make_empty
			Result.set_imp (Current.imp.twin)
			Result.subtract (g)
			Result.Void_item
		ensure -- from SET
			current_set_unchanged: Current ~ old Current.deep_twin
			case_of_subtraction: has (g) implies Current ~ Result + g
			case_of_no_subtraction: not has (g) implies Current ~ Result
		end

	unioned alias "|\/|" (other: REL [G, H]): REL [G, H]
			-- Return a new set representing the union of current and 'other'.
			-- (from SET)
		do
			create Result.make_empty
			Result.set_imp (Current.imp.twin)
			across
				other as it
			loop
				Result.extend (it.item)
			end
			Result.Void_item
		ensure -- from SET
			current_set_unchanged: Current ~ old Current.deep_twin
			items_from_either_set: across
					Result as it
				all
					Current.has (it.item) or other.has (it.item)
				end
		end
	
feature -- Query and command for arbitrary set members

	choose_item
			-- Choose an arbitrary element of the
			-- set and store it in item
			-- (from SET)
		require -- from SET
				not is_empty
		do
			create item_imp.put (imp [count])
		ensure -- from SET
				has (item)
				chosen
		end

	chosen: BOOLEAN
			-- (from SET)
		do
			Result := attached item_imp
		end

	item: PAIR [G, H]
			-- Return an arbitrary member from the set.
			-- (from SET)
		require -- from SET
				not is_empty
				chosen
		do
			check
					attached item_imp as l_item
			then
				Result := l_item.item
			end
		ensure -- from SET
				has (Result)
		end

	remove_item
			-- Remove an arbitrary member (i.e., 'item') from the set.
			-- (from SET)
		require -- from SET
				not is_empty
				chosen
		do
			imp.go_i_th (count)
			imp.remove
			item_imp := Void
		ensure -- from SET
				Current ~ old Current.deep_twin - old item
				not chosen
		end
	
feature {NONE} -- Retrieval

	frozen internal_correct_mismatch
			-- Called from runtime to perform a proper dynamic dispatch on correct_mismatch
			-- from MISMATCH_CORRECTOR.
			-- (from ANY)
		local
			l_msg: STRING_8
			l_exc: EXCEPTIONS
		do
			if attached {MISMATCH_CORRECTOR} Current as l_corrector then
				l_corrector.correct_mismatch
			else
				create l_msg.make_from_string ("Mismatch: ")
				create l_exc
				l_msg.append (generating_type.name)
				l_exc.raise_retrieval_exception (l_msg)
			end
		end
	
feature -- Status queries

	domain: SET [G]
			-- Return the domain set of relation.
		do
			create Result.make_empty
			across
				Current as c
			loop
				Result.extend (c.item.first)
			end
		end

	is_function: BOOLEAN
			-- Is current relation a function?
			-- i.e., each domain value maps to at most one value.
		local
			c: ITERATION_CURSOR [PAIR [G, H]]
		do
			from
				Result := True
				c := new_cursor
			until
				not Result or else c.after
			loop
				if # image (c.item.first) > 1 then
					Result := False
				end
				c.forth
			end
		ensure
				Result = across
					domain as d
				all
					# image (d.item) = 1
				end
		end

	is_injection: BOOLEAN
			-- Is current relation an injective function?
			-- i.e., no two domain values map to the same range vaule.
		do
			Result := is_function and then inverse.is_function
		ensure
				Result = (is_function and inverse.is_function)
		end

	range: SET [H]
			-- Return the range set of relation.
		do
			create Result.make_empty
			across
				Current as c
			loop
				Result.extend (c.item.second)
			end
		end
	
feature {SET} -- agent functions

	eq (v1, v2: PAIR [G, H]): BOOLEAN
			-- (from SET)
		do
			Result := v1 ~ v2
		end

	g_to_true (v: PAIR [G, H]): BOOLEAN
			-- (from SET)
		do
			Result := True
		ensure -- from SET
			definition: Result
		end

	in_both_set (g: PAIR [G, H]; s1, s2: SET [PAIR [G, H]]): BOOLEAN
			-- (from SET)
		do
			Result := s1.has (g) and then s2.has (g)
		end

	in_either_set (g: PAIR [G, H]; s1, s2: SET [PAIR [G, H]]): BOOLEAN
			-- (from SET)
		do
			Result := s1.has (g) or else s2.has (g)
		end
	
invariant
		-- from SET
		imp.object_comparison
	no_duplicates: across
			imp.Lower |..| imp.upper as i
		all
			across
				imp.Lower |..| imp.upper as j
			all
				imp [i.item] ~ imp [j.item] implies i.item = j.item
			end
		end

		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)

end -- class REL

Generated by ISE EiffelStudio