[m-rev.] for review: unification expressions

Simon Taylor stayl at cs.mu.OZ.AU
Fri Nov 2 23:41:16 AEDT 2001


Estimated hours taken: 1

Add unification expressions, written `X @ Y', to the language. 
`X @ Y' unifies X and Y and returns the result. Unification
expressions can make switches more compact.

library/ops.m:
	Add the operator `@'.

compiler/make_hlds.m:
	Expand unification expressions.

NEWS:
doc/reference_manual.texi:
w3/news/newsdb.inc:
	Document the new syntax.

tests/hard_coded/Mmakefile:
tests/hard_coded/unify_expression.{m,exp}:
	Test case.

Index: NEWS
===================================================================
RCS file: /home/mercury1/repository/mercury/NEWS,v
retrieving revision 1.222
diff -u -u -r1.222 NEWS
--- NEWS	26 Oct 2001 07:55:44 -0000	1.222
+++ NEWS	2 Nov 2001 12:12:16 -0000
@@ -17,6 +17,18 @@
   This change only affects programs which use `./2' explicitly.
   Programs which only use the `[H | T]' syntax will be unaffected.
 
+* We've added a new kind of expression to the language.
+  A unification expression, written `X @ Y', unifies X and Y
+  and returns the result.
+
+  Unification expressions are most useful when writing switches:
+	p(X, X) :- X = f(_, _).
+  can now be written as
+	p(X @ f(_, _), X).
+
+  See the "Data-terms" section of the "Syntax" chapter of the
+  Mercury Language Reference Manual for more details.
+
 * We've extended the language to allow you to specify different clauses
   for different modes of a predicate or function.  This is done by
   putting mode annotations in the head of each clause.
Index: compiler/make_hlds.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/make_hlds.m,v
retrieving revision 1.388
diff -u -u -r1.388 make_hlds.m
--- compiler/make_hlds.m	24 Oct 2001 13:34:19 -0000	1.388
+++ compiler/make_hlds.m	2 Nov 2001 10:29:05 -0000
@@ -7113,6 +7113,22 @@
 		unravel_unification(term__variable(X), RVal,
 			Context, MainContext, SubContext, VarSet0,
 			Purity, Goal, VarSet, Info1, Info)
+	;
+		% Handle unification expressions.
+		{ F = term__atom("@") },
+		{ Args = [LVal, RVal] }
+	->
+		unravel_unification(term__variable(X), LVal,
+			Context, MainContext, SubContext,
+			VarSet0, Purity, Goal1, VarSet1, Info0, Info1),
+		unravel_unification(term__variable(X), RVal,
+			Context, MainContext, SubContext,
+			VarSet1, Purity, Goal2, VarSet, Info1, Info),
+		{ goal_info_init(GoalInfo) },
+		{ goal_to_conj_list(Goal1, ConjList1) },
+		{ goal_to_conj_list(Goal2, ConjList2) },
+		{ list__append(ConjList1, ConjList2, ConjList) },
+		{ conj_list_to_goal(ConjList, GoalInfo, Goal) }
 	;	
 	    {
 		% handle lambda expressions
Index: doc/reference_manual.texi
===================================================================
RCS file: /home/mercury1/repository/mercury/doc/reference_manual.texi,v
retrieving revision 1.220
diff -u -u -r1.220 reference_manual.texi
--- doc/reference_manual.texi	24 Oct 2001 13:34:41 -0000	1.220
+++ doc/reference_manual.texi	2 Nov 2001 12:27:39 -0000
@@ -412,6 +412,7 @@
 
 Operator          Category  Specifier Priority
 
+@@                 after     xfx       50
 ^                 after     xfy       99
 ^                 before    fx        100
 **                after     xfy       200
@@ -956,6 +957,7 @@
 @menu
 * Data-functors::
 * Record syntax::
+* Unification expressions::
 * Conditional expressions::
 * Lambda expressions::
 * Higher-order function applications::
@@ -1058,6 +1060,35 @@
 @end example
 
 @end table
+
+ at node Unification expressions
+ at subsection Unification expressions
+
+A unification expression is an expression of the form
+ at example
+X @@ Y
+ at end example
+where @var{X} and @var{Y} are data-terms.
+
+The semantics of a unification expression is that @var{X} and @var{Y}
+are unified, and the expression is equivalent to the unified value.
+
+Unification expressions are most useful when writing switches
+ at pxref{Determinism checking and inference}.
+
+For example
+
+ at example
+p(X @@ f(_, _), X).
+ at end example
+
+ at noindent
+is equivalent to 
+
+ at example
+p(X, X) :-
+        X = f(_, _).
+ at end example
 
 @node Conditional expressions
 @subsection Conditional expressions
Index: library/ops.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/ops.m,v
retrieving revision 1.33
diff -u -u -r1.33 ops.m
--- library/ops.m	24 Oct 2001 10:42:57 -0000	1.33
+++ library/ops.m	2 Nov 2001 09:13:02 -0000
@@ -164,6 +164,7 @@
 ops__op_table(">=", after, xfx, 700).		% standard ISO Prolog
 ops__op_table(">>", after, yfx, 400).		% standard ISO Prolog
 ops__op_table("?-", before, fx, 1200).		% standard ISO Prolog (*)
+ops__op_table("@", after, xfx, 90).		% standard ISO Prolog
 ops__op_table("@<", after, xfx, 700).		% standard ISO Prolog
 ops__op_table("@=<", after, xfx, 700).		% standard ISO Prolog
 ops__op_table("@>", after, xfx, 700).		% standard ISO Prolog
Index: tests/hard_coded/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/Mmakefile,v
retrieving revision 1.132
diff -u -u -r1.132 Mmakefile
--- tests/hard_coded/Mmakefile	2 Nov 2001 00:50:51 -0000	1.132
+++ tests/hard_coded/Mmakefile	2 Nov 2001 11:40:11 -0000
@@ -128,6 +128,7 @@
 	type_spec_ho_term \
 	type_spec_modes \
 	type_to_term_bug \
+	unify_expression \
 	unused_float_box_test \
 	user_defined_equality2 \
 	write \
Index: tests/hard_coded/unify_expression.exp
===================================================================
RCS file: tests/hard_coded/unify_expression.exp
diff -N tests/hard_coded/unify_expression.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/unify_expression.exp	2 Nov 2001 11:41:29 -0000
@@ -0,0 +1 @@
+f(1, 2)
Index: tests/hard_coded/unify_expression.m
===================================================================
RCS file: tests/hard_coded/unify_expression.m
diff -N tests/hard_coded/unify_expression.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/unify_expression.m	2 Nov 2001 11:41:20 -0000
@@ -0,0 +1,29 @@
+:- module unify_expression.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io__state::di, io__state::uo) is det.
+
+:- implementation.
+
+:- import_module require.
+
+:- type t
+	--->	f(int, int)
+	;	g(t).
+
+main -->
+	( { p(g(f(1, 2)), X) } ->
+		io__write(X),
+		io__nl
+	;
+		io__write_string("failed\n")
+	).
+
+:- pred p(t::in, t::out) is semidet.
+
+p(X @ f(_, _), X).
+p(g(X @ f(_, _)), X).
+p(g(g(_)), _) :- error("p").
Index: w3/news/newsdb.inc
===================================================================
RCS file: /home/mercury1/repository/w3/news/newsdb.inc,v
retrieving revision 1.63
diff -u -u -r1.63 newsdb.inc
--- w3/news/newsdb.inc	28 Oct 2001 12:46:35 -0000	1.63
+++ w3/news/newsdb.inc	2 Nov 2001 12:31:49 -0000
@@ -21,6 +21,15 @@
 */
 
 $newsdb = array(
+"5 November 2001" => array("Unification expressions",
+
+"We've added a new kind of expression to the language.
+A unification expression, written `X @ Y', unifies X and Y and returns
+the result. Unification expressions are most useful when writing switches.
+See the \"Data-terms\" section of the \"Syntax\" chapter of the
+<A HREF=\"$ref_man_latest\">Mercury Language Reference Manual</A>
+for more details."
+),
 
 "26 October 2001" => array("New paper",
 
--------------------------------------------------------------------------
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