Erlang -- qlc

STDLIB

Reference Manual

Version 3.13

- User's Guide
- Reference Manual
- Release Notes
- PDF
- Top

- 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
- assert.hrl
- base64
  - Top of manual page
  - decode/1
  - decode_to_string/1
  - encode/1
  - encode_to_string/1
  - mime_decode/1
  - mime_decode_to_string/1
- beam_lib
  - Top of manual page
  - all_chunks/1
  - build_module/1
  - chunks/2
  - chunks/3
  - clear_crypto_key_fun/0
  - cmp/2
  - cmp_dirs/2
  - crypto_key_fun/1
  - diff_dirs/2
  - format_error/1
  - info/1
  - md5/1
  - strip/1
  - strip/2
  - strip_files/1
  - strip_files/2
  - strip_release/1
  - strip_release/2
  - version/1
- 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
  - c/3
  - cd/1
  - erlangrc/1
  - flush/0
  - h/1
  - h/2
  - h/3
  - help/0
  - ht/1
  - ht/2
  - ht/3
  - i/0
  - i/3
  - l/1
  - lc/1
  - lm/0
  - ls/0
  - ls/1
  - m/0
  - m/1
  - memory/0
  - memory/1
  - mm/0
  - nc/1
  - nc/2
  - ni/0
  - nl/1
  - nregs/0
  - pid/3
  - pwd/0
  - q/0
  - regs/0
  - uptime/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_datetime/1
  - now_to_local_time/1
  - now_to_universal_time/1
  - rfc3339_to_system_time/1
  - rfc3339_to_system_time/2
  - seconds_to_daystime/1
  - seconds_to_time/1
  - system_time_to_local_time/2
  - system_time_to_rfc3339/1
  - system_time_to_rfc3339/2
  - system_time_to_universal_time/2
  - 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
  - init_table/3
  - 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
  - table/2
  - 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_empty/1
  - is_key/2
  - map/2
  - merge/3
  - new/0
  - size/1
  - store/3
  - take/2
  - to_list/1
  - update/3
  - update/4
  - update_counter/3
- digraph
  - Top of manual page
  - add_edge/3
  - add_edge/4
  - add_edge/5
  - add_vertex/1
  - add_vertex/2
  - add_vertex/3
  - 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
  - subgraph/3
  - topsort/1
- epp
  - Top of manual page
  - close/1
  - default_encoding/0
  - encoding_to_string/1
  - format_error/1
  - open/1
  - open/2
  - open/3
  - parse_erl_form/1
  - parse_file/2
  - parse_file/3
  - read_encoding/1
  - read_encoding/2
  - read_encoding_from_binary/1
  - read_encoding_from_binary/2
  - set_encoding/1
  - set_encoding/2
- erl_anno
  - Top of manual page
  - column/1
  - end_location/1
  - file/1
  - from_term/1
  - generated/1
  - is_anno/1
  - line/1
  - location/1
  - new/1
  - set_file/2
  - set_generated/2
  - set_line/2
  - set_location/2
  - set_record/2
  - set_text/2
  - text/1
  - to_term/1
- erl_eval
  - Top of manual page
  - add_binding/3
  - binding/2
  - bindings/1
  - del_binding/2
  - expr/2
  - expr/3
  - expr/4
  - expr/5
  - expr_list/2
  - expr_list/3
  - expr_list/4
  - exprs/2
  - exprs/3
  - exprs/4
  - new_bindings/0
- 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
  - add_predefined_functions/1
  - arith_op/2
  - bif/2
  - bool_op/2
  - comp_op/2
  - guard_bif/2
  - list_op/2
  - op_type/2
  - send_op/2
  - type_test/2
- erl_lint
  - Top of manual page
  - format_error/1
  - is_guard_test/1
  - module/1
  - module/2
  - module/3
- erl_parse
  - Top of manual page
  - abstract/1
  - abstract/2
  - anno_from_term/1
  - anno_to_term/1
  - fold_anno/3
  - format_error/1
  - map_anno/2
  - mapfold_anno/3
  - new_anno/1
  - normalise/1
  - parse_exprs/1
  - parse_form/1
  - parse_term/1
  - tokens/1
  - tokens/2
- erl_pp
  - Top of manual page
  - attribute/1
  - attribute/2
  - expr/1
  - expr/2
  - expr/3
  - expr/4
  - exprs/1
  - exprs/2
  - exprs/3
  - form/1
  - form/2
  - function/1
  - function/2
  - guard/1
  - guard/2
- erl_scan
  - Top of manual page
  - category/1
  - column/1
  - end_location/1
  - format_error/1
  - line/1
  - location/1
  - reserved_word/1
  - string/1
  - string/2
  - string/3
  - symbol/1
  - text/1
  - tokens/3
  - tokens/4
- erl_tar
  - Top of manual page
  - add/3
  - add/4
  - close/1
  - create/2
  - create/3
  - extract/1
  - extract/2
  - format_error/1
  - init/3
  - open/2
  - t/1
  - table/1
  - table/2
  - 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/1
  - match/2
  - match/3
  - match_delete/2
  - match_object/1
  - match_object/2
  - match_object/3
  - 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/1
  - select/2
  - select/3
  - select_count/2
  - select_delete/2
  - select_replace/2
  - select_reverse/1
  - select_reverse/2
  - select_reverse/3
  - setopts/2
  - slot/2
  - tab2file/2
  - tab2file/3
  - tab2list/1
  - tabfile_info/1
  - table/1
  - table/2
  - take/2
  - test_ms/2
  - to_dets/2
  - update_counter/3
  - update_counter/4
  - update_element/3
  - whereis/1
- file_sorter
  - Top of manual page
  - check/1
  - check/2
  - keycheck/2
  - keycheck/3
  - keymerge/3
  - keymerge/4
  - keysort/2
  - keysort/3
  - keysort/4
  - merge/2
  - merge/3
  - sort/1
  - sort/2
  - sort/3
- filelib
  - Top of manual page
  - ensure_dir/1
  - file_size/1
  - find_file/2
  - find_file/3
  - find_source/1
  - find_source/2
  - find_source/3
  - fold_files/5
  - is_dir/1
  - is_file/1
  - is_regular/1
  - last_modified/1
  - safe_relative_path/2
  - wildcard/1
  - wildcard/2
- filename
  - Top of manual page
  - absname/1
  - absname/2
  - absname_join/2
  - basedir/2
  - basedir/3
  - basename/1
  - basename/2
  - dirname/1
  - extension/1
  - find_src/1
  - find_src/2
  - flatten/1
  - join/1
  - join/2
  - nativename/1
  - pathtype/1
  - rootname/1
  - rootname/2
  - safe_relative_path/1
  - split/1
- gb_sets
  - Top of manual page
  - add/2
  - add_element/2
  - balance/1
  - del_element/2
  - delete/2
  - delete_any/2
  - difference/2
  - empty/0
  - filter/2
  - fold/3
  - from_list/1
  - from_ordset/1
  - insert/2
  - intersection/1
  - intersection/2
  - is_disjoint/2
  - is_element/2
  - is_empty/1
  - is_member/2
  - is_set/1
  - is_subset/2
  - iterator/1
  - iterator_from/2
  - largest/1
  - new/0
  - next/1
  - singleton/1
  - size/1
  - smallest/1
  - subtract/2
  - take_largest/1
  - take_smallest/1
  - to_list/1
  - union/1
  - union/2
- gb_trees
  - Top of manual page
  - balance/1
  - delete/2
  - delete_any/2
  - empty/0
  - enter/3
  - from_orddict/1
  - get/2
  - insert/3
  - is_defined/2
  - is_empty/1
  - iterator/1
  - iterator_from/2
  - keys/1
  - largest/1
  - lookup/2
  - map/2
  - next/1
  - size/1
  - smallest/1
  - take/2
  - take_any/2
  - take_largest/1
  - take_smallest/1
  - to_list/1
  - update/3
  - values/1
- gen_event
  - Top of manual page
  - add_handler/3
  - add_sup_handler/3
  - call/3
  - call/4
  - check_response/2
  - delete_handler/3
  - notify/2
  - send_request/3
  - start/0
  - start/1
  - start/2
  - start_link/0
  - start_link/1
  - start_link/2
  - start_monitor/0
  - start_monitor/1
  - start_monitor/2
  - stop/1
  - stop/3
  - swap_handler/3
  - swap_sup_handler/3
  - sync_notify/2
  - wait_response/2
  - which_handlers/1
  - Module:code_change/3
  - Module:format_status/2
  - Module:handle_call/2
  - Module:handle_event/2
  - Module:handle_info/2
  - Module:init/1
  - Module:terminate/2
- gen_fsm
- gen_server
  - Top of manual page
  - abcast/2
  - abcast/3
  - call/2
  - call/3
  - cast/2
  - check_response/2
  - enter_loop/3
  - enter_loop/4
  - enter_loop/4
  - enter_loop/5
  - multi_call/2
  - multi_call/3
  - multi_call/4
  - reply/2
  - send_request/2
  - start/3
  - start/4
  - start_link/3
  - start_link/4
  - start_monitor/3
  - start_monitor/4
  - stop/1
  - stop/3
  - wait_response/2
  - Module:code_change/3
  - Module:format_status/2
  - Module:handle_call/3
  - Module:handle_cast/2
  - Module:handle_continue/2
  - Module:handle_info/2
  - Module:init/1
  - Module:terminate/2
- gen_statem
  - Top of manual page
  - call/2
  - call/3
  - cast/2
  - check_response/2
  - enter_loop/4
  - enter_loop/5
  - enter_loop/6
  - reply/1
  - reply/2
  - send_request/2
  - start/3
  - start/4
  - start_link/3
  - start_link/4
  - start_monitor/3
  - start_monitor/4
  - stop/1
  - stop/3
  - wait_response/1
  - wait_response/2
  - Module:StateName/3
  - Module:StateName/3
  - Module:callback_mode/0
  - Module:code_change/4
  - Module:format_status/2
  - Module:handle_event/4
  - Module:handle_event/4
  - Module:init/1
  - Module:terminate/3
- io
  - Top of manual page
  - columns/0
  - columns/1
  - format/1
  - format/2
  - format/3
  - fread/2
  - fread/3
  - fwrite/1
  - fwrite/2
  - fwrite/3
  - get_chars/2
  - get_chars/3
  - get_line/1
  - get_line/2
  - getopts/0
  - getopts/1
  - nl/0
  - nl/1
  - parse_erl_exprs/1
  - parse_erl_exprs/2
  - parse_erl_exprs/3
  - parse_erl_exprs/4
  - parse_erl_form/1
  - parse_erl_form/2
  - parse_erl_form/3
  - parse_erl_form/4
  - printable_range/0
  - put_chars/1
  - put_chars/2
  - read/1
  - read/2
  - read/3
  - read/4
  - rows/0
  - rows/1
  - scan_erl_exprs/1
  - scan_erl_exprs/2
  - scan_erl_exprs/3
  - scan_erl_exprs/4
  - scan_erl_form/1
  - scan_erl_form/2
  - scan_erl_form/3
  - scan_erl_form/4
  - setopts/1
  - setopts/2
  - write/1
  - write/2
- io_lib
  - Top of manual page
  - build_text/1
  - char_list/1
  - deep_char_list/1
  - deep_latin1_char_list/1
  - format/2
  - format/3
  - fread/2
  - fread/3
  - fwrite/2
  - fwrite/3
  - indentation/2
  - latin1_char_list/1
  - nl/0
  - print/1
  - print/4
  - printable_latin1_list/1
  - printable_list/1
  - printable_unicode_list/1
  - scan_format/2
  - unscan_format/1
  - write/1
  - write/2
  - write_atom/1
  - write_atom_as_latin1/1
  - write_char/1
  - write_char_as_latin1/1
  - write_latin1_char/1
  - write_latin1_string/1
  - write_string/1
  - write_string_as_latin1/1
- lists
  - Top of manual page
  - all/2
  - any/2
  - append/1
  - append/2
  - concat/1
  - delete/2
  - droplast/1
  - dropwhile/2
  - duplicate/2
  - filter/2
  - filtermap/2
  - flatlength/1
  - flatmap/2
  - flatten/1
  - flatten/2
  - foldl/3
  - foldr/3
  - foreach/2
  - join/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
  - search/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
- maps
  - Top of manual page
  - filter/2
  - find/2
  - fold/3
  - from_list/1
  - get/2
  - get/3
  - is_key/2
  - iterator/1
  - keys/1
  - map/2
  - merge/2
  - new/0
  - next/1
  - put/3
  - remove/2
  - size/1
  - take/2
  - to_list/1
  - update/3
  - update_with/3
  - update_with/4
  - values/1
  - with/2
  - without/2
- math
  - Top of manual page
  - acos/1
  - acosh/1
  - asin/1
  - asinh/1
  - atan/1
  - atan2/2
  - atanh/1
  - ceil/1
  - cos/1
  - cosh/1
  - erf/1
  - erfc/1
  - exp/1
  - floor/1
  - fmod/2
  - log/1
  - log10/1
  - log2/1
  - pi/0
  - pow/2
  - sin/1
  - sinh/1
  - sqrt/1
  - tan/1
  - tanh/1
- ms_transform
  - Top of manual page
  - format_error/1
  - parse_transform/2
  - transform_from_shell/3
- 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_empty/1
  - is_key/2
  - map/2
  - merge/3
  - new/0
  - size/1
  - store/3
  - take/2
  - to_list/1
  - update/3
  - update/4
  - update_counter/3
- ordsets
  - Top of manual page
  - add_element/2
  - del_element/2
  - filter/2
  - fold/3
  - from_list/1
  - intersection/1
  - intersection/2
  - is_disjoint/2
  - is_element/2
  - is_empty/1
  - is_set/1
  - is_subset/2
  - new/0
  - size/1
  - subtract/2
  - to_list/1
  - union/1
  - union/2
- pool
  - Top of manual page
  - attach/1
  - get_node/0
  - get_nodes/0
  - pspawn/3
  - pspawn_link/3
  - start/1
  - start/2
  - stop/0
- proc_lib
  - Top of manual page
  - format/1
  - format/2
  - format/3
  - hibernate/3
  - init_ack/1
  - init_ack/2
  - initial_call/1
  - 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
  - start_monitor/3
  - start_monitor/4
  - start_monitor/5
  - stop/1
  - stop/3
  - translate_initial_call/1
- 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
  - cursor/2
  - delete_cursor/1
  - e/1
  - e/2
  - eval/1
  - eval/2
  - fold/3
  - fold/4
  - format_error/1
  - info/1
  - info/2
  - keysort/2
  - keysort/3
  - next_answers/1
  - next_answers/2
  - q/1
  - q/2
  - sort/1
  - sort/2
  - string_to_handle/1
  - string_to_handle/2
  - string_to_handle/3
  - table/2
- queue
  - Top of manual page
  - cons/2
  - daeh/1
  - drop/1
  - drop_r/1
  - filter/2
  - from_list/1
  - get/1
  - get_r/1
  - head/1
  - in/2
  - in_r/2
  - init/1
  - is_empty/1
  - is_queue/1
  - join/2
  - lait/1
  - last/1
  - len/1
  - liat/1
  - member/2
  - new/0
  - out/1
  - out_r/1
  - peek/1
  - peek_r/1
  - reverse/1
  - snoc/2
  - split/2
  - tail/1
  - to_list/1
- rand
  - Top of manual page
  - export_seed/0
  - export_seed_s/1
  - jump/0
  - jump/1
  - normal/0
  - normal/2
  - normal_s/1
  - normal_s/3
  - seed/1
  - seed/2
  - seed_s/1
  - seed_s/2
  - uniform/0
  - uniform/1
  - uniform_real/0
  - uniform_real_s/1
  - uniform_s/1
  - uniform_s/2
- random
  - Top of manual page
  - seed/0
  - seed/1
  - seed/3
  - seed0/0
  - uniform/0
  - uniform/1
  - uniform_s/1
  - uniform_s/2
- re
  - Top of manual page
  - compile/1
  - compile/2
  - inspect/2
  - replace/3
  - replace/4
  - run/2
  - run/3
  - split/2
  - split/3
  - version/0
- sets
  - Top of manual page
  - add_element/2
  - del_element/2
  - filter/2
  - fold/3
  - from_list/1
  - intersection/1
  - intersection/2
  - is_disjoint/2
  - is_element/2
  - is_empty/1
  - is_set/1
  - is_subset/2
  - new/0
  - size/1
  - subtract/2
  - to_list/1
  - union/1
  - union/2
- shell
  - Top of manual page
  - catch_exception/1
  - history/1
  - prompt_func/1
  - results/1
  - start_restricted/1
  - stop_restricted/0
  - strings/1
- shell_default
- shell_docs
  - Top of manual page
  - normalize/1
  - render/2
  - render/3
  - render/4
  - render_callback/2
  - render_callback/3
  - render_callback/4
  - render_type/2
  - render_type/3
  - render_type/4
  - validate/1
- slave
  - Top of manual page
  - pseudo/1
  - pseudo/2
  - relay/1
  - start/1
  - start/2
  - start/3
  - start_link/1
  - start_link/2
  - start_link/3
  - stop/1
- sofs
  - Top of manual page
  - a_function/1
  - a_function/2
  - canonical_relation/1
  - composite/2
  - constant_function/2
  - converse/1
  - difference/2
  - digraph_to_family/1
  - digraph_to_family/2
  - domain/1
  - drestriction/2
  - drestriction/3
  - empty_set/0
  - extension/3
  - family/1
  - family/2
  - 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_digraph/2
  - family_to_relation/1
  - family_union/1
  - family_union/2
  - field/1
  - from_external/2
  - from_sets/1
  - from_term/1
  - from_term/2
  - 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/2
  - relation_to_family/1
  - relative_product/1
  - relative_product/2
  - relative_product1/2
  - restriction/2
  - restriction/3
  - set/1
  - set/2
  - 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
  - casefold/1
  - centre/2
  - centre/3
  - chars/2
  - chars/3
  - chomp/1
  - chr/2
  - concat/2
  - copies/2
  - cspan/2
  - equal/2
  - equal/3
  - equal/4
  - find/2
  - find/3
  - is_empty/1
  - join/2
  - left/2
  - left/3
  - len/1
  - length/1
  - lexemes/2
  - lowercase/1
  - next_codepoint/1
  - next_grapheme/1
  - nth_lexeme/3
  - pad/2
  - pad/3
  - pad/4
  - prefix/2
  - rchr/2
  - replace/3
  - replace/4
  - reverse/1
  - right/2
  - right/3
  - rstr/2
  - slice/2
  - slice/3
  - span/2
  - split/2
  - split/3
  - str/2
  - strip/1
  - strip/2
  - strip/3
  - sub_string/2
  - sub_string/3
  - sub_word/2
  - sub_word/3
  - substr/2
  - substr/3
  - take/2
  - take/3
  - take/4
  - titlecase/1
  - to_float/1
  - to_graphemes/1
  - to_integer/1
  - to_lower/1
  - to_upper/1
  - tokens/2
  - trim/1
  - trim/2
  - trim/3
  - uppercase/1
  - words/1
  - words/2
- supervisor
  - Top of manual page
  - check_childspecs/1
  - count_children/1
  - delete_child/2
  - get_childspec/2
  - restart_child/2
  - start_child/2
  - start_link/2
  - start_link/3
  - terminate_child/2
  - which_children/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
  - change_code/4
  - change_code/5
  - debug_options/1
  - get_debug/3
  - get_log/1
  - get_state/1
  - get_state/2
  - get_status/1
  - get_status/2
  - handle_debug/4
  - handle_system_msg/6
  - install/2
  - install/3
  - log/2
  - log/3
  - log_to_file/2
  - log_to_file/3
  - no_debug/1
  - no_debug/2
  - print_log/1
  - remove/2
  - remove/3
  - replace_state/2
  - replace_state/3
  - resume/1
  - resume/2
  - statistics/2
  - statistics/3
  - suspend/1
  - suspend/2
  - terminate/2
  - terminate/3
  - trace/2
  - trace/3
  - Module:system_code_change/4
  - Module:system_continue/3
  - Module:system_get_state/1
  - Module:system_replace_state/2
  - Module:system_terminate/4
- timer
  - Top of manual page
  - apply_after/4
  - apply_interval/4
  - cancel/1
  - exit_after/2
  - exit_after/3
  - hms/3
  - hours/1
  - kill_after/1
  - kill_after/2
  - minutes/1
  - now_diff/2
  - seconds/1
  - send_after/2
  - send_after/3
  - send_interval/2
  - send_interval/3
  - sleep/1
  - start/0
  - tc/1
  - tc/2
  - tc/3
- unicode
  - Top of manual page
  - bom_to_encoding/1
  - characters_to_binary/1
  - characters_to_binary/2
  - characters_to_binary/3
  - characters_to_list/1
  - characters_to_list/2
  - characters_to_nfc_binary/1
  - characters_to_nfc_list/1
  - characters_to_nfd_binary/1
  - characters_to_nfd_list/1
  - characters_to_nfkc_binary/1
  - characters_to_nfkc_list/1
  - characters_to_nfkd_binary/1
  - characters_to_nfkd_list/1
  - encoding_to_bom/1
- uri_string
  - Top of manual page
  - compose_query/1
  - compose_query/2
  - dissect_query/1
  - normalize/1
  - normalize/2
  - parse/1
  - recompose/1
  - resolve/2
  - resolve/3
  - transcode/2
- 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
  - create/2
  - create/3
  - extract/1
  - extract/2
  - foldl/3
  - list_dir/1
  - list_dir/2
  - t/1
  - table/1
  - table/2
  - tt/1
  - unzip/1
  - unzip/2
  - zip/2
  - zip/3
  - zip_close/1
  - zip_get/1
  - zip_get/2
  - zip_list_dir/1
  - zip_open/1
  - zip_open/2

# qlc

### Module

qlc

### Module Summary

Query interface to Mnesia, ETS, Dets, and so on.

### Description

This module provides a query interface to Mnesia, ETS, Dets, and other data structures that provide an iterator style traversal of objects.

### Overview

This module provides a query interface to QLC tables. Typical QLC tables are Mnesia, ETS, and Dets tables. Support is also provided for user-defined tables, see section Implementing a QLC Table. A query is expressed using Query List Comprehensions (QLCs). The answers to a query are determined by data in QLC tables that fulfill the constraints expressed by the QLCs of the query. QLCs are similar to ordinary list comprehensions as described in Erlang Reference Manual and Programming Examples, except that variables introduced in patterns cannot be used in list expressions. In the absence of optimizations and options such as cache and unique (see section Common Options, every QLC free of QLC tables evaluates to the same list of answers as the identical ordinary list comprehension.

While ordinary list comprehensions evaluate to lists, calling q/1,2 returns a query handle. To obtain all the answers to a query, eval/1,2 is to be called with the query handle as first argument. Query handles are essentially functional objects (funs) created in the module calling q/1,2. As the funs refer to the module code, be careful not to keep query handles too long if the module code is to be replaced. Code replacement is described in section Compilation and Code Loading in the Erlang Reference Manual. The list of answers can also be traversed in chunks by use of a query cursor. Query cursors are created by calling cursor/1,2 with a query handle as first argument. Query cursors are essentially Erlang processes. One answer at a time is sent from the query cursor process to the process that created the cursor.

### Syntax

Syntactically QLCs have the same parts as ordinary list comprehensions:

    [Expression || Qualifier1, Qualifier2, ...]

Expression (the template) is any Erlang expression. Qualifiers are either filters or generators. Filters are Erlang expressions returning boolean(). Generators have the form Pattern <- ListExpression, where ListExpression is an expression evaluating to a query handle or a list. Query handles are returned from append/1,2, keysort/2,3, q/1,2, sort/1,2, string_to_handle/1,2,3, and table/2.

### Evaluation

A query handle is evaluated in the following order:

- Inspection of options and the collection of information about tables. As a result, qualifiers are modified during the optimization phase.

- All list expressions are evaluated. If a cursor has been created, evaluation takes place in the cursor process. For list expressions that are QLCs, the list expressions of the generators of the QLCs are evaluated as well. Be careful if list expressions have side effects, as list expressions are evaluated in unspecified order.

- The answers are found by evaluating the qualifiers from left to right, backtracking when some filter returns false, or collecting the template when all filters return true.

Filters that do not return boolean() but fail are handled differently depending on their syntax: if the filter is a guard, it returns false, otherwise the query evaluation fails. This behavior makes it possible for the qlc module to do some optimizations without affecting the meaning of a query. For example, when testing some position of a table and one or more constants for equality, only the objects with equal values are candidates for further evaluation. The other objects are guaranteed to make the filter return false, but never fail. The (small) set of candidate objects can often be found by looking up some key values of the table or by traversing the table using a match specification. It is necessary to place the guard filters immediately after the table generator, otherwise the candidate objects are not restricted to a small set. The reason is that objects that could make the query evaluation fail must not be excluded by looking up a key or running a match specification.

### Join

The qlc module supports fast join of two query handles. Fast join is possible if some position P1 of one query handler and some position P2 of another query handler are tested for equality. Two fast join methods are provided:

- Lookup join traverses all objects of one query handle and finds objects of the other handle (a QLC table) such that the values at P1 and P2 match or compare equal. The qlc module does not create any indexes but looks up values using the key position and the indexed positions of the QLC table.

- Merge join sorts the objects of each query handle if necessary and filters out objects where the values at P1 and P2 do not compare equal. If many objects with the same value of P2 exist, a temporary file is used for the equivalence classes.

The qlc module warns at compile time if a QLC combines query handles in such a way that more than one join is possible. That is, no query planner is provided that can select a good order between possible join operations. It is up to the user to order the joins by introducing query handles.

The join is to be expressed as a guard filter. The filter must be placed immediately after the two joined generators, possibly after guard filters that use variables from no other generators but the two joined generators. The qlc module inspects the operands of =:=/2, ==/2, is_record/2, element/2, and logical operators (and/2, or/2, andalso/2, orelse/2, xor/2) when determining which joins to consider.

### Common Options

The following options are accepted by cursor/2, eval/2, fold/4, and info/2:

- {cache_all, Cache}, where Cache is equal to ets or list adds a {cache, Cache} option to every list expression of the query except tables and lists. Defaults to {cache_all, no}. Option cache_all is equivalent to {cache_all, ets}.

- {max_list_size, MaxListSize}, where MaxListSize is the size in bytes of terms on the external format. If the accumulated size of collected objects exceeds MaxListSize, the objects are written onto a temporary file. This option is used by option {cache, list} and by the merge join method. Defaults to 512*1024 bytes.

- {tmpdir_usage, TmpFileUsage} determines the action taken when qlc is about to create temporary files on the directory set by option tmpdir. If the value is not_allowed, an error tuple is returned, otherwise temporary files are created as needed. Default is allowed, which means that no further action is taken. The values info_msg, warning_msg, and error_msg mean that the function with the corresponding name in module error_logger is called for printing some information (currently the stacktrace).

- {tmpdir, TempDirectory} sets the directory used by merge join for temporary files and by option {cache, list}. The option also overrides option tmpdir of keysort/3 and sort/2. Defaults to "", which means that the directory returned by file:get_cwd() is used.

- {unique_all, true} adds a {unique, true} option to every list expression of the query. Defaults to {unique_all, false}. Option unique_all is equivalent to {unique_all, true}.

### Getting Started

As mentioned earlier, queries are expressed in the list comprehension syntax as described in section Expressions in Erlang Reference Manual. In the following, some familiarity with list comprehensions is assumed. The examples in section List Comprehensions in Programming Examples can get you started. Notice that list comprehensions do not add any computational power to the language; anything that can be done with list comprehensions can also be done without them. But they add syntax for expressing simple search problems, which is compact and clear once you get used to it.

Many list comprehension expressions can be evaluated by the qlc module. Exceptions are expressions, such that variables introduced in patterns (or filters) are used in some generator later in the list comprehension. As an example, consider an implementation of lists:append(L): [X ||Y <- L, X <- Y]. Y is introduced in the first generator and used in the second. The ordinary list comprehension is normally to be preferred when there is a choice as to which to use. One difference is that eval/1,2 collects answers in a list that is finally reversed, while list comprehensions collect answers on the stack that is finally unwound.

What the qlc module primarily adds to list comprehensions is that data can be read from QLC tables in small chunks. A QLC table is created by calling qlc:table/2. Usually qlc:table/2 is not called directly from the query but through an interface function of some data structure. Erlang/OTP includes a few examples of such functions: mnesia:table/1,2, ets:table/1,2, and dets:table/1,2. For a given data structure, many functions can create QLC tables, but common for these functions is that they return a query handle created by qlc:table/2. Using the QLC tables provided by Erlang/OTP is usually probably sufficient, but for the more advanced user section Implementing a QLC Table describes the implementation of a function calling qlc:table/2.

Besides qlc:table/2, other functions return query handles. They are used more seldom than tables, but are sometimes useful. qlc:append/1,2 traverses objects from many tables or lists after each other. If, for example, you want to traverse all answers to a query QH and then finish off by a term {finished}, you can do that by calling qlc:append(QH, [{finished}]). append/2 first returns all objects of QH, then {finished}. If a tuple {finished} exists among the answers to QH, it is returned twice from append/2.

As another example, consider concatenating the answers to two queries QH1 and QH2 while removing all duplicates. This is accomplished by using option unique:

    qlc:q([X || X <- qlc:append(QH1, QH2)], {unique, true})

The cost is substantial: every returned answer is stored in an ETS table. Before returning an answer, it is looked up in the ETS table to check if it has already been returned. Without the unique option, all answers to QH1 would be returned followed by all answers to QH2. The unique option keeps the order between the remaining answers.

If the order of the answers is not important, there is an alternative to the unique option, namely to sort the answers uniquely:

    qlc:sort(qlc:q([X || X <- qlc:append(QH1, QH2)], {unique, true})).

This query also removes duplicates but the answers are sorted. If there are many answers, temporary files are used. Notice that to get the first unique answer, all answers must be found and sorted. Both alternatives find duplicates by comparing answers, that is, if A1 and A2 are answers found in that order, then A2 is a removed if A1 == A2.

To return only a few answers, cursors can be used. The following code returns no more than five answers using an ETS table for storing the unique answers:

    C = qlc:cursor(qlc:q([X || X <- qlc:append(QH1, QH2)],{unique,true})),
    R = qlc:next_answers(C, 5),
    ok = qlc:delete_cursor(C),
    R.

QLCs are convenient for stating constraints on data from two or more tables. The following example does a natural join on two query handles on position 2:

    qlc:q([{X1,X2,X3,Y1} ||
              {X1,X2,X3} <- QH1,
              {Y1,Y2} <- QH2,
              X2 =:= Y2])

The qlc module evaluates this differently depending on the query handles QH1 and QH2. If, for example, X2 is matched against the key of a QLC table, the lookup join method traverses the objects of QH2 while looking up key values in the table. However, if not X2 or Y2 is matched against the key or an indexed position of a QLC table, the merge join method ensures that QH1 and QH2 are both sorted on position 2 and next do the join by traversing the objects one by one.

Option join can be used to force the qlc module to use a certain join method. For the rest of this section it is assumed that the excessively slow join method called "nested loop" has been chosen:

    qlc:q([{X1,X2,X3,Y1} ||
              {X1,X2,X3} <- QH1,
              {Y1,Y2} <- QH2,
              X2 =:= Y2],
          {join, nested_loop})

In this case the filter is applied to every possible pair of answers to QH1 and QH2, one at a time. If there are M answers to QH1 and N answers to QH2, the filter is run M*N times.

If QH2 is a call to the function for gb_trees, as defined in section Implementing a QLC Table, then gb_table:table/1, the iterator for the gb-tree is initiated for each answer to QH1. The objects of the gb-tree are then returned one by one. This is probably the most efficient way of traversing the table in that case, as it takes minimal computational power to get the following object. But if QH2 is not a table but a more complicated QLC, it can be more efficient to use some RAM memory for collecting the answers in a cache, particularly if there are only a few answers. It must then be assumed that evaluating QH2 has no side effects so that the meaning of the query does not change if QH2 is evaluated only once. One way of caching the answers is to evaluate QH2 first of all and substitute the list of answers for QH2 in the query. Another way is to use option cache. It is expressed like this:

    QH2' = qlc:q([X || X <- QH2], {cache, ets})

or only

    QH2' = qlc:q([X || X <- QH2], cache)

The effect of option cache is that when generator QH2' is run the first time, every answer is stored in an ETS table. When the next answer of QH1 is tried, answers to QH2' are copied from the ETS table, which is very fast. As for option unique the cost is a possibly substantial amount of RAM memory.

Option {cache, list} offers the possibility to store the answers in a list on the process heap. This has the potential of being faster than ETS tables, as there is no need to copy answers from the table. However, it can often result in slower evaluation because of more garbage collections of the process heap and increased RAM memory consumption because of larger heaps. Another drawback with cache lists is that if the list size exceeds a limit, a temporary file is used. Reading the answers from a file is much slower than copying them from an ETS table. But if the available RAM memory is scarce, setting the limit to some low value is an alternative.

Option cache_all can be set to ets or list when evaluating a query. It adds a cache or {cache, list} option to every list expression except QLC tables and lists on all levels of the query. This can be used for testing if caching would improve efficiency at all. If the answer is yes, further testing is needed to pinpoint the generators that are to be cached.

### Implementing a QLC Table

As an example of how to use function table/2, the implementation of a QLC table for the gb_trees module is given:

    -module(gb_table).

    -export([table/1]).

    table(T) ->
        TF = fun() -> qlc_next(gb_trees:next(gb_trees:iterator(T))) end,
        InfoFun = fun(num_of_objects) -> gb_trees:size(T);
                     (keypos) -> 1;
                     (is_sorted_key) -> true;
                     (is_unique_objects) -> true;
                     (_) -> undefined
                  end,
        LookupFun =
            fun(1, Ks) ->
                    lists:flatmap(fun(K) ->
                                          case gb_trees:lookup(K, T) of
                                              {value, V} -> [{K,V}];
                                              none -> []
                                          end
                                  end, Ks)
            end,
        FormatFun =
            fun({all, NElements, ElementFun}) ->
                    ValsS = io_lib:format("gb_trees:from_orddict(~w)",
                                          [gb_nodes(T, NElements, ElementFun)]),
                    io_lib:format("gb_table:table(~s)", [ValsS]);
               ({lookup, 1, KeyValues, _NElements, ElementFun}) ->
                    ValsS = io_lib:format("gb_trees:from_orddict(~w)",
                                          [gb_nodes(T, infinity, ElementFun)]),
                    io_lib:format("lists:flatmap(fun(K) -> "
                                  "case gb_trees:lookup(K, ~s) of "
                                  "{value, V} -> [{K,V}];none -> [] end "
                                  "end, ~w)",
                                  [ValsS, [ElementFun(KV) || KV <- KeyValues]])
            end,
        qlc:table(TF, [{info_fun, InfoFun}, {format_fun, FormatFun},
                       {lookup_fun, LookupFun},{key_equality,'=='}]).

    qlc_next({X, V, S}) ->
        [{X,V} | fun() -> qlc_next(gb_trees:next(S)) end];
    qlc_next(none) ->
        [].

    gb_nodes(T, infinity, ElementFun) ->
        gb_nodes(T, -1, ElementFun);
    gb_nodes(T, NElements, ElementFun) ->
        gb_iter(gb_trees:iterator(T), NElements, ElementFun).

    gb_iter(_I, 0, _EFun) ->
        '...';
    gb_iter(I0, N, EFun) ->
        case gb_trees:next(I0) of
            {X, V, I} ->
                [EFun({X,V}) | gb_iter(I, N-1, EFun)];
            none ->
                []
        end.

TF is the traversal function. The qlc module requires that there is a way of traversing all objects of the data structure. gb_trees has an iterator function suitable for that purpose. Notice that for each object returned, a new fun is created. As long as the list is not terminated by [], it is assumed that the tail of the list is a nullary function and that calling the function returns further objects (and functions).

The lookup function is optional. It is assumed that the lookup function always finds values much faster than it would take to traverse the table. The first argument is the position of the key. As qlc_next/1 returns the objects as {Key, Value} pairs, the position is 1. Notice that the lookup function is to return {Key, Value} pairs, as the traversal function does.

The format function is also optional. It is called by info/1,2 to give feedback at runtime of how the query is to be evaluated. Try to give as good feedback as possible without showing too much details. In the example, at most seven objects of the table are shown. The format function handles two cases: all means that all objects of the table are traversed; {lookup, 1, KeyValues} means that the lookup function is used for looking up key values.

Whether the whole table is traversed or only some keys looked up depends on how the query is expressed. If the query has the form

    qlc:q([T || P <- LE, F])

and P is a tuple, the qlc module analyzes P and F in compile time to find positions of tuple P that are tested for equality to constants. If such a position at runtime turns out to be the key position, the lookup function can be used, otherwise all objects of the table must be traversed. The info function InfoFun returns the key position. There can be indexed positions as well, also returned by the info function. An index is an extra table that makes lookup on some position fast. Mnesia maintains indexes upon request, and introduces so called secondary keys. The qlc module prefers to look up objects using the key before secondary keys regardless of the number of constants to look up.

### Key Equality

Erlang/OTP has two operators for testing term equality: ==/2 and =:=/2. The difference is all about the integers that can be represented by floats. For example, 2 == 2.0 evaluates to true while 2 =:= 2.0 evaluates to false. Normally this is a minor issue, but the qlc module cannot ignore the difference, which affects the user's choice of operators in QLCs.

If the qlc module at compile time can determine that some constant is free of integers, it does not matter which one of ==/2 or =:=/2 is used:

    1> E1 = ets:new(t, [set]), % uses =:=/2 for key equality
    Q1 = qlc:q([K ||
    {K} <- ets:table(E1),
    K == 2.71 orelse K == a]),
    io:format("~s~n", [qlc:info(Q1)]).
    ets:match_spec_run(
           lists:flatmap(fun(V) ->
    			    ets:lookup(#Ref<0.3098908599.2283929601.256025>,
    				       V)
    		     end,
    		     [a, 2.71]),
           ets:match_spec_compile([{{'$1'}, [], ['$1']}]))

In the example, operator ==/2 has been handled exactly as =:=/2 would have been handled. However, if it cannot be determined at compile time that some constant is free of integers, and the table uses =:=/2 when comparing keys for equality (see option key_equality), then the qlc module does not try to look up the constant. The reason is that there is in the general case no upper limit on the number of key values that can compare equal to such a constant; every combination of integers and floats must be looked up:

    2> E2 = ets:new(t, [set]),
    true = ets:insert(E2, [{{2,2},a},{{2,2.0},b},{{2.0,2},c}]),
    F2 = fun(I) ->
    qlc:q([V || {K,V} <- ets:table(E2), K == I])
    end,
    Q2 = F2({2,2}),
    io:format("~s~n", [qlc:info(Q2)]).
    ets:table(#Ref<0.3098908599.2283929601.256125>,
              [{traverse,
                {select,
                 [{{'$1', '$2'}, [{'==', '$1', {const, {2, 2}}}], ['$2']}]}}])
    3> lists:sort(qlc:e(Q2)).
    [a,b,c]

Looking up only {2,2} would not return b and c.

If the table uses ==/2 when comparing keys for equality, the qlc module looks up the constant regardless of which operator is used in the QLC. However, ==/2 is to be preferred:

    4> E3 = ets:new(t, [ordered_set]), % uses ==/2 for key equality
    true = ets:insert(E3, [{{2,2.0},b}]),
    F3 = fun(I) ->
    qlc:q([V || {K,V} <- ets:table(E3), K == I])
    end,
    Q3 = F3({2,2}),
    io:format("~s~n", [qlc:info(Q3)]).
    ets:match_spec_run(ets:lookup(#Ref<0.3098908599.2283929601.256211>,
                                  {2, 2}),
                       ets:match_spec_compile([{{'$1', '$2'}, [], ['$2']}]))
    5> qlc:e(Q3).
    [b]

Lookup join is handled analogously to lookup of constants in a table: if the join operator is ==/2, and the table where constants are to be looked up uses =:=/2 when testing keys for equality, then the qlc module does not consider lookup join for that table.

### Data Types

abstract_expr() = erl_parse:abstract_expr()

Parse trees for Erlang expression, see section The Abstract Format in the ERTS User's Guide.

answer() = term()
answers() = [answer()]
cache() = ets | list | no
match_expression() = ets:match_spec()

Match specification, see section Match Specifications in Erlang in the ERTS User's Guide and ms_transform(3).

no_files() = integer() >= 1

An integer > 1.

key_pos() = integer() >= 1 | [integer() >= 1]
max_list_size() = integer() >= 0
order() = ascending | descending | order_fun()
order_fun() = fun((term(), term()) -> boolean())
query_cursor()

A query cursor.

query_handle()

A query handle.

query_handle_or_list() = query_handle() | list()
query_list_comprehension() = term()

A literal query list comprehension.

spawn_options() = default | [proc_lib:spawn_option()]
sort_options() = [sort_option()] | sort_option()
sort_option() =
{compressed, boolean()} |
{no_files, no_files()} |
{order, order()} |
{size, integer() >= 1} |
{tmpdir, tmp_directory()} |
{unique, boolean()}

See file_sorter(3).

tmp_directory() = [] | file:name()
tmp_file_usage() =
allowed | not_allowed | info_msg | warning_msg | error_msg

### Exports

append(QHL) -> QH |

### Types

QHL = [query_handle_or_list()]
QH = query_handle()

Returns a query handle. When evaluating query handle QH, all answers to the first query handle in QHL are returned, followed by all answers to the remaining query handles in QHL.

append(QH1, QH2) -> QH3 |

### Types

QH1 = QH2 = query_handle_or_list()
QH3 = query_handle()

Returns a query handle. When evaluating query handle QH3, all answers to QH1 are returned, followed by all answers to QH2.

append(QH1, QH2) is equivalent to append([QH1, QH2]).

cursor(QH) -> Cursor |

cursor(QH, Options) -> Cursor |

### Types

QH = query_handle_or_list()
Options = [Option] | Option
Option =
{cache_all, cache()} |
cache_all |
{max_list_size, max_list_size()} |
{spawn_options, spawn_options()} |
{tmpdir_usage, tmp_file_usage()} |
{tmpdir, tmp_directory()} |
{unique_all, boolean()} |
unique_all
Cursor = query_cursor()

Creates a query cursor and makes the calling process the owner of the cursor. The cursor is to be used as argument to next_answers/1,2 and (eventually) delete_cursor/1. Calls erlang:spawn_opt/2 to spawn and link to a process that evaluates the query handle. The value of option spawn_options is used as last argument when calling spawn_opt/2. Defaults to [link].

Example:

    1> QH = qlc:q([{X,Y} || X <- [a,b], Y <- [1,2]]),
    QC = qlc:cursor(QH),
    qlc:next_answers(QC, 1).
    [{a,1}]
    2> qlc:next_answers(QC, 1).
    [{a,2}]
    3> qlc:next_answers(QC, all_remaining).
    [{b,1},{b,2}]
    4> qlc:delete_cursor(QC).
    ok

cursor(QH) is equivalent to cursor(QH, []).

delete_cursor(QueryCursor) -> ok |

### Types

QueryCursor = query_cursor()

Deletes a query cursor. Only the owner of the cursor can delete the cursor.

e(QH) -> Answers | Error |

e(QH, Options) -> Answers | Error |

eval(QH) -> Answers | Error |

eval(QH, Options) -> Answers | Error |

### Types

QH = query_handle_or_list()
Answers = answers()
Options = [Option] | Option
Option =
{cache_all, cache()} |
cache_all |
{max_list_size, max_list_size()} |
{tmpdir_usage, tmp_file_usage()} |
{tmpdir, tmp_directory()} |
{unique_all, boolean()} |
unique_all
Error = {error, module(), Reason}
Reason = file_sorter:reason()

Evaluates a query handle in the calling process and collects all answers in a list.

Example:

    1> QH = qlc:q([{X,Y} || X <- [a,b], Y <- [1,2]]),
    qlc:eval(QH).
    [{a,1},{a,2},{b,1},{b,2}]

eval(QH) is equivalent to eval(QH, []).

fold(Function, Acc0, QH) -> Acc1 | Error |

fold(Function, Acc0, QH, Options) -> Acc1 | Error |

### Types

QH = query_handle_or_list()
Function = fun((answer(), AccIn) -> AccOut)
Acc0 = Acc1 = AccIn = AccOut = term()
Options = [Option] | Option
Option =
{cache_all, cache()} |
cache_all |
{max_list_size, max_list_size()} |
{tmpdir_usage, tmp_file_usage()} |
{tmpdir, tmp_directory()} |
{unique_all, boolean()} |
unique_all
Error = {error, module(), Reason}
Reason = file_sorter:reason()

Calls Function on successive answers to the query handle together with an extra argument AccIn. The query handle and the function are evaluated in the calling process. Function must return a new accumulator, which is passed to the next call. Acc0 is returned if there are no answers to the query handle.

Example:

    1> QH = [1,2,3,4,5,6],
    qlc:fold(fun(X, Sum) -> X + Sum end, 0, QH).
    21

fold(Function, Acc0, QH) is equivalent to fold(Function, Acc0, QH, []).

format_error(Error) -> Chars |

### Types

Error = {error, module(), term()}
Chars = io_lib:chars()

Returns a descriptive string in English of an error tuple returned by some of the functions of the qlc module or the parse transform. This function is mainly used by the compiler invoking the parse transform.

info(QH) -> Info |

info(QH, Options) -> Info |

### Types

QH = query_handle_or_list()
Options = [Option] | Option
Option = EvalOption | ReturnOption
EvalOption =
{cache_all, cache()} |
cache_all |
{max_list_size, max_list_size()} |
{tmpdir_usage, tmp_file_usage()} |
{tmpdir, tmp_directory()} |
{unique_all, boolean()} |
unique_all
ReturnOption =
{depth, Depth} |
{flat, boolean()} |
{format, Format} |
{n_elements, NElements}
Depth = infinity | integer() >= 0
Format = abstract_code | string
NElements = infinity | integer() >= 1
Info = abstract_expr() | string()

Returns information about a query handle. The information describes the simplifications and optimizations that are the results of preparing the query for evaluation. This function is probably mainly useful during debugging.

The information has the form of an Erlang expression where QLCs most likely occur. Depending on the format functions of mentioned QLC tables, it is not certain that the information is absolutely accurate.

Options:

- The default is to return a sequence of QLCs in a block, but if option {flat, false} is specified, one single QLC is returned.

- The default is to return a string, but if option {format, abstract_code} is specified, abstract code is returned instead. In the abstract code, port identifiers, references, and pids are represented by strings.

- The default is to return all elements in lists, but if option {n_elements, NElements} is specified, only a limited number of elements are returned.

- The default is to show all parts of objects and match specifications, but if option {depth, Depth} is specified, parts of terms below a certain depth are replaced by '...'.

info(QH) is equivalent to info(QH, []).

Examples:

In the following example two simple QLCs are inserted only to hold option {unique, true}:

    1> QH = qlc:q([{X,Y} || X <- [x,y], Y <- [a,b]]),
    io:format("~s~n", [qlc:info(QH, unique_all)]).
    begin
        V1 =
            qlc:q([
                   SQV ||
                       SQV <- [x, y]
                  ],
                  [{unique, true}]),
        V2 =
            qlc:q([
                   SQV ||
                       SQV <- [a, b]
                  ],
                  [{unique, true}]),
        qlc:q([
               {X,Y} ||
                   X <- V1,
                   Y <- V2
              ],
              [{unique, true}])
    end

In the following example QLC V2 has been inserted to show the joined generators and the join method chosen. A convention is used for lookup join: the first generator (G2) is the one traversed, the second (G1) is the table where constants are looked up.

    1> E1 = ets:new(e1, []),
    E2 = ets:new(e2, []),
    true = ets:insert(E1, [{1,a},{2,b}]),
    true = ets:insert(E2, [{a,1},{b,2}]),
    Q = qlc:q([{X,Z,W} ||
    {X, Z} <- ets:table(E1),
    {W, Y} <- ets:table(E2),
    X =:= Y]),
    io:format("~s~n", [qlc:info(Q)]).
    begin
        V1 =
            qlc:q([
                   P0 ||
                       P0 = {W, Y} <-
                           ets:table(#Ref<0.3098908599.2283929601.256549>)
                  ]),
        V2 =
            qlc:q([
                   [G1 | G2] ||
                       G2 <- V1,
                       G1 <-
                           ets:table(#Ref<0.3098908599.2283929601.256548>),
                       element(2, G1) =:= element(1, G2)
                  ],
                  [{join, lookup}]),
        qlc:q([
               {X, Z, W} ||
                   [{X, Z} | {W, Y}] <- V2
              ])
    end

keysort(KeyPos, QH1) -> QH2 |

keysort(KeyPos, QH1, SortOptions) -> QH2 |

### Types

KeyPos = key_pos()
SortOptions = sort_options()
QH1 = query_handle_or_list()
QH2 = query_handle()

Returns a query handle. When evaluating query handle QH2, the answers to query handle QH1 are sorted by file_sorter:keysort/4 according to the options.

The sorter uses temporary files only if QH1 does not evaluate to a list and the size of the binary representation of the answers exceeds Size bytes, where Size is the value of option size.

keysort(KeyPos, QH1) is equivalent to keysort(KeyPos, QH1, []).

next_answers(QueryCursor) -> Answers | Error |

next_answers(QueryCursor, NumberOfAnswers) -> Answers | Error |

### Types

QueryCursor = query_cursor()
Answers = answers()
NumberOfAnswers = all_remaining | integer() >= 1
Error = {error, module(), Reason}
Reason = file_sorter:reason()

Returns some or all of the remaining answers to a query cursor. Only the owner of QueryCursor can retrieve answers.

Optional argument NumberOfAnswers determines the maximum number of answers returned. Defaults to 10. If less than the requested number of answers is returned, subsequent calls to next_answers return [].

q(QLC) -> QH |

q(QLC, Options) -> QH |

### Types

QH = query_handle()
Options = [Option] | Option
Option =
{max_lookup, MaxLookup} |
{cache, cache()} |
cache |
{join, Join} |
{lookup, Lookup} |
{unique, boolean()} |
unique
MaxLookup = integer() >= 0 | infinity
Join = any | lookup | merge | nested_loop
Lookup = boolean() | any
QLC = query_list_comprehension()

Returns a query handle for a QLC. The QLC must be the first argument to this function, otherwise it is evaluated as an ordinary list comprehension. It is also necessary to add the following line to the source code:

    -include_lib("stdlib/include/qlc.hrl").

This causes a parse transform to substitute a fun for the QLC. The (compiled) fun is called when the query handle is evaluated.

When calling qlc:q/1,2 from the Erlang shell, the parse transform is automatically called. When this occurs, the fun substituted for the QLC is not compiled but is evaluated by erl_eval(3). This is also true when expressions are evaluated by file:eval/1,2 or in the debugger.

To be explicit, this does not work:

    ...
    A = [X || {X} <- [{1},{2}]],
    QH = qlc:q(A),
    ...

Variable A is bound to the evaluated value of the list comprehension ([1,2]). The compiler complains with an error message ("argument is not a query list comprehension"); the shell process stops with a badarg reason.

q(QLC) is equivalent to q(QLC, []).

Options:

- Option {cache, ets} can be used to cache the answers to a QLC. The answers are stored in one ETS table for each cached QLC. When a cached QLC is evaluated again, answers are fetched from the table without any further computations. Therefore, when all answers to a cached QLC have been found, the ETS tables used for caching answers to the qualifiers of the QLC can be emptied. Option cache is equivalent to {cache, ets}.

- Option {cache, list} can be used to cache the answers to a QLC like {cache, ets}. The difference is that the answers are kept in a list (on the process heap). If the answers would occupy more than a certain amount of RAM memory, a temporary file is used for storing the answers. Option max_list_size sets the limit in bytes and the temporary file is put on the directory set by option tmpdir.

  Option cache has no effect if it is known that the QLC is to be evaluated at most once. This is always true for the top-most QLC and also for the list expression of the first generator in a list of qualifiers. Notice that in the presence of side effects in filters or callback functions, the answers to QLCs can be affected by option cache.

- Option {unique, true} can be used to remove duplicate answers to a QLC. The unique answers are stored in one ETS table for each QLC. The table is emptied every time it is known that there are no more answers to the QLC. Option unique is equivalent to {unique, true}. If option unique is combined with option {cache, ets}, two ETS tables are used, but the full answers are stored in one table only. If option unique is combined with option {cache, list}, the answers are sorted twice using keysort/3; once to remove duplicates and once to restore the order.

Options cache and unique apply not only to the QLC itself but also to the results of looking up constants, running match specifications, and joining handles.

Example:

In the following example the cached results of the merge join are traversed for each value of A. Notice that without option cache the join would have been carried out three times, once for each value of A.

    1> Q = qlc:q([{A,X,Z,W} ||
    A <- [a,b,c],
    {X,Z} <- [{a,1},{b,4},{c,6}],
    {W,Y} <- [{2,a},{3,b},{4,c}],
    X =:= Y],
    {cache, list}),
    io:format("~s~n", [qlc:info(Q)]).
    begin
        V1 =
            qlc:q([
                   P0 ||
                       P0 = {X, Z} <-
                           qlc:keysort(1, [{a, 1}, {b, 4}, {c, 6}], [])
                  ]),
        V2 =
            qlc:q([
                   P0 ||
                       P0 = {W, Y} <-
                           qlc:keysort(2, [{2, a}, {3, b}, {4, c}], [])
                  ]),
        V3 =
            qlc:q([
                   [G1 | G2] ||
                       G1 <- V1,
                       G2 <- V2,
                       element(1, G1) == element(2, G2)
                  ],
                  [{join, merge}, {cache, list}]),
        qlc:q([
               {A, X, Z, W} ||
                   A <- [a, b, c],
                   [{X, Z} | {W, Y}] <- V3,
                   X =:= Y
              ])
    end

sort/1,2 and keysort/2,3 can also be used for caching answers and for removing duplicates. When sorting answers are cached in a list, possibly stored on a temporary file, and no ETS tables are used.

Sometimes (see table/2) traversal of tables can be done by looking up key values, which is assumed to be fast. Under certain (rare) circumstances there can be too many key values to look up. Option {max_lookup, MaxLookup} can then be used to limit the number of lookups: if more than MaxLookup lookups would be required, no lookups are done but the table is traversed instead. Defaults to infinity, which means that there is no limit on the number of keys to look up.

Example:

In the following example, using the gb_table module from section Implementing a QLC Table, there are six keys to look up: {1,a}, {1,b}, {1,c}, {2,a}, {2,b}, and {2,c}. The reason is that the two elements of key {X, Y} are compared separately.

    1> T = gb_trees:empty(),
    QH = qlc:q([X || {{X,Y},_} <- gb_table:table(T),
    ((X == 1) or (X == 2)) andalso
    ((Y == a) or (Y == b) or (Y == c))]),
    io:format("~s~n", [qlc:info(QH)]).
    ets:match_spec_run(
           lists:flatmap(fun(K) ->
                                case
                                    gb_trees:lookup(K,
                                                    gb_trees:from_orddict([]))
                                of
                                    {value, V} ->
                                        [{K, V}];
                                    none ->
                                        []
                                end
                         end,
                         [{1, a},
                          {1, b},
                          {1, c},
                          {2, a},
                          {2, b},
                          {2, c}]),
           ets:match_spec_compile([{{{'$1', '$2'}, '_'},
                                    [],
                                    ['$1']}]))

Options:

- Option {lookup, true} can be used to ensure that the qlc module looks up constants in some QLC table. If there are more than one QLC table among the list expressions of the generators, constants must be looked up in at least one of the tables. The evaluation of the query fails if there are no constants to look up. This option is useful when it would be unacceptable to traverse all objects in some table. Setting option lookup to false ensures that no constants are looked up ({max_lookup, 0} has the same effect). Defaults to any, which means that constants are looked up whenever possible.

- Option {join, Join} can be used to ensure that a certain join method is used:

  - {join, lookup} invokes the lookup join method.
  - {join, merge} invokes the merge join method.
  - {join, nested_loop} invokes the method of matching every pair of objects from two handles. This method is mostly very slow.

  The evaluation of the query fails if the qlc module cannot carry out the chosen join method. Defaults to any, which means that some fast join method is used if possible.

sort(QH1) -> QH2 |

sort(QH1, SortOptions) -> QH2 |

### Types

SortOptions = sort_options()
QH1 = query_handle_or_list()
QH2 = query_handle()

Returns a query handle. When evaluating query handle QH2, the answers to query handle QH1 are sorted by file_sorter:sort/3 according to the options.

The sorter uses temporary files only if QH1 does not evaluate to a list and the size of the binary representation of the answers exceeds Size bytes, where Size is the value of option size.

sort(QH1) is equivalent to sort(QH1, []).

string_to_handle(QueryString) -> QH | Error |

string_to_handle(QueryString, Options) -> QH | Error |

string_to_handle(QueryString, Options, Bindings) -> QH | Error |

### Types

QueryString = string()
Options = [Option] | Option
Option =
{max_lookup, MaxLookup} |
{cache, cache()} |
cache |
{join, Join} |
{lookup, Lookup} |
{unique, boolean()} |
unique
MaxLookup = integer() >= 0 | infinity
Join = any | lookup | merge | nested_loop
Lookup = boolean() | any
Bindings = erl_eval:binding_struct()
QH = query_handle()
Error = {error, module(), Reason}
Reason = erl_parse:error_info() | erl_scan:error_info()

A string version of q/1,2. When the query handle is evaluated, the fun created by the parse transform is interpreted by erl_eval(3). The query string is to be one single QLC terminated by a period.

Example:

    1> L = [1,2,3],
    Bs = erl_eval:add_binding('L', L, erl_eval:new_bindings()),
    QH = qlc:string_to_handle("[X+1 || X <- L].", [], Bs),
    qlc:eval(QH).
    [2,3,4]

string_to_handle(QueryString) is equivalent to string_to_handle(QueryString, []).

string_to_handle(QueryString, Options) is equivalent to string_to_handle(QueryString, Options, erl_eval:new_bindings()).

This function is probably mainly useful when called from outside of Erlang, for example from a driver written in C.

table(TraverseFun, Options) -> QH |

### Types

TraverseFun = TraverseFun0 | TraverseFun1
TraverseFun0 = fun(() -> TraverseResult)
TraverseFun1 = fun((match_expression()) -> TraverseResult)
TraverseResult = Objects | term()
Objects = [] | [term() | ObjectList]
ObjectList = TraverseFun0 | Objects
Options = [Option] | Option
Option =
{format_fun, FormatFun} |
{info_fun, InfoFun} |
{lookup_fun, LookupFun} |
{parent_fun, ParentFun} |
{post_fun, PostFun} |
{pre_fun, PreFun} |
{key_equality, KeyComparison}
FormatFun = undefined | fun((SelectedObjects) -> FormatedTable)
SelectedObjects =
all |
{all, NElements, DepthFun} |
{match_spec, match_expression()} |
{lookup, Position, Keys} |
{lookup, Position, Keys, NElements, DepthFun}
NElements = infinity | integer() >= 1
DepthFun = fun((term()) -> term())
FormatedTable = {Mod, Fun, Args} | abstract_expr() | string()
InfoFun = undefined | fun((InfoTag) -> InfoValue)
InfoTag = indices | is_unique_objects | keypos | num_of_objects
InfoValue = undefined | term()
LookupFun = undefined | fun((Position, Keys) -> LookupResult)
LookupResult = [term()] | term()
ParentFun = undefined | fun(() -> ParentFunValue)
PostFun = undefined | fun(() -> term())
PreFun = undefined | fun((PreArgs) -> term())
PreArgs = [PreArg]
PreArg = {parent_value, ParentFunValue} | {stop_fun, StopFun}
ParentFunValue = undefined | term()
StopFun = undefined | fun(() -> term())
KeyComparison = '=:=' | '=='
Position = integer() >= 1
Keys = [term()]
Mod = Fun = atom()
Args = [term()]
QH = query_handle()

Returns a query handle for a QLC table. In Erlang/OTP there is support for ETS, Dets, and Mnesia tables, but many other data structures can be turned into QLC tables. This is accomplished by letting function(s) in the module implementing the data structure create a query handle by calling qlc:table/2. The different ways to traverse the table and properties of the table are handled by callback functions provided as options to qlc:table/2.

- Callback function TraverseFun is used for traversing the table. It is to return a list of objects terminated by either [] or a nullary fun to be used for traversing the not yet traversed objects of the table. Any other return value is immediately returned as value of the query evaluation. Unary TraverseFuns are to accept a match specification as argument. The match specification is created by the parse transform by analyzing the pattern of the generator calling qlc:table/2 and filters using variables introduced in the pattern. If the parse transform cannot find a match specification equivalent to the pattern and filters, TraverseFun is called with a match specification returning every object.

  - Modules that can use match specifications for optimized traversal of tables are to call qlc:table/2 with an unary TraverseFun. An example is ets:table/2.

  - Other modules can provide a nullary TraverseFun. An example is gb_table:table/1 in section Implementing a QLC Table.

- Unary callback function PreFun is called once before the table is read for the first time. If the call fails, the query evaluation fails.

  Argument PreArgs is a list of tagged values. There are two tags, parent_value and stop_fun, used by Mnesia for managing transactions.

  - The value of parent_value is the value returned by ParentFun, or undefined if there is no ParentFun. ParentFun is called once just before the call of PreFun in the context of the process calling eval/1,2, fold/3,4, or cursor/1,2.

  - The value of stop_fun is a nullary fun that deletes the cursor if called from the parent, or undefined if there is no cursor.

- Nullary callback function PostFun is called once after the table was last read. The return value, which is caught, is ignored. If PreFun has been called for a table, PostFun is guaranteed to be called for that table, even if the evaluation of the query fails for some reason.

  The pre (post) functions for different tables are evaluated in unspecified order.

  Other table access than reading, such as calling InfoFun, is assumed to be OK at any time.

- Binary callback function LookupFun is used for looking up objects in the table. The first argument Position is the key position or an indexed position and the second argument Keys is a sorted list of unique values. The return value is to be a list of all objects (tuples), such that the element at Position is a member of Keys. Any other return value is immediately returned as value of the query evaluation. LookupFun is called instead of traversing the table if the parse transform at compile time can determine that the filters match and compare the element at Position in such a way that only Keys need to be looked up to find all potential answers.

  The key position is obtained by calling InfoFun(keypos) and the indexed positions by calling InfoFun(indices). If the key position can be used for lookup, it is always chosen, otherwise the indexed position requiring the least number of lookups is chosen. If there is a tie between two indexed positions, the one occurring first in the list returned by InfoFun is chosen. Positions requiring more than max_lookup lookups are ignored.

- Unary callback function InfoFun is to return information about the table. undefined is to be returned if the value of some tag is unknown:

  indices
    Returns a list of indexed positions, a list of positive integers.
  is_unique_objects
    Returns true if the objects returned by TraverseFun are unique.
  keypos
    Returns the position of the table key, a positive integer.
  is_sorted_key
    Returns true if the objects returned by TraverseFun are sorted on the key.
  num_of_objects
    Returns the number of objects in the table, a non-negative integer.

- Unary callback function FormatFun is used by info/1,2 for displaying the call that created the query handle of the table. Defaults to undefined, which means that info/1,2 displays a call to '$MOD':'$FUN'/0. It is up to FormatFun to present the selected objects of the table in a suitable way. However, if a character list is chosen for presentation, it must be an Erlang expression that can be scanned and parsed (a trailing dot is added by info/1,2 though).

  FormatFun is called with an argument that describes the selected objects based on optimizations done as a result of analyzing the filters of the QLC where the call to qlc:table/2 occurs. The argument can have the following values:

  {lookup, Position, Keys, NElements, DepthFun}.

    LookupFun is used for looking up objects in the table.

  {match_spec, MatchExpression}

    No way of finding all possible answers by looking up keys was found, but the filters could be transformed into a match specification. All answers are found by calling TraverseFun(MatchExpression).

  {all, NElements, DepthFun}

    No optimization was found. A match specification matching all objects is used if TraverseFun is unary.

    NElements is the value of the info/1,2 option n_elements.

    DepthFun is a function that can be used for limiting the size of terms; calling DepthFun(Term) substitutes '...' for parts of Term below the depth specified by the info/1,2 option depth.

    If calling FormatFun with an argument including NElements and DepthFun fails, FormatFun is called once again with an argument excluding NElements and DepthFun ({lookup, Position, Keys} or all).

- The value of option key_equality is to be '=:=' if the table considers two keys equal if they match, and to be '==' if two keys are equal if they compare equal. Defaults to '=:='.

For the various options recognized by table/1,2 in respective module, see ets(3), dets(3), and mnesia(3).

### See Also

dets(3), erl_eval(3), erlang(3), error_logger(3), ets(3), file(3), file_sorter(3), mnesia(3), shell(3), Erlang Reference Manual, Programming Examples