Coding standard change: module qualification in library interfaces.

compiler/notes/coding_standards.html:
	Specify that library module interfaces should avoid module
	qualification where possible.

	Minor formatting improvements and (a few) rewordings.
Avoid module qualification in library interfaces where possible.

NOTE: this change does not affect the io module -- I've left that for a
separate change.

library/*.m:
	As per the recent change to the coding standard, avoid module
	qualification in library interfaces where possible.

	Reformat declarations and descriptive comments to better utilise
	any space freed up by the above.
Fix array.sort.

array.sort was broken since 2001 when the implementation was changed
from merge sort to SAM sort.

library/array.m:
	Fix the confusion as to which output argument of samsort_up is
	sorted.

	Add optional runtime checks of the pre- and post-conditions
	of samsort_up and samsort_down when built with a trace flag.

	Add array.sort_fix_2014.

tests/hard_coded/Mmakefile:
tests/hard_coded/array_sort.exp:
tests/hard_coded/array_sort.m:
	Add test program.

NEWS:
	Announce the fix.
Modify code for stricter mode checking.

The following changes are necessary to compile the code following an
upcoming change to the mode checker.  Some of them are mode errors.

browser/declarative_oracle.m:
	The result of `query_oracle_kb' should be specific
	to avoid mode errors in callers.

compiler/accumulator.m:
	Help the compiler infer the declared inst.

compiler/mode_robdd.tfeirn.m:
	`zero' must be declared to return a normalised tfeirn value to
	avoid mode errors.  Change `one' for symmetry.

	`var_restrict_true' and `var_restrict_false' may return
	unnormalised tfeirn values (judging by the code).

	Modify `normalise' to help the compiler infer
	the declared inst.

compiler/tupling.m:
deep_profiler/autopar_find_best_par.m:
	Delete final insts that the compiler cannot verify.

library/tree234.m:
	Help the mode checker out using an explicit switch.

tests/hard_coded/bitmap_test.m:
tests/hard_coded/bitmap_tester.m:
	Add missing modes.
Modify exception module for stricter mode checking.

library/exception.m:
	Fix errors that prevent the module from compiling following an
	upcoming change to the mode checker.

	Simplify code by creating higher-order terms from multi-moded
	predicates.  There is no need to switch on the determinism now.

tests/debugger/declarative/catch_retry.exp:
tests/debugger/declarative/catch_retry.inp:
	Update test case.
Modify solutions module for stricter mode checking.

library/solutions.m:
	Fix mode error in `solutions' predicate in the `multi' mode.
	Convince the compiler with a run-time check that the solution
	list will be non-empty.

	Similarly for `builtin_solutions'.

tests/debugger/declarative/solns.exp3:
tests/debugger/loopcheck.exp3:
	Update expected outputs.
Do not allow ground insts to match incomplete bound insts.

The mode checker contains a hack where ground matches bound insts even
if the bound inst is incomplete for the type.  The stated reason is that
otherwise the mode checker is too conservative in the absence of alias
tracking.  However, it runs counter to the typical behaviour expected of
the compiler.

Disable the hack, thus rejecting more mode-incorrect code but also some
mode-correct code.

Some compiler-generated unify predicates would now be rejected.
We retain the hack only for those predicates.

NOTE: this change exposes some bugs which will be fixed in subsequent
changes.  A few test cases will fail for now: contravariance_poly.m and
some declarative debugging test cases.

compiler/hlds_pred.m:
	Add helper predicate `is_unify_pred'.

compiler/inst_match.m:
	Add a variant of `inst_matches_final_typed' to select if ground
	matches incomplete bound insts.

	Do not allow ground to match incomplete bound insts in
	`inst_matches_final_3' unless explicitly enabled.

	Do not allow ground to match incomplete bound insts in
	`inst_matches_binding_3'.

compiler/modes.m:
	Use lax `ground_matches_bound_always' behaviour for checking the
	final insts of compiler-generated unify predicates only.

NEWS:
	Announce change.
Fix computation of bound variables in goal_info_to_atomic_goal_rep_fields.

instmap_changed_vars calls inst_matches_final_typed to compare the insts
of a variable between two instmaps.  Previously inst_matches_final_typed
contained a hack such that ground matches even incomplete bound(...) insts,
so variables changing instantiatedness from ground to bound(...) would
NOT be included in the result set of instmap_changed_vars.  With the
removal of that hack, the behaviour of instmap_changed_vars changed to
match its documentation.

goal_info_to_atomic_goal_rep_fields used instmap_changed_vars to compute
BoundVars.  Variables whose instantiation state changes from ground to
bound, but are not actually bound by the goal, would now be in BoundVars.
This bug led to changes in the goal representation (e.g. from
unify_deconstruct_rep to partial_deconstruct_rep) and caused some
declarative debugging test cases to fail.

compiler/prog_rep.m:
	Change goal_info_to_atomic_goal_rep_fields to exclude variables
	which are ground before the goal from BoundVars.

	Reorder some code to execution order.
Fix problems with higher-order inst matching with polymorphic modes.

These changes allow the compiler to accept the contravariance_poly.m
test case that requires higher order pred insts to be contravariant in
the initial argument insts.  Previously the compiler would have rejected
it, if not for the hack that allows ground to match incomplete bound insts.

compiler/inst_match.m:
	pred_inst_argmodes_matches enabled the cs_reverse mode when
	matching initial insts, which causes handle_inst_var_subs to
	swap the insts for the inst var substitution calculation.
	The insts are swapped back for inst matching, but cs_reverse
	remained effective.  A recursive call to match two insts would
	swap the insts *again*, to the wrong order.  The fix is to
	invert cs_reverse to cs_forward when the insts are swapped back
	for matching.

	In pred_inst_argmodes_matches apply the incrementally calculated
	inst var substitution before matching.

compiler/modecheck_util.m:
	Make handle_implied_mode use inst_matches_initial_no_implied_modes_sub.
	The non-sub variant does not compute inst var substitutions and
	fails where the higher-order argument inst has an inst variable
	(as in contravariance_poly.m), then handle_implied_mode
	incorrectly deduces that the call is to an implied mode.
Avoid module qualification in the interface of the io module where possible.

library/io.m:
	Avoid module qualification in the interface where possible.
	I've left the module qualifiers on the types io.state/0 and io.error/0
	since they just look strange without them.  (We don't really use io.state/0
	much these days anyway.)

	Most the result types (e.g. io.res/0) still require module
	qualification in order to distinguish them from the result types
	in the stream module.

	s/input_stream/text_input_stream/ for the all uses of the type
	input_stream.  Similarly for output streams.  This was what was
	intended when the equivalences were originally introduced and the
	debugger has used the text_* names for many years now.

library/store.m:
	s/io\.state/io/ in a few spots.
Merge branch 'version-14_01-branch'
Restore promise_pure declaration for catch_impl.

I inadvertently deleted a promise_pure declaration for catch_impl in
commit 3f220a009735b1bda71e1ce320025e66811fb4b5.  It is necessary in
java and csharp grades as catch_impl is implemented with mode-specific
clauses in those grades so the compiler infers impure.

library/exception.m:
	Restore the promise_pure declaration.

	Add Zoltan's explanation for why catch_impl is impure.

	Explain why we currently pretend catch_impl is pure.
boehm_gc: Apply upstream BoehmGC changes for lock elision bug workaround

This code was written by Ivan Maidanski <ivmai@mail.ru> in response to my
patch to the the Boehm GC to work-around the lock elision bug in glibc.

Fix and code refactoring of lock elision workaround (Linux/x64)
https://github.com/ivmai/bdwgc/commit/3d342554dc03af8d27925706a6513080995e1904

boehm_gc/private/gcconfig.h:
    + (GLIBC_2_19_TSX_BUG): New macro defined for Linux/x86_64 (if Glibc
      used) to workaround a bug in Glibc lock elision implementation.

boehm_gc/misc.c:
    + (GC_init): Reformat code.

boehm_gc/pthread_support.c:
    + Move include of gnu/libc-version.h to gcconfig.h (used to check
      whether lock elision workaround needed).

    + (mark_mutex): Initialize (to PTHREAD_MUTEX_INITIALIZER) even lock
      elision workaround is needed (revert change in previous commit).

    + (parse_version): New static function (defined only if
      GLIBC_2_19_TSX_BUG).

    + (GC_setup_mark_lock): Use parse_version to check target Glibc version
      properly; do not reinitialize mutex unless workaround needed; call
      ABORT (with the appropriate message) in case of a failure in
      pthread_mutexattr_init/settype, pthread_mutex_init.
boehm_gc: Use PTHREAD_MUTEX_NORMAL in the lock elision bug workaround

Peter Wang found that PTHREAD_MUTEX_NORMAL is sufficient to disable lock
elision for a mutex and avoid comprimising performance with
PTHREAD_MUTEX_ERRORCHECK.

I'll be writing to Ivan to let him know about this change.

boehm_gc/pthread_support.c:
    As above.
boehm_gc: Fix declration of setup_mark_lock()

These patches have been applied to upstream boehm_gc by Ivan Maidanski.
We're applying them here as we're tracking their fixes for the TSX lock
elision bug.

boehm_gc/pthread_support.c:
    Rename GC_setup_mark_lock() to setup_mark_lock() and make it static.
    Add static declration of setup_mark_lock();
    Call setup_mark_lock() while setting up parallel marking.

boehm_gc/include/private/pthread_support.h:
    Remove declaration of GC_setup_mark_lock();

boehm_gc/misc.c:
    Remove call to GC_setup_mark_lock();
boehm_gc: Enable the TSX bug workaround on x86

Jan Alexander Steffens made this change to upstream boehm_gc.

boehm_gc/include/private/gcconfig.h:
    As above.
Merge branch 'version-14_01-branch'

Merge boehm_gc changes.