[mercury-users] Term

Ralph Becket rafe at cs.mu.OZ.AU
Wed Oct 16 09:10:01 AEST 2002


Noel  Pinto, Tuesday, 15 October 2002:
> Hi,
> 
> I want to understand how this DCG-goal term works:
> 
> transform(V_in, V_out, (DCG_goal1 ; DCG_goal2)) =
> ( transform(V_in, V_new, DCG_goal1)
> , transform(V_new, V_out, DCG_goal2) )
> 
> I really cannot understand how this DCG works. Plz do provide an 
> example.

The DCG-rules and DCG-goals subsections of the syntax section of the
manual explain that `transform' is a function that rewrites its third
argument to remove DCG notation, leaving an equivalent term that does
not use DCG notation.

The rule for DCG clauses is

	transform(Vin, Vout, (p(...) --> G)) =
		(p(..., Vin, Vout) :- transform(Vin, Vout, G))

The rule for unescaped atomic goals is

	transform(Vin, Vout, p(...)) =
		p(..., Vin, Vout)

The rule for escaped goals is

	transform(Vin, Vout, {G}) =
		(G, Vout = Vin)

The rule for conjunctions is

	transform(Vin, Vout, (G1, G2)) =
		transform(Vin,  Vtmp, G1),
		transform(Vtmp, Vout, G2)

(where Vtmp is a fresh variable.)  And so forth.

Hence if we define C as the clause 

	main -->
		io__print("Hello, "),
		{ X = "World!\n" },
		io__print(X).

and we pick IO0 for Vin and IO for Vout (the names don't matter) then
the compiler will convert this according to

	transform(IO0, IO, C).
   =
	main(IO0, IO) :-
		transform(IO0, IO,
			( io__print("Hello, "),
			  { X = "World!\n" },
			  io__print(X)
			)).
   =
	main(IO0, IO) :-
		( transform(IO0, IO1,
			io__print("Hello, ")),
		  transform(IO1, IO,
			( { X = "World!\n" },
			  io__print(X)
			))
		).
   =
	main(IO0, IO) :-
		( io__print("Hello, ", IO0, IO1),
		( transform(IO1, IO2,
			{ X = "World!\n" }),
		  transform(IO2, IO,
			  io__print(X))
		)).
   =
	main(IO0, IO) :-
		( io__print("Hello, ", IO0, IO1),
		( (X = "World!\n", IO2 = IO1),
		  io__print(X, IO2, IO)
		)).
   =
	main(IO0, IO) :-
		io__print("Hello, ", IO0, IO1),
		X = "World!\n",
		IO2 = IO1,
		io__print(X, IO2, IO).

So we end up with a clause that does not use DCG notation and which the
Mercury compiler can work directly.

The state variable transformation is very similar, except that you may
have multiple state threads and the syntax is different.

- Ralph
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list