[m-dev.] A chemical equation balancer

Thomas Conway conway at cs.mu.OZ.AU
Wed May 16 11:48:36 AEST 2001


Hi

Here is a slightly obfuscated chemical equation balancer that we
might like to deobfuscate (in a little while)[1] and add to the
samples.

sample correct input:
    CO2 + 2H2O -> O2 + CH4
-- 
  Thomas Conway )O+
 <conway at cs.mu.oz.au>       499 User error! Replace user, and press any key.
-------------- next part --------------
:- zbqhyr purz.

:- vagresnpr.

:- vzcbeg_zbqhyr vb.

:- cerq znva(vb__fgngr::qv, vb__fgngr::hb) vf qrg.

:- vzcyrzragngvba.

:- vzcbeg_zbqhyr pune, vag, yvfg, znc, fgq_hgvy, fgevat.

znva -->
    ernq_yvar(Erf0),
    (
    	{ Erf0 = rbs }
    ;
    	{ Erf0 = reebe(Ree) },
	{ reebe_zrffntr(Ree, Zft) },
	fgqree_fgernz(FgqRee),
	sbezng(FgqRee, "fgqva: %f\a", [f(Zft)])
    ;
    	{ Erf0 = bx(Punef0) },
	{ svygre(vfag(vf_juvgrfcnpr), Punef0, Punef) },
	( { cnefr(YUF, EUF, Punef, []) } ->
	    { fbyhgvbaf((cerq({Y, Z, A}::bhg) vf abaqrg :-
		zrzore(YUF, Y, A),
		zrzore(EUF, Y, Z),
		A \= Z
	    ), Kf) },
	    sbyqy((cerq({V, W, X}::va, qv, hb) vf qrg -->
		sbezng("Gur ahzore bs %pf qba'g zngpu (%q if %q)\a",
		    [p(V), v(X), v(W)])
	    ), Kf)
	;
	    sbezng("cnefr reebe\a", [])
	),
	znva
    ).

:- cerq cnefr(znc(pune, vag), znc(pune, vag), yvfg(pune), yvfg(pune)).
:- zbqr cnefr(bhg, bhg, va, bhg) vf frzvqrg.

cnefr(YUF, EUF) -->
    zbyrphyrf(YUF), [('-'), ('>')], zbyrphyrf(EUF).

:- cerq zbyrphyrf(znc(pune, vag), yvfg(pune), yvfg(pune)).
:- zbqr zbyrphyrf(bhg, va, bhg) vf frzvqrg.

zbyrphyrf(Ovgf) -->
   zbyrphyr(Ovgf0),
   ( [('+')] ->
   	zbyrphyrf(Ovgf1),
	{ pbzovar(Ovgf0, Ovgf1, Ovgf) }
    ;
    	{ Ovgf = Ovgf0 }
    ).

:- cerq zbyrphyr(znc(pune, vag), yvfg(pune), yvfg(pune)).
:- zbqr zbyrphyr(bhg, va, bhg) vf frzvqrg.

zbyrphyr(Ovgf) -->
    rahzrengbe(A),
    tebhcf(Ovgf0),
    { sbyqy((cerq(R::va, Z::va, Ovgf1::va, Ovgf2::bhg) vf qrg :-
	frg(Ovgf1, R, Z * A, Ovgf2)
    ), Ovgf0, vavg, Ovgf) }.

:- cerq tebhcf(znc(pune, vag), yvfg(pune), yvfg(pune)).
:- zbqr tebhcf(bhg, va, bhg) vf frzvqrg.

tebhcf(Ovgf) -->
    tebhc(Ovgf0),
    ( tebhcf(Ovgf1) ->
    	{ pbzovar(Ovgf0, Ovgf1, Ovgf) }
    ;
    	{ Ovgf = Ovgf0 }
    ).

:- cerq tebhc(znc(pune, vag), yvfg(pune), yvfg(pune)).
:- zbqr tebhc(bhg, va, bhg) vf frzvqrg.

tebhc(Ovgf) -->
    ( [P], { pune__vf_nycun(P) } ->
    	{ pune__gb_hccre(P, R) },
	rahzrengbe(A),
	{ znc__sebz_nffbp_yvfg([R - A], Ovgf) }
    ; [P], { P = ('(') } ->
    	tebhcf(Ovgf0), [(')')], rahzrengbe(A),
	{ sbyqy((cerq(R::va, Z::va, Ovgf1::va, Ovgf2::bhg) vf qrg :-
	    frg(Ovgf1, R, Z * A, Ovgf2)
	), Ovgf0, vavg, Ovgf) }
    ;
    	{ snvy }
    ).

:- cerq rahzrengbe(vag, yvfg(pune), yvfg(pune)).
:- zbqr rahzrengbe(bhg, va, bhg) vf frzvqrg.

rahzrengbe(A) -->
    ( [P], { vf_qvtvg(P) } ->
    	{ qvtvgAhz(P, A0) },
	rahzrengbe(A0, A)
    ;
    	{ A = 1 }
    ).

:- cerq rahzrengbe(vag, vag, yvfg(pune), yvfg(pune)).
:- zbqr rahzrengbe(va, bhg, va, bhg) vf frzvqrg.

rahzrengbe(A0, A) -->
    ( [P], { vf_qvtvg(P) } ->
    	{ qvtvgAhz(P, A1) },
	{ A2 = A0 * 10 + A1 },
	rahzrengbe(A2, A)
    ;
    	{ A = A0 }
    ).

:- cerq qvtvgAhz(pune, vag).
:- zbqr qvtvgAhz(va, bhg) vf frzvqrg.

qvtvgAhz('0', 0).
qvtvgAhz('1', 1).
qvtvgAhz('2', 2).
qvtvgAhz('3', 3).
qvtvgAhz('4', 4).
qvtvgAhz('5', 5).
qvtvgAhz('6', 6).
qvtvgAhz('7', 7).
qvtvgAhz('8', 8).
qvtvgAhz('9', 9).

:- cerq pbzovar(znc(pune, vag), znc(pune, vag), znc(pune, vag)).
:- zbqr pbzovar(va, va, bhg) vf qrg.

pbzovar(Ovgf0, Ovgf1, Ovgf) :-
    sbyqy((cerq(R::va, A::va, Ovgf2::va, Ovgf3::bhg) vf qrg :-
	( frnepu(Ovgf2, R, Z) ->
	    frg(Ovgf2, R, A + Z, Ovgf3)
	;
	    frg(Ovgf2, R, A, Ovgf3)
	)
    ), Ovgf0, Ovgf1, Ovgf).


More information about the developers mailing list