/* Deriv benchmark in Picat. Prolog code from http://www.jekejeke.ch/idatab/doclet/prod/en/docs/05_run/15_stdy/06_bench/09_programs/03_deriv/01_deriv.p.html This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. % import cp. main => go. % Prolog code /** * Prolog code for the symbolic derivation benchmark. * * This is the benchmark of page 222 of: * Warren, D.H.D. (1983): Applied Logic – Its Use and * Implementation as a Programming Tool, * Technical Note 290, SRI International, 1983 * * Copyright 2010, XLOG Technologies GmbH, Switzerland * Jekejeke Prolog 0.8.3 (a fast and small prolog interpreter) */ /* d(U + V, X, DU + DV) => d(U, X, DU), d(V, X, DV). d(U - V, X, DU - DV) => d(U, X, DU), d(V, X, DV). d(U * V, X, DU * V + U * DV) => d(U, X, DU), d(V, X, DV). d(U / V, X, (DU * V - U * DV) / ^(V, 2)) => d(U, X, DU), d(V, X, DV). d(^(U, N), X, DU * N * ^(U, N1)) => N1 = N - 1, d(U, X, DU). d(-U, X, -DU) => d(U, X, DU). d(exp(U), X, exp(U) * DU) => d(U, X, DU). d(log(U), X, DU / U) => d(U, X, DU). d(X, X, 1) => true. d(_, _, Zero) => Zero = 0. */ % % From http://www.picat.org/download/exs.pi % % Note: This adds some extra parenthesis compared to the Prolog version, % e.g. x^2 -> (x^2) % d(U+V,X,D) => D = $DU+DV, d(U,X,DU), d(V,X,DV). d(U-V,X,D) => D = $DU-DV, d(U,X,DU), d(V,X,DV). d(U*V,X,D) => D = $DU*V+U*DV, d(U,X,DU), d(V,X,DV). d(U/V,X,D) => D = $(DU*V-U*DV)/(^(V,2)), d(U,X,DU), d(V,X,DV). d(^(U,N),X,D) => D = $DU*N*(^(U,N1)), integer(N), N1=N-1, d(U,X,DU). d(-U,X,D) => D = $-DU, d(U,X,DU). d(exp(U),X,D) => D = $exp(U)*DU, d(U,X,DU). d(log(U),X,D) => D = $DU/U, d(U,X,DU). d(X,X,D) => D=1. d(_,_,D) => D=0. % Answer should be: % (1+0)*((x^2+2)*(x^3+3))+(x+1)*((1*2*x^1+0)*(x^3+3)+(x^2+2)*(1*3*x^2+0)) ops8(E) => Exp = $((x+1) * ((^(x,2)+2)*(^(x,3)+3))), d(Exp,x,E). % Answer should be: % (((((((((1*x-x*1)/x^2*x-x/x*1)/x^2*x-x/x/x*1)/x^2*x-x/x/x/x*1)/x^2*x-x/x/x/x/x*1)/x^2*x-x/x/x/x/x/x*1)/x^2*x-x/x/x/x/x/x/x*1)/x^2*x-x/x/x/x/x/x/x/x*1)/x^2*x-x/x/x/x/x/x/x/x/x*1)/x^2 divide10(E) => Exp= $((((((((x/x)/x)/x)/x)/x)/x)/x)/x)/x, d(Exp,x,E). % Answer should be: % 1/x/log(x)/log(log(x))/log(log(log(x)))/log(log(log(log(x))))/log(log(log(log(log(x)))))/log(log(log(log(log(log(x))))))/log(log(l log10(E) => Exp = $(log(log(log(log(log(log(log(log(log(log(x))))))))))), d(Exp, x, E). % Answer should be: % ((((((((1*x+x*1)*x+x*x*1)*x+x*x*x*1)*x+x*x*x*x*1)*x+x*x*x*x*x*1)*x+x*x*x*x*x*x*1)*x+x*x*x*x*x*x*x*1)*x+x*x*x*x*x*x*x*x*1)*x+x*x*x*x*x*x*x*x*x*1 times10(E) => Exp = $(((((((((x * x) * x) * x) * x) * x) * x) * x) * x) * x), d(Exp, x, E). deriv => ops8(E1), println(ops8=E1), divide10(E2), println(divide10=E2), log10(E3), println(log10=E3), times10(E4), println(time10=E4). deriv2 => ops8(_E1), divide10(_E2), log10(_E3), times10(_E4). go => deriv. go2 => deriv2.