Erlang -- sofs

User's Guide
Reference Manual
Release Notes
PDF
Top

STDLIB
Reference Manual
Version 1.17.3

Expand All
Contract All

Table of Contents

- STDLIB (App)
- array
  - Top of manual page
  - default/1
  - fix/1
  - foldl/3
  - foldr/3
  - from_list/1
  - from_list/2
  - from_orddict/1
  - from_orddict/2
  - get/2
  - is_array/1
  - is_fix/1
  - map/2
  - new/0
  - new/1
  - new/2
  - relax/1
  - reset/2
  - resize/1
  - resize/2
  - set/3
  - size/1
  - sparse_foldl/3
  - sparse_foldr/3
  - sparse_map/2
  - sparse_size/1
  - sparse_to_list/1
  - sparse_to_orddict/1
  - to_list/1
  - to_orddict/1
- base64
  - Top of manual page
  - encode/1
  - encode_to_string/1
  - decode/1
  - decode_to_string/1
  - mime_decode/1
  - mime_decode_to_string/1
- beam_lib
  - Top of manual page
  - chunks/2
  - chunks/3
  - version/1
  - md5/1
  - info/1
  - cmp/2
  - cmp_dirs/2
  - diff_dirs/2
  - strip/1
  - strip_files/1
  - strip_release/1
  - format_error/1
  - crypto_key_fun/1
  - clear_crypto_key_fun/0
- binary
  - Top of manual page
  - at/2
  - bin_to_list/1
  - bin_to_list/2
  - bin_to_list/3
  - compile_pattern/1
  - copy/1
  - copy/2
  - decode_unsigned/1
  - decode_unsigned/2
  - encode_unsigned/1
  - encode_unsigned/2
  - first/1
  - last/1
  - list_to_bin/1
  - longest_common_prefix/1
  - longest_common_suffix/1
  - match/2
  - match/3
  - matches/2
  - matches/3
  - part/2
  - part/3
  - referenced_byte_size/1
  - replace/3
  - replace/4
  - split/2
  - split/3
- c
  - Top of manual page
  - bt/1
  - c/1
  - c/2
  - cd/1
  - flush/0
  - help/0
  - i/0
  - ni/0
  - i/3
  - l/1
  - lc/1
  - ls/0
  - ls/1
  - m/0
  - m/1
  - memory/0
  - memory/1
  - memory/1
  - nc/1
  - nc/2
  - nl/1
  - pid/3
  - pwd/0
  - q/0
  - regs/0
  - nregs/0
  - xm/1
  - y/1
  - y/2
- calendar
  - Top of manual page
  - date_to_gregorian_days/1
  - date_to_gregorian_days/3
  - datetime_to_gregorian_seconds/1
  - day_of_the_week/1
  - day_of_the_week/3
  - gregorian_days_to_date/1
  - gregorian_seconds_to_datetime/1
  - is_leap_year/1
  - iso_week_number/0
  - iso_week_number/1
  - last_day_of_the_month/2
  - local_time/0
  - local_time_to_universal_time/1
  - local_time_to_universal_time_dst/1
  - now_to_local_time/1
  - now_to_universal_time/1
  - now_to_datetime/1
  - seconds_to_daystime/1
  - seconds_to_time/1
  - time_difference/2
  - time_to_seconds/1
  - universal_time/0
  - universal_time_to_local_time/1
  - valid_date/1
  - valid_date/3
- dets
  - Top of manual page
  - all/0
  - bchunk/2
  - close/1
  - delete/2
  - delete_all_objects/1
  - delete_object/2
  - first/1
  - foldl/3
  - foldr/3
  - from_ets/2
  - info/1
  - info/2
  - init_table/2
  - insert/2
  - insert_new/2
  - is_compatible_bchunk_format/2
  - is_dets_file/1
  - lookup/2
  - match/1
  - match/2
  - match/3
  - match_delete/2
  - match_object/1
  - match_object/2
  - match_object/3
  - member/2
  - next/2
  - open_file/1
  - open_file/2
  - pid2name/1
  - repair_continuation/2
  - safe_fixtable/2
  - select/1
  - select/2
  - select/3
  - select_delete/2
  - slot/2
  - sync/1
  - table/1
  - to_ets/2
  - traverse/2
  - update_counter/3
- dict
  - Top of manual page
  - append/3
  - append_list/3
  - erase/2
  - fetch/2
  - fetch_keys/1
  - filter/2
  - find/2
  - fold/3
  - from_list/1
  - is_key/2
  - map/2
  - merge/3
  - new/0
  - size/1
  - store/3
  - to_list/1
  - update/3
  - update/4
  - update_counter/3
- digraph
  - Top of manual page
  - add_edge/5
  - add_edge/4
  - add_edge/3
  - add_vertex/3
  - add_vertex/2
  - add_vertex/1
  - del_edge/2
  - del_edges/2
  - del_path/3
  - del_vertex/2
  - del_vertices/2
  - delete/1
  - edge/2
  - edges/1
  - edges/2
  - get_cycle/2
  - get_path/3
  - get_short_cycle/2
  - get_short_path/3
  - in_degree/2
  - in_edges/2
  - in_neighbours/2
  - info/1
  - new/0
  - new/1
  - no_edges/1
  - no_vertices/1
  - out_degree/2
  - out_edges/2
  - out_neighbours/2
  - vertex/2
  - vertices/1
- digraph_utils
  - Top of manual page
  - arborescence_root/1
  - components/1
  - condensation/1
  - cyclic_strong_components/1
  - is_acyclic/1
  - is_arborescence/1
  - is_tree/1
  - loop_vertices/1
  - postorder/1
  - preorder/1
  - reachable/2
  - reachable_neighbours/2
  - reaching/2
  - reaching_neighbours/2
  - strong_components/1
  - subgraph/2
  - topsort/1
- epp
  - Top of manual page
  - open/2
  - open/3
  - close/1
  - parse_erl_form/1
  - parse_file/3
- erl_eval
  - Top of manual page
  - exprs/2
  - exprs/3
  - exprs/4
  - expr/2
  - expr/3
  - expr/4
  - expr/5
  - expr_list/2
  - expr_list/3
  - expr_list/4
  - new_bindings/0
  - bindings/1
  - binding/2
  - add_binding/3
  - del_binding/2
- erl_expand_records
  - Top of manual page
  - module/2
- erl_id_trans
  - Top of manual page
  - parse_transform/2
- erl_internal
  - Top of manual page
  - bif/2
  - guard_bif/2
  - type_test/2
  - arith_op/2
  - bool_op/2
  - comp_op/2
  - list_op/2
  - send_op/2
  - op_type/2
- erl_lint
  - Top of manual page
  - module/1
  - module/2
  - module/3
  - is_guard_test/1
  - format_error/1
- erl_parse
  - Top of manual page
  - parse_form/1
  - parse_exprs/1
  - parse_term/1
  - format_error/1
  - tokens/1
  - tokens/2
  - normalise/1
  - abstract/1
- erl_pp
  - Top of manual page
  - form/1
  - form/2
  - attribute/1
  - attribute/2
  - function/1
  - function/2
  - guard/1
  - guard/2
  - exprs/1
  - exprs/2
  - exprs/3
  - expr/1
  - expr/2
  - expr/3
  - expr/4
- erl_scan
  - Top of manual page
  - string/1
  - string/2
  - string/3
  - tokens/3
  - tokens/4
  - reserved_word/1
  - token_info/1
  - token_info/2
  - attributes_info/1
  - attributes_info/2
  - set_attribute/3
  - format_error/1
- erl_tar
  - Top of manual page
  - add/3
  - add/4
  - close/1
  - create/2
  - create/3
  - extract/1
  - extract/2
  - format_error/1
  - open/2
  - table/1
  - table/2
  - t/1
  - tt/1
- ets
  - Top of manual page
  - all/0
  - delete/1
  - delete/2
  - delete_all_objects/1
  - delete_object/2
  - file2tab/1
  - file2tab/2
  - first/1
  - foldl/3
  - foldr/3
  - from_dets/2
  - fun2ms/1
  - give_away/3
  - i/0
  - i/1
  - info/1
  - info/2
  - init_table/2
  - insert/2
  - insert_new/2
  - is_compiled_ms/1
  - last/1
  - lookup/2
  - lookup_element/3
  - match/2
  - match/3
  - match/1
  - match_delete/2
  - match_object/2
  - match_object/3
  - match_object/1
  - match_spec_compile/1
  - match_spec_run/2
  - member/2
  - new/2
  - next/2
  - prev/2
  - rename/2
  - repair_continuation/2
  - safe_fixtable/2
  - select/2
  - select/3
  - select/1
  - select_count/2
  - select_delete/2
  - select_reverse/2
  - select_reverse/3
  - select_reverse/1
  - setopts/2
  - slot/2
  - tab2file/2
  - tab2file/3
  - tab2list/1
  - tabfile_info/1
  - table/1
  - test_ms/2
  - to_dets/2
  - update_counter/3
  - update_counter/3
  - update_counter/3
  - update_element/3
  - update_element/3
- file_sorter
  - Top of manual page
  - sort/1
  - sort/2
  - sort/3
  - keysort/2
  - keysort/3
  - keysort/4
  - merge/2
  - merge/3
  - keymerge/3
  - keymerge/4
  - check/1
  - check/2
  - keycheck/2
  - keycheck/3
- filelib
  - Top of manual page
  - ensure_dir/1
  - file_size/1
  - fold_files/5
  - is_dir/1
  - is_file/1
  - is_regular/1
  - last_modified/1
  - wildcard/1
  - wildcard/2
- filename
  - Top of manual page
  - absname/1
  - absname/2
  - absname_join/2
  - basename/1
  - basename/2
  - dirname/1
  - extension/1
  - flatten/1
  - join/1
  - join/2
  - nativename/1
  - pathtype/1
  - rootname/1
  - rootname/2
  - split/1
  - find_src/1
  - find_src/2
- gb_sets
  - Top of manual page
  - add/2
  - add_element/2
  - balance/1
  - delete/2
  - delete_any/2
  - del_element/2
  - difference/2
  - subtract/2
  - empty/0
  - new/0
  - filter/2
  - fold/3
  - from_list/1
  - from_ordset/1
  - insert/2
  - intersection/2
  - intersection/1
  - is_disjoint/2
  - is_empty/1
  - is_member/2
  - is_element/2
  - is_set/1
  - is_subset/2
  - iterator/1
  - largest/1
  - next/1
  - singleton/1
  - size/1
  - smallest/1
  - take_largest/1
  - take_smallest/1
  - to_list/1
  - union/2
  - union/1
- gb_trees
  - Top of manual page
  - balance/1
  - delete/2
  - delete_any/2
  - empty/0
  - enter/3
  - from_orddict/1
  - get/2
  - lookup/2
  - insert/3
  - is_defined/2
  - is_empty/1
  - iterator/1
  - keys/1
  - largest/1
  - map/2
  - next/1
  - size/1
  - smallest/1
  - take_largest/1
  - take_smallest/1
  - to_list/1
  - update/3
  - values/1
- gen_event
  - Top of manual page
  - start_link/0
  - start_link/1
  - start/0
  - start/1
  - add_handler/3
  - add_sup_handler/3
  - notify/2
  - sync_notify/2
  - call/3
  - call/4
  - delete_handler/3
  - swap_handler/3
  - swap_sup_handler/3
  - which_handlers/1
  - stop/1
  - Module:init/1
  - Module:handle_event/2
  - Module:handle_call/2
  - Module:handle_info/2
  - Module:terminate/2
  - Module:code_change/3
  - Module:format_status/2
- gen_fsm
  - Top of manual page
  - start_link/3
  - start_link/4
  - start/3
  - start/4
  - send_event/2
  - send_all_state_event/2
  - sync_send_event/2
  - sync_send_event/3
  - sync_send_all_state_event/2
  - sync_send_all_state_event/3
  - reply/2
  - send_event_after/2
  - start_timer/2
  - cancel_timer/1
  - enter_loop/4
  - enter_loop/5
  - enter_loop/5
  - enter_loop/6
  - Module:init/1
  - Module:StateName/2
  - Module:handle_event/3
  - Module:StateName/3
  - Module:handle_sync_event/4
  - Module:handle_info/3
  - Module:terminate/3
  - Module:code_change/4
  - Module:format_status/2
- gen_server
  - Top of manual page
  - start_link/3
  - start_link/4
  - start/3
  - start/4
  - call/2
  - call/3
  - multi_call/2
  - multi_call/3
  - multi_call/4
  - cast/2
  - abcast/2
  - abcast/3
  - reply/2
  - enter_loop/3
  - enter_loop/4
  - enter_loop/4
  - enter_loop/5
  - Module:init/1
  - Module:handle_call/3
  - Module:handle_cast/2
  - Module:handle_info/2
  - Module:terminate/2
  - Module:code_change/3
  - Module:format_status/2
- io
  - Top of manual page
  - columns/0
  - columns/1
  - put_chars/1
  - put_chars/2
  - nl/0
  - nl/1
  - get_chars/2
  - get_chars/3
  - get_line/1
  - get_line/2
  - getopts/0
  - getopts/1
  - setopts/1
  - setopts/2
  - write/1
  - write/2
  - read/1
  - read/2
  - read/3
  - fwrite/1
  - fwrite/2
  - fwrite/3
  - format/1
  - format/2
  - format/3
  - fread/2
  - fread/3
  - rows/0
  - rows/1
  - scan_erl_exprs/1
  - scan_erl_exprs/2
  - scan_erl_exprs/3
  - scan_erl_form/1
  - scan_erl_form/2
  - scan_erl_form/3
  - parse_erl_exprs/1
  - parse_erl_exprs/2
  - parse_erl_exprs/3
  - parse_erl_form/1
  - parse_erl_form/2
  - parse_erl_form/3
- io_lib
  - Top of manual page
  - nl/0
  - write/1
  - write/2
  - print/1
  - print/4
  - fwrite/2
  - format/2
  - fread/2
  - fread/3
  - write_atom/1
  - write_string/1
  - write_char/1
  - indentation/2
  - char_list/1
  - deep_char_list/1
  - printable_list/1
- lib
  - Top of manual page
  - flush_receive/0
  - error_message/2
  - progname/0
  - nonl/1
  - send/2
  - sendw/2
- lists
  - Top of manual page
  - all/2
  - any/2
  - append/1
  - append/2
  - concat/1
  - delete/2
  - dropwhile/2
  - duplicate/2
  - filter/2
  - flatlength/1
  - flatmap/2
  - flatten/1
  - flatten/2
  - foldl/3
  - foldr/3
  - foreach/2
  - keydelete/3
  - keyfind/3
  - keymap/3
  - keymember/3
  - keymerge/3
  - keyreplace/4
  - keysearch/3
  - keysort/2
  - keystore/4
  - keytake/3
  - last/1
  - map/2
  - mapfoldl/3
  - mapfoldr/3
  - max/1
  - member/2
  - merge/1
  - merge/2
  - merge/3
  - merge3/3
  - min/1
  - nth/2
  - nthtail/2
  - partition/2
  - prefix/2
  - reverse/1
  - reverse/2
  - seq/2
  - seq/3
  - sort/1
  - sort/2
  - split/2
  - splitwith/2
  - sublist/2
  - sublist/3
  - subtract/2
  - suffix/2
  - sum/1
  - takewhile/2
  - ukeymerge/3
  - ukeysort/2
  - umerge/1
  - umerge/2
  - umerge/3
  - umerge3/3
  - unzip/1
  - unzip3/1
  - usort/1
  - usort/2
  - zip/2
  - zip3/3
  - zipwith/3
  - zipwith3/4
- log_mf_h
  - Top of manual page
  - init/3
  - init/4
- math
  - Top of manual page
  - pi/0
  - sin/1
  - cos/1
  - tan/1
  - asin/1
  - acos/1
  - atan/1
  - atan2/2
  - sinh/1
  - cosh/1
  - tanh/1
  - asinh/1
  - acosh/1
  - atanh/1
  - exp/1
  - log/1
  - log10/1
  - pow/2
  - sqrt/1
  - erf/1
  - erfc/1
- ms_transform
  - Top of manual page
  - parse_transform/2
  - transform_from_shell/3
  - format_error/1
- orddict
  - Top of manual page
  - append/3
  - append_list/3
  - erase/2
  - fetch/2
  - fetch_keys/1
  - filter/2
  - find/2
  - fold/3
  - from_list/1
  - is_key/2
  - map/2
  - merge/3
  - new/0
  - size/1
  - store/3
  - to_list/1
  - update/3
  - update/4
  - update_counter/3
- ordsets
  - Top of manual page
  - new/0
  - is_set/1
  - size/1
  - to_list/1
  - from_list/1
  - is_element/2
  - add_element/2
  - del_element/2
  - union/2
  - union/1
  - intersection/2
  - intersection/1
  - is_disjoint/2
  - subtract/2
  - is_subset/2
  - fold/3
  - filter/2
- pg
  - Top of manual page
  - create/1
  - create/2
  - join/2
  - send/2
  - esend/2
  - members/1
- pool
  - Top of manual page
  - start/1
  - start/2
  - attach/1
  - stop/0
  - get_nodes/0
  - pspawn/3
  - pspawn_link/3
  - get_node/0
- proc_lib
  - Top of manual page
  - spawn/1
  - spawn/2
  - spawn/3
  - spawn/4
  - spawn_link/1
  - spawn_link/2
  - spawn_link/3
  - spawn_link/4
  - spawn_opt/2
  - spawn_opt/3
  - spawn_opt/4
  - spawn_opt/5
  - start/3
  - start/4
  - start/5
  - start_link/3
  - start_link/4
  - start_link/5
  - init_ack/2
  - init_ack/1
  - format/1
  - initial_call/1
  - translate_initial_call/1
  - hibernate/3
- proplists
  - Top of manual page
  - append_values/2
  - compact/1
  - delete/2
  - expand/2
  - get_all_values/2
  - get_bool/2
  - get_keys/1
  - get_value/2
  - get_value/3
  - is_defined/2
  - lookup/2
  - lookup_all/2
  - normalize/2
  - property/1
  - property/2
  - split/2
  - substitute_aliases/2
  - substitute_negations/2
  - unfold/1
- qlc
  - Top of manual page
  - append/1
  - append/2
  - cursor/1
  - delete_cursor/1
  - eval/1
  - e/1
  - fold/3
  - format_error/1
  - info/1
  - keysort/2
  - next_answers/1
  - q/1
  - sort/1
  - string_to_handle/1
  - table/2
- queue
  - Top of manual page
  - new/0
  - is_queue/1
  - is_empty/1
  - len/1
  - in/2
  - in_r/2
  - out/1
  - out_r/1
  - from_list/1
  - to_list/1
  - reverse/1
  - split/2
  - join/2
  - filter/2
  - member/2
  - get/1
  - get_r/1
  - drop/1
  - drop_r/1
  - peek/1
  - peek_r/1
  - cons/2
  - head/1
  - tail/1
  - snoc/2
  - daeh/1
  - last/1
  - liat/1
  - init/1
  - lait/1
- random
  - Top of manual page
  - seed/0
  - seed/3
  - seed/1
  - seed0/0
  - uniform/0
  - uniform/1
  - uniform_s/1
  - uniform_s/2
- re
  - Top of manual page
  - compile/1
  - compile/2
  - run/2
  - run/3
  - replace/3
  - replace/4
  - split/2
  - split/3
- regexp
  - Top of manual page
  - match/2
  - first_match/2
  - matches/2
  - sub/3
  - gsub/3
  - split/2
  - sh_to_awk/1
  - parse/1
  - format_error/1
- sets
  - Top of manual page
  - new/0
  - is_set/1
  - size/1
  - to_list/1
  - from_list/1
  - is_element/2
  - add_element/2
  - del_element/2
  - union/2
  - union/1
  - intersection/2
  - intersection/1
  - is_disjoint/2
  - subtract/2
  - is_subset/2
  - fold/3
  - filter/2
- shell
  - Top of manual page
  - history/1
  - results/1
  - catch_exception/1
  - prompt_func/1
  - start_restricted/1
  - stop_restricted/0
- shell_default
- slave
  - Top of manual page
  - start/1
  - start/2
  - start/3
  - start_link/1
  - start_link/2
  - start_link/3
  - stop/1
  - pseudo/1
  - pseudo/2
  - relay/1
- sofs
  - Top of manual page
  - a_function/1
  - canonical_relation/1
  - composite/2
  - constant_function/2
  - converse/1
  - difference/2
  - digraph_to_family/1
  - domain/1
  - drestriction/2
  - drestriction/3
  - empty_set/0
  - extension/3
  - family/1
  - family_difference/2
  - family_domain/1
  - family_field/1
  - family_intersection/1
  - family_intersection/2
  - family_projection/2
  - family_range/1
  - family_specification/2
  - family_to_digraph/1
  - family_to_relation/1
  - family_union/1
  - family_union/2
  - field/1
  - from_external/2
  - from_sets/1
  - from_sets/1
  - from_term/1
  - image/2
  - intersection/1
  - intersection/2
  - intersection_of_family/1
  - inverse/1
  - inverse_image/2
  - is_a_function/1
  - is_disjoint/2
  - is_empty_set/1
  - is_equal/2
  - is_set/1
  - is_sofs_set/1
  - is_subset/2
  - is_type/1
  - join/4
  - multiple_relative_product/2
  - no_elements/1
  - partition/1
  - partition/2
  - partition/3
  - partition_family/2
  - product/1
  - product/2
  - projection/2
  - range/1
  - relation/1
  - relation_to_family/1
  - relative_product/1
  - relative_product/2
  - relative_product1/2
  - restriction/2
  - restriction/3
  - set/1
  - specification/2
  - strict_relation/1
  - substitution/2
  - symdiff/2
  - symmetric_partition/2
  - to_external/1
  - to_sets/1
  - type/1
  - union/1
  - union/2
  - union_of_family/1
  - weak_relation/1
- string
  - Top of manual page
  - len/1
  - equal/2
  - concat/2
  - chr/2
  - rchr/2
  - str/2
  - rstr/2
  - span/2
  - cspan/2
  - substr/2
  - substr/3
  - tokens/2
  - join/2
  - chars/2
  - chars/3
  - copies/2
  - words/1
  - words/2
  - sub_word/2
  - sub_word/3
  - strip/1
  - strip/2
  - strip/3
  - left/2
  - left/3
  - right/2
  - right/3
  - centre/2
  - centre/3
  - sub_string/2
  - sub_string/3
  - to_float/1
  - to_integer/1
  - to_lower/1
  - to_lower/1
  - to_upper/1
  - to_upper/1
- supervisor
  - Top of manual page
  - start_link/2
  - start_link/3
  - start_child/2
  - terminate_child/2
  - delete_child/2
  - restart_child/2
  - which_children/1
  - count_children/1
  - check_childspecs/1
  - Module:init/1
- supervisor_bridge
  - Top of manual page
  - start_link/2
  - start_link/3
  - Module:init/1
  - Module:terminate/2
- sys
  - Top of manual page
  - log/2
  - log/3
  - log_to_file/2
  - log_to_file/3
  - statistics/2
  - statistics/3
  - trace/2
  - trace/3
  - no_debug/1
  - no_debug/2
  - suspend/1
  - suspend/2
  - resume/1
  - resume/2
  - change_code/4
  - change_code/5
  - get_status/1
  - get_status/2
  - install/2
  - install/3
  - remove/2
  - remove/3
  - debug_options/1
  - get_debug/3
  - handle_debug/4
  - handle_system_msg/6
  - print_log/1
  - Mod:system_continue/3
  - Mod:system_terminate/4
  - Mod:system_code_change/4
- timer
  - Top of manual page
  - start/0
  - apply_after/4
  - send_after/3
  - send_after/2
  - exit_after/3
  - exit_after/2
  - kill_after/2
  - kill_after/1
  - apply_interval/4
  - send_interval/3
  - send_interval/2
  - cancel/1
  - sleep/1
  - tc/3
  - tc/2
  - now_diff/2
  - seconds/1
  - minutes/1
  - hours/1
  - hms/3
- unicode
  - Top of manual page
  - bom_to_encoding/1
  - characters_to_list/1
  - characters_to_list/2
  - characters_to_binary/1
  - characters_to_binary/2
  - characters_to_binary/3
  - encoding_to_bom/1
- win32reg
  - Top of manual page
  - change_key/2
  - change_key_create/2
  - close/1
  - current_key/1
  - delete_key/1
  - delete_value/2
  - expand/1
  - format_error/1
  - open/1
  - set_value/3
  - sub_keys/1
  - value/2
  - values/1
- zip
  - Top of manual page
  - zip/2
  - zip/3
  - create/2
  - create/3
  - unzip/1
  - unzip/2
  - extract/1
  - extract/2
  - foldl/3
  - list_dir/1
  - list_dir/2
  - table/1
  - table/2
  - t/1
  - tt/1
  - zip_open/1
  - zip_open/2
  - zip_list_dir/1
  - zip_get/1
  - zip_get/2
  - zip_close/1

# sofs

### MODULE

sofs

### MODULE SUMMARY

Functions for Manipulating Sets of Sets

### DESCRIPTION

The sofs module implements operations on finite sets and relations represented as sets. Intuitively, a set is a collection of elements; every element belongs to the set, and the set contains every element.

Given a set A and a sentence S(x), where x is a free variable, a new set B whose elements are exactly those elements of A for which S(x) holds can be formed, this is denoted B = {x in A : S(x)}. Sentences are expressed using the logical operators "for some" (or "there exists"), "for all", "and", "or", "not". If the existence of a set containing all the specified elements is known (as will always be the case in this module), we write B = {x : S(x)}.

The unordered set containing the elements a, b and c is denoted {a, b, c}. This notation is not to be confused with tuples. The ordered pair of a and b, with first coordinate a and second coordinate b, is denoted (a, b). An ordered pair is an ordered set of two elements. In this module ordered sets can contain one, two or more elements, and parentheses are used to enclose the elements. Unordered sets and ordered sets are orthogonal, again in this module; there is no unordered set equal to any ordered set.

The set that contains no elements is called the empty set. If two sets A and B contain the same elements, then A is equal to B, denoted A = B. Two ordered sets are equal if they contain the same number of elements and have equal elements at each coordinate. If a set A contains all elements that B contains, then B is a subset of A. The union of two sets A and B is the smallest set that contains all elements of A and all elements of B. The intersection of two sets A and B is the set that contains all elements of A that belong to B. Two sets are disjoint if their intersection is the empty set. The difference of two sets A and B is the set that contains all elements of A that do not belong to B. The symmetric difference of two sets is the set that contains those element that belong to either of the two sets, but not both. The union of a collection of sets is the smallest set that contains all the elements that belong to at least one set of the collection. The intersection of a non-empty collection of sets is the set that contains all elements that belong to every set of the collection.

The Cartesian product of two sets X and Y, denoted X × Y, is the set {a : a = (x, y) for some x in X and for some y in Y}. A relation is a subset of X × Y. Let R be a relation. The fact that (x, y) belongs to R is written as x R y. Since relations are sets, the definitions of the last paragraph (subset, union, and so on) apply to relations as well. The domain of R is the set {x : x R y for some y in Y}. The range of R is the set {y : x R y for some x in X}. The converse of R is the set {a : a = (y, x) for some (x, y) in R}. If A is a subset of X, then the image of A under R is the set {y : x R y for some x in A}, and if B is a subset of Y, then the inverse image of B is the set {x : x R y for some y in B}. If R is a relation from X to Y and S is a relation from Y to Z, then the relative product of R and S is the relation T from X to Z defined so that x T z if and only if there exists an element y in Y such that x R y and y S z. The restriction of R to A is the set S defined so that x S y if and only if there exists an element x in A such that x R y. If S is a restriction of R to A, then R is an extension of S to X. If X = Y then we call R a relation in X. The field of a relation R in X is the union of the domain of R and the range of R. If R is a relation in X, and if S is defined so that x S y if x R y and not x = y, then S is the strict relation corresponding to R, and vice versa, if S is a relation in X, and if R is defined so that x R y if x S y or x = y, then R is the weak relation corresponding to S. A relation R in X is reflexive if x R x for every element x of X; it is symmetric if x R y implies that y R x; and it is transitive if x R y and y R z imply that x R z.

A function F is a relation, a subset of X × Y, such that the domain of F is equal to X and such that for every x in X there is a unique element y in Y with (x, y) in F. The latter condition can be formulated as follows: if x F y and x F z then y = z. In this module, it will not be required that the domain of F be equal to X for a relation to be considered a function. Instead of writing (x, y) in F or x F y, we write F(x) = y when F is a function, and say that F maps x onto y, or that the value of F at x is y. Since functions are relations, the definitions of the last paragraph (domain, range, and so on) apply to functions as well. If the converse of a function F is a function F', then F' is called the inverse of F. The relative product of two functions F1 and F2 is called the composite of F1 and F2 if the range of F1 is a subset of the domain of F2.

Sometimes, when the range of a function is more important than the function itself, the function is called a family. The domain of a family is called the index set, and the range is called the indexed set. If x is a family from I to X, then x[i] denotes the value of the function at index i. The notation "a family in X" is used for such a family. When the indexed set is a set of subsets of a set X, then we call x a family of subsets of X. If x is a family of subsets of X, then the union of the range of x is called the union of the family x. If x is non-empty (the index set is non-empty), the intersection of the family x is the intersection of the range of x. In this module, the only families that will be considered are families of subsets of some set X; in the following the word "family" will be used for such families of subsets.

A partition of a set X is a collection S of non-empty subsets of X whose union is X and whose elements are pairwise disjoint. A relation in a set is an equivalence relation if it is reflexive, symmetric and transitive. If R is an equivalence relation in X, and x is an element of X, the equivalence class of x with respect to R is the set of all those elements y of X for which x R y holds. The equivalence classes constitute a partitioning of X. Conversely, if C is a partition of X, then the relation that holds for any two elements of X if they belong to the same equivalence class, is an equivalence relation induced by the partition C. If R is an equivalence relation in X, then the canonical map is the function that maps every element of X onto its equivalence class.

Relations as defined above (as sets of ordered pairs) will from now on be referred to as binary relations. We call a set of ordered sets (x[1], ..., x[n]) an (n-ary) relation, and say that the relation is a subset of the Cartesian product X[1] × ... × X[n] where x[i] is an element of X[i], 1 <= i <= n. The projection of an n-ary relation R onto coordinate i is the set {x[i] : (x[1], ..., x[i], ..., x[n]) in R for some x[j] in X[j], 1 <= j <= n and not i = j}. The projections of a binary relation R onto the first and second coordinates are the domain and the range of R respectively. The relative product of binary relations can be generalized to n-ary relations as follows. Let TR be an ordered set (R[1], ..., R[n]) of binary relations from X to Y[i] and S a binary relation from (Y[1] × ... × Y[n]) to Z. The relative product of TR and S is the binary relation T from X to Z defined so that x T z if and only if there exists an element y[i] in Y[i] for each 1 <= i <= n such that x R[i] y[i] and (y[1], ..., y[n]) S z. Now let TR be a an ordered set (R[1], ..., R[n]) of binary relations from X[i] to Y[i] and S a subset of X[1] × ... × X[n]. The multiple relative product of TR and S is defined to be the set {z : z = ((x[1], ..., x[n]), (y[1],...,y[n])) for some (x[1], ..., x[n]) in S and for some (x[i], y[i]) in R[i], 1 <= i <= n}. The natural join of an n-ary relation R and an m-ary relation S on coordinate i and j is defined to be the set {z : z = (x[1], ..., x[n], y[1], ..., y[j-1], y[j+1], ..., y[m]) for some (x[1], ..., x[n]) in R and for some (y[1], ..., y[m]) in S such that x[i] = y[j]}.

The sets recognized by this module will be represented by elements of the relation Sets, defined as the smallest set such that:

- for every atom T except '_' and for every term X, (T, X) belongs to Sets (atomic sets);
- (['_'], []) belongs to Sets (the untyped empty set);
- for every tuple T = {T[1], ..., T[n]} and for every tuple X = {X[1], ..., X[n]}, if (T[i], X[i]) belongs to Sets for every 1 <= i <= n then (T, X) belongs to Sets (ordered sets);
- for every term T, if X is the empty list or a non-empty sorted list [X[1], ..., X[n]] without duplicates such that (T, X[i]) belongs to Sets for every 1 <= i <= n, then ([T], X) belongs to Sets (typed unordered sets).

An external set is an element of the range of Sets. A type is an element of the domain of Sets. If S is an element (T, X) of Sets, then T is a valid type of X, T is the type of S, and X is the external set of S. from_term/2 creates a set from a type and an Erlang term turned into an external set.

The actual sets represented by Sets are the elements of the range of the function Set from Sets to Erlang terms and sets of Erlang terms:

- Set(T,Term) = Term, where T is an atom;
- Set({T[1], ..., T[n]}, {X[1], ..., X[n]}) = (Set(T[1], X[1]), ..., Set(T[n], X[n]));
- Set([T], [X[1], ..., X[n]]) = {Set(T, X[1]), ..., Set(T, X[n])};
- Set([T], []) = {}.

When there is no risk of confusion, elements of Sets will be identified with the sets they represent. For instance, if U is the result of calling union/2 with S1 and S2 as arguments, then U is said to be the union of S1 and S2. A more precise formulation would be that Set(U) is the union of Set(S1) and Set(S2).

The types are used to implement the various conditions that sets need to fulfill. As an example, consider the relative product of two sets R and S, and recall that the relative product of R and S is defined if R is a binary relation to Y and S is a binary relation from Y. The function that implements the relative product, relative_product/2, checks that the arguments represent binary relations by matching [{A,B}] against the type of the first argument (Arg1 say), and [{C,D}] against the type of the second argument (Arg2 say). The fact that [{A,B}] matches the type of Arg1 is to be interpreted as Arg1 representing a binary relation from X to Y, where X is defined as all sets Set(x) for some element x in Sets the type of which is A, and similarly for Y. In the same way Arg2 is interpreted as representing a binary relation from W to Z. Finally it is checked that B matches C, which is sufficient to ensure that W is equal to Y. The untyped empty set is handled separately: its type, ['_'], matches the type of any unordered set.

A few functions of this module (drestriction/3, family_projection/2, partition/2, partition_family/2, projection/2, restriction/3, substitution/2) accept an Erlang function as a means to modify each element of a given unordered set. Such a function, called SetFun in the following, can be specified as a functional object (fun), a tuple {external, Fun}, or an integer. If SetFun is specified as a fun, the fun is applied to each element of the given set and the return value is assumed to be a set. If SetFun is specified as a tuple {external, Fun}, Fun is applied to the external set of each element of the given set and the return value is assumed to be an external set. Selecting the elements of an unordered set as external sets and assembling a new unordered set from a list of external sets is in the present implementation more efficient than modifying each element as a set. However, this optimization can only be utilized when the elements of the unordered set are atomic or ordered sets. It must also be the case that the type of the elements matches some clause of Fun (the type of the created set is the result of applying Fun to the type of the given set), and that Fun does nothing but selecting, duplicating or rearranging parts of the elements. Specifying a SetFun as an integer I is equivalent to specifying {external, fun(X) -> element(I, X) end}, but is to be preferred since it makes it possible to handle this case even more efficiently. Examples of SetFuns:

    {sofs, union}
    fun(S) -> sofs:partition(1, S) end
    {external, fun(A) -> A end}
    {external, fun({A,_,C}) -> {C,A} end}
    {external, fun({_,{_,C}}) -> C end}
    {external, fun({_,{_,{_,E}=C}}) -> {E,{E,C}} end}
    2

The order in which a SetFun is applied to the elements of an unordered set is not specified, and may change in future versions of sofs.

The execution time of the functions of this module is dominated by the time it takes to sort lists. When no sorting is needed, the execution time is in the worst case proportional to the sum of the sizes of the input arguments and the returned value. A few functions execute in constant time: from_external, is_empty_set, is_set, is_sofs_set, to_external, type.

The functions of this module exit the process with a badarg, bad_function, or type_mismatch message when given badly formed arguments or sets the types of which are not compatible.

When comparing external sets the operator ==/2 is used.

Types

    anyset() = - an unordered, ordered or atomic set -
    binary_relation() = - a binary relation -
    bool() = true | false
    external_set() = - an external set -
    family() = - a family (of subsets) -
    function() = - a function -
    ordset() = - an ordered set -
    relation() = - an n-ary relation -
    set() = - an unordered set -
    set_of_sets() = - an unordered set of set() -
    set_fun() = integer() >= 1
              | {external, fun(external_set()) -> external_set()}
              | fun(anyset()) -> anyset()
    spec_fun() = {external, fun(external_set()) -> bool()}
               | fun(anyset()) -> bool()
    type() = - a type -

### EXPORTS

a_function(Tuples [, Type]) -> Function

Types:

Function = function()
Tuples = [tuple()]
Type = type()

Creates a function. a_function(F, T) is equivalent to from_term(F, T), if the result is a function. If no type is explicitly given, [{atom, atom}] is used as type of the function.

canonical_relation(SetOfSets) -> BinRel

Types:

BinRel = binary_relation()
SetOfSets = set_of_sets()

Returns the binary relation containing the elements (E, Set) such that Set belongs to SetOfSets and E belongs to Set. If SetOfSets is a partition of a set X and R is the equivalence relation in X induced by SetOfSets, then the returned relation is the canonical map from X onto the equivalence classes with respect to R.

    1> Ss = sofs:from_term([[a,b],[b,c]]),
    CR = sofs:canonical_relation(Ss),
    sofs:to_external(CR).
    [{a,[a,b]},{b,[a,b]},{b,[b,c]},{c,[b,c]}]

composite(Function1, Function2) -> Function3

Types:

Function1 = Function2 = Function3 = function()

Returns the composite of the functions Function1 and Function2.

    1> F1 = sofs:a_function([{a,1},{b,2},{c,2}]),
    F2 = sofs:a_function([{1,x},{2,y},{3,z}]),
    F = sofs:composite(F1, F2),
    sofs:to_external(F).
    [{a,x},{b,y},{c,y}]

constant_function(Set, AnySet) -> Function

Types:

AnySet = anyset()
Function = function()
Set = set()

Creates the function that maps each element of the set Set onto AnySet.

    1> S = sofs:set([a,b]),
    E = sofs:from_term(1),
    R = sofs:constant_function(S, E),
    sofs:to_external(R).
    [{a,1},{b,1}]

converse(BinRel1) -> BinRel2

Types:

BinRel1 = BinRel2 = binary_relation()

Returns the converse of the binary relation BinRel1.

    1> R1 = sofs:relation([{1,a},{2,b},{3,a}]),
    R2 = sofs:converse(R1),
    sofs:to_external(R2).
    [{a,1},{a,3},{b,2}]

difference(Set1, Set2) -> Set3

Types:

Set1 = Set2 = Set3 = set()

Returns the difference of the sets Set1 and Set2.

digraph_to_family(Graph [, Type]) -> Family

Types:

Graph = digraph() - see digraph(3) -
Family = family()
Type = type()

Creates a family from the directed graph Graph. Each vertex a of Graph is represented by a pair (a, {b[1], ..., b[n]}) where the b[i]'s are the out-neighbours of a. If no type is explicitly given, [{atom, [atom]}] is used as type of the family. It is assumed that Type is a valid type of the external set of the family.

If G is a directed graph, it holds that the vertices and edges of G are the same as the vertices and edges of family_to_digraph(digraph_to_family(G)).

domain(BinRel) -> Set

Types:

BinRel = binary_relation()
Set = set()

Returns the domain of the binary relation BinRel.

    1> R = sofs:relation([{1,a},{1,b},{2,b},{2,c}]),
    S = sofs:domain(R),
    sofs:to_external(S).
    [1,2]

drestriction(BinRel1, Set) -> BinRel2

Types:

BinRel1 = BinRel2 = binary_relation()
Set = set()

Returns the difference between the binary relation BinRel1 and the restriction of BinRel1 to Set.

    1> R1 = sofs:relation([{1,a},{2,b},{3,c}]),
    S = sofs:set([2,4,6]),
    R2 = sofs:drestriction(R1, S),
    sofs:to_external(R2).
    [{1,a},{3,c}]

drestriction(R, S) is equivalent to difference(R, restriction(R, S)).

drestriction(SetFun, Set1, Set2) -> Set3

Types:

SetFun = set_fun()
Set1 = Set2 = Set3 = set()

Returns a subset of Set1 containing those elements that do not yield an element in Set2 as the result of applying SetFun.

    1> SetFun = {external, fun({_A,B,C}) -> {B,C} end},
    R1 = sofs:relation([{a,aa,1},{b,bb,2},{c,cc,3}]),
    R2 = sofs:relation([{bb,2},{cc,3},{dd,4}]),
    R3 = sofs:drestriction(SetFun, R1, R2),
    sofs:to_external(R3).
    [{a,aa,1}]

drestriction(F, S1, S2) is equivalent to difference(S1, restriction(F, S1, S2)).

empty_set() -> Set

Types:

Set = set()

Returns the untyped empty set. empty_set() is equivalent to from_term([], ['_']).

extension(BinRel1, Set, AnySet) -> BinRel2

Types:

AnySet = anyset()
BinRel1 = BinRel2 = binary_relation()
Set = set()

Returns the extension of BinRel1 such that for each element E in Set that does not belong to the domain of BinRel1, BinRel2 contains the pair (E, AnySet).

    1> S = sofs:set([b,c]),
    A = sofs:empty_set(),
    R = sofs:family([{a,[1,2]},{b,[3]}]),
    X = sofs:extension(R, S, A),
    sofs:to_external(X).
    [{a,[1,2]},{b,[3]},{c,[]}]

family(Tuples [, Type]) -> Family

Types:

Family = family()
Tuples = [tuple()]
Type = type()

Creates a family of subsets. family(F, T) is equivalent to from_term(F, T), if the result is a family. If no type is explicitly given, [{atom, [atom]}] is used as type of the family.

family_difference(Family1, Family2) -> Family3

Types:

Family1 = Family2 = Family3 = family()

If Family1 and Family2 are families, then Family3 is the family such that the index set is equal to the index set of Family1, and Family3[i] is the difference between Family1[i] and Family2[i] if Family2 maps i, Family1[i] otherwise.

    1> F1 = sofs:family([{a,[1,2]},{b,[3,4]}]),
    F2 = sofs:family([{b,[4,5]},{c,[6,7]}]),
    F3 = sofs:family_difference(F1, F2),
    sofs:to_external(F3).
    [{a,[1,2]},{b,[3]}]

family_domain(Family1) -> Family2

Types:

Family1 = Family2 = family()

If Family1 is a family and Family1[i] is a binary relation for every i in the index set of Family1, then Family2 is the family with the same index set as Family1 such that Family2[i] is the domain of Family1[i].

    1> FR = sofs:from_term([{a,[{1,a},{2,b},{3,c}]},{b,[]},{c,[{4,d},{5,e}]}]),
    F = sofs:family_domain(FR),
    sofs:to_external(F).
    [{a,[1,2,3]},{b,[]},{c,[4,5]}]

family_field(Family1) -> Family2

Types:

Family1 = Family2 = family()

If Family1 is a family and Family1[i] is a binary relation for every i in the index set of Family1, then Family2 is the family with the same index set as Family1 such that Family2[i] is the field of Family1[i].

    1> FR = sofs:from_term([{a,[{1,a},{2,b},{3,c}]},{b,[]},{c,[{4,d},{5,e}]}]),
    F = sofs:family_field(FR),
    sofs:to_external(F).
    [{a,[1,2,3,a,b,c]},{b,[]},{c,[4,5,d,e]}]

family_field(Family1) is equivalent to family_union(family_domain(Family1), family_range(Family1)).

family_intersection(Family1) -> Family2

Types:

Family1 = Family2 = family()

If Family1 is a family and Family1[i] is a set of sets for every i in the index set of Family1, then Family2 is the family with the same index set as Family1 such that Family2[i] is the intersection of Family1[i].

If Family1[i] is an empty set for some i, then the process exits with a badarg message.

    1> F1 = sofs:from_term([{a,[[1,2,3],[2,3,4]]},{b,[[x,y,z],[x,y]]}]),
    F2 = sofs:family_intersection(F1),
    sofs:to_external(F2).
    [{a,[2,3]},{b,[x,y]}]

family_intersection(Family1, Family2) -> Family3

Types:

Family1 = Family2 = Family3 = family()

If Family1 and Family2 are families, then Family3 is the family such that the index set is the intersection of Family1's and Family2's index sets, and Family3[i] is the intersection of Family1[i] and Family2[i].

    1> F1 = sofs:family([{a,[1,2]},{b,[3,4]},{c,[5,6]}]),
    F2 = sofs:family([{b,[4,5]},{c,[7,8]},{d,[9,10]}]),
    F3 = sofs:family_intersection(F1, F2),
    sofs:to_external(F3).
    [{b,[4]},{c,[]}]

family_projection(SetFun, Family1) -> Family2

Types:

SetFun = set_fun()
Family1 = Family2 = family()
Set = set()

If Family1 is a family then Family2 is the family with the same index set as Family1 such that Family2[i] is the result of calling SetFun with Family1[i] as argument.

    1> F1 = sofs:from_term([{a,[[1,2],[2,3]]},{b,[[]]}]),
    F2 = sofs:family_projection({sofs, union}, F1),
    sofs:to_external(F2).
    [{a,[1,2,3]},{b,[]}]

family_range(Family1) -> Family2

Types:

Family1 = Family2 = family()

If Family1 is a family and Family1[i] is a binary relation for every i in the index set of Family1, then Family2 is the family with the same index set as Family1 such that Family2[i] is the range of Family1[i].

    1> FR = sofs:from_term([{a,[{1,a},{2,b},{3,c}]},{b,[]},{c,[{4,d},{5,e}]}]),
    F = sofs:family_range(FR),
    sofs:to_external(F).
    [{a,[a,b,c]},{b,[]},{c,[d,e]}]

family_specification(Fun, Family1) -> Family2

Types:

Fun = spec_fun()
Family1 = Family2 = family()

If Family1 is a family, then Family2 is the restriction of Family1 to those elements i of the index set for which Fun applied to Family1[i] returns true. If Fun is a tuple {external, Fun2}, Fun2 is applied to the external set of Family1[i], otherwise Fun is applied to Family1[i].

    1> F1 = sofs:family([{a,[1,2,3]},{b,[1,2]},{c,[1]}]),
    SpecFun = fun(S) -> sofs:no_elements(S) =:= 2 end,
    F2 = sofs:family_specification(SpecFun, F1),
    sofs:to_external(F2).
    [{b,[1,2]}]

family_to_digraph(Family [, GraphType]) -> Graph

Types:

Graph = digraph()
Family = family()
GraphType = - see digraph(3) -

Creates a directed graph from the family Family. For each pair (a, {b[1], ..., b[n]}) of Family, the vertex a as well the edges (a, b[i]) for 1 <= i <= n are added to a newly created directed graph.

If no graph type is given, digraph:new/1 is used for creating the directed graph, otherwise the GraphType argument is passed on as second argument to digraph:new/2.

It F is a family, it holds that F is a subset of digraph_to_family(family_to_digraph(F), type(F)). Equality holds if union_of_family(F) is a subset of domain(F).

Creating a cycle in an acyclic graph exits the process with a cyclic message.

family_to_relation(Family) -> BinRel

Types:

Family = family()
BinRel = binary_relation()

If Family is a family, then BinRel is the binary relation containing all pairs (i, x) such that i belongs to the index set of Family and x belongs to Family[i].

    1> F = sofs:family([{a,[]}, {b,[1]}, {c,[2,3]}]),
    R = sofs:family_to_relation(F),
    sofs:to_external(R).
    [{b,1},{c,2},{c,3}]

family_union(Family1) -> Family2

Types:

Family1 = Family2 = family()

If Family1 is a family and Family1[i] is a set of sets for each i in the index set of Family1, then Family2 is the family with the same index set as Family1 such that Family2[i] is the union of Family1[i].

    1> F1 = sofs:from_term([{a,[[1,2],[2,3]]},{b,[[]]}]),
    F2 = sofs:family_union(F1),
    sofs:to_external(F2).
    [{a,[1,2,3]},{b,[]}]

family_union(F) is equivalent to family_projection({sofs,union}, F).

family_union(Family1, Family2) -> Family3

Types:

Family1 = Family2 = Family3 = family()

If Family1 and Family2 are families, then Family3 is the family such that the index set is the union of Family1's and Family2's index sets, and Family3[i] is the union of Family1[i] and Family2[i] if both maps i, Family1[i] or Family2[i] otherwise.

    1> F1 = sofs:family([{a,[1,2]},{b,[3,4]},{c,[5,6]}]),
    F2 = sofs:family([{b,[4,5]},{c,[7,8]},{d,[9,10]}]),
    F3 = sofs:family_union(F1, F2),
    sofs:to_external(F3).
    [{a,[1,2]},{b,[3,4,5]},{c,[5,6,7,8]},{d,[9,10]}]

field(BinRel) -> Set

Types:

BinRel = binary_relation()
Set = set()

Returns the field of the binary relation BinRel.

    1> R = sofs:relation([{1,a},{1,b},{2,b},{2,c}]),
    S = sofs:field(R),
    sofs:to_external(S).
    [1,2,a,b,c]

field(R) is equivalent to union(domain(R), range(R)).

from_external(ExternalSet, Type) -> AnySet

Types:

ExternalSet = external_set()
AnySet = anyset()
Type = type()

Creates a set from the external set ExternalSet and the type Type. It is assumed that Type is a valid type of ExternalSet.

from_sets(ListOfSets) -> Set

Types:

Set = set()
ListOfSets = [anyset()]

Returns the unordered set containing the sets of the list ListOfSets.

    1> S1 = sofs:relation([{a,1},{b,2}]),
    S2 = sofs:relation([{x,3},{y,4}]),
    S = sofs:from_sets([S1,S2]),
    sofs:to_external(S).
    [[{a,1},{b,2}],[{x,3},{y,4}]]

from_sets(TupleOfSets) -> Ordset

Types:

Ordset = ordset()
TupleOfSets = tuple-of(anyset())

Returns the ordered set containing the sets of the non-empty tuple TupleOfSets.

from_term(Term [, Type]) -> AnySet

Types:

AnySet = anyset()
Term = term()
Type = type()

Creates an element of Sets by traversing the term Term, sorting lists, removing duplicates and deriving or verifying a valid type for the so obtained external set. An explicitly given type Type can be used to limit the depth of the traversal; an atomic type stops the traversal, as demonstrated by this example where "foo" and {"foo"} are left unmodified:

    1> S = sofs:from_term([{{"foo"},[1,1]},{"foo",[2,2]}], [{atom,[atom]}]),
    sofs:to_external(S).
    [{{"foo"},[1]},{"foo",[2]}]

from_term can be used for creating atomic or ordered sets. The only purpose of such a set is that of later building unordered sets since all functions in this module that do anything operate on unordered sets. Creating unordered sets from a collection of ordered sets may be the way to go if the ordered sets are big and one does not want to waste heap by rebuilding the elements of the unordered set. An example showing that a set can be built "layer by layer":

    1> A = sofs:from_term(a),
    S = sofs:set([1,2,3]),
    P1 = sofs:from_sets({A,S}),
    P2 = sofs:from_term({b,[6,5,4]}),
    Ss = sofs:from_sets([P1,P2]),
    sofs:to_external(Ss).
    [{a,[1,2,3]},{b,[4,5,6]}]

Other functions that create sets are from_external/2 and from_sets/1. Special cases of from_term/2 are a_function/1,2, empty_set/0, family/1,2, relation/1,2, and set/1,2.

image(BinRel, Set1) -> Set2

Types:

BinRel = binary_relation()
Set1 = Set2 = set()

Returns the image of the set Set1 under the binary relation BinRel.

    1> R = sofs:relation([{1,a},{2,b},{2,c},{3,d}]),
    S1 = sofs:set([1,2]),
    S2 = sofs:image(R, S1),
    sofs:to_external(S2).
    [a,b,c]

intersection(SetOfSets) -> Set

Types:

Set = set()
SetOfSets = set_of_sets()

Returns the intersection of the set of sets SetOfSets.

Intersecting an empty set of sets exits the process with a badarg message.

intersection(Set1, Set2) -> Set3

Types:

Set1 = Set2 = Set3 = set()

Returns the intersection of Set1 and Set2.

intersection_of_family(Family) -> Set

Types:

Family = family()
Set = set()

Returns the intersection of the family Family.

Intersecting an empty family exits the process with a badarg message.

    1> F = sofs:family([{a,[0,2,4]},{b,[0,1,2]},{c,[2,3]}]),
    S = sofs:intersection_of_family(F),
    sofs:to_external(S).
    [2]

inverse(Function1) -> Function2

Types:

Function1 = Function2 = function()

Returns the inverse of the function Function1.

    1> R1 = sofs:relation([{1,a},{2,b},{3,c}]),
    R2 = sofs:inverse(R1),
    sofs:to_external(R2).
    [{a,1},{b,2},{c,3}]

inverse_image(BinRel, Set1) -> Set2

Types:

BinRel = binary_relation()
Set1 = Set2 = set()

Returns the inverse image of Set1 under the binary relation BinRel.

    1> R = sofs:relation([{1,a},{2,b},{2,c},{3,d}]),
    S1 = sofs:set([c,d,e]),
    S2 = sofs:inverse_image(R, S1),
    sofs:to_external(S2).
    [2,3]

is_a_function(BinRel) -> Bool

Types:

Bool = bool()
BinRel = binary_relation()

Returns true if the binary relation BinRel is a function or the untyped empty set, false otherwise.

is_disjoint(Set1, Set2) -> Bool

Types:

Bool = bool()
Set1 = Set2 = set()

Returns true if Set1 and Set2 are disjoint, false otherwise.

is_empty_set(AnySet) -> Bool

Types:

AnySet = anyset()
Bool = bool()

Returns true if Set is an empty unordered set, false otherwise.

is_equal(AnySet1, AnySet2) -> Bool

Types:

AnySet1 = AnySet2 = anyset()
Bool = bool()

Returns true if the AnySet1 and AnySet2 are equal, false otherwise. This example shows that ==/2 is used when comparing sets for equality:

    1> S1 = sofs:set([1.0]),
    S2 = sofs:set([1]),
    sofs:is_equal(S1, S2).
    true

is_set(AnySet) -> Bool

Types:

AnySet = anyset()
Bool = bool()

Returns true if AnySet is an unordered set, and false if AnySet is an ordered set or an atomic set.

is_sofs_set(Term) -> Bool

Types:

Bool = bool()
Term = term()

Returns true if Term is an unordered set, an ordered set or an atomic set, false otherwise.

is_subset(Set1, Set2) -> Bool

Types:

Bool = bool()
Set1 = Set2 = set()

Returns true if Set1 is a subset of Set2, false otherwise.

is_type(Term) -> Bool

Types:

Bool = bool()
Term = term()

Returns true if the term Term is a type.

join(Relation1, I, Relation2, J) -> Relation3

Types:

Relation1 = Relation2 = Relation3 = relation()
I = J = integer() > 0

Returns the natural join of the relations Relation1 and Relation2 on coordinates I and J.

    1> R1 = sofs:relation([{a,x,1},{b,y,2}]),
    R2 = sofs:relation([{1,f,g},{1,h,i},{2,3,4}]),
    J = sofs:join(R1, 3, R2, 1),
    sofs:to_external(J).
    [{a,x,1,f,g},{a,x,1,h,i},{b,y,2,3,4}]

multiple_relative_product(TupleOfBinRels, BinRel1) -> BinRel2

Types:

TupleOfBinRels = tuple-of(BinRel)
BinRel = BinRel1 = BinRel2 = binary_relation()

If TupleOfBinRels is a non-empty tuple {R[1], ..., R[n]} of binary relations and BinRel1 is a binary relation, then BinRel2 is the multiple relative product of the ordered set (R[i], ..., R[n]) and BinRel1.

    1> Ri = sofs:relation([{a,1},{b,2},{c,3}]),
    R = sofs:relation([{a,b},{b,c},{c,a}]),
    MP = sofs:multiple_relative_product({Ri, Ri}, R),
    sofs:to_external(sofs:range(MP)).
    [{1,2},{2,3},{3,1}]

no_elements(ASet) -> NoElements

Types:

ASet = set() | ordset()
NoElements = integer() >= 0

Returns the number of elements of the ordered or unordered set ASet.

partition(SetOfSets) -> Partition

Types:

SetOfSets = set_of_sets()
Partition = set()

Returns the partition of the union of the set of sets SetOfSets such that two elements are considered equal if they belong to the same elements of SetOfSets.

    1> Sets1 = sofs:from_term([[a,b,c],[d,e,f],[g,h,i]]),
    Sets2 = sofs:from_term([[b,c,d],[e,f,g],[h,i,j]]),
    P = sofs:partition(sofs:union(Sets1, Sets2)),
    sofs:to_external(P).
    [[a],[b,c],[d],[e,f],[g],[h,i],[j]]

partition(SetFun, Set) -> Partition

Types:

SetFun = set_fun()
Partition = set()
Set = set()

Returns the partition of Set such that two elements are considered equal if the results of applying SetFun are equal.

    1> Ss = sofs:from_term([[a],[b],[c,d],[e,f]]),
    SetFun = fun(S) -> sofs:from_term(sofs:no_elements(S)) end,
    P = sofs:partition(SetFun, Ss),
    sofs:to_external(P).
    [[[a],[b]],[[c,d],[e,f]]]

partition(SetFun, Set1, Set2) -> {Set3, Set4}

Types:

SetFun = set_fun()
Set1 = Set2 = Set3 = Set4 = set()

Returns a pair of sets that, regarded as constituting a set, forms a partition of Set1. If the result of applying SetFun to an element of Set1 yields an element in Set2, the element belongs to Set3, otherwise the element belongs to Set4.

    1> R1 = sofs:relation([{1,a},{2,b},{3,c}]),
    S = sofs:set([2,4,6]),
    {R2,R3} = sofs:partition(1, R1, S),
    {sofs:to_external(R2),sofs:to_external(R3)}.
    {[{2,b}],[{1,a},{3,c}]}

partition(F, S1, S2) is equivalent to {restriction(F, S1, S2), drestriction(F, S1, S2)}.

partition_family(SetFun, Set) -> Family

Types:

Family = family()
SetFun = set_fun()
Set = set()

Returns the family Family where the indexed set is a partition of Set such that two elements are considered equal if the results of applying SetFun are the same value i. This i is the index that Family maps onto the equivalence class.

    1> S = sofs:relation([{a,a,a,a},{a,a,b,b},{a,b,b,b}]),
    SetFun = {external, fun({A,_,C,_}) -> {A,C} end},
    F = sofs:partition_family(SetFun, S),
    sofs:to_external(F).
    [{{a,a},[{a,a,a,a}]},{{a,b},[{a,a,b,b},{a,b,b,b}]}]

product(TupleOfSets) -> Relation

Types:

Relation = relation()
TupleOfSets = tuple-of(set())

Returns the Cartesian product of the non-empty tuple of sets TupleOfSets. If (x[1], ..., x[n]) is an element of the n-ary relation Relation, then x[i] is drawn from element i of TupleOfSets.

    1> S1 = sofs:set([a,b]),
    S2 = sofs:set([1,2]),
    S3 = sofs:set([x,y]),
    P3 = sofs:product({S1,S2,S3}),
    sofs:to_external(P3).
    [{a,1,x},{a,1,y},{a,2,x},{a,2,y},{b,1,x},{b,1,y},{b,2,x},{b,2,y}]

product(Set1, Set2) -> BinRel

Types:

BinRel = binary_relation()
Set1 = Set2 = set()

Returns the Cartesian product of Set1 and Set2.

    1> S1 = sofs:set([1,2]),
    S2 = sofs:set([a,b]),
    R = sofs:product(S1, S2),
    sofs:to_external(R).
    [{1,a},{1,b},{2,a},{2,b}]

product(S1, S2) is equivalent to product({S1, S2}).

projection(SetFun, Set1) -> Set2

Types:

SetFun = set_fun()
Set1 = Set2 = set()

Returns the set created by substituting each element of Set1 by the result of applying SetFun to the element.

If SetFun is a number i >= 1 and Set1 is a relation, then the returned set is the projection of Set1 onto coordinate i.

    1> S1 = sofs:from_term([{1,a},{2,b},{3,a}]),
    S2 = sofs:projection(2, S1),
    sofs:to_external(S2).
    [a,b]

range(BinRel) -> Set

Types:

BinRel = binary_relation()
Set = set()

Returns the range of the binary relation BinRel.

    1> R = sofs:relation([{1,a},{1,b},{2,b},{2,c}]),
    S = sofs:range(R),
    sofs:to_external(S).
    [a,b,c]

relation(Tuples [, Type]) -> Relation

Types:

N = integer()
Type = N | type()
Relation = relation()
Tuples = [tuple()]

Creates a relation. relation(R, T) is equivalent to from_term(R, T), if T is a type and the result is a relation. If Type is an integer N, then [{atom, ..., atom}]), where the size of the tuple is N, is used as type of the relation. If no type is explicitly given, the size of the first tuple of Tuples is used if there is such a tuple. relation([]) is equivalent to relation([], 2).

relation_to_family(BinRel) -> Family

Types:

Family = family()
BinRel = binary_relation()

Returns the family Family such that the index set is equal to the domain of the binary relation BinRel, and Family[i] is the image of the set of i under BinRel.

    1> R = sofs:relation([{b,1},{c,2},{c,3}]),
    F = sofs:relation_to_family(R),
    sofs:to_external(F).
    [{b,[1]},{c,[2,3]}]

relative_product(TupleOfBinRels [, BinRel1]) -> BinRel2

Types:

TupleOfBinRels = tuple-of(BinRel)
BinRel = BinRel1 = BinRel2 = binary_relation()

If TupleOfBinRels is a non-empty tuple {R[1], ..., R[n]} of binary relations and BinRel1 is a binary relation, then BinRel2 is the relative product of the ordered set (R[i], ..., R[n]) and BinRel1.

If BinRel1 is omitted, the relation of equality between the elements of the Cartesian product of the ranges of R[i], range R[1] × ... × range R[n], is used instead (intuitively, nothing is "lost").

    1> TR = sofs:relation([{1,a},{1,aa},{2,b}]),
    R1 = sofs:relation([{1,u},{2,v},{3,c}]),
    R2 = sofs:relative_product({TR, R1}),
    sofs:to_external(R2).
    [{1,{a,u}},{1,{aa,u}},{2,{b,v}}]

Note that relative_product({R1}, R2) is different from relative_product(R1, R2); the tuple of one element is not identified with the element itself.

relative_product(BinRel1, BinRel2) -> BinRel3

Types:

BinRel1 = BinRel2 = BinRel3 = binary_relation()

Returns the relative product of the binary relations BinRel1 and BinRel2.

relative_product1(BinRel1, BinRel2) -> BinRel3

Types:

BinRel1 = BinRel2 = BinRel3 = binary_relation()

Returns the relative product of the converse of the binary relation BinRel1 and the binary relation BinRel2.

    1> R1 = sofs:relation([{1,a},{1,aa},{2,b}]),
    R2 = sofs:relation([{1,u},{2,v},{3,c}]),
    R3 = sofs:relative_product1(R1, R2),
    sofs:to_external(R3).
    [{a,u},{aa,u},{b,v}]

relative_product1(R1, R2) is equivalent to relative_product(converse(R1), R2).

restriction(BinRel1, Set) -> BinRel2

Types:

BinRel1 = BinRel2 = binary_relation()
Set = set()

Returns the restriction of the binary relation BinRel1 to Set.

    1> R1 = sofs:relation([{1,a},{2,b},{3,c}]),
    S = sofs:set([1,2,4]),
    R2 = sofs:restriction(R1, S),
    sofs:to_external(R2).
    [{1,a},{2,b}]

restriction(SetFun, Set1, Set2) -> Set3

Types:

SetFun = set_fun()
Set1 = Set2 = Set3 = set()

Returns a subset of Set1 containing those elements that yield an element in Set2 as the result of applying SetFun.

    1> S1 = sofs:relation([{1,a},{2,b},{3,c}]),
    S2 = sofs:set([b,c,d]),
    S3 = sofs:restriction(2, S1, S2),
    sofs:to_external(S3).
    [{2,b},{3,c}]

set(Terms [, Type]) -> Set

Types:

Set = set()
Terms = [term()]
Type = type()

Creates an unordered set. set(L, T) is equivalent to from_term(L, T), if the result is an unordered set. If no type is explicitly given, [atom] is used as type of the set.

specification(Fun, Set1) -> Set2

Types:

Fun = spec_fun()
Set1 = Set2 = set()

Returns the set containing every element of Set1 for which Fun returns true. If Fun is a tuple {external, Fun2}, Fun2 is applied to the external set of each element, otherwise Fun is applied to each element.

    1> R1 = sofs:relation([{a,1},{b,2}]),
    R2 = sofs:relation([{x,1},{x,2},{y,3}]),
    S1 = sofs:from_sets([R1,R2]),
    S2 = sofs:specification({sofs,is_a_function}, S1),
    sofs:to_external(S2).
    [[{a,1},{b,2}]]

strict_relation(BinRel1) -> BinRel2

Types:

BinRel1 = BinRel2 = binary_relation()

Returns the strict relation corresponding to the binary relation BinRel1.

    1> R1 = sofs:relation([{1,1},{1,2},{2,1},{2,2}]),
    R2 = sofs:strict_relation(R1),
    sofs:to_external(R2).
    [{1,2},{2,1}]

substitution(SetFun, Set1) -> Set2

Types:

SetFun = set_fun()
Set1 = Set2 = set()

Returns a function, the domain of which is Set1. The value of an element of the domain is the result of applying SetFun to the element.

    1> L = [{a,1},{b,2}].
    [{a,1},{b,2}]
    2> sofs:to_external(sofs:projection(1,sofs:relation(L))).
    [a,b]
    3> sofs:to_external(sofs:substitution(1,sofs:relation(L))).
    [{{a,1},a},{{b,2},b}]
    4> SetFun = {external, fun({A,_}=E) -> {E,A} end},
    sofs:to_external(sofs:projection(SetFun,sofs:relation(L))).
    [{{a,1},a},{{b,2},b}]

The relation of equality between the elements of {a,b,c}:

    1> I = sofs:substitution(fun(A) -> A end, sofs:set([a,b,c])),
    sofs:to_external(I).
    [{a,a},{b,b},{c,c}]

Let SetOfSets be a set of sets and BinRel a binary relation. The function that maps each element Set of SetOfSets onto the image of Set under BinRel is returned by this function:

    images(SetOfSets, BinRel) ->
       Fun = fun(Set) -> sofs:image(BinRel, Set) end,
       sofs:substitution(Fun, SetOfSets).

Here might be the place to reveal something that was more or less stated before, namely that external unordered sets are represented as sorted lists. As a consequence, creating the image of a set under a relation R may traverse all elements of R (to that comes the sorting of results, the image). In images/2, BinRel will be traversed once for each element of SetOfSets, which may take too long. The following efficient function could be used instead under the assumption that the image of each element of SetOfSets under BinRel is non-empty:

    images2(SetOfSets, BinRel) ->
       CR = sofs:canonical_relation(SetOfSets),
       R = sofs:relative_product1(CR, BinRel),
       sofs:relation_to_family(R).

symdiff(Set1, Set2) -> Set3

Types:

Set1 = Set2 = Set3 = set()

Returns the symmetric difference (or the Boolean sum) of Set1 and Set2.

    1> S1 = sofs:set([1,2,3]),
    S2 = sofs:set([2,3,4]),
    P = sofs:symdiff(S1, S2),
    sofs:to_external(P).
    [1,4]

symmetric_partition(Set1, Set2) -> {Set3, Set4, Set5}

Types:

Set1 = Set2 = Set3 = Set4 = Set5 = set()

Returns a triple of sets: Set3 contains the elements of Set1 that do not belong to Set2; Set4 contains the elements of Set1 that belong to Set2; Set5 contains the elements of Set2 that do not belong to Set1.

to_external(AnySet) -> ExternalSet

Types:

ExternalSet = external_set()
AnySet = anyset()

Returns the external set of an atomic, ordered or unordered set.

to_sets(ASet) -> Sets

Types:

ASet = set() | ordset()
Sets = tuple_of(AnySet) | [AnySet]

Returns the elements of the ordered set ASet as a tuple of sets, and the elements of the unordered set ASet as a sorted list of sets without duplicates.

type(AnySet) -> Type

Types:

AnySet = anyset()
Type = type()

Returns the type of an atomic, ordered or unordered set.

union(SetOfSets) -> Set

Types:

Set = set()
SetOfSets = set_of_sets()

Returns the union of the set of sets SetOfSets.

union(Set1, Set2) -> Set3

Types:

Set1 = Set2 = Set3 = set()

Returns the union of Set1 and Set2.

union_of_family(Family) -> Set

Types:

Family = family()
Set = set()

Returns the union of the family Family.

    1> F = sofs:family([{a,[0,2,4]},{b,[0,1,2]},{c,[2,3]}]),
    S = sofs:union_of_family(F),
    sofs:to_external(S).
    [0,1,2,3,4]

weak_relation(BinRel1) -> BinRel2

Types:

BinRel1 = BinRel2 = binary_relation()

Returns a subset S of the weak relation W corresponding to the binary relation BinRel1. Let F be the field of BinRel1. The subset S is defined so that x S y if x W y for some x in F and for some y in F.

    1> R1 = sofs:relation([{1,1},{1,2},{3,1}]),
    R2 = sofs:weak_relation(R1),
    sofs:to_external(R2).
    [{1,1},{1,2},{2,2},{3,1},{3,3}]

### See Also

dict(3), digraph(3), orddict(3), ordsets(3), sets(3)