[m-dev.] for review: support constructions for existential data types

Fergus Henderson fjh at cs.mu.OZ.AU
Thu Jul 8 18:14:13 AEST 1999


I accidentally left the changes to type_util.m out of my original diff.
Here is the log message for that file, the diff of that file,
and a relative diff for doc/reference_manual.texi that addresses
dgj's review comments.  I will go ahead and commit this change now.

compiler/type_util.m:
	Define a new predicate `remove_new_prefix', for use by typecheck.m
	and polymorphism.m.

Index: type_util.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/type_util.m,v
retrieving revision 1.68
diff -u -u -r1.68 type_util.m
--- type_util.m	1999/06/30 17:12:42	1.68
+++ type_util.m	1999/07/07 16:56:24
@@ -62,6 +62,16 @@
 :- pred is_introduced_type_info_type(type).
 :- mode is_introduced_type_info_type(in) is semidet.
 
+	% In the forwards mode, this predicate checks for a "new " prefix
+	% at the start of the functor name, and removes it if present;
+	% it fails if there is no such prefix.
+	% In the reverse mode, this predicate prepends such a prefix.
+	% (These prefixes are used for construction unifications
+	% with existentially typed functors.)
+:- pred remove_new_prefix(sym_name, sym_name).
+:- mode remove_new_prefix(in, out) is semidet.
+:- mode remove_new_prefix(out, in) is det.
+
 	% Given a type, determine what sort of type it is.
 
 :- pred classify_type(type, module_info, builtin_type).
@@ -277,7 +287,7 @@
 %-----------------------------------------------------------------------------%
 
 :- implementation.
-:- import_module bool, require, std_util.
+:- import_module bool, require, std_util, string.
 :- import_module prog_io, prog_io_goal, prog_util.
 
 type_util__type_id_module(_ModuleInfo, TypeName - _Arity, ModuleName) :-
@@ -317,6 +327,11 @@
 	; Name = "base_typeclass_info"
 	),
 	mercury_private_builtin_module(PrivateBuiltin).
+
+remove_new_prefix(unqualified(Name0), unqualified(Name)) :-
+	string__append("new ", Name, Name0).
+remove_new_prefix(qualified(Module, Name0), qualified(Module, Name)) :-
+	string__append("new ", Name, Name0).
 
 %-----------------------------------------------------------------------------%
 
--- reference_manual.texi.old	Thu Jul  8 18:02:58 1999
+++ reference_manual.texi	Thu Jul  8 18:04:52 1999
@@ -3490,15 +3490,24 @@
 For example:
 
 @example
+% A simple heterogeneous list type
 :- type list_of_any
 	---> nil_any
 	;    some [T] cons_any(T, list_of_any).
 
+% A heterogeneous list type with a type class constraint
 :- typeclass showable(T) where [ func show(T) = string ].
-:- type list_of_showable
+:- type showable_list
 	---> nil
 	;    some [T] (cons(T, list_of_any) => showable(T)).
 
+% A different way of doing the same kind of thing, this
+% time using the standard type list(T).
+:- type showable ---> some [T] (s(T) => showable(T)).
+:- type list_of_showable == list(showable).
+
+% Here's an arbitrary example involving multiple
+% type variables and multiple constraints
 :- typeclass foo(T1, T2) where [ /* ... */ ].
 :- type bar(T)
 	---> f1
@@ -3516,10 +3525,11 @@
 are inverses: when constructing a value of an existentially quantified
 data type, the ``existentially quantified'' functor acts for purposes
 of type checking like a universally quantified function: the caller
-will determine the value of the type variable.
+will determine the values of the type variables.
 Conversely, for deconstruction the functor acts like an
 existentially quantified function: the caller must be defined so
-as to work for all types which are an instance of the declared type.
+as to work for all possible values of the existentially quantified
+type variables which satisfy the declared type class constraints.
 
 In order to make this distinction clear to the compiler,
 whenever you want to construct a value using an existentially
@@ -3542,7 +3552,7 @@
 :- instance showable(float).
 :- instance showable(string).
 
-:- func make_list = list_of_showable.
+:- func make_list = showable_list.
 make_list = List :-
 	Int = 42,
 	Float = 1.0,

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list