[m-rev.] State variable syntax transformation

Simon Taylor stayl at cs.mu.OZ.AU
Thu Feb 14 03:25:22 AEDT 2002


On 07-Feb-2002, Ralph Becket <rafe at cs.mu.OZ.AU> wrote:
Index: compiler/prog_io_state_vars.m
====================================
> +:- type subs 
> +    --->    subs(
> +                this    :: varmap,
> +                next    :: varmap
> +            ).

Documentation?

> +:- pred init_subs(term, term, subs, subs, varset, varset).
> +:- mode init_subs(in, in, in, out, in, out) is det.
> +
> +init_subs(Head, Body, ParentSubs, LocalSubs, VS0, VS) :-
> +    ParentSubs = subs(PThis, PNext),
> +    HeadSVs    = state_vars(yes, Head),
> +    BodySVs    = state_vars(no,  Body),
> +    SVs        = list__merge_and_remove_dups(HeadSVs, BodySVs),
> +    list__foldl2(set_state_var_map(this), SVs, map__init, LThis0, VS0, VS1),
> +    list__foldl2(set_state_var_map(next), SVs, map__init, LNext0, VS1, VS ),
> +
> +        % The LocalSubs may inherit state variables provided they are
> +        % not local (i.e. appear in the local Head.)

I'm not sure what you mean here.

> +:- pred lambda_term(term, term, const, term).
> +:- mode lambda_term(in, out, out, out) is semidet.
> +
> +lambda_term(T0, Head, Connective, Body) :-
> +
> +    T0 = functor(atom(A0), Args0, _),
> +    (
> +        A0         = ":-",
> +        Args0      = [Head, Body],
> +        Connective = atom(A0)
> +    ;
> +        A0         = "-->",
> +        Args0      = [Head, Body],
> +        Connective = atom(A0)
> +    ;
> +        A0         = "=",
> +        Args0      = [functor(atom("func"), _, _), _RHS],
> +        Head       = T0,
> +        Connective = atom(":-"),
> +        Body       = true_term
> +    ;
> +        A0         = "is",
> +        Args0      = [functor(atom("pred"), _, _), _Detism],
> +        Head       = T0,
> +        Connective = atom(":-"),
> +        Body       = true_term
> +    ;
> +        A0         = "is",
> +        Args0      = [functor(atom("="), T1, _), _Detism],
> +        T1         = [functor(atom("func"), _, _), _RHS],
> +        Head       = T0,
> +        Connective = atom(":-"),
> +        Body       = true_term
> +    ).

This is another place where you are duplicating work done by the parser
(and this isn't quite right -- it will succeed for terms which aren't
recognized as lambda terms by later passes). It's probably better to use
parse_*_expression in prog_io_goal.m.

> +:- pred transform_state_vars(term, term, term, term, subs, varset, varset).
> +:- mode transform_state_vars(in, out, in, out, in, in, out) is det.
> +
> +transform_state_vars(Head0, Head, Body0, Body, ParentSubs, VarSet0, VarSet) :-
> +
> +        % Only apply the transformation if the clause contains
> +        % state variables.
> +        %
> +    ( if ( contains_state_var(Head0) ; contains_state_var(Body0) ) then
> +
> +            % Ensure that we first strip off any determinism declaration
> +            % on the head term (this can be the case with lambdas).
> +            %
> +        ( if Head0 = functor(atom("is"), [H, D], CIs) then
> +
> +            AddDetism = yes({D, CIs}),
> +            Head1     = H
> +
> +          else
> +
> +            AddDetism = no,
> +            Head1     = Head0
> +        ),
> +        Body1 = Body0,
> +
> +            % Ensure that we transform functions so that the RHS
> +            % of the `=' is considered as part of the body.
> +            %
> +        ( if
> +
> +            Head1   = functor(atom("="), [LHS, RHS], C)
> +
> +          then
> +
> +            varset__new_var(VarSet0, X, VarSet1),
> +            Head2   = functor(atom("="), [LHS, variable(X)], C),
> +            Body2   = Body1 `conj` functor(atom("="), [variable(X), RHS], C)

make_hlds.m inserts the function return value before the goal.
It would be better to avoid changing the structure of the goal
here anyway.

> +:- pred xform_goal(subs, term, term, varset, varset).
> +:- mode xform_goal(in, in, out, in, out) is det.
> +
> +xform_goal(S, T0, T, VS0, VS) :-
> +    ( if   xform_compound(S, T0, T1, VS0, VS1)
> +      then T = T1, VS = VS1
> +      else xform_atomic(S, T0, T,  VS0, VS )
> +    ).

Please use more descriptive variable names throughout this module
(e.g. s/VS/VarSet/, s/S/Subst/).

Simon.
--------------------------------------------------------------------------
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