Formal Models of Computation Part II The Logic Model

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 5: Control Structures II (Repetition)
Artificial Intelligence: Natural Language and Prolog
Advanced Piloting Cruise Plot.
Copyright © 2003 Pearson Education, Inc. Slide 1.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
Combining Like Terms. Only combine terms that are exactly the same!! Whats the same mean? –If numbers have a variable, then you can combine only ones.
February 21, 2002 Simplex Method Continued
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
You will need some paper!
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Peer-to-peer and agent-based computing Basic Theory of Agency (Contd)
Intelligent Architectures for Electronic Commerce A (Brief) Prolog Tutorial.
CS4026 Formal Models of Computation Running Haskell Programs – power.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 8 – Search and conclusions.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
Formal Models of Computation Part III Computability & Complexity
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Lecture 15 Linked Lists part 2
Introduction A function is called higher-order if it takes a function as an argument or returns a function as a result. twice :: (a  a)  a  a twice.
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Chapter 17 Linked Lists.
Data Structures ADT List
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Modern Programming Languages, 2nd ed.
ABC Technology Project
ML Lists.1 Standard ML Lists. ML Lists.2 Lists A list is a finite sequence of elements. [3,5,9] ["a", "list" ] [] Elements may appear more than once [3,4]
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-
Basic Permutations and Combinations
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
Squares and Square Root WALK. Solve each problem REVIEW:
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Chapter 5 Test Review Sections 5-1 through 5-4.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Addition 1’s to 20.
25 seconds left…...
Week 1.
We will resume in: 25 Minutes.
TASK: Skill Development A proportional relationship is a set of equivalent ratios. Equivalent ratios have equal values using different numbers. Creating.
How Cells Obtain Energy from Food
Introduction to Recursion and Recursive Algorithms
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
Formal Models of Computation Part II The Logic Model
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Functional Programming Lecture 3 - Lists Muffy Calder.
Haskell Chapter 4.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

Formal Models of Computation Part II The Logic Model Lecture 5 – Lists

formal models of computation Lists Data structure for non-numeric programming. A sequence of any number of items. The items can be any Prolog term (including nested lists!). Useful when we don’t know in advance how many items we are going to have. Flexible and generic. In Prolog, a list is represented as a sequence of terms, separated by commas and within “[…]”: [123,hello,Var1,p(a,X),[1,2,3]] formal models of computation

Types in lists: Haskell vs Prolog In Haskell, for every type t there is a type [t] for lists whose elements are of type t In Haskell, every list must have type [t] (for some t), and hence all its elements must be of the same type So you can’t for instance have a list, some of whose elements have type int and some of whose elements have type [int], e.g. [1,[2,3],4] Prolog places no restrictions on what elements of lists can be Less restrictive, but you lose the advantages of type checking formal models of computation

formal models of computation Lists Lists are ordinary Prolog terms. The notation […] is just a shorthand: [1,2,3]  .(1,.(2,.(3,[]))) Lists are thus represented internally as trees: N.B.: the last element is always the empty list [] . 1 2 3 [] formal models of computation

formal models of computation Lists Lists are recursively defined as: The empty list []; or A term followed by a list. Lists can enter queries: . 1 2 3 [] ?- List = [1,2,3]. List = [1,2,3] yes ?- formal models of computation

formal models of computation Lists (Cont’d) Prolog provides a notation for manipulating lists. The notation is [H|T], a shortand for .(H,T) H stands for the first element of the list. T stands for the remaining elements of the list and must be a list. We can build and decompose lists with [H|T]: ?- List = [1,2,3], List = [A|B]. A = 1, B = [2,3], List = [1,2,3] ?- List = [A|B], A = 1, B = [2,3]. formal models of computation

formal models of computation Lists (Cont’d) The [H|T] notation allows for variations: [E1,E2|T] (a list with at least 2 elements) [1|[2,3|T]] (a list [1,2,3|T]) [H|T] is also a term, hence a tree: ?- List = [A|B], A = 1, B = [2,3]. A = 1, B = [2,3], List = [1,2,3] List A B . 1 [] 2 . 3 formal models of computation

Haskell vs Prolog list notations [1,2,3] [] x:xs [X|Xs] or .(X,Xs) 1:2:xs [1,2|Xs] formal models of computation

formal models of computation Operations on Lists We can access elements from the front of a list We can add elements to the front of a list: ?- L = [2,3], NewL = [1|L] L [] 2 . 3 1 . NewL formal models of computation

Programming with Lists Because of their recursive definition, lists are naturally manipulated with recursive programs. Let’s revisit our first loop program: Enable it to build a list with values typed in An argument is required to return the value of the list built loop(L):- % a loop to create a list L L = [X|Xs], % List L has form [X|Xs] read(X), % X is read from the standard Input (keyboard) X \= end, % if input different from “end” then loop(Xs). % build the tail recursively loop([]). % if input is “end”, then List is empty. ?- loop(MyList). |: a. |: b. |: end. MyList = [a,b] formal models of computation

Programming with Lists (Cont’d) Its execution: Xs1 X1 . loop(L):- L = [X|Xs], read(X), X \= end, loop(Xs). loop([]). loop(L1):- L1 = [X1|Xs1], read(X1), X1 \= end, loop(Xs1). L1 a Xs2 X2 . loop(L2):- L2 = [X2|Xs2], read(X2), X2 \= end, loop(Xs2). L2 ?- loop(MyList). |: a. |: b. ?- loop(MyList). ?- loop(MyList). |: a. ?- loop(MyList). |: a. |: b. |: end. b loop(L3):- L3 = [X3|Xs3], read(X3), X3 \= end, loop(Xs3). loop(L3):- L3 = [X3|Xs3], read(X3), X3 \= end, loop(Xs3). Xs3 X3 . L3 end formal models of computation

Programming with Lists (Cont’d) Its execution: Xs1 X1 . loop(L):- L = [X|Xs], read(X), X \= end, loop(Xs). loop([]). loop(L1):- L1 = [X1|Xs1], read(X1), X1 \= end, loop(Xs1). L1 a Xs2 X2 . loop(L2):- L2 = [X2|Xs2], read(X2), X2 \= end, loop(Xs2). L2 ?- loop(MyList). |: a. |: b. |: end. ?- loop(MyList). |: a. |: b. |: end. MyList = [a,b] b [] loop([]). formal models of computation

Programming with Lists (Cont’d) To check if an element appears in a list: Check if element is the head; Otherwise, check if element appears in tail This formulation is guaranteed to stop because lists are finite (!) and the process breaks list apart; In Prolog (first formulation): member(X,List):- % X is a member of List List = [Y|Ys], % if List is of form [Y|Ys] X = Y. % and X is equal to Y member(X,List):- % otherwise (if not as above) List = [Y|Ys], % break the list apart member(X,Ys). % and check if X appears in Ys formal models of computation

Programming with Lists (Cont’d) Second formulation: member(X,[Y|Ys]):- % X is a member of [Y|Ys] X = Y. % if X is equal to Y member(X,[Y|Ys]):- % otherwise (if not as above) member(X,Ys). % check if X appears in Ys formal models of computation

Programming with Lists (Cont’d) Third (final) formulation: General point: Uses of the built-in predicate = can almost always be replaced more elegantly with pattern-matching in the head of a clause. member(X,[X|_]). % X is member if it is head of list member(X,[_|Ys]):- % otherwise (if not as above) member(X,Ys). % check if X appears in Ys formal models of computation

formal models of computation Prolog vs Haskell Cf Haskell member :: a -> [a] -> Bool member x [] = False member x (y:ys) = (x==y) || (member x ys) Note: [] case member(X,[X|_]). member(X,[_|Ys]) :- member(X,Ys). formal models of computation

Programming with Lists (Cont’d) Let’s run the previous program: member(X,[X|_]). member(X,[_|Ys]):- member(X,Ys). The query member(3,[1,2,3])does not match the 1st clause!! Prolog matches member(3,[1,2,3])with head of 2nd clause: member(X1,[_|Ys1]):- member(X1,Ys1) We thus get the values for the variables: {X1/3, Ys1/[2,3]} Prolog now tries to prove member(X1,Ys1) that is member(3,[2,3]) ?- member(3,[1,2,3]). yes ?- ?- member(3,[1,2,3]). The goal member(3,[2,3])does not match the 1st clause!! Prolog matches member(3,[2,3])with head of 2nd clause: member(X2,[_|Ys2]):- member(X2,Ys2) We thus get the values for the variables: {X2/3, Ys2/[3]} Prolog now tries to prove member(X2,Ys2) that is member(3,[3]) The query member(3,[3])matches the 1st clause!! formal models of computation

Programming with Lists (Cont’d) Member program can be run in two ways: To check if an element occurs in a list; To obtain the elements of a list, one at a time It depends on the query – the program is exactly the same!! To obtain the elements of a list, we query: member(Elem,[1,2,3]) We then get in Elem, the different elements ?- member(Elem,[1,2,3]). Elem = 1 ? ; Elem = 2 ? ; Elem = 3 ? ?- member(Elem,[1,2,3]). Elem = 1 ? ; Elem = 2 ? ; Elem = 3 ? ; no ?- ?- member(Elem,[1,2,3]). Elem = 1 ? ; Elem = 2 ? ?- member(Elem,[1,2,3]). Elem = 1 ? ?- member(Elem,[1,2,3]). Can you trace this query? formal models of computation

Programming with Lists (Cont’d) To concatenate two lists Xs and Ys: Create a 3rd list Zs by copying all elements of Xs, When we get to the end of Xs, put Ys as the tail of Zs Diagrammatically: Ys [] a . b Xs 1 2 Zs . . 1 2 formal models of computation

Programming with Lists (Cont’d) We need a predicate with 3 arguments: 1st argument is the list Xs 2nd argument is the list Ys 3rd argument is the concatenation Xs.Ys (in this order) The first list Xs will control the loop As the first list Xs is traversed, its elements are copied onto the 3rd argument (the concatenation). When we get an empty list, we stop and make Ys the value of the 3rd argument formal models of computation

Programming with Lists (Cont’d) First attempt: A more compact format: append([],Ys,Zs):- % if Xs is empty, Zs = Ys. % then Zs is Ys append([X|Xs],Ys,[Z|Zs]):- % otherwise X = Z, % copy X onto 3rd argument append(Xs,Ys,Zs). % carry on recursively append([],Ys,Ys). % if Xs empty, then Zs is Ys append([X|Xs],Ys,[X|Zs]):- % else copy X onto 3rd arg. append(Xs,Ys,Zs). % carry on recursively formal models of computation

Programming with Lists (Cont’d) Execution: aliasing: Res/[X1|Zs1] append([],Ys,Ys). append([X|Xs],Ys,[X|Zs]):- append(Xs,Ys,Zs). append([X1|Xs1],Ys1,[X1|Zs1]):- append(Xs1,Ys1,Zs1). {X1/1,Xs1/[2],Ys1/[a,b]} append([X1|Xs1],Ys1,[X1|Zs1]):- append(Xs1,Ys1,Zs1). append([X1|Xs1],Ys1,[X1|Zs1]):- append(Xs1,Ys1,Zs1). {X1/1,Xs1/[2],Ys1/[a,b]} aliasing: Zs1/[X2|Zs2] ?- append([1,2],[a,b],Res). Res = [1,2,a,b] ? yes ?- ?- append([1,2],[a,b],Res). append([X2|Xs2],Ys2,[X2|Zs2]):- append(Xs2,Ys2,Zs2). append([X2|Xs2],Ys2,[X2|Zs2]):- append(Xs2,Ys2,Zs2). {X2/2,Xs2/[],Ys2/[a,b]} append([X2|Xs2],Ys2,[X2|Zs2]):- append(Xs2,Ys2,Zs2). {X2/2,Xs2/[],Ys2/[a,b]} aliasing: Zs2/Ys3 append([],Ys3,Ys3). {Ys3/[a,b]} append([],Ys3,Ys3). formal models of computation

Programming with Lists (Cont’d) 1 Res Zs1 X1 . 2 Zs2 X2 a b [] Execution: append([],Ys,Ys). append([X|Xs],Ys,[X|Zs]):- append(Xs,Ys,Zs). append([X1|Xs1],Ys1,[X1|Zs1]):- append(Xs1,Ys1,Zs1). {X1/1,Xs1/[2],Ys1/[a,b]} ?- append([1,2],[a,b],Res). Res = [1,2,a,b] ? yes ?- append([X2|Xs2],Ys2,[X2|Zs2]):- append(Xs2,Ys2,Zs2). {X2/2,Xs2/[],Ys2/[a,b]} append([],Ys3,Ys3). {Ys3/[a,b]} formal models of computation

“Reversible definitions” in Prolog As with “member”, “append” can be used in different ways, e.g. ?- append([a,b,c],[d],X). ?- append(X,[a,b,l,e],[e,a,t,a,b,l,e]). ?- append([e,a,t],X,[e,a,t,a,b,l,e]). ?- append(X,Y,[e,a,t,a,b,l,e]). Each of these would require a different function/method definition in Haskell/Java… formal models of computation