Alice Project

The Alt structure

________ Synopsis ____________________________________________________

    signature ALT
    structure Alt : ALT

The Alt structure defines the alt type, used for holding binary alternatives (sums), and provides a collection of common combinators.

The type alt and its constructors, as well as the functions isFst and isSnd are available in the top-level environment.

See also: Option, Pair

________ Import ______________________________________________________

Imported implicitly.

________ Interface ___________________________________________________

    signature ALT =
	datatype ('a,'b) alt = FST of 'a | SND of 'b
	type     ('a,'b) t   = ('a,'b) alt

	exception Alt

	val isFst :   ('a,'b) alt -> bool
	val isSnd :   ('a,'b) alt -> bool
	val fst :     ('a,'b) alt -> 'a
	val snd :     ('a,'b) alt -> 'b
	val getFst :  ('a,'b) alt * 'a -> 'a
	val getSnd :  ('a,'b) alt * 'b -> 'b

	val app :     ('a -> unit) * ('b -> unit) -> ('a,'b) alt -> unit
	val appFst :  ('a -> unit) -> ('a,'b) alt -> unit
	val appSnd :  ('b -> unit) -> ('a,'b) alt -> unit
	val map :     ('a -> 'c) * ('b -> 'd) -> ('a,'b) alt -> ('c,'d) alt
	val mapFst :  ('a -> 'c) -> ('a,'b) alt -> ('c,'b) alt
	val mapSnd :  ('b -> 'c) -> ('a,'b) alt -> ('a,'c) alt
	val mapBoth : ('a -> 'b) -> ('a,'a) alt -> ('b,'b) alt

	val equal :   ('a * 'a -> bool) * ('b * 'b -> bool) -> ('a,'b) alt * ('a,'b) alt -> bool
	val collate : ('a * 'a -> order) * ('b * 'b -> order) -> ('a,'b) alt * ('a,'b) alt -> order

________ Description _________________________________________________

datatype ('a,'b) alt = FST of 'a | SND of 'b
type t = alt

The type of binary sums. It is useful to pass values that can have two alternative types.

exception Alt

Raised on invalid accesses to alternatives.

isFst alt
isSnd alt

The function isFst returns true if alt is FST v, false otherwise. The function isSnd returns true if alt is SND v, false otherwise.

fst alt
snd alt

The function fst returns v if alt is FST v, and raises Alt otherwise. The function snd returns v if alt is SND v, and raises Alt otherwise.

getFst (alt, v')
getSnd (alt, v')

The function getFst returns v if alt is FST v, else v'. The function getSnd returns v if alt is SND v, else v'.

app (f,g) alt
appFst f alt
appSnd g alt

The function app applies the functions f or g to the given constituent value of alt, respectively. For the functions appFst and appSnd the following equivalences hold:

	appFst f alt = app (f, ignore) alt
	appSnd g alt = app (ignore, g) alt
map (f,g) alt
mapFst f alt
mapSnd g alt
mapBoth f alt

The function map produces an alternative by mapping the functions f or g on the constituent value of alt. For the functions mapFst, mapSnd and mapBoth the following equivalences hold:

	mapFst f alt  = map (f, id) alt
	mapSnd g alt  = map (id, g) alt
	mapBoth f alt = map (f, f) alt

where id is the identity function.

equal (eqFst, eqSnd) (alt1, alt2)

Creates an equality function on alternatives, given suitable equality functions for each constituent type.

collate (f, g) (alt1, alt2)

Performs comparison on alternatives, given suitable orderings f and g for the respective constituent types. The constructed ordering is defined as follows:

      fun collate (f, g) =
	  fn (FST x1, FST x2) => f (x1, x2)
	   | (SND y1, SND y2) => g (y1, y2)
	   | (FST _,  SND _ ) => LESS
	   | (SND _,  FST _ ) => GREATER

last modified 2007/Mar/30 17:10