note
	description: "Summary description for {FUN}."
	author: "JSO, Jackie Wang"
	date: "$Date$"
	revision: "$Revision$"

class interface
	FUN [G -> attached ANY, H -> attached ANY]

create 
	make_empty,
	make_from_array

convert
	make_from_array ({ARRAY [TUPLE [G, H]]}),
	as_array: {ARRAY [PAIR [G, H]]}

feature -- Constructors

	make_empty
			-- Initialize an empty function
		ensure
				# Current = 0
				is_empty

	has_duplicates (a: ARRAY [TUPLE [fst: G; snd: H]]): BOOLEAN
			-- does array a have duplicates?
			-- hence cannot construct a function

	make_from_array (a: ARRAY [TUPLE [fst: G; snd: H]])
			-- Initialize a function from array 'a'.
		require
			no_duplicates: across
					a.lower |..| a.upper as i
				all
					across
						a.lower |..| a.upper as j
					all
						a [i.item].fst ~ a [j.item].fst implies i.item = j.item
					end
				end
		ensure
			consistent_counts: # Current = a.count
	
feature -- Iterator

	new_cursor: ITERATION_CURSOR [PAIR [G, H]]
			-- Fresh cursor associated with current structure
	
feature -- Set Commands

	extend (p: PAIR [G, H])
			-- Extend current function with mapping 'g |-> h'
		require
				not domain.has (p.first)
		ensure
				old Current.deep_twin ~ Current - p

	union (other: like Current)
			-- Extend current function with pairs in 'other'
		require
				(domain |/\| other.domain).is_empty
		ensure
				old Current.deep_twin |<: Current
				other |<: Current
			definition: hold_count (agent in_either_fun (?, old Current.deep_twin, other)) = count

	subtract (p: PAIR [G, H])
			-- Subtract the mapping 'g |-> h' from current function.
		ensure
			case_of_subtraction: (old Current.deep_twin).has (p) implies old Current.deep_twin ~ Current + p
			case_of_no_subtraction: not (old Current.deep_twin).has (p) implies old Current.deep_twin ~ Current

	difference (other: like Current)
			-- Subtract all mappings in 'other' from current function.
		ensure
			definition: old Current.deep_twin ~ Current |\/| (old Current.deep_twin |/\| other)

	intersect (other: like Current)
			-- Intersect with mappings in 'other'
		ensure
				Current |<: old Current.deep_twin
				Current |<: other
			definition: hold_count (agent in_both_fun (?, old Current.deep_twin, other)) = count
	
feature -- Relation Commands

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

	domain_restrict_by (g: G)
			-- Keep pairs whose first values are 'd'.
		ensure
			new_fun_is_a_subset: Current |<: (old Current.deep_twin)
			only_g_in_new_fun: ((old Current.deep_twin) |\ (Current)).domain ~ (old Current.deep_twin.domain |\ singleton_g (g))

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

	range_restrict_by (h: H)
			-- Keep all pairs whose second values are 'h'.
		ensure
			new_fun_is_a_subset: Current |<: (old Current.deep_twin)
			only_h_in_new_fun: ((old Current.deep_twin) |\ (Current)).range ~ (old Current.deep_twin.range |\ singleton_h (h))

	domain_subtract (ds: SET [G])
			-- Subtract all pairs whose first values are members of 'ds'.
		ensure
			new_fun_is_a_subset: Current |<: (old Current.deep_twin)
			no_ds_in_new_fun: ((old Current.deep_twin) |\ (Current)).domain ~ ds.comprehension (agent in_domain (?, (old Current.deep_twin).domain))

	domain_subtract_by (g: G)
			-- Subtract the pair whose first value is 'd'
		ensure
			new_fun_is_a_subset: Current |<: (old Current.deep_twin)
			no_g_in_new_fun: ((old Current.deep_twin) |\ (Current)).domain ~ singleton_g (g).comprehension (agent in_domain (?, (old Current.deep_twin).domain))

	range_subtract (rs: SET [H])
			-- Subtract all pairs whose second values are members of 'rs'.
		ensure
			new_fun_is_a_subset: Current |<: (old Current.deep_twin)
			no_rs_in_new_fun: ((old Current.deep_twin) |\ (Current)).range ~ rs.comprehension (agent in_range (?, (old Current.deep_twin).range))

	range_subtract_by (h: H)
			-- Subtract pairs whose second values are 'h'.
		ensure
			new_fun_is_a_subset: Current |<: (old Current.deep_twin)
			no_h_in_new_fun: ((old Current.deep_twin) |\ (Current)).range ~ singleton_h (h).comprehension (agent in_range (?, (old Current.deep_twin).range))

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

	override_by (t: TUPLE [g: G; h: H])
			-- Update current function such that it aggres on 'g |-> h'.
		ensure
				Current ~ ((old Current.deep_twin @<< t.g) |\/| singleton_gh (t))

	put (h: H; g: G)
		require
				domain.has (g)
		ensure
				Current [g] ~ h
	
feature -- Set Queries

	count alias "#": INTEGER_32
			-- How many pair are in current function?
		ensure
				Result = # domain

	is_empty: BOOLEAN
			-- Does current function contain no pairs?
		ensure
				Result = (# Current = 0)

	has (p: PAIR [G, H]): BOOLEAN
			-- Does current contain the mapping 'g -> h'?

	is_subset_of alias "|<:" (other: like Current): BOOLEAN
			-- Is current function a subset of 'other'?
		ensure
			current_fun_unchanged: Current ~ old Current.deep_twin
				Result = across
					Current as cur
				all
					other.has (create {PAIR [G, H]}.make (cur.item.first, cur.item.second))
				end

	extended alias "+" (p: PAIR [G, H]): like Current
			-- Return a new function representing the addtion of current and 'g'.
		require
				not domain.has (p.first)
		ensure
			current_fun_unchanged: Current ~ old Current.deep_twin
			extension: Current ~ Result - p

	unioned alias "|\/|" (other: like Current): like Current
			-- Return a new function representing the union of current and 'other'.
		require
				(domain |/\| other.domain).is_empty
		ensure
			current_fun_unchanged: Current ~ old Current.deep_twin
			items_from_either_set: across
					Result as it
				all
					Current.has (create {PAIR [G, H]}.make (it.item.first, it.item.second)) or other.has (create {PAIR [G, H]}.make (it.item.first, it.item.second))
				end

	subtracted alias "-" (p: PAIR [G, H]): like Current
			-- Return a new function representing the difference between Current and 't.g |-> t.h'.
		ensure
			current_fun_unchanged: Current ~ old Current.deep_twin
			case_of_subtraction: has (p) implies Current ~ Result + p
			case_of_no_subtraction: not has (p) implies Current ~ Result

	differenced alias "|\" (other: like Current): like Current
			-- Return a new function representing the difference between Current and 'other'.
		ensure
			current_fun_unchanged: Current ~ old Current.deep_twin
			definition: Current ~ Result |\/| (Current |/\| other)

	intersected alias "|/\|" (other: like Current): like Current
			-- Return a new fun representing the intersection of current and 'other'.
		ensure
			current_set_unchanged: Current ~ old Current.deep_twin
			items_from_both_set: across
					Result as it
				all
					Current.has (create {PAIR [G, H]}.make (it.item.first, it.item.second)) and other.has (create {PAIR [G, H]}.make (it.item.first, it.item.second))
				end
	
feature -- Relation Status Queries

	is_function: BOOLEAN
			-- Is Current a function?

	is_injection: BOOLEAN
			-- Is Current an injective function?

	inverse: FUN [H, G]
		require
				is_injection

	domain: SET [G]
			-- Return the domain set of function.

	range: SET [H]
			-- Return the range set of function.
	
feature -- Relation Operation Queries

	item alias "[]" (g: G): H assign put
		require
				domain.has (g)
		ensure then
			current_fun_unchanged: Current ~ old Current.deep_twin
			result_in_range: range.has (Result)

	domain_restricted alias "|<" (ds: SET [G]): like Current
			-- Return a copy of current function with
			-- all pairs whose first values are members of 'ds' kept.
		ensure
			current_fun_unchanged: Current ~ old Current.deep_twin
			new_fun_is_a_subset: Result |<: Current
			only_ds_in_new_fun: (Current |\ Result).domain ~ (domain |\ ds)

	domain_restricted_by alias "@<" (g: G): like Current
			-- Return a copy of current funcion with
			-- all pairs whose first values are 'g' kept.
		ensure
			current_fun_unchanged: Current ~ old Current.deep_twin
			new_fun_is_a_subset: Result |<: Current
			only_g_in_new_fun: (Current |\ Result).domain ~ (domain |\ singleton_g (g))

	range_restricted alias "|>" (rs: SET [H]): like Current
			-- Return a copy of current function with
			-- all pairs whose second values are members of 'rs' kept.
		ensure
			current_fun_unchanged: Current ~ old Current.deep_twin
			new_fun_is_a_subset: Result |<: Current
			only_rs_in_new_fun: (Current |\ Result).range ~ (range |\ rs)

	range_restricted_by alias "@>" (h: H): like Current
			-- Return a copy of current function with
			-- all pairs whose second values are 'h' kept.
		ensure
			current_fun_unchanged: Current ~ old Current.deep_twin
			new_fun_is_a_subset: Result |<: Current
			only_h_in_new_fun: (Current |\ Result).range ~ (range |\ singleton_h (h))

	domain_subtracted alias "|<<" (ds: SET [G]): like Current
			-- Return a new copy of current function with
			-- all pairs whose first values are members of 'ds' subtracted.
		ensure
			current_fun_unchanged: Current ~ old Current.deep_twin
			new_fun_is_a_subset: Result |<: Current
			no_ds_in_new_fun: (Current |\ Result).domain ~ ds.comprehension (agent in_domain (?, domain))

	domain_subtracted_by alias "@<<" (g: G): like Current
			-- Return a new copy of current function with
			-- all pairs whose first values 'g' subtracted.
		ensure
			current_fun_unchanged: Current ~ old Current.deep_twin
			new_fun_is_a_subset: Result |<: Current
			no_ds_in_new_fun: (Current |\ Result).domain ~ singleton_g (g).comprehension (agent in_domain (?, domain))

	range_subtracted alias "|>>" (rs: SET [H]): like Current
			-- Return a new copy of current function with
			-- all pairs whose second values are members of 'rs' subtracted.
		ensure
			current_fun_unchanged: Current ~ old Current.deep_twin
			new_fun_is_a_subset: Result |<: Current
			no_rs_in_new_fun: (Current |\ Result).range ~ rs.comprehension (agent in_range (?, range))

	range_subtracted_by alias "@>>" (h: H): like Current
			-- Return a new copy of current function with
			-- all pairs whose second values are 'h' subtracted.
		ensure
			current_fun_unchanged: Current ~ old Current.deep_twin
			new_fun_is_a_subset: Result |<: Current
			no_rs_in_new_fun: (Current |\ Result).range ~ singleton_h (h).comprehension (agent in_range (?, range))

	overriden alias "|<+" (r: like Current): like Current
			-- Return a copy of current function that agrres on 'r'.
		ensure
			current_fun_unchanged: Current ~ old Current.deep_twin
				Result ~ ((Current |<< r.domain) |\/| r)

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

	range_as_bag: BAG [H]
			-- Returns the range of function as a bag.
	
feature -- Quantifiers

	hold_count (exp: PREDICATE [PAIR [G, H]]): INTEGER_32
			-- How many items satisfying exp are in Current?
		ensure
			current_set_unchanged: Current ~ old Current.deep_twin
			maximum_result: Result <= count

	comprehension alias "|" (exp: PREDICATE [PAIR [G, H]]): like Current
			-- Largest subset of the current fun whose elements satisfy exp
		ensure
			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
	
feature -- Conversion

	as_set: SET [PAIR [G, H]]
			-- Return current function as a set of pairs

	as_array: ARRAY [PAIR [G, H]]
	
feature -- Equality

	is_equal (other: like Current): BOOLEAN
			-- Is current function equal to 'other'?
	
feature -- Outputs

	debug_output: STRING_8
			-- String that should be displayed in debugger to represent Current.

	out: STRING_8
			-- New string containing terse printable representation
			-- of current object
	
invariant
	is_a_function: is_function

end -- class FUN

Generated by ISE EiffelStudio