Hello, we used Hugs' Prolog Demo as a base for a project in Programing
Languages, and found out that the cut implementation is wrong.


Here it is the portion of code in the latest release:

----------------------------------------------------------------------------------
prove      :: Database -> [Term] -> [Subst]
prove db gl = solve 1 nullSubst gl []
 where
   solve :: Int -> Subst -> [Term] -> Stack -> [Subst]
   solve n s []     ow          = s : backtrack n ow
   solve n s (g:gs) ow
                    | g==theCut = solve n s gs (cut ow)
                    | otherwise = choose n s gs (alts db n (app s g)) ow

   choose ...
   backtrack ...

--- Special definitions for the cut predicate:

theCut   ...

cut                  :: Stack -> Stack
cut (top:(s,gl,_):ss) = top:(s,gl,[]):ss
cut ss                = ss
-----------------------------------------------------------------------------------




And here it is our version, that we think is correct:

----------------------------------------------------------------------------------
prove      :: Database -> [Term] -> [Subst]
prove db gl = solve 1 nullSubst gl []
 where
   solve :: Int -> Subst -> [Term] -> Stack -> [Subst]
   solve n s []     ow          = s : backtrack n ow
   solve n s (g:gs) ow
                    | g == theCut       = solve s gs (cut2 (1 + numberOC gs)
ow)     -- Changed
                    | otherwise = choose n s gs (alts db n (app s g)) ow

   choose ...
   backtrack ...


--- Special definitions for the cut predicate (modified):

theCut   ...
numberOC = length . filter (==theCut) -- Number of Cuts

cut2                  :: Int -> Stack -> Stack
cut2 n [] = []
cut2 n ((s,gs,_):ow)
                | (numberOC gs) >= n    = (s,gs,[]): cut2 n ow
                | (numberOC gs) < n     = (s,gs,[]): ow

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

Sergio Urinovsky
[EMAIL PROTECTED]

Nicolas Wolovick
[EMAIL PROTECTED]

Undergraduate cs students at FaMAF - UNC
Cordoba - Argentina

Reply via email to