1 / 7

How to write a Prolog program

How to write a Prolog program. Design data structures to represent input, output and intermediary data as required

kieu
Download Presentation

How to write a Prolog program

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simply Logical – Chapter 3 How to write a Prolog program

  2. Simply Logical – Chapter 3 • Design data structures to represent input, output and intermediary data as required • Design algorithms to perform the task using only 2 forms of control: selection and recursion. The algorithm is developed ina hierarchicaltop-down mannerand terminates when all subtasks are elementary. • Translate the algorithms form the step 2 into Prolog programs. • Check correctness of programs by checking correctness of each procedure (testing is carried out bottom-up). • Add comments specifying a typical query to the program.

  3. Simply Logical – Chapter 3 p.74-6 • Write down declarative specification of partition • % partition(L,N,Littles,Bigs) <- Littles contains numbers • % in L smaller than N, • % Bigs contains the rest • Identify recursion and ‘output’ arguments • Write down skeleton • partition([],N,[],[]). • partition([Head|Tail],N,?Littles,?Bigs):-/* do something with Head */partition(Tail,N,Littles,Bigs). Logic programming methodology

  4. Simply Logical – Chapter 3 p.74-6 • Complete bodies • partition([],N,[],[]). • partition([Head|Tail],N,?Littles,?Bigs):-Head < N,partition(Tail,N,Littles,Bigs),?Littles = [Head|Littles],?Bigs = Bigs. • partition([Head|Tail],N,?Littles,?Bigs):-Head >= N,partition(Tail,N,Littles,Bigs),?Littles = Littles,?Bigs = [Head|Bigs]. • Fill in ‘output’ arguments • partition([],N,[],[]). • partition([Head|Tail],N,[Head|Littles],Bigs):-Head < N,partition(Tail,N,Littles,Bigs). • partition([Head|Tail],N,Littles,[Head|Bigs]):-Head >= N,partition(Tail,N,Littles,Bigs). Methodology

  5. Simply Logical – Chapter 3 p.76 • Write down declarative specification for sorting • % sort(L,S) <- S is a sorted permutation of list L • Write down skeleton • sort([],[]). • sort([Head|Tail],?Sorted):-/* do something with Head */sort(Tail,Sorted). • Complete body (auxiliary predicate needed) • sort([],[]). • sort([Head|Tail],WholeSorted):-sort(Tail,Sorted),insert(Head,Sorted,WholeSorted). Writing a sort predicate

  6. Simply Logical – Chapter 3 p.77 • Write down declarative specification • % insert(X,In,Out) <- In is a sorted list,e.g.[2,4,7,9] • % Out is In with X inserted in the proper place • % ?- insert(5,[2,4,7,9],Out). Out=[2,4,5,7,9] • Write down skeleton • insert(X,[],?Inserted). • insert(X,[Head|Tail],?Inserted):-/* do something with Head */insert(X,Tail,Inserted). Writing an insert predicate

  7. Simply Logical – Chapter 3 p.77 • Complete bodies • insert(X,[],?Inserted):-?Inserted=[X]. • insert(X,[Head|Tail],?Inserted):-X > Head,insert(X,Tail,Inserted),?Inserted = [Head|Inserted]. • insert(X,[Head|Tail],?Inserted):-X =< Head,?Inserted = [X,Head|Tail]. • Fill in ‘output’ arguments • insert(X,[],[X]). • insert(X,[Head|Tail],[X,Head|Tail]):-X =< Head. • insert(X,[Head|Tail],[Head|Inserted]):-X > Head,insert(X,Tail,Inserted). Writing an insert predicate

More Related