Commit Graph

1448 Commits

Author SHA1 Message Date
Julien Fischer
2a366cf295 Deprecate --no-ansi and --no-ansi-c.
--no-ansi (mgnuc) and --no-ansi-c (mmc) have not actually done anything for
many years now. Deprecate these options and remove their "use" throughout most
of the Mercury system. (The remaining uses are in the Makefiles for the Boehm
GC, which need to be updated separately.)

Also deprecate the internal compiler option --cflags-for-ansi.

compiler/options.m:
    Document that --no-ansi-c is now deprecated.

    Document that the internal option --cflags-for-ansi is now
    deprecated.

compiler/compile_target_code.m:
    Do not pass the ANSI options to the C compiler.

scripts/mgnuc.in:
scripts/mgnuc_file_opts.sh-subr:
    Deprecate the --no-ansi option; delete code that no longer does
    anything useful.

configure.ac:
    Delete the configuration variable CFLAGS_FOR_ANSI; it is only ever
    set to be empty. (The comment talks about --no-ansi doing other things
    in the mgnuc script. It used to also cause some preprocessor macros
    to be defined for compatibility with the system headers on some
    platforms -- that has not been the case since 2013.)

doc/user_guide.texi:
    Document that --no-ansi-c is deprecated.

bytecode/Mmakefile:
compiler/Mercury.options:
library/Mercury.options:
extras/odbc/odbc.m:
runtime/Mmakefile:
scripts/Mercury.config.bootstrap.in:
scripts/Mercury.config.in:
tests/hard_coded/Mercury.options:
tests/valid/Mercury.options:
trace/Mmakefile:
util/Mmakefile:
    Conform to the above change.

NEWS.md:
    Announce the above.
2023-05-31 17:44:26 +10:00
Zoltan Somogyi
9364d4cbc5 Replace tables with spaces. 2023-05-02 02:25:38 +10:00
Zoltan Somogyi
3364b69e34 Simplify a test. 2023-05-02 02:22:46 +10:00
Zoltan Somogyi
b2ab9f8711 Wrap the test term in angle brackets.
tests/hard_coded/write_binary.{m,exp}:
    Wrap the test term in angle brackets, in order to make the presence
    of spaces, tabs and newlines apparent.
2023-04-27 21:27:21 +10:00
Zoltan Somogyi
810d95a7ea Update the hard_coded/write_binary test case.
tests/hard_coded/write_binary.m:
    Print the term whose write-out-and-read-back is being tested
    before the test, not after. Print the result of the test after this.

    If the test succeeds, don't print the term out again.

    Put blank lines between different kinds of tests, and between
    tests of different terms.

    Return error messages as strings, rather than as exceptions,
    where possible.

    Make the diagnostic for "read back term differs from original"
    more useful for debugging write_binary/read_binary.

    Put the declaration of a predicate just before the predicate.

tests/hard_coded/write_binary.exp:
    Expect the updated output.
2023-04-27 21:19:51 +10:00
Zoltan Somogyi
4281a28f73 Make text_{input,output}_stream standalone types ...
... and make {input,output}_stream synonyms for them, rather than vice versa.

library/io.m:
    As above.

library/bitmap.m:
library/dir.m:
library/io.primitives_read.m:
library/io.stream_db.m:
library/io.text_read.m:
library/mercury_term_lexer.m:
library/stream.string_writer.m:
    Conform to the change above.

tests/hard_coded/stream_string_writer_types.exp:
    Expect the new type_ctor for text streams.
2023-04-24 13:52:10 +10:00
Julien Fischer
5de97e355a Replace references to the c_header_code pragma.
tests/hard_coded/needs_init.m:
tests/hard_coded/pragma_c_code.m:
    As above.
2023-02-25 12:55:08 +11:00
Zoltan Somogyi
3c931d074c Delete old_list_to_set.
library/fat_sparse_bitset.m:
library/sparse_bitset.m:
    As above.

tests/hard_coded/speedtest_bitset.m:
    Delete references to old_list_to_set.
2023-02-03 19:30:53 +11:00
Zoltan Somogyi
ee0f9f73d7 Add library/fatter_sparse_bitset.m.
library/fatter_sparse_bitset.m:
    Add this version of fat_sparse_bitset.m, which stores *two* words
    worth of bits in each cell, not one. This word would otherwise be unused,
    because the Boehm-Demers-Weiser allocator rounds up requests for three
    word cells to four.

library/MODULES_DOC:
library/library.m:
    Add the new module to the list of library modules.

library/fat_sparse_bitset.m:
library/sparse_bitset.m:
library/tree_bitset.m:
    Update the documentation of all these other bitset modules. Copy
    the same basic introduction to all the relevant modules. Add documentation
    of the differences to tree_bitset.m and fatter_sparse_bitset.m, with
    a pointer in fat_sparse_bitset.m to fatter_sparse_bitset.m.

library/test_bitset.m:
    Test the new module as well as the others.

tests/hard_coded/speedtest_bitset.m:
    Extend the benchmarking of list_to_set operations to the new module.
    To allow the benchmarking to be tough enough to be informative, comment
    out the benchmarking of the old_list_to_set operations.
2023-01-31 18:36:16 +11:00
Zoltan Somogyi
6f6d30b8f2 Improve the speed of fat_sparse_bitset.list_to_set ...
... by using the algorithm now in sparse_bitset.list_to_set.

Keep the old list_to_set algorithm around in both modules for a short while
to allow comparative benchmarking.

library/fat_sparse_bitset.m:
library/sparse_bitset.m:
    As above.

tests/hard_coded/speedtest_bitset.m:
    A benchmark program to compare the old and new list_to_set algorithms,
    both versus each other, and between sparse_bitset and fat_sparse_bitset.
2023-01-29 07:57:44 +11:00
Zoltan Somogyi
80b1d4a7e3 Test all bitset implementation at once.
library/test_bitset.m:
    Instead of comparing just one bitset module (tree_bitset by default)
    against set_ordlist, compare all of them (tree_bitset, sparse_bitset,
    and fat_sparse_bitset) against set_ordlist.

tests/hard_coded/test_bitsets.{m,exp}:
    Rename this test case from test_tree_bitset to test_bitsets, since
    (through library/test_bitset.m) we now test ALL bitset implementations,
    not just tree_bitset.

tests/hard_coded/Mmakefile:
    Refer to the test by its new name.
2023-01-29 04:04:24 +11:00
Julien Fischer
bbff6f8609 Fix a bug in string.format.
The current implementation of the unsigned conversion specifiers (i.e. %x, %X,
%o, %u) for fixed-size 8-, 16- and 32-bit signed integers works by casting
theses values into (word-sized) ints and then using the existing code for
formatting ints as unsigned values. This does not work for negative values as
they are being sign extended when converted to ints. For example,

      io.format("%x", [i8(min_int8)], !IO)

prints:

      ffffffffffffff80 (on a 64-bit machine)

rather than just:

      80

Fix the above problem by casting ints, int8s, int16s etc. that are being
formatted as unsigned values to uints and using the uint formatting code.

NOTE: the formatting code for 64-bit integers follows a different code path
and is not affected by any of this.

library/string.format.m:
    Implement unsigned conversion specifiers for non-64-bit signed
    integers by casting to uint and using the uint formatting code.

    Add predicates for converting signed integers into uints.

    The format_unsigned_int_* predicates can be deleted after this change
    is installed.

compiler/format_call.m:
    Implement unsigned conversion specifiers for non-64-bit signed
    integers by casting to uint and using the uint formatting code.

compiler/introduced_call_table.m:
    Update the table of introduced predicates.

compiler/options.m:
    Add an option that can be used to test whether this fix is
    installed.

tests/hard_coded/Mmakefile:
tests/hard_coded/Mercury.options:
tests/hard_coded/opt_format_sign_extend.{m,exp}:
    Test that formatting code introduced by the compiler does not
    accidentally sign-extend negative values.

tests/string_format/string_format_{o,x,u}.{m,exp,exp2}:
    Make these tests much more comprehensive then they previously
    were.
2023-01-27 17:16:54 +11:00
Peter Wang
9fa20d1e71 Delete unused transitive closure implementations.
library/digraph.m:
    Delete simple_tc and stack_tc.

    Reorder some code.

tests/hard_coded/digraph_tc.m:
    Delete code for testing simple_tc and stack_tc.
2023-01-25 16:14:11 +11:00
Peter Wang
0eb4281a90 Implement two more transitive closure algorithms.
Implement two transitive closure algorithms in the digraph module:

  - Basic_TC by Yannis Ioannidis et al.

  - STACK_TC by Esko Nuutila, a refinement of the SIMPLE_TC algorithm
    previously implemented

On 450 graphs randomly generated by tests/hard_coded/digraph_tc.m,
ranging from 100 to 3000 vertices:

  - basic_tc ran from 0.79 to 1.66 times as fast as simple_tc
    (mean 1.139, stdev 0.136)

  - basic_tc ran from 0.83 to 1.81 times as fast as stack_tc
    (mean 1.131, stdev 0.160)

Therefore, after this commit, I will delete the simple_tc and stack_tc
implementations, but they will be available in the version history.

library/digraph.m:
    Implement Basic_TC and STACK_TC.

    Use map.transform_value in key_set_map_union to replace a search
    followed by update.

tests/hard_coded/digraph_tc.m:
    Test and benchmark the new algorithms.

    Also compare inverse graphs to check that predecessor maps are
    maintained properly.
2023-01-25 12:28:45 +11:00
Zoltan Somogyi
56ed26f650 Improve the remove_{leq,gt} sparse bitset ops.
library/sparse_bitset.m:
library/fat_sparse_bitset.m:
    Speed up the remove_leq and remove_gt operations by moving a
    loop invariant computation, the conversion of the boundary item's index
    into an <offset,bitposn> pair, out of the loop.

    Eliminate some unnecessary differences between the two modules,
    e.g. clear_bit being a predicate rather than a function.

library/test_bitset.m:
    Add facilities to test the remove_leq and remove_gt operations
    of sparse_bitset.m, fat_sparse_bitset.m, and tree_bitset.m
    against the same operations on plain old set_ordlists.

    Bring this module up to date by requiring set elements to be
    members of the uenum typeclass, not the enum typeclass.

    Make the test_bitset type a bespoke type.

library/tree_bitset.m:
    Add predicate versions of the remove_leq and remove_gt operations
    alongside the existing function versions, to allow the new code
    in test_bitset.m to work the same way regardless of which bitset module
    it is testing.

    For uniformity with the other bitset modules, require set elements to be
    members of the uenum typeclass, not the enum typeclass.

    Change the other integers, such as level numbers, to be unsigned
    as well, to avoid the need for casts.

NEWS:
    Announce the new additions and changes.

tests/hard_coded/test_tree_bitset.{m,exp}:
    Use those new facilities to test those operations, and add some
    test sets designed for that purpose.

    Add a comment about the limitations of this testing strategy.

tests/hard_coded/bitset_tester.m:
    Delete this long-unused module. (It was the original basis of
    the test_bitset.m module in the library directory, but it became unused
    when test_tree_bitset.m switched to using that module a long time ago.)
2023-01-19 14:42:34 +11:00
Peter Wang
b270b6d01c Delete old transitive closure implementation.
library/digraph.m:
    Delete digraph.old_tc, digraph.old_rtc and digraph.slow_rtc.

tests/hard_coded/digraph_tc.m:
tests/hard_coded/digraph_tc.exp:
    Delete comparisons using old_tc, old_rtc and slow_rtc.
2023-01-18 16:50:41 +11:00
Peter Wang
685e632a13 Implement simple_tc algorithm.
Implement transitive closure using the simple_tc algorithm from
Esko Nuutila's doctoral thesis.

On a sample of graphs randomly generated by tests/hard_coded/digraph_tc.m,
ranging from 100 to 3000 vertices, the simple_tc implementation ran
from 2.33 to 93 times as fast as the old implementation on my machine.

library/digraph.m:
    Rename digraph.tc and digraph.rtc to digraph.old_tc and
    digraph.old_rtc. They are kept around for benchmarking,
    and will be deleted soon.

    Use the simple_tc algorithm to implement digraph.tc.

    Use digraph.tc to implement digraph.rtc.

    Let key_set_map_add call sparse_bitset.insert_new instead of
    sparse_bitset.contains followed by sparse_bitset.insert.

tests/hard_coded/digraph_tc.m:
    Add code to benchmark the new and old TC implementations.
2023-01-18 16:50:41 +11:00
Julien Fischer
97735d9630 Do not allow std_util.pow/3 with negative values.
library/std_util.m:
    Make std_util.pow/3 throw an exception if it is called with a negative
    integer as its second argument.

tests/hard_coded/Mmakefile:
tests/hard_coded/func_exp.{m,exp}:
    Add a test of pow/3.

NEWS:
    Announce the above change.
2023-01-11 17:10:00 +11:00
Peter Wang
fba3fda155 Fix digraph.tc and digraph.rtc.
The implementation of digraph.rtc was incorrect (as demonstrated in the
new test case), which meant that digraph.tc was also incorrect.

library/digraph.m:
    Fix the implementation of rtc (reflexive transitive closure):

    - Following the algorithm used in digraph.cliques, it needs to
      traverse the graph G in *reverse* depth-first order.

    - To find the clique containing a vertex X, it needs to do a DFS on
      the *reversed* graph to find the vertices with a path to X.
      The vertices that were previously unvisited will be members of
      the same clique as X.

    - Previously it found the "followers" of the elements of the clique,
      and the followers of those followers, then added edges from the
      members of the current clique to those followers. However, that
      only includes vertices two steps removed from the clique.
      I have fixed it to add edges to *all* vertices reachable from
      members of the clique.

    Add straightforward implementations of tc and rtc for comparison.

    Add some comments.

tests/hard_coded/Mmakefile:
tests/hard_coded/digraph_tc.exp:
tests/hard_coded/digraph_tc.inp:
tests/hard_coded/digraph_tc.m:
    Add test case.

NEWS:
    Announce the fixes.
2023-01-11 11:04:26 +11:00
Zoltan Somogyi
5cbcfaa0ed Move X_to_doc functions to pretty_printer.m.
library/array.m:
library/char.m:
library/float.m:
library/int.m:
library/int16.m:
library/int32.m:
library/int64.m:
library/int8.m:
library/list.m:
library/one_or_more.m:
library/string.m:
library/tree234.m:
library/uint.m:
library/uint16.m:
library/uint32.m:
library/uint64.m:
library/uint8.m:
library/version_array.m:
    Mark the X_to_doc function in each of these modules as obsolete,
    and make it a forwarding function to the actual implementation
    in pretty_printer.m. The intention is that when these forwarding
    functions are eventually removed, this will also remove the dependency
    of these modules on pretty_printer.m. This should help at least some
    of these modules escape the giant SCC in the library's dependency graph.
    (It does not make sense that a library module that adds code to increment
    an int thereby becomes dependent on pretty_printer.m through int.m.)

library/pretty_printer.m:
    Move all the X_to_doc functions from the above modules here.

    Fix the one_or_more_to_doc function, which was

    - missing the comma between the two arguments of the one_or_more
      function symbol, and

    - would print "..., ...]" instead of just "...]" at the end of the
      tail list when that list exceeded the limits of the specified pp_params.

    Rename one of the moved types along with its function symbols,
    to reduce ambiguity.

    Put arrays before their indexes in the argument lists of some of
    the moved functions.

    Some of the moved X_to_doc functions for compound types returned
    a doc that had an indent wrapper. These indents differed between the
    various X_to_doc functions without any visible reason, but they are
    also redundant. The callers can trivially add such wrappers if they
    want to, but taking them off, if they want them off, is harder.
    Eliminate the problem by deleting all such indent wrappers.

    Add formatters for the intN, uintN and one_or_more types to the
    default formatter map. Their previous absence was an oversight.

    Add a function, get_formatter_map_entry_types, that returns the ids
    of the types in the formatter_map given to the function. It is intended
    for tests/hard_coded/test_pretty_printer_defaults.m, but is exported
    for anyone to use.

tests/hard_coded/test_pretty_printer_defaults.{m,exp}:
    Use get_formatter_map_entry_types to print the default formatter map
    in a format that is much more easily readable.

NEWS:
    Announce all the user-visible changes above.
2022-12-27 18:27:52 +11:00
Julien Fischer
5274170784 Make characters an instance of the uenum typeclass
The recent change to sparse_bitsets broke the lex library in extras.
Specifically, we now now need to make characters an instance of the
uenum typeclass. This diff does so.

library/char.m:
     Add predicates and functions for converting between unsigned integers
     and characters.

     Make characters an instance of the uenum typeclass.

tests/hard_coded/Mmakefile:
tests/hard_coded/char_uint_conv.{m,exp,exp2}:
     Add a test of the above conversions.

NEWS:
     Announce the additions.

extras/lex/lex.m:
     Conform to recent changes.
2022-12-20 20:18:54 +11:00
Zoltan Somogyi
fc3c6b462c Update an expected output file for Java ...
... for a change to io.file.m on 2022 aug 24.
2022-12-09 23:56:59 +11:00
Zoltan Somogyi
4c528d429d Add <<u and >>u to library/{int,uint}*.m ...
... along with their unchecked equivalents. These differ from <<, >> and
their unchecked equivalents in that they take the shift amount as a uint,
instead of an int.

library/int.m:
library/int16.m:
library/int32.m:
library/int64.m:
library/int8.m:
library/uint.m:
library/uint16.m:
library/uint32.m:
library/uint64.m:
library/uint8.m:
    As above. The unchecked versions have only declarations, since
    these operations have been recognized as builtins for a while now.

NEWS:
    Document the new operations, and the recent change to recognize
    <<u and >>u as single tokens, and fix a typo in a recent addition.

configure.ac:
    Require the compiler to be sufficiently recent to be able to parse
    <<u and >>u as operators.

compiler/options.m:
    Provide a way for a later change to configure.ac to detect the presence
    of this change.

tests/hard_coded/bitwise_int.exp:
tests/hard_coded/bitwise_int.exp2:
tests/hard_coded/bitwise_int.m:
tests/hard_coded/bitwise_int16.exp:
tests/hard_coded/bitwise_int16.m:
tests/hard_coded/bitwise_int32.exp:
tests/hard_coded/bitwise_int32.m:
tests/hard_coded/bitwise_int64.exp:
tests/hard_coded/bitwise_int64.m:
tests/hard_coded/bitwise_int8.exp:
tests/hard_coded/bitwise_int8.m:
tests/hard_coded/bitwise_uint.exp:
tests/hard_coded/bitwise_uint.exp2:
tests/hard_coded/bitwise_uint.m:
tests/hard_coded/bitwise_uint16.exp:
tests/hard_coded/bitwise_uint16.m:
tests/hard_coded/bitwise_uint32.exp:
tests/hard_coded/bitwise_uint32.m:
tests/hard_coded/bitwise_uint64.exp:
tests/hard_coded/bitwise_uint64.m:
tests/hard_coded/bitwise_uint8.exp:
tests/hard_coded/bitwise_uint8.m:
    Check that <<u and >>u compute the same results as << and >> respectively.
2022-12-07 23:12:33 +11:00
Zoltan Somogyi
d551bc743a Add an expected output file for 32 bit targets.
tests/hard_coded/int_uenum.exp2:
    Add this expected output file.

tests/hard_coded/int_uenum.m:
    Note what conditions call for each expected output file.
2022-12-07 11:16:18 +11:00
Zoltan Somogyi
925db2061d Update expected outputs for 32-bit machines. 2022-12-07 11:15:43 +11:00
Zoltan Somogyi
3c3c072a33 Make bitwise op tests' .exp files more readable.
tests/hard_coded/bitwise_int.m:
tests/hard_coded/bitwise_int16.m:
tests/hard_coded/bitwise_int32.m:
tests/hard_coded/bitwise_int64.m:
tests/hard_coded/bitwise_int8.m:
tests/hard_coded/bitwise_uint.m:
tests/hard_coded/bitwise_uint16.m:
tests/hard_coded/bitwise_uint32.m:
tests/hard_coded/bitwise_uint64.m:
tests/hard_coded/bitwise_uint8.m:
    Most of the expected output files of these test cases were
    easily readable already, but there were some exceptions caused
    by unnecessary differences between their source codes.

    The main problem was with bitwise_uint.m (uint_bitwise.m until recently),
    whose .exp file contained very long lines of the form A op B = C
    where A, B and C could each be 64 bits (and chars) long. Fix this
    by adopting the format used by the other modules:

        A op
        B =
        C

    The other problem was that in some test cases, the indentation had
    an off-by-one bug. They generated output such as

        \ aaaa =
         bbbb

    instead of the much-easier-to-visually-check

        \ aaaa =
          bbbb

    A third, much less important change is the deletion of unnecessary
    blank lines.

tests/hard_coded/bitwise_int.exp:
tests/hard_coded/bitwise_int16.exp:
tests/hard_coded/bitwise_int32.exp:
tests/hard_coded/bitwise_int64.exp:
tests/hard_coded/bitwise_int8.exp:
tests/hard_coded/bitwise_uint.exp:
tests/hard_coded/bitwise_uint16.exp:
tests/hard_coded/bitwise_uint32.exp:
tests/hard_coded/bitwise_uint64.exp:
tests/hard_coded/bitwise_uint8.exp:
    Conform to the changes in codes of the tests.
2022-12-05 21:25:07 +11:00
Zoltan Somogyi
91864db2cd Rename the uint_bitwise.m test case ...
... to bitwise_uint.m, to fit in with

    bitwise_int.m
    bitwise_int{8,16,32,64}.m and
    bitwise_uint{8,16,32,64}.m.
2022-12-05 19:54:41 +11:00
Zoltan Somogyi
ec20b1ed0a Make sparse_bitset.m operate on uints.
NEWS:
    Mention all the user-visible changes below.

library/enum.m:
    Add the typeclass uenum, which is a version of the existing enum typeclass
    that maps items to uints, not ints. It also uses a semidet predicate,
    not a semidet function, to get back to the item from the uint.

library/sparse_bitset.m:
library/fat_sparse_bitset.m:
    Make these modules operate on uints, which means requiring the items
    in the sets to be instances of uenum, not enum.

    If a few places, improve loops by doing previously-repeated conversions
    of [u]ints into <offset, bit-to-set> pairs just once.

library/counter.m:
    Define ucounters, which allocate uints. Improve documentation.

library/digraph.m:
    Change digraph_keys from ints to uints, since we put them into
    sparse_bitsets.

library/int.m:
    Make int an instance of the uenum typeclass. This can help users
    who currently put ints into sparse_bitsets.

library/pprint.m:
    Prettyprint sparse_bitsets as lists of uints.

library/term.m:
    Make vars instances of uenum as well as enum.

library/uint.m:
    Make uint an instance of the uenum typeclass.

    Add the ubits_per_uint function, which allows some casts to be avoided.

compiler/make.deps_set.m:
    Change the indexes we put into sparse_bitsets from ints to uints.

compiler/make.make_info.m:
    Change the source of those indexes from ints to uints.

compiler/make.top_level.m:
compiler/make.util.m:
    Conform to the changes above.

compiler/pre_quantification.m:
    Change zones from ints to uints, since we put them into sparse_bitsets.

tests/hard_coded/int_uenum.{m,exp}:
tests/hard_coded/Mmakefile:
    Enable the new test case.

tests/valid/use_import_only_for_instance.m:
    Update this extract from library/digraph.m the same way as
    library/digraph.m itself.
2022-12-05 09:45:11 +11:00
Peter Wang
c8a5a7755d Delete unnecessary conditions on hard_coded/parse.
We don't need to prevent building the 'parse' test on platforms that
don't support static linking any more, as the static linking aspect of
that test case was disabled in commit fd088a4f6.

tests/hard_coded/Mmakefile:
    Delete conditions to avoid static linking on some platforms.

    Rename STATIC_LINK_PROGS to DEBUG_PROGS.
2022-11-23 16:48:31 +11:00
Peter Wang
7362493e1c Skip or delete tests that fail in low-level parallel grades.
Since the compiler now refuses to allow debugging in parallel grades,
tests that require debugging need to be skipped over in low-level
parallel grades or deleted.

tests/hard_coded/Mmakefile:
    Disable 'parse' test in parallel grades as it happens to link with
    debug libraries.

tests/par_conj/Mercury.options:
tests/par_conj/Mmakefile:
tests/par_conj/par_ddeath.exp:
tests/par_conj/par_ddeath.m:
tests/par_conj/par_ddeath_2.exp:
tests/par_conj/par_ddeath_2.m:
    Delete par_ddeath and par_ddeath_2 tests that once triggered a
    compiler abort with --trace deep in parallel grades.
    I don't think there is much value in keeping them around.

tests/valid/Mmake.valid.common:
    Skip LLDS_PROGS (i.e. test cases that required debugging) in
    parallel grades.

tests/valid/Mercury.options:
tests/valid/Mmakefile:
tests/valid/untuple_bug.m:
    Delete untuple_bug. It tests an old bug in the untupling
    transformation, which was more a programming exercise than something
    that should be used anyway.
2022-11-23 16:15:59 +11:00
Zoltan Somogyi
d481a42f59 Give each op class (infix, prefix etc) its own slot.
library/ops.m:
    Instead of an op_table mapping a string to a list of one or more op_infos,
    make it map the string to a single structure, the op_infos, which has
    four slots, one each for infix, binary prefix, prefix and postfix
    op information. This allows parsers and unparsers to go directly to
    the kind of operator (e.g. infix or prefix) that they are interested in.

NEWS:
    Announce the change.

compiler/parse_tree_out_term.m:
library/mercury_term_parser.m:
library/pretty_printer.m:
library/stream.string_writer.m:
library/string.to_string.m:
samples/calculator2.m:
tests/hard_coded/bug383.m:
    Conform to the change. In several places, the new operator representation
    allows the replacement of loops with direct lookups, and the replacement
    of if-then-else chains with switches.

    Add reminders about keeping two predicates in sync.
2022-11-14 13:46:22 +11:00
Zoltan Somogyi
c6d857cb6b Cleanups of ops-related code.
library/io.m:
    Delete the set_io_table predicate, which did nothing and was never
    called from anywhere, and the get_io_table predicate, which always
    returned the same op_table. They were in io.m's interface, but in the
    not-publicly-visible part of the interface.

library/ops.m:
    Rename the lookup_op method to is_op.

    Delete references to the predicates deleted from io.m.

NEWS:
    Announce the lookup_op->is_op rename.

compiler/parse_tree_out_term.m:
library/mercury_term_parser.m:
library/string.to_string.m:
library/term_io.m:
samples/calculator2.m:
tests/hard_coded/bug383.m:
    Conform to the lookup_op->is_op rename.

    Replace calls to get_io_table with code that directly gets
    the Mercury op table.

    In parse_tree_out_term.m, call the predicates operating on the
    Mercury op table directly, not through the op_table type class.

    In mercury_term_parser.m, update some comments.

    In term_io.m, use OpTable to refer to op_tables.

tests/hard_coded/stdlib_init.{m,exp}:
    Don't test get_io_table.
2022-11-12 12:53:07 +11:00
Zoltan Somogyi
de75b98b18 Make higher operator priorities bind tighter.
Mercury inherited its original system of operator priorities from Prolog,
because during its initial development, we wanted to be able execute
the Mercury compiler using NU-Prolog and later SICStus Prolog.
That consideration has long been obsolete, and now we may fix the
design error that gifted Prolog with its counter-intuitive system
of operator priorities, in which higher *numerical* operator priorities
mean lower *actual* priorities. This diff does that.

library/ops.m:
    Change the meaning of operator priorities, to make higher numerical
    priorities mean also higher actual priorities.

    This semantic change requires corresponding changes in any other module
    that uses ops.m. To force this change to occur, change the type
    representing priorities from being a synonym for a bare int to being
    a notag wrapper around an uint.

    The old "assoc" type had a misleading name, since it is related to
    associativity but is not itself a representation of associativity.
    Its two function symbols, which used to be just "x" and "y", meant that
    the priority of an argument must be (respectively) greater than,
    or greater than equal to, the priority of the operator. So rename
    x to arg_gt, y to arg_ge, and assoc to arg_prio_gt_or_ge.

    Rename the old adjust_priority_for_assoc predicate to min_priority_for_arg,
    which better expresses its semantics. Turn it into a function, since
    some of its users want it that way, and move its declaration to the
    public part of the interface.

    Add a method named tightest_op_priority to replace the use of 0
    as a priority.

    Rename the max_priority method as the loosest_op_priority method.

    Add a method named universal_priority to replace the use of
    max_priority + 1 as a priority.

    Add a function to return the priority of the comma operator,
    to allow other modules to stop hardcoding it.

    Add operations for comparing priorities and for incrementing/decrementing
    priorities.

    Change the prefix on the names of the predicates that take op_infos
    as inputs from "mercury_op_table_" to "op_infos_", since the old prefix
    was misleading.

    Add a note on an significant old problem with an exported type synonym.

library/mercury_term_parser.m:
    Conform to the changes above.

    Delete unnecessary module qualifiers, since they were just clutter.

    Add "XXX OPS" to note further opportunities for improvement.

library/pprint.m:
    Conform to the changes above.

    Rename a function to avoid ambiguity.

library/pretty_printer.m:
library/stream.string_writer.m:
library/string.to_string.m:
library/term_io.m:
    Conform to the changes above.

library/string.m:
    Add a note on an significant old problem.

NEWS:
    Announce the user-visible changes.

tests/hard_coded/bug383.m:
    Update this test case to use the new system of operator priorities.

tests/hard_coded/term_io_test.{m,inp}:
    Fix white space.

extras/old_library_modules/old_mercury_term_parser.m:
extras/old_library_modules/old_ops.m:
    The old contents of the mercury_term_parser and ops modules,
    in case anyone wants to continue using them instead of updating
    their code to use their updated equivalents.

samples/calculator2.m:
    Import the old versions of mercury_term_parser and ops.
2022-11-11 00:11:44 +11:00
Peter Wang
064e9ddf1d Add regression test for maybe_int/maybe_string options.
tests/hard_coded/.gitignore:
tests/hard_coded/Mmakefile:
tests/hard_coded/getopt_maybe_option.exp:
tests/hard_coded/getopt_maybe_option.m:
    Add test case for preceding fix for maybe_string options.
2022-11-08 17:14:25 +11:00
Zoltan Somogyi
fa14feedf0 Speed up pretty_printing a *lot*.
library/pretty_printer.m:
    Replace the indent_stack data structure with one that

    - can represent deep indentation in a compact data structure,
      as long as that indentation consists only of standard indents, and

    - can tell you how many code points the current indent stack consists of
      *without* counting the code points in all the individual indent pieces
      every time.

    This yields a big speedup, because the code used to spend a very large
    fraction of its time (between 40 and 50%) just counting the code points
    in indentation. This was on stress test data which had very deep
    indentation, but the cost would have been substantial even on moderately
    indented docs.

    Now that the indent_stack can represent more than one level of indentation
    in one data structure, print up to 30 levels of standard indentation
    with just one call to stream.put. By amortizing the overheads of stream.put
    over a must larger part of the output, this also yields a significant
    speedup.

    Move the indent_stack type and its (significantly expanded) set of
    operations to after the code that outputs docs, since it is just an
    implementation detail (though as explained above, an important detail).

tests/hard_coded/pretty_printer_stress_test.{m,data,exp}:
    A stress test for the pretty_printer whose profiling lead to the
    identification of the performance problems fixed above.

tests/hard_coded/Mmakefile:
    Enable the new test case, now that it is not too expensive to run
    on every bootcheck.
2022-08-30 13:35:54 +10:00
Zoltan Somogyi
8e6912fe30 Add a more controllable test of terms with operators. 2022-08-29 22:24:09 +10:00
Peter Wang
aaa6ac5fe1 Introduce io.system_error to io.m public interface.
Implement the error handling proposals from February 2022 on the
mercury-users list, and August 2022 on the mercury-reviews list.

We add io.system_error to the public interface of io.m
and document what its foreign representation is for each backend.

We allow io.error to optionally contain an io.system_error value,
and provide predicates to retrieve the io.system_error from an io.error.
The user may then inspect the system error via foreign code.

We also provide a predicate that takes an io.error and returns a name
for the system error it contains (if any). This makes it relatively easy
for Mercury programs to check for specific error conditions.

By returning platform-specific (actually, implementation-dependent)
error names, we are pushing the responsibility of mapping strings to
error conditions onto the application programmer. On the other hand, it
is not practical for us to map all possible system-specific error codes
to some common set of values. We could do it for a small set of common
error codes/exceptions, perhaps.

The standard library will construct io.error values containing
io.system_errors. However, we do not yet provide a facility for user
code to do the same.

library/io.m:
    Move io.system_error to the public interface.

    Change the internal representation of io.error to support containing
    a io.system_error. An io.system_error may originate from an errno
    value or a Windows system error code; the constructor distinguishes
    those cases.

    Add predicates to retrieve a system_error from io.error.

    Add predicate to return the name of the system error in an io.error.

    Replace make_err_msg with make_io_error_from_system_error.

    Replace make_maybe_win32_err_msg with
    make_io_error_from_maybe_win32_error.

    Delete ML_make_err_msg and ML_make_win32_err_msg macros.

browser/listing.m:
library/bitmap.m:
library/dir.m:
library/io.call_system.m:
library/io.environment.m:
library/io.file.m:
library/io.text_read.m:
mdbcomp/program_representation.m:
    Conform to changes.

    Leave comments for followup work.

tools/generate_errno_name:
tools/generate_windows_error_name:
    Add scripts to generate mercury_errno_name.c and
    mercury_windows_error_name.c.

runtime/Mmakefile:
runtime/mercury_errno_name.c:
runtime/mercury_errno_name.h:
runtime/mercury_windows_error_name.c:
runtime/mercury_windows_error_name.h:
    Add MR_errno_name() and MR_win32_error_name() functions,
    used by io.m to convert error codes to string names.

tests/hard_coded/null_char.exp:
    Update expected output.
2022-08-23 16:39:48 +10:00
Peter Wang
2ff6b119ef Fix some handling of ill-formed sequences in string module.
library/string.m:
    Fix string.all_match to fail if the string being tested contains
    any ill-formed code unit sequences.

    Fix the Mercury implementation of string.contains_char to continue
    searching for the character past any ill-formed code unit sequences.
    The foreign code implementations already did so.

    Fix unsafe_index_next_repl and unsafe_prev_index_repl in C grades.
    We indexed the C string with `ReplacedCodeUnit = Str[Index]' but
    since Mercury strings have the C type `char *', ReplacedCodeUnit
    could take on a negative value. When ReplacedCodeUnit == -1,
    the caller would assume there is a valid encoding of a code point
    beginning at Index, and therefore return `not_replaced' instead of
    `replaced_code_unit(255)'.

    Add casts to `unsigned char' in other places where we index C
    strings.

    Clarify documentation.

    Document that string.duplicate_char may throw an exception.

tests/hard_coded/string_all_match.m:
tests/hard_coded/string_all_match.exp:
tests/hard_coded/string_all_match.exp2:
    Add test for string.all_match.

tests/hard_coded/contains_char_2.m:
tests/hard_coded/contains_char_2.exp:
    Delete older test case for string.contains_char.

tests/hard_coded/string_contains_char.m:
tests/hard_coded/string_contains_char.exp:
tests/hard_coded/string_contains_char.exp2:
    Add better test for string.contains_char.

tests/hard_coded/Mmakefile:
    Enable the tests.

NEWS:
    Announce the changes.
2022-07-27 14:56:49 +10:00
Zoltan Somogyi
55521025bb Test type_spec pragmas for functions.
tests/hard_coded/type_spec_modes.{m,exp}:
    Add a test of type specialization pragmas for functions, since
    none of our other tests of type_spec pragmas does so.
2022-07-27 11:18:57 +10:00
Julien Fischer
ae0525af53 Add string.contains_match/2.
Add a new predicate that tests if string contains any characters that succeed
for a given test predicate.

library/string.m:
    Add the new predicate.

compiler/options.m:
    Replace the predicate string_contains_whitespace/1 defined here
    with a call to the new library predicate.

NEWS:
    Announce the new predicate.

tests/hard_coded/Mmakefile:
tests/hard_coded/string_contains_match.{m,exp}:
    Add a test of the new predicate.
2022-07-25 19:38:01 +10:00
Zoltan Somogyi
8da84c4ee5 Make test_pretty_printer's output more checkable.
tests/hard_coded/test_pretty_printer.m:
tests/hard_coded/test_pretty_printer_helper.m:
    This test checks the operation of the library's pretty_printer module

    - on several docs, and
    - with several parameters.

    With the old organization of this test case, the output consisted of

    - all docs with the first set of parameter values,
    - all docs with the next set of parameter values,
    and so on.

    This diff reorders the output so that it now consists of

    - the first doc with all sets of parameter values,
    - the next doc with all sets of parameter values,
    and so on.

    This makes it much easier to check the output.

    The new approach also

    - gives a name to each doc,
    - prints the doc using the largest parameter values (longest lines,
      most lines allowed, etc) first, which allows all the later outputs
      for the same doc with smaller parameters values to be checked against
      it more easily.

    This diff also moves the code that serves as the infrastructure
    of the test case to the new module test_pretty_printer_helper.m,
    leaving test_pretty_printer.m to contain just the code that
    constructs the docs whose prettyprinting is to be tested. (Previously,
    these two classes of code were interleaved in the source code.)

tests/hard_coded/test_pretty_printer.exp:
    Expect the output in the updated order.
2022-07-25 07:13:17 +10:00
Zoltan Somogyi
3d47ded492 Improve programming style. 2022-07-25 06:48:34 +10:00
Zoltan Somogyi
9473b939d7 Add more functionality to ra_list.m.
The changes to cord.m and list.m are to reduce unnecessary differences
between cord.m, list.m, and ra_list.m.

library/ra_list.m:
    Add new procedures singleton, is_empty, is_not_empty, is_singleton,
    length, list_to_ra_list, map, foldl and foldr.

    Make ra_list_to_list operate without unnecessary memory allocations.

library/cord.m:
    Add a semidet predicate head as a synonym for get_first.

library/list.m:
    Add a semidet predicate is_singleton.

    Add semidet predicates head and tail next to their semidet function
    versions (which should be deprecated). Document them.

    Add det predicates det_head and det_tail next to their det function
    versions.

    Avoid the overhead of calling a closure used for func-to-pred conversion
    once for each list element in the function versions of foldl and foldr.

    Fix some other documentation.

NEWS:
    Mention the new additions to standard library.

library/term_to_xml.m:
tests/hard_coded/construct_packed.m:
    Avoid ambiguities between function and predicate forms.

tests/hard_coded/ra_list_test.{m,exp}:
    Add tests of length, list_to_ra_list, map, foldl and foldr.
2022-06-12 19:42:47 +10:00
Zoltan Somogyi
3459266a7a Carve stdlib module ra_list.m out of bt_array.m.
library/ra_list.m:
    As above.

    Rename ra_list_lookup to ra_list_search, since it fails if the item
    to be accessed does not exist. Add a version, ra_list_lookup, which
    aborts in that case.

    Add predicates to append two ra_lists, and to convert ra_lists
    to just plain lists.

    Change the argument order of some predicates to be more
    state variable friendly.

    Add some documentation.

library/bt_array.m:
    Delete the moved code, and update the references to the changed predicates.

library/Mercury.options:
    Now that the ra_list predicates have been moved to their own module,
    stop giving bt_array.m a free pass on inconsistent predicate order.

library/MODULES_DOC:
library/library.m:
    Add ra_list.m as a documented module of the Mercury standard library.

library/array.m:
    Delete a predicate that served as a test for an ancient bug fix.

NEWS:
    Announce both the new ra_list module, and the deletion of the predicate
    from array.m.

tests/hard_coded/array_sort.m:
    Don't call the predicate deleted from array.m.

tests/hard_coded/ra_list_test.{m,exp}:
    A new test case to test operations on ra_lists.

tests/hard_coded/Mmakefile:
    Enable the new test case.
2022-06-11 11:11:30 +10:00
Julien Fischer
5f5c67b281 Add more operations on cords.
library/cord.m:
    Add is_singleton/2, foldr2/6 and foldr3/8.

NEWS:
     Announce the above additions.

tests/hard_coded/Mmakefile:
tests/hard_coded/test_cord3.{m,exp}:
     Add a test of is_singleton/2 (and is_empty/1).
2022-06-07 16:37:45 +10:00
Peter Wang
8d27074213 Fix variable name.
tests/hard_coded/thread_commit.m:
    As above.
2022-05-26 16:55:03 +10:00
Peter Wang
1b7e5adecf Don't use __builtin_setjmp, __builtin_longjmp on AArch64 with older GCCs.
There is an issue on Linux/aarch64 with older versions of gcc where a
commit performed on a thread using the gcc __builtin_longjmp function
causes an assertion failure in pthread_create() if the Mercury runtime
is dynamically linked:

    pthread_create.c:430: start_thread: Assertion `freesize < pd->stackblock_size' failed.

or a crash if the Mercury runtime is statically linked:

    *** Mercury runtime: caught segmentation violation ***
    cause: address not mapped to object

The gcc versions tested are:

    BAD - gcc version 6.3.0 20170516 (Debian 6.3.0-18) from Debian 9
    BAD - gcc version 8.3.0 (Debian 8.3.0-2) from Debian 10
    OK  - gcc version 10.2.1 20210110 (Debian 10.2.1-6) from Debian 11

on Debian with glibc.

runtime/mercury.h:
    Define MR_USE_GCC_BUILTIN_SETJMP_LONGJMP if we decide to use gcc's
    __builtin_setjmp and __builtin_longjmp functions instead of the
    standard functions.

    Use standard setjmp/longjmp on aarch64 if gcc version is < 10.

    Update the comment for MR_builtin_jmp_buf to refer to the GCC manual
    instead of the GCC source code. Use an array of 'intptr_t's instead
    of 'void *'s, to match the manual.

tests/hard_coded/Mmakefile
tests/hard_coded/thread_commit.m:
tests/hard_coded/thread_commit.exp:
tests/hard_coded/thread_commit.exp2:
    Add test case.
2022-05-26 12:47:21 +10:00
Zoltan Somogyi
b96a90f948 Parse disjunctions using tail recursive code ...
to allow even very long disjunctions to be parsed in constant stack space.
This fixes Mantis bug #559.

compiler/prog_item.m:
    We used to represent a disjunction like ( GoalA ; GoalB ; GoalC ; GoalD )
    in the parse tree as

        disj_expr(ContextA, GoalA,
            disj_expr(ContextB, GoalB,
                disj_expr(ContextC, GoalC,
                    GoalD)))

    To enable the changes in parse_goal.m and parse_dcg_goal.m, switch over
    to representing them as

        disj_expr(ContextA, GoalA, GoalB, [GoalC, GoalD])

    The type of this term enforces the invariant that a disjunction
    must have at least two disjuncts.

    The fact that this throws away ContextB and ContextC is not a problem;
    they were never used, being thrown away when converting the parse tree
    to the HLDS.

compiler/parse_goal.m:
compiler/parse_dcg_goal.m:
    After seeing the first comma in the above disjunction, these parsers
    used to (1) parse its left operand, GoalA, (2) parse its right operand,
    ( GoalB ; GoalC ; GoalD), and then (3) check for errors. This code
    was prevented from being tail recursive both by the presence of step 3,
    and the fact that step 2 indirectly invokes another predicate that
    (until my previous change to parse_goal.m) had a different determinism.

    Fix the first issue by having the new predicate parse_goal_disjunction,
    and its DCG variant, accumulate errors *alongside* disjuncts,
    to be checked just once, at the end, outside the loop. Fix the second
    issue by having parse_goal_disjunction test whether the right operand
    of the semicolon has the form of a disjunction, and if it does,
    recursing on it directly. This makes parse_goal_disjunction
    self-tail-recursive, which should allow it to process disjunctions
    of arbtrary length using fixed stack space (in grades that support
    tail recursion, that is).

    Move the code to flatten disjunctions from goal_expr_to_goal.m to
    these modules, because it is simpler to get the right result this way
    in DCG clauses (for non-DCG clauses, it works simply either way).

compiler/goal_expr_to_goal.m:
    Convert the updated parse tree representation of disjunctions to HLDS,
    and don't flatten disjunctions here anymore.

compiler/parse_item.m:
    Add some infrastructure for debugging changes like this.

compiler/add_clause.m:
    Improve the infrastructure of debugging changes like this, by making it
    more selective.

    To make this possible, pass the predicate name to a predicate
    that did not need it before. Fix the argument order of that predicate.

compiler/make_hlds_warn.m:
    Don't flatten parse tree disjunctions, since the code constructing them
    has done it already.

compiler/get_dependencies.m:
compiler/module_qual.collect_mq_info.m:
compiler/parse_tree_out_clause.m:
compiler/prog_item_stats.m:
compiler/prog_util.m:
    Conform to the change in prog_item.m.

compiler/instance_method_clauses.m:
    Conform to the change in add_clause.m.

tests/hard_coded/flatten_disjunctions.{m,exp}:
    A new test case both testing and documenting the need for flattening
    disjunctions.

tests/hard_coded/Mmakefile:
    Enable the new test case.

tests/invalid/require_switch_arms_detism.err_exp:
    Expect updated numbers for anonymous variables. This is due to
    goal_expr_to_goal.m now processing disjuncts in a different order
    than before.
2022-05-06 15:19:49 +10:00
Julien Fischer
fd088a4f69 Avoid the failure of hard_coded/parse.
The test hard_coded/parse tests two separate things:

1. for the presence of a bug, reported in Mercury 0.9, that occurred when a
   local module name shadowed that of a browser module.

2. static linking.

Specifically, the latter tests whether statically linking against the system
libraries works. As most operating systems these days do not ship with static
versions of their system libraries installed by default, this test fails more
often than not. (Indeed, it will never pass on macOS since executables are
required to use the libSystem dylib.)

Delete the static linking aspect of this test, since it is failing almost
everywhere anyway.

tests/hard_coded/Mercury.options:
    Do not link the parse test case with --static.
2022-05-04 20:58:04 +10:00
Zoltan Somogyi
3cae28f921 Improve programming style. 2022-05-04 09:14:47 +10:00