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

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Aug 18 01:08:09 AEST 2003


On 18-Aug-2003, Simon Taylor <stayl at cs.mu.OZ.AU> wrote:
> On 18-Aug-2003, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> > +:- 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").
> 
> You should implement equality and comparison for time_t_rep.

Good point. 

I tried the following patch, which defines just the comparison predicate,
but it triggered a bug in the compiler:

	Uncaught Mercury exception:
	Software Error: type error in pred call: no matching pred

Simon, would you mind having a look at this one, please?

According to the reference manual, this should be fine, at least for
discriminated union types; if the comparison predicate is specified,
and the equality predicate is not, the compiler will synthesize the
equality predicate in terms of the comparison predicate.
The reference manual is not quite so clear about foreign types.
It says

 |    As with discriminated union types, programmers can specify the
 | unification and comparison predicates to use for values of the type
 | using the following syntax (*note User-defined equality and
 | comparison::):
 | 
 |      :- pragma foreign_type(LANG, MERCURYTYPENAME, FOREIGNTYPEDESCRIPTOR)
 |              where equality is EQUALITYPRED, comparison is COMPAREPRED.

but the phrase "as with discriminated union types" certainly suggests to me
that you should be able to specify just one of the two rather than both.
If that is indeed the case, I suggest changing the wording here to say
"unification and/or comparison" rather than "unification and comparison".

--- time.m.old	Mon Aug 18 00:50:45 2003
+++ time.m	Mon Aug 18 00:50:26 2003
@@ -209,7 +209,13 @@
 :- 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").
+:- pragma foreign_type("C", time_t_rep, "time_t")
+	where comparison is compare_time_t_reps.
+
+:- pred compare_time_t_reps(comparison_result::uo, time_t::in, time_t::in)
+	is det.
+compare_time_t_reps(Result, X, Y) :-
+	compare(Result, difftime(X, Y), 0.0).
 
 %-----------------------------------------------------------------------------%
 
So I guess until that bug is fixed (and installed everywhere,
to avoid bootstrapping issues) I'll have to stick with defining
both of them:

--- time.m.old	Mon Aug 18 00:50:45 2003
+++ time.m	Mon Aug 18 00:59:07 2003
@@ -209,7 +209,17 @@
 :- 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").
+:- pragma foreign_type("C", time_t_rep, "time_t")
+	where equality is unify_time_t_reps, comparison is compare_time_t_reps.
+
+:- pred unify_time_t_reps(time_t::in, time_t::in) is semidet.
+unify_time_t_reps(X, Y) :-
+	compare_time_t_reps((=), X, Y).
+
+:- pred compare_time_t_reps(comparison_result::uo, time_t::in, time_t::in)
+	is det.
+compare_time_t_reps(Result, X, Y) :-
+	compare(Result, difftime(X, Y), 0.0).
 
 %-----------------------------------------------------------------------------%
 
-- 
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