/* McCarthy 91 function in Picat. https://en.wikipedia.org/wiki/McCarthy_91_function """ The McCarthy 91 function is a recursive function, defined by the computer scientist John McCarthy as a test case for formal verification within computer science. The McCarthy 91 function is defined as M(n) = n - 10 if n > 101 M(M(n+11)) if n <= 101 The results of evaluating the function are given by M(n) = 91 for all integer arguments n ≤ 100, and M(n) = n − 10 for n > 100. Indeed, the result of M(101) is also 91 (101 - 10 = 91). All results of M(n) after n = 101 are continually increasing by 1, e.g. M(102) = 92, M(103) = 93. """ Inspired by http://stackoverflow.com/questions/23464932/prolog-mccarthy-91 This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => foreach(N0 in 1..110) m(N0,N), println([N0,N]) end, nl. go2 => N0 :: 1..200, m(N0,N), println(n0=N0), println(n=N), N #> 91, solve(N), println([N0,N]), nl. go3 => foreach(N0 in 1..110) m2(N0,N), println([N0,N]) end, nl. m(N0, N) ?=> N0 #> 100, N #= N0-10. m(N0,N) => N0 #=< 100, N1 #= N0+11, m(N1, N2), m(N2, N). % plain predicate m2(N0, N) ?=> N0 > 100, N = N0-10. m2(N0, N) => N0 =< 100, N1 = N0 + 11, m2(N1, N2), m2(N2, N).