Files
mercury/tests/hard_coded/field_syntax.m
Zoltan Somogyi 4292e6fd94 Fix an old bug: don't insist that two parts of the same field access
Estimated hours taken: 0.5
Branches: main

compiler/prog_io.m:
	Fix an old bug: don't insist that two parts of the same field access
	goal have the same context.

	Always get the context for field access goals from the ^ character.

tests/hard_coded/field_access.m:
	Modify this old test case by giving different parts of a field access
	different contexts. The old version of the compiler mistakenly rejects
	it, but the fixed version accepts it.
2008-12-03 05:06:07 +00:00

70 lines
2.2 KiB
Mathematica

%-----------------------------------------------------------------------------%
% field_syntax.m
% Ralph Becket <rafe@cs.mu.oz.au>
% Tue Dec 17 14:13:23 EST 2002
% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
%
%-----------------------------------------------------------------------------%
:- module field_syntax.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module array, int, list, string.
%-----------------------------------------------------------------------------%
:- func array(T) ^ elem_from_end(int) = T.
:- mode array_ui ^ elem_from_end(in) = out is det.
A ^ elem_from_end(I) = A ^ elem(max(A) - I).
%-----------------------------------------------------------------------------%
:- func (array(T) ^ elem_from_end(int) := T) = array(T).
:- mode (array_di ^ elem_from_end(in) := in) = array_uo is det.
% This formatting gives each part of the term a different context.
% This is a regression test for a bug in which the code that recognized
% field accesses insisted on the same context for parts of the term.
(A
^
elem_from_end(I)
:=
X)
=
(A
^
elem(max(A) - I)
:=
X).
%-----------------------------------------------------------------------------%
main(!IO) :-
A = array([1, 2, 3]),
io.format("A = ", [], !IO),
io.print(A, !IO),
io.nl(!IO),
io.format("A ^ elem(0) = %d\n",
[i(A ^ elem(0))], !IO),
io.format("A ^ elem_from_end(0) = %d\n",
[i(A ^ elem_from_end(0))], !IO),
io.format("(A ^ elem_from_end(2) := 4) = ", [], !IO),
io.print(A ^ elem_from_end(2) := 4, !IO),
io.nl(!IO).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%