L I S P (Pl 실습)

L I S P (Pl 실습)

2005. PROLOG Yonsei Univ. 1 Prolog Terms Constants, Variables, Compound Terms Facts, Rules SWI-Prolog

Operators Lists findall, bagof, setof Problem solving 2 Prolog 1970 (A. Colmerauer)A. Colmerauer)

Programming in Logic : (A. Colmerauer) , ) (A. Colmerauer)Declarative Language) : (A. Colmerauer)Facts) (A. Colmerauer)Rules): Terms Constants (A. Colmerauer)integers, real numbers, atoms) Variables Compound Terms 3 Constants Terms Integer constants(A. Colmerauer) ) : 1, 2, 3,

Real constants(A. Colmerauer) ) : 1.23, Atom(A. Colmerauer) ) : , , ex) john, book, mjk0206, ex) *, ., =, @#$, (A. Colmerauer) ) ex) george-smith Atom Variable : not bound to anything, never equal to anythin g else (A. Colmerauer)string constants ) 4 Variables , (A. Colmerauer)_) Terms

ex) X, Child, Gross_Pay, _3_blind_mice (A. Colmerauer)anonymous variable) _ ex) ?-likes(A. Colmerauer)_,mary). 5 Compound Terms (A. Colmerauer)Component) Terms Structured data (A. Colmerauer) ) (A. Colmerauer) )

structure-name (A. Colmerauer) attribute, ..., attribute ) Ex) Maker, Age, Price 3 car(A. Colmerauer)ford, 3, 5000) has(A. Colmerauer)joe, car(A. Colmerauer)toyota,5,1000)). has(A. Colmerauer)mick, car(A. Colmerauer)ford,2,2000)). ?- has(A. Colmerauer)mick, car(A. Colmerauer)ford,Age,Price)) Age Price ! Answer: Age=2, Price=2000 6 (A. Colmerauer)Facts, Rules, Questions)

Facts (A. Colmerauer). ) (A. Colmerauer) , , ). female(A. Colmerauer)jane). parent(A. Colmerauer)kim,holly). Rules (A. Colmerauer). ) (A. Colmerauer)head):- (A. Colmerauer)conditions). and(A. Colmerauer),) / or(A. Colmerauer);) greatgrandparent(A. Colmerauer)GGP,GGC) :- parent(A. Colmerauer)GGP,GP), parent(A. Colmerauer)GP,P), parent(A. Colmerauer)P,GGC). 7 Rules Rules ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :parent(Z,Y), ancestor(X,Z).

Rules Rules grandparent(GP,GC) :parent(GP,P), parent(P,GC). greatgrandparent(GGP,GGC) :grandparent(GGP,P), parent(P,GGC). 8 SWI-Prolog Welcome to SWI-Prolog (Multi-threaded, Version 5.4.7) Copyright (c) 1990-2003 University of Amsterdam. SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit http://www.swi-prolog.org for details. For help, use ?- help(Topic). or ?- apropos(Word). ?- Prompting for a query with ?- ,

9 relations.pl parent(A. Colmerauer)kim,holly). /* comment */ parent(A. Colmerauer)margaret,kim). % comment SWI-Prolog parent(A. Colmerauer)margaret,kent). parent(A. Colmerauer)esther,margaret). parent(A. Colmerauer)herbert,margaret). parent(A. Colmerauer)herbert,jean). greatgrandparent(A. Colmerauer)GGP,GGC) :parent(A. Colmerauer)GGP,GP), parent(A. Colmerauer)GP,P), parent(A. Colmerauer)P,GGC). 10 Interpreter SWI-Prolog

?- consult(relations). % relations compiled 0.00 sec, 1,420 bytes Yes ??- ['relations']. % relations compiled 0.00 sec, 1,420 bytes Yes ?- ?- parent(margaret,kent). Yes ?- 11 SWI-Prolog ?- parent(margaret,Child). ?- parent(margaret,Child). Child = kim

. Child = kim ; Child = kent ; Yes No ?- ?- parent(A. Colmerauer)Parent,jean) parent(A. Colmerauer)esther,Child) Enter ; Prolog system , parent(A. Colmerauer)margaret,X), parent(A. Colmerauer)X,holly).

12 Rule SWI-Prolog ?- greatgrandparent(esther,GrateGrandchild). GrateGrandchild = holly Yes ?- greatgrandparent(esther,GreatGrandchild) GGP esther, GGC GreatGrandChild parent(esther,GP), parent(GP,P), parent(P,GreatGrandchild) GP margaret parent(margaret,P), parent(P,GreatGrandchild) P kim parent(kim,GreatGrandchild) GreatGrandchild holly 13 Operators +(A. Colmerauer)1,*(A. Colmerauer)2,3)), 1+ *(A. Colmerauer)2,3), +(A. Colmerauer)1,2*3), (A. Colmerauer)1+(A. Colmerauer)2*3)), 1+2*3 7

?- X = 1+2*3. X = 1+2*3. Yes is term (A. Colmerauer)evaluate) ?- Y=X+2, X=1. ?- X=1, Y is X+2. Y = 1+2 X = 1 X = 1 Y = 3 Yes Yes 14 Lists List notation Term denoted

[] [] [1] .(1,[]) [1,2,3] .(1,.(2,.(3,[]))) [1,parent(X,Y)] .(1,.(parent(X,Y),[])) [1|X] .(1,X) [1,2|X] .(1,.(2,X))) [1,2|[3,4]] same as [1,2,3,4]

[ ] empty list | term Tail [1,2|X] = [1,2,3,4,5]. 1,2 list tail x bind 15 List Predicates append(A. Colmerauer)X,Y,Z) ?- append([1,2],[3,4],Z). Z = [1, 2, 3, 4] ?- append(X,Y,[1,2,3]). X = [] Y = [1, 2, 3] ; Yes X = [1] Y = [2, 3] ;

?- append(X,[3,4],[1,2,3,4]). X = [1, 2] Y = [3] ; X = [1, 2] Yes Lists X = [1, 2, 3] Y = [] ; No member(A. Colmerauer)X,Y) List Y X 16 findall, bagof, setof ?-findall(A. Colmerauer)C,parent(A. Colmerauer)john,C),L). L = [peter,paul,mary] ?-findall(A. Colmerauer)C,parent(A. Colmerauer)P,C),L). L = [peter,paul,mary,davy,dee,dozy]

parent(A. Colmerauer)john,peter). parent(A. Colmerauer)john,paul). parent(A. Colmerauer)john,mary). parent(A. Colmerauer)mick,davy). parent(A. Colmerauer)mick,dee). parent(A. Colmerauer)mick,dozy). ?-bagof(A. Colmerauer)C,parent(A. Colmerauer)P,C),L). P = john L = [peter,paul,mary]; P = mick ?-bagof(A. Colmerauer)C,P^parent(A. Colmerauer)P,C),L). L = [peter,paul,mary,davy,dee,dozy] L = [davy,dee,dozy] setof/3 : bagof/3 sort 17 Problem solving (predicate) Prolog control structure recursion ) partition/4 predicate: partition(A. Colmerauer)L,N,Littles,Bigs)

Step 1. declarative specification % partition(A. Colmerauer)L,N,Littles,Bigs) % list L N Littles , Bigs Step 2. recursion arguments identify (A. Colmerauer) ) partition(A. Colmerauer)[ ],N,[ ],[ ]). partition(A. Colmerauer)[Head|Tail],N,?Littles,?Bigs):/* do something with Head */ partition(A. Colmerauer)Tail,N,Littles,Bigs). 18 Problem solving (predicate) (cont.) Step 3. body partition(A. Colmerauer)[],N,[],[]). partition(A. Colmerauer)[Head|Tail],N,?Littles,?Bigs):Head < N, partition(A. Colmerauer)Tail,N,Littles,Bigs), ?Littles = [Head|Littles],?Bigs = Bigs. partition(A. Colmerauer)[Head|Tail],N,?Littles,?Bigs):Head >= N, partition(A. Colmerauer)Tail,N,Littles,Bigs), ?Littles = Littles,?Bigs = [Head |Bigs].

Step 4. argument partition(A. Colmerauer)[],N,[],[]). partition(A. Colmerauer)[Head|Tail],N,[Head|Littles],Bigs):Head < N, partition(A. Colmerauer)Tail,N,Littles,Bigs). partition(A. Colmerauer)[Head|Tail],N,Littles,[Head|Bigs]):Head >= N, partition(A. Colmerauer)Tail,N,Littles,Bigs). 19 Tom Arnold Nick Nicole Kitty Michael Jack Ann

Julia Mike Sue male, female, father, mother fact , predicate . 1. brother_and_sister(A. Colmerauer)X,Y). %X, Y ? 2. grandparent(A. Colmerauer)X,Y). %X Y (A. Colmerauer) ) ? 3. ancestor(A. Colmerauer)X,Y). %X Y ? 20

Recently Viewed Presentations

  • C H A P T E R 12 Temperature and Heat - Winthrop Chemistry

    C H A P T E R 12 Temperature and Heat - Winthrop Chemistry

    Common Temperature Scales. Fahrenheit scale. Celsius scale. Boiling point . 212. 100 Unknown T. f. T. c. Freezing point. 32. 0
  • QUICK REVIEW: End of the New Deal

    QUICK REVIEW: End of the New Deal

    FDR openly expressed his favor for intervention & gradually give aid to the Allies (ESPECIALLY BRITAIN)…it was the BEST WAY FOR US TO STAY OUT OF WAR!. Cash and Carry " (1939) = ALLIES could buy US arms for war...
  • Presentation Name

    Presentation Name

    2015 - SBE establishes first "cut scores" on Smarter Balanced Assessments for graduation, taking an "equal impact" approach as the state transitions to Common Core and the standard of career and college readiness for all.
  • Foreign Travel on Federal Awards

    Foreign Travel on Federal Awards

    Effective July 1, 2012 international travel on federal awards will no longer need ORS approval. All travel advances and reimbursements will be sent directly to Individual Disbursement Services (Travel Office). This change in the routing process has no affect on...
  • THE FIRST REAL SOLUTION FOR REDUCING YOUR EXPOSURE

    THE FIRST REAL SOLUTION FOR REDUCING YOUR EXPOSURE

    Matrix 2 has a "receiving grid" that absorbs the microwave energy being emitted from the cell phone. These microwaves are then fed into a Tesla-style pancake coil that has been constructed to cross cancel the electromagnetic field.
  • English 1060 England to 1600 English Civilization in
  • COVENANT

    COVENANT

    ETYMOLOGICAL DEFINITION. The Hebrew word for covenant is berit which could mean various things, "Shackle", "Cut". It came to mean a form of binding agreement between people and the god.
  • ControlEdge PLC Microsite Planned - Honeywell

    ControlEdge PLC Microsite Planned - Honeywell

    Honeywell's new PLC is part of the ControlEdge™ family, a next generation family of controllers providing unprecedented connectivity through all levels of process and business operations. The result is optimized operations and maintenance efficiencies that liberate personnel from manual processes.