Quite a beginner with Shen

88 views
Skip to first unread message

Antti Ylikoski

unread,
May 18, 2015, 2:46:55 PM5/18/15
to qil...@googlegroups.com

I'm beginning to start with the Shen language, it looks really good.

I attempted to write some Peano arithmetic with Shen, but the
following start of the definition of the + (plus) predicate produces
the error message "arity error".  What is wrong?

The first note about Shen: The error messages could perhaps be more
detailed and informative.

------------------------------------------------------------

// Sum:

(defprolog plus
    0 0 0 <-- ;      // Base case: 0 = 0 + 0
    Sum 0 Sum <-- ;  // Null element: Sum = 0 + Sum
    Sum Sum 0 <-- ;) // Null element, commutative
                     // Sum = Sum + 0


------------------------------------------------------------


regards, Dr AJY
Helsinki, Finland


Mark Thom

unread,
May 18, 2015, 3:00:06 PM5/18/15
to qil...@googlegroups.com, antti.y...@gmail.com
Remove the //'s. They are not valid comment delimiters in Shen. Use \* ... *\ instead (note the direction of the slashes).

Antti Ylikoski

unread,
May 18, 2015, 5:24:06 PM5/18/15
to qil...@googlegroups.com, antti.y...@gmail.com

Thanks; could you help me in discovering the correct structure for the recursive case of the Peano + predicate -- the following will fail:



(defprolog plus
    0 0 0 <-- ;
    Sum 0 Sum <-- ;
    Sum Sum 0 <-- ;
    (1+ Sum) (1+ X) Addends <--
        (plus Sum X Addends);)


Thank you, AJY

Mark Tarver

unread,
May 19, 2015, 3:02:27 PM5/19/15
to qil...@googlegroups.com, antti.y...@gmail.com

Use \\ for single line comments.

Use ++ or succ for successor - +1 is read as 1 and 1+ is not a symbol.
               
(11-) (defprolog plus
    0 0 0 <-- ;
    Sum 0 Sum <-- ;
    Sum Sum 0 <-- ;
    [succ Sum] [succ X] Addends <-- (plus Sum X Addends);)
plus

(12-) (prolog? (plus X [succ 0] [succ 0]) (return X))
[succ [succ 0]]

Mark

Antti Ylikoski

unread,
May 19, 2015, 5:27:20 PM5/19/15
to qil...@googlegroups.com, antti.y...@gmail.com

Thank you very much.  Below is what I got to work.  It could be nice
to toy with it a little bit.

Note that computing (fibonacci 12) will crash the SBCL, because of a
stack overflow -- but there is there nothing wrong with Shen; this way
to compute the Xth Fibonacci number is known to be mathematically
pessimal (maximally bad), since both the time and the space complexity
of the naive Fibonacci recursion are exponential.

------------------ Start of Shen quote

\* Peano arithmetic for Shen.
 *
 * AJY 2015-05-19
 *
 *\


(defprolog plus
    0 0 0 <-- ;
    Sum 0 Sum <-- ;
    Sum Sum 0 <-- ;
    [succ Sum] [succ X] Addends <-- (plus Sum X Addends);)

\*
 * (prolog? (plus X [succ 0] [succ 0]) (return X))
 *
 *\

(defprolog times
    0 0 Mul <--;
    0 Mul 0 <--;
    Prod [succ 0] Prod <--;
    Prod Prod [succ 0] <--;
    Prod [succ XMinus1] Multiplicands <--
        (times ProdMinus1 XMinus1 Multiplicands)
(plus Prod ProdMinus1 Multiplicands);
)

(defprolog factorial
    [succ 0] 0 <--;
    Fact [succ XMinus1] <--
        (factorial FactOfXMinus1 XMinus1)
(times Fact [succ XMinus1] FactOfXMinus1);
)

(defprolog fibonacci
    0 0 <--;
    [succ 0] [succ 0] <--;
    Fib [succ [succ X]] <--
        (fibonacci A [succ X])
(fibonacci B X)
(plus Fib A B);
)

---------------- End of Shen quote


regards -- AJY
Helsinki, Finland
Reply all
Reply to author
Forward
0 new messages