tags:

views:

468

answers:

2

I've found such an example of naive sort written in prolog and I am trying to understand it:

naive_sort(List,Sorted):-perm(List,Sorted),is_sorted(Sorted).

is_sorted([]).
is_sorted)[_]).
is_sorted([X,Y|T]):-X=<Y,is_sorted([Y|T]).


perm(List,[H|Perm]):-delete(H,List,Rest),perm(Rest,Perm).
perm([],[]).

delete(X,[X|T],T).
delete(X,[H|T],[H|NT]):-delete(X,T,NT).

Naive_sort call works correctly but I just can't figure out why. The main problem is the permutation. When it is called implicitly it always returns only one value. How is it then possible that in naive_sort function call all permutations are checked? Also how could I modify perm function to write all permutations?

+3  A: 

This is truly a naive sort -- it traverses the tree of all possible permutations until it luckily finds a sorted one. That's have a complexity of O(n!) i presume :>

About the permutation function -- it works "backwards" -- note that the definition takes the head out of the result. If you turn around your point of view, you'll notice that instead of deleting it actually inserts values by working backwards. As the algorithm is working backwards, hence the Head chosen may be anything that will allow a result to be created, hence any unused value from List.

Basically the permutation algorithm translates to the following procedural implementation:

  1. pick an item from List
  2. put it into front of Sorted

This way you generate permutations. All of them.

In short - perm generates the whole space of possible solutions by starting out of an empty solution and checking how the given solution is possible from a valid delete.

?-  perm( [ 1, 2, 3 ] , P ) 
P = [1, 2, 3]; 
P = [1, 3, 2]; 
P = [2, 1, 3]; 
P = [2, 3, 1]; 
P = [3, 1, 2]; 
P = [3, 2, 1]; 
no
Kornel Kisielewicz
so why whe calling this function it does display only ONE result, not all of them?
agnieszka
@agnieszka - it's not a function! It's a **predicate** that is true ONLY if the first argument is a permutation of the second.
Kornel Kisielewicz
@agnieszka - using GNU Prolog or SWI?
Kornel Kisielewicz
YESSS I know that, still you've written "it generates permutations. all of them." - it does not. it finds first combination of the unknown arguments that satisfies the predicate - so only ONE permutation. so how, how is it possible that it works when calling naive_sort?
agnieszka
using SWI-------
agnieszka
@agnieszka -- because it keeps "calling" perm, until either is_sorted is true or it fails.
Kornel Kisielewicz
@agnieszka, perm zwraca jedno rozwiązanie, prolog sprawdza -- jeśli otrzyma nie, to próbuje dostać kolejne od perm aż wyczerpie wszystkie możliwe rozwiązania.
Kornel Kisielewicz
1. but when i call perm many times i always get the same result. 2. according to your edit: my result is different after that call - it displays only one result and i don't know how to modify it to display them all
agnieszka
tylko ze wielokrotne wywolywanie perm zawsze daje te same wyniki
agnieszka
@agnieszka - bo wielokrotnie wywołujesz, a nie ściągasz kolejne wyniki :). Wpisz średnik i enter po pierwszym wyniku -- będziesz miała szukanie dalszych rozwiązań -- http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/1.html
Kornel Kisielewicz
z nieba mi spadles dzien przed egzaminem... :D czyli po prostu prolog wie ze rozwiazan perm-a wyolaniu naive_sort jest wiele i skoro jedno nie dziala to probuje drugie. 2 dni na nauczenie prologa to jednak troche za malo - wielkie dzieki! ;)
agnieszka
Prolog wie, że póki perm mu nie powie "No", to może próbować czerpać z niego rozwiązania. 2 dni to zdecydowanie za mało, bo prolog to inny paradygmat programowania (programowanie logiczne) który znacząco różni się od szeroko znanych... powowdzenia!
Kornel Kisielewicz
:D Łojczysty język na stacku. no patrzcie... :D
naugtur
@naugtur, zbliżający się egzamin zmusza do radykalnych rozwiązań ;>
Kornel Kisielewicz
+3  A: 

The main problem is the permutation function. When it is called implicitly it always retrns only one value.

Prolog is a language that always attempts on proving the truth of a statement, by deriving it using the axioms (facts or rules) given.

perm is not a function in the sense of procedural programming. perm is a predicate, about which we tell prolog two things:

  1. The empty list is a permutation of itself.
  2. List is a permutation of [H|Perm] if there is a list Rest such that Rest is obtained by deleting H from List, and Rest is a permutation of Perm.

When asked whether some list is a permutation of another, prolog will attempt to apply these derivation steps (recursively) to prove it. If this recursion reaches a dead end, i.e. a statement which can not be proven as no rules can be applied to it, it backtracks.

meriton
i knew all that except of the backtracking if anything fails - this explains a lot
agnieszka
Describing it is so much easier if english is one's native language -_-
Kornel Kisielewicz
I take that as a compliment, because it is not my native language either :-)
meriton
+1: if that's the case ;)
Kornel Kisielewicz