[m-rev.] fix lcc type errors in hlc grades

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Aug 18 00:04:19 AEST 2003


Estimated hours taken: 2
Branches: main

Avoid the use of abstract equivalence types or abstract foreign types in
a couple of places in the standard library where they cause type errors
(for lcc) or warnings (for gcc) in --high-level-code grades for some
test cases. 

library/random.m:
	Use a no-tag type rather than an abstract equivalence type.

library/time.m:
	Fix an old XXX about representing time_t as a int: use a pragma
	foreign_type instead.  Then wrap this in a no-tag type,
	to avoid making time.time_t an abstract foreign_type.

Workspace: /home/ceres/fjh/mercury
Index: library/random.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/random.m,v
retrieving revision 1.21
diff -u -d -r1.21 random.m
--- library/random.m	26 Sep 2002 06:11:28 -0000	1.21
+++ library/random.m	17 Aug 2003 13:04:20 -0000
@@ -115,17 +115,17 @@
 :- implementation.
 :- import_module int, array.
 
-:- type random__supply		==	int.	% I(j)
+:- type random__supply ---> rs(int). % I(j)
 
 :- pred random__params(int, int, int).	% a, c, m
 :- mode random__params(out, out, out) is det.
 
 random__params(9301, 49297, 233280).
 
-random__init(I0, RS) :-
+random__init(I0, rs(RS)) :-
 	copy(I0, RS).
 
-random__random(I, RS0, RS) :-
+random__random(I, rs(RS0), rs(RS)) :-
 	RS0 = I0,
 	random__params(A, C, M),
 	I = ((I0 * A) + C) mod M,
Index: library/time.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/time.m,v
retrieving revision 1.34
diff -u -d -r1.34 time.m
--- library/time.m	29 May 2003 12:08:26 -0000	1.34
+++ library/time.m	17 Aug 2003 13:48:08 -0000
@@ -178,10 +178,6 @@
 
 :- import_module int, exception.
 
-% XXX The assumption that a C `time_t' can fit into a Mercury `MR_Integer'
-% is not very portable.
-:- type time_t == int.
-
 :- pragma foreign_decl("C",
 "
 	#include <time.h>
@@ -200,6 +196,21 @@
 	#include ""mercury_string.h"" /* for MR_make_aligned_string_copy() */
 ").
 
+% We use a no-tag wrapper type for time_t, rather than defining it as an
+% equivalence type or just using a d.u./pragma foreign_type directly,
+% to avoid the following problems:
+%
+%	- type errors in --high-level-code grades, due to the caller seeing
+%	  the abstract type, but the callee seeing the equivalence type
+%	  definition or the foreign_type definition.
+%
+%	- users can't define instance declarations for abstract equiv. types.
+%
+:- type time_t ---> time_t(time_t_rep).
+
+:- type time_t_rep ---> time_t_rep(c_pointer).
+:- pragma foreign_type("C", time_t_rep, "time_t").
+
 %-----------------------------------------------------------------------------%
 
 %:- pred time__clock(clock_t, io__state, io__state).
@@ -311,49 +322,62 @@
 
 time__time(Result, IO0, IO) :-
 	time__c_time(Ret, IO0, IO),
-	( Ret = -1 ->
+	( time__time_t_is_invalid(Ret) ->
 		throw(time_error("can't get time value"))
 	;
-		Result = Ret
+		Result = time_t(Ret)
 	).
 
-:- pred time__c_time(int, io__state, io__state).
+:- pred time__c_time(time_t_rep, io__state, io__state).
 :- mode time__c_time(out, di, uo) is det.
 
 :- pragma foreign_proc("C",
 	time__c_time(Ret::out, IO0::di, IO::uo),
 	[will_not_call_mercury, promise_pure, tabled_for_io],
 "{
-	Ret = (MR_Integer) time(NULL);
+	Ret = time(NULL);
 	MR_update_io(IO0, IO);
 }").
 
+:- pred time__time_t_is_invalid(time_t_rep).
+:- mode time__time_t_is_invalid(in) is semidet.
+
+:- pragma foreign_proc("C",
+	time__time_t_is_invalid(Val::in),
+	[will_not_call_mercury, promise_pure],
+"{
+	SUCCESS_INDICATOR = (Val == -1);
+}").
+
+
+
 %-----------------------------------------------------------------------------%
 
 %:- func time__difftime(time_t, time_t) = float.
 
-time__difftime(T1, T0) = Diff :-
+time__difftime(time_t(T1), time_t(T0)) = Diff :-
 	time__c_difftime(T1, T0, Diff).
 
-:- pred time__c_difftime(int, int, float).
+:- pred time__c_difftime(time_t_rep, time_t_rep, float).
 :- mode time__c_difftime(in, in, out) is det.
 
 :- pragma foreign_proc("C",
 	time__c_difftime(T1::in, T0::in, Diff::out),
 	[will_not_call_mercury, promise_pure],
 "{
-	Diff = (MR_Float) difftime((time_t) T1, (time_t) T0);
+	Diff = (MR_Float) difftime(T1, T0);
 }").
 
 %-----------------------------------------------------------------------------%
 
 %:- func time__localtime(time_t) = tm.
 
-time__localtime(Time) = TM :-
+time__localtime(time_t(Time)) = TM :-
 	time__c_localtime(Time, Yr, Mnt, MD, Hrs, Min, Sec, YD, WD, N),
 	TM = tm(Yr, Mnt, MD, Hrs, Min, Sec, YD, WD, int_to_maybe_dst(N)).
 
-:- pred time__c_localtime(int, int, int, int, int, int, int, int, int, int).
+:- pred time__c_localtime(time_t_rep, int, int, int, int, int, int,
+		int, int, int).
 :- mode time__c_localtime(in, out, out, out, out, out, out,
 		out, out, out) is det.
 
@@ -385,11 +409,12 @@
 
 %:- func time__gmtime(time_t) = tm.
 
-time__gmtime(Time) = TM :-
+time__gmtime(time_t(Time)) = TM :-
 	time__c_gmtime(Time, Yr, Mnt, MD, Hrs, Min, Sec, YD, WD, N),
 	TM = tm(Yr, Mnt, MD, Hrs, Min, Sec, YD, WD, int_to_maybe_dst(N)).
 
-:- pred time__c_gmtime(int, int, int, int, int, int, int, int, int, int).
+:- pred time__c_gmtime(time_t_rep, int, int, int, int, int,
+			int, int, int, int).
 :- mode time__c_gmtime(in, out, out, out, out, out,
 			out, out, out, out) is det.
 
@@ -433,12 +458,12 @@
 
 %:- func time__mktime(tm) = time_t.
 
-time__mktime(TM) = Time :-
+time__mktime(TM) = time_t(Time) :-
 	TM = tm(Yr, Mnt, MD, Hrs, Min, Sec, YD, WD, DST),
 	time__c_mktime(Yr, Mnt, MD, Hrs, Min, Sec, YD, WD,
 		maybe_dst_to_int(DST), Time).
 
-:- pred time__c_mktime(int, int, int, int, int, int, int, int, int, int).
+:- pred time__c_mktime(int, int, int, int, int, int, int, int, int, time_t_rep).
 :- mode time__c_mktime(in, in, in, in, in, in, in, in, in, out) is det.
 
 :- pragma foreign_proc("C",
@@ -458,7 +483,7 @@
 	t.tm_yday = YD;
 	t.tm_isdst = N;
 
-	Time = (MR_Integer) mktime(&t);
+	Time = mktime(&t);
 }").
 
 :- func maybe_dst_to_int(maybe(dst)) = int.
@@ -511,10 +536,10 @@
 
 %:- func time__ctime(time_t) = string.
 
-time__ctime(Time) = Str :-
+time__ctime(time_t(Time)) = Str :-
 	time__c_ctime(Time, Str).
 
-:- pred time__c_ctime(int, string).
+:- pred time__c_ctime(time_t_rep, string).
 :- mode time__c_ctime(in, out) is det.
 
 :- pragma foreign_proc("C",

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list