mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-24 05:43:53 +00:00
Estimated hours taken: 30
Implement builtin tuple types, similar to those in Haskell.
Tuples are constructed and deconstructed using
the syntax X = {Arg1, Arg2, ...}.
Tuples have type `{Arg1, Arg2, ...}'.
Unary tuples (X = {Arg}) do work, unlike in Haskell. The rationale
for this is that it is useful to be able to construct unary tuples
to be passed to a polymorphic predicate which uses std_util__deconstruct
to deal with a tuple of any arity. Since this is probably the only
use for unary tuples, it's not really worth the effort of treating
them as no_tag types, so we don't.
The type-infos for tuples have the same structure as for higher-order
types. There is a single type_ctor_info for tuples, and the arity
is placed before the argument type_infos.
library/parser.m:
Change the way '{}/N' terms are parsed, so that the parsed
representation is consistent with the way other functors
are represented (previously the arguments were left as
unparsed ','/2 terms). This avoids special case code
in prog_io__parse_qualified_term, term__term_to_type
and term__type_to_term.
compiler/prog_io_dcg.m:
compiler/prog_io_util.m:
Handle the new structure of '{}/N' terms when parsing DCG escapes
by converting the argument list back into a single ','/2 term.
compiler/module_qual.m:
Treat tuples as a builtin type.
compiler/typecheck.m:
Typecheck tuple constructors.
compiler/mode_util.m:
Propagate types into tuple bound insts.
compiler/type_util.m:
Add type_is_tuple/2 and type_id_is_tuple/1 to identify tuple types.
Add tuples to the list of types which are not atomic types.
Handle tuple types in `type_constructors' and
`get_cons_id_arg_types' and `switch_type_num_functors'.
compiler/tabling.m:
Handle tabling of tuples.
compiler/term_util.m:
Handle tuples in the code to compute functor norms.
compiler/magic_util.m:
compiler/rl.m:
compiler/rl_key.m:
Handle tuple types in the Aditi back end.
compiler/mercury_to_mercury.m:
library/io.m:
library/term_io.m:
Handle output of '{}/N' terms.
compiler/higher_order.m:
compiler/simplify.m:
Don't specialize complicated unifications of tuple
types into calls to a specific unification procedure --
even if the procedure were implemented, it probably
wouldn't be that much more efficient.
compiler/unify_proc.m:
Generate unification procedures for complicated unifications
of tuples (other than in-in unifications). These are generated
lazily as required.
compiler/make_hlds.m:
Export add_special_pred for use by unify_proc.m.
compiler/polymorphism.m:
Export polymorphism__process_pred for use by unify_proc.m.
compiler/bytecode_gen.m:
compiler/code_util.m:
compiler/ml_code_util.m:
Handle unify procedure names and tags for tuple types.
compiler/mlds_to_c.m:
Output tuple types as MR_Tuple.
compiler/ml_unify_gen.m:
Compute the field types for tuples.
compiler/polymorphism.m:
compiler/pseudo_type_info.m:
Treat tuple type_infos in a similar way to higher-order type_infos.
compiler/hlds_data.m:
Document how cons_ids for tuple types are represented.
compiler/switch_gen.m:
compiler/table_gen.m:
Add tuple types to switches on type_util__builtin_type.
compiler/llds_out.m:
util/mdemangle.c:
profiler/demangle.m:
Transform items named "{}" to "f_tuple" when mangling symbols.
library/builtin.m:
Define the type_ctor_info used for tuples.
library/private_builtin.m:
Add `builtin_unify_tuple/2' and `builtin_compare_tuple/3',
both of which abort. All comparisons and in-in unifications
of tuples are performed by the generic unification functions
in runtime/mercury_ho_call.c and runtime/mercury.c.
library/std_util.m:
Implement the various RTTI functions for tuples.
Encode tuple `TypeCtorDesc's in a similar way to that
used for higher-order types. This has the consequence that the limit
on the arity of higher-order types is now MAX_VIRTUAL_REG,
rather than 2*MAX_VIRTUAL_REG.
Avoid calling MR_GC_free for the type-info vector returned
from ML_expand() for tuples because unlike the vectors
for du types, it is not copied.
runtime/mercury_type_info.h:
Add macros for extracting fields from tuple type-infos.
These just call the macros for extracting fields from higher-order
type-infos.
Add a macro MR_type_ctor_rep_is_variable_arity(), which
returns TRUE for tuples and higher-order types.
The distinction between higher-order and first-order types
is now misnamed -- the distinction is really between fixed arity
types and builtin variable arity types. I'm not sure whether
it's worth renaming everything.
runtime/mercury.h:
runtime/mercury.c:
Define unification and comparison of tuples in
high-level code grades.
runtime/mercury_deep_copy_body.h:
runtime/mercury_make_type_info_body.h:
runtime/mercury_tabling.c:
runtime/mercury_unify_compare_body.h:
Handle tuple types in code which traverses data using RTTI.
tests/hard_coded/construct.{m,exp}:
tests/hard_coded/expand.{m,exp}:
Test RTTI functions from std_util.m applied to tuples.
tests/hard_coded/tuple_test.{m,exp}:
Test unification, comparison, term_to_type etc. applied to tuples.
tests/hard_coded/deep_copy.{m,exp}:
Test deep copy of tuples.
tests/hard_coded/typeclasses/tuple_instance.{m,exp}:
Test instance declarations for tuples.
tests/tabling/expand_tuple.{m,exp}:
Test tabling of tuples.
tests/hard_coded/write.m:
Add some module qualifications for code which uses
`{}/1' constructors which are not tuples.
tests/invalid/errors2.{m,err_exp,err_exp2}:
Test handling of tuples in type errors messages.
NEWS:
doc/reference_manual.texi:
w3/news/newsdb.inc:
Document tuples.
doc/transition_guide.texi:
Document the change to the parsing of '{}/N' terms.
124 lines
3.2 KiB
Mathematica
124 lines
3.2 KiB
Mathematica
:- module tuple_test.
|
|
|
|
:- interface.
|
|
|
|
:- import_module io.
|
|
|
|
:- pred main(io__state::di, io__state::uo) is det.
|
|
|
|
:- implementation.
|
|
|
|
:- import_module bool, list, char, string, int.
|
|
:- import_module term, std_util, term_io, varset.
|
|
|
|
main -->
|
|
io__write_string("testing io__write:\n"),
|
|
{ Tuple = {'a', {'b', 1, [1,2,3], {}, {1}}, "string"} },
|
|
io__write(Tuple),
|
|
io__nl,
|
|
io__write({}('a', {'b', 1, [1,2,3], {}, {1}}, "string")),
|
|
io__nl,
|
|
|
|
io__write_string("testing type_to_term:\n"),
|
|
{ type_to_term(Tuple, TupleTerm) },
|
|
{ is_generic_term(TupleTerm) },
|
|
{ varset__init(VarSet) },
|
|
term_io__write_term(VarSet, TupleTerm),
|
|
io__nl,
|
|
|
|
io__write_string("testing term_to_type:\n"),
|
|
(
|
|
{ has_type(NewTuple, type_of(Tuple)) },
|
|
{ term_to_type(TupleTerm, NewTuple) }
|
|
->
|
|
io__write_string("term_to_type succeeded\n"),
|
|
io__write(NewTuple),
|
|
io__nl
|
|
;
|
|
io__write_string("term_to_type failed\n")
|
|
),
|
|
|
|
% Test in-in unification of tuples.
|
|
io__write_string("testing unification:\n"),
|
|
( { unify_tuple({1, 'a', 1, "string"}, {1, 'a', 1, "string"}) } ->
|
|
io__write_string("unify test 1 succeeded\n")
|
|
;
|
|
io__write_string("unify test 1 failed\n")
|
|
),
|
|
( { unify_tuple({2, 'b', 1, "foo"}, {2, 'b', 1, "bar"}) } ->
|
|
io__write_string("unify test 2 failed\n")
|
|
;
|
|
io__write_string("unify test 2 succeeded\n")
|
|
),
|
|
|
|
% Test comparison of tuples.
|
|
io__write_string("testing comparison:\n"),
|
|
{ compare(Res1, {1, 'a', 1, "string"}, {1, 'a', 1, "string"}) },
|
|
( { Res1 = (=) } ->
|
|
io__write_string("comparison test 1 succeeded\n")
|
|
;
|
|
io__write_string("comparison test 1 failed\n")
|
|
),
|
|
|
|
{ compare(Res2, {2, 'b', 1, "foo"}, {2, 'b', 1, "bar"}) },
|
|
( { Res2 = (>) } ->
|
|
io__write_string("comparison test 2 succeeded\n")
|
|
;
|
|
io__write_string("comparison test 2 failed\n")
|
|
),
|
|
|
|
io__write_string("testing tuple switches:\n"),
|
|
{ solutions(tuple_switch_test({1, 2}), Solns1) },
|
|
io__write(Solns1),
|
|
io__nl,
|
|
|
|
{ tuple_switch_test_2({no, 3, 4}, Int) },
|
|
io__write_int(Int),
|
|
io__nl,
|
|
|
|
%
|
|
% These tests should generate an out-of-line unification
|
|
% predicate for the unification with the output argument.
|
|
%
|
|
io__write_string("testing complicated unification\n"),
|
|
( { choose(yes, {1, "b", 'c'}, {4, "e", 'f'}, {1, _, _}) } ->
|
|
io__write_string("complicated unification test 1 succeeded\n")
|
|
;
|
|
io__write_string("complicated unification test 1 failed\n")
|
|
),
|
|
( { choose(yes, {5, "b", 'c'}, {9, "e", 'f'}, {1, _, _}) } ->
|
|
io__write_string("complicated unification test 2 failed\n")
|
|
;
|
|
io__write_string("complicated unification test 2 succeeded\n")
|
|
).
|
|
|
|
:- pred is_generic_term(term::unused).
|
|
|
|
is_generic_term(_).
|
|
|
|
:- type foo(A, B, C) == {int, A, B, C}.
|
|
|
|
:- pred unify_tuple(foo(A, B, C)::in,
|
|
{int, A, B, C}::in(bound({ground, ground, ground, ground}))) is semidet.
|
|
:- pragma no_inline(unify_tuple/2).
|
|
|
|
unify_tuple(X, X).
|
|
|
|
:- pred choose(bool::in, T::in, T::in, T::out) is det.
|
|
:- pragma no_inline(choose/4).
|
|
|
|
choose(yes, A, _, A).
|
|
choose(no, _, B, B).
|
|
|
|
:- pred tuple_switch_test({int, int}::in, int::out) is multi.
|
|
:- pragma no_inline(tuple_switch_test/2).
|
|
|
|
tuple_switch_test({A, _}, A).
|
|
tuple_switch_test({_, B}, B).
|
|
|
|
:- pred tuple_switch_test_2({bool, int, int}::in, int::out) is det.
|
|
:- pragma no_inline(tuple_switch_test_2/2).
|
|
|
|
tuple_switch_test_2({yes, A, _}, A).
|
|
tuple_switch_test_2({no, _, B}, B).
|