Files
mercury/tests/hard_coded/construct_test_exist.m
Simon Taylor 6de3b102ba Add support for deconstructing by functor number rather than name,
Estimated hours taken: 20
Branches: main

Add support for deconstructing by functor number rather than name,
for use by write_binary.

library/deconstruct.m:
runtime/mercury_deconstruct.h:
runtime/mercury_deconstruct.c:
runtime/mercury_ml_expand_body.h:
runtime/mercury_ml_deconstruct_body.h:
	Add predicates deconstruct.functor_number and
	deconstruct.deconstruct.du, which returns a functor number
	suitable for use by construct.construct rather than a functor
	name.

library/construct.m:
library/term.m:
browser/term_rep.m:
extras/quickcheck/qcheck.m:
tests/valid/agc_unbound_typevars.m:
tests/valid/agc_unbound_typevars2.m:
	Add a function get_functor_lex, which returns the lexicographic
	functor number given an ordinal functor number.

	Add equivalence types to make it clearer which ordering is
	being used by which functor numbers.

	Remove a C-ism: num_functors now fails rather than returning -1
	for types without functors.

NEWS:
	Document the new predicates and functions.

runtime/mercury_type_info.h:
runtime/mercury_builtin_types.c:
runtime/mercury_mcpp.h:
compiler/rtti.m:
compiler/rtti_out.m:
compiler/type_ctor_info.m:
compiler/rtti_to_mlds.m:
compiler/opt_debug.m:
	Add a field to MR_TypeCtorInfo which contains a mapping from
	an ordinal functor number to a lexicographic functor number
	which can be passed to construct.construct.

	Bump MR_RTTI_VERSION.

tests/hard_coded/expand.m:
tests/hard_coded/expand.exp:
tests/hard_coded/expand.exp2:
tests/hard_coded/construct_test.m:
tests/hard_coded/construct_test.exp:
tests/hard_coded/construct_test_exist.m:
tests/hard_coded/construct_test_exist.exp:
	Test cases.
2007-01-05 02:19:46 +00:00

143 lines
3.2 KiB
Mathematica

% Test case for get_functor on a functor with existentially typed arguments.
%
% Author: zs
:- module construct_test_exist.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module list, int, maybe, pair, term, map, string, require.
:- import_module type_desc, construct.
:- typeclass tc1(V) where [
func m1(V) = int
].
:- type t1
---> f11
; f12(int)
; some [T] f13(int, T, list(T))
; some [T, U] f14(T, list(U))
; some [T, U] f15(T, list(U), U) => tc1(T).
:- type t2
---> f21
; some [T] f22(
f21name :: int,
f22name :: T,
f23name :: list(T),
f24name :: T,
f25name :: float
).
:- type t3(T, U)
---> f31(T, U)
; some [V, W] f32(
f31name :: int,
f32name :: pair(T, V),
f33name :: map(T, pair(U, pair(V, W))),
f34name :: U
).
%----------------------------------------------------------------------------%
main(!IO) :-
test_all(f11, !IO),
test_all(f21, !IO),
test_all(f31(3, "three"), !IO),
test_all(f31([3], 3.0), !IO).
:- pred test_all(T::in, io::di, io::uo) is det.
test_all(T, !IO) :-
TypeInfo = type_desc__type_of(T),
( N = construct__num_functors(TypeInfo) ->
io__write_int(N, !IO),
io__write_string(" functors in this type", !IO),
io__nl(!IO),
test_all_functors(TypeInfo, N, !IO),
io__nl(!IO)
;
io__write_string("no functors in this type\n", !IO)
).
:- pred test_all_functors(type_desc__type_desc::in, int::in, io::di, io::uo)
is det.
test_all_functors(TypeInfo, N, !IO) :-
( N =< 0 ->
true
;
test_nth_functor(TypeInfo, N - 1, !IO),
test_all_functors(TypeInfo, N - 1, !IO)
).
:- pred test_nth_functor(type_desc__type_desc::in, int::in, io::di, io::uo)
is det.
test_nth_functor(TypeInfo, N, !IO) :-
io__write_int(N, !IO),
(
construct__get_functor_with_names(TypeInfo, N, Name, Arity,
ArgTypes, Names)
->
io__write_string(" - ", !IO),
io__write_string(Name, !IO),
io__write_string("/", !IO),
io__write_int(Arity, !IO),
io__write_string(" [", !IO),
io__write_list(ArgTypes, ", ", print_arg_type, !IO),
io__write_string("] ", !IO),
io__write_string(" [", !IO),
io__write_list(Names, ", ", print_maybe_name, !IO),
io__write_string("]\n", !IO)
;
io__write_string(" failed ", !IO),
io__nl(!IO)
).
:- pred print_arg_type(type_desc__pseudo_type_desc::in, io::di, io::uo)
is det.
print_arg_type(PseudoTypeDesc, !IO) :-
PseudoTypeRep = pseudo_type_desc_to_rep(PseudoTypeDesc),
(
PseudoTypeRep = bound(TypeCtorDesc, ArgPseudoTypeInfos),
io__write_string(type_desc__type_ctor_name(TypeCtorDesc), !IO),
(
ArgPseudoTypeInfos = []
;
ArgPseudoTypeInfos = [_ | _],
io__write_string("(", !IO),
io__write_list(ArgPseudoTypeInfos, ", ",
print_arg_type, !IO),
io__write_string(")", !IO)
)
;
PseudoTypeRep = univ_tvar(TypeVarNum),
io__write_string("U", !IO),
io__write_int(TypeVarNum, !IO)
;
PseudoTypeRep = exist_tvar(TypeVarNum),
io__write_string("E", !IO),
io__write_int(TypeVarNum, !IO)
).
:- pred print_maybe_name(maybe(string)::in, io::di, io::uo) is det.
print_maybe_name(MaybeName, !IO) :-
(
MaybeName = yes(FieldName),
io__write_string(FieldName, !IO)
;
MaybeName = no,
io__write_string("_", !IO)
).
%----------------------------------------------------------------------------%