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

Jonathan Morgan jonmmorgan at gmail.com
Fri Mar 2 15:48:39 AEDT 2007


For review by Ralph.

Estimated hours taken: 2
Branches: main

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

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

doc/reference_manual.texi:
    Document the change.

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	1 Mar 2007 13:13:53 -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.385
diff -u -r1.385 reference_manual.texi
--- doc/reference_manual.texi	22 Feb 2007 06:08:20 -0000	1.385
+++ doc/reference_manual.texi	1 Mar 2007 13:14:24 -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
--------------------------------------------------------------------------
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