[m-rev.] For review: State-variable record update transformation

Jonathan Morgan jonmmorgan at gmail.com
Sat Mar 3 11:58:46 AEDT 2007


On 3/2/07, Julien Fischer <juliensf at csse.unimelb.edu.au> wrote:
> On Fri, 2 Mar 2007, Peter Schachte wrote:
>
> > Jonathan Morgan wrote:
> >
> >> Enhance the state variable notation to recognise !X ^ field_list := Term as
> >> !:X = !.X ^ field_list := Term.
> >
> > If you can write
> >
> >       !X ^ f1 ^ f2 := Expr
> > and
> >       !X ^ f3 := Expr
> >
> > it seems a bit inconsistent not to be able to write
> >
> >       !X := Expr
> >
>
> I don't think it is inconsistent not being able to write that.  The
> proposed syntatic sugar is for field updates in combination with
> state variables.  Field updates are recognised by the presence of the
> field accessor, '^', in addition to ':='.  There is no field accessor
> in your goal above so the sugar doesn't apply and it should just
> be a syntax error.
>
> > (as syntactic sugar for !:X = Expr).  Perhaps that should be added, too.
>
> I don't think it should be added.  For one thing when I see !X I am
> expecting that to be exapnded into !.X, !:X in some fashion.

I agree with Julien on both points.

Below is an updated diff.

Jon

========================================================
Estimated hours taken: 2.5
Branches: main

Enhance the state variable notation to recognise !X ^ field_list :=
Term as a synonym for
!:X = !.X ^ field_list := Term.

NEWS:
    Announce the new construct.

compiler/add_clause.m:
   Recognise this new construct, and transform it.

doc/reference_manual.texi:
   Document the change.

tests/hard_coded/Mmakefile:
tests/hard_coded/sv_record_update.m:
tests/hard_coded/sv_record_update.exp:
    A test case for this feature.

Index: NEWS
===================================================================
RCS file: /home/mercury1/repository/mercury/NEWS,v
retrieving revision 1.447
diff -u -r1.447 NEWS
--- NEWS	15 Feb 2007 00:41:47 -0000	1.447
+++ NEWS	3 Mar 2007 00:47:28 -0000
@@ -16,6 +16,8 @@
   escape sequence.
 * promise_equivalent_solutions scopes (et al.) must now also list variables
   with inst any that may be constrained by the goal.
+* We now support !X ^ field_list := Term as a synonym for
+!:X = !.X ^ field_list := Term.

 Changes to the Mercury standard library:

Index: compiler/add_clause.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/add_clause.m,v
retrieving revision 1.42
diff -u -r1.42 add_clause.m
--- compiler/add_clause.m	19 Jan 2007 07:04:07 -0000	1.42
+++ compiler/add_clause.m	3 Mar 2007 00:47:34 -0000
@@ -820,6 +820,26 @@
             !SInfo, !Specs),
         finish_call(!VarSet, !SInfo)
     ;
+        % check for a state var record assignment:
+        % !Var ^ field := Value
+        Name = unqualified(":="),
+        Args1 = [LHS0, RHS0],
+        LHS0 = functor(atom("^"), [StateVar0, Remainder], FieldListContext),
+        StateVar0 = functor(atom("!"), Args @ [variable(_, _)],
+                StateVarContext)
+    ->
+        prepare_for_call(!SInfo),
+        % !Var ^ field := Value is defined as !:Var = !.Var ^ field := Value.
+        LHS = functor(atom("!:"), Args, StateVarContext),
+        StateVar = functor(atom("!."), Args, StateVarContext),
+        FieldList = functor(atom("^"), [StateVar, Remainder],
+                FieldListContext),
+        RHS = functor(atom(":="), [FieldList, RHS0], Context),
+        transform_goal_2(unify_expr(LHS, RHS, Purity),
+            Context, Subst, Goal, NumAdded, !VarSet, !ModuleInfo, !QualInfo,
+            !SInfo, !Specs),
+        finish_call(!VarSet, !SInfo)
+    ;
         % check for a DCG field access goal:
         % get: Field =^ field
         % set: ^ field := Field
Index: doc/reference_manual.texi
===================================================================
RCS file: /home/mercury1/repository/mercury/doc/reference_manual.texi,v
retrieving revision 1.383
diff -u -r1.383 reference_manual.texi
--- doc/reference_manual.texi	13 Feb 2007 12:35:04 -0000	1.383
+++ doc/reference_manual.texi	3 Mar 2007 00:48:05 -0000
@@ -1016,6 +1016,17 @@
 some @var{Vars} transform(@var{Goal}, @var{X}, @var{ThisX}, @var{NextX})
 @end example

+ at item !@var{X} ^ @var{field_list} := @var{Term}
+A field update.
+Replaces a field in the argument.
+ at var{Term} must be a valid data-term.
+ at var{field_list} must be a valid field list.
+ at xref{Record syntax}.
+ at example
+transform((!@var{X} ^ @var{field_list} := @var{Term}), @var{X},
@var{ThisX}, @var{NextX}) =
+NextX = ThisX ^ field_list := Term
+ at end example
+
 @item @var{Call_or_Unification}
 If @samp{!:@var{X}} does not appear in @var{Call_or_Unification} then
 @example
Index: tests/hard_coded/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/Mmakefile,v
retrieving revision 1.312
diff -u -r1.312 Mmakefile
--- tests/hard_coded/Mmakefile	27 Feb 2007 02:12:37 -0000	1.312
+++ tests/hard_coded/Mmakefile	3 Mar 2007 00:48:17 -0000
@@ -203,6 +203,7 @@
 	string_suffix_bug \
 	string_various \
 	sv_nested_closures \
+	sv_record_update \
 	switch_detect \
 	system_sort \
 	target_mlobjs \



Added files:
tests/hard_coded/sv_record_update.m:
% vim: ft=mercury ts=4 sw=4 et
:- module sv_record_update.
:- interface.
:- import_module io.

:- pred main(io::di, io::uo) is det.

:- implementation.

:- import_module int.

:- type type1 --->
    type1(
        field1::int,
        field2::int
    ).

:- type type2 --->
    type2(
        type1::type1,
        field3::int
    ).

main(!IO) :-
    test1(type1(3,4), Type1),
    io.write(Type1, !IO),
    io.nl(!IO),
    test2(type2(Type1, 5), Type2),
    io.write(Type2, !IO),
    io.nl(!IO).

:- pred test1(type1::in, type1::out) is det.
test1(!Type1) :-
    !Type1 ^ field2 := 12.

:- pred test2(type2::in, type2::out) is det.
test2(!Type2) :-
    !Type2 ^ type1 ^ field1 := 5,
    !Type2 ^ field3 := 13.

tests/hard_coded/sv_record_update.exp:
type1(3, 12)
type2(type1(5, 12), 13)
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list