Académique Documents
Professionnel Documents
Culture Documents
-----------------------------------------PROGRAMMING LANGUAGE
def: a *programming language* is
a language that is capable of expressing all
computations.
Idea of Turing Machine what is meant to be
capable?
def: a *general-purpose language*
is one that is not tailored to some
particular application area.
Not designed for any particular domain
Examples: Java, C, C++, Python
def: a *special-purpose language*
is a language that is designed to
support some particular application.
is designed for some special domain purpose
Examples: SQL, Javascript, Visual Basic, MATLAB,
COBOL, Fortan, bash, sh, zsh
B. objectives
What are your objectives for this course?
How do you want this course to help you in 5
years?
-----------------------------------------COURSE OBJECTIVES
Quickly Learn new programming languages
Master them, and effectively use them
Evaluate what combination of features as models
are best for a problem
-----------------------------------------C. outcomes
-----------------------------------------LEARNING OUTCOMES
ClassDecl
- distributed or relational programming
InterfaceDecl
|
|
|
We will use "real" languages to help you
_______v___________ MethodDecl
evaluate them and learn about learning them
| Static Checker
|
|
V
|___________________|
i.e. symbol table
|
annotated AST
|
_______v___________
| Code Generation |
|___________________|
|
v
object code
-----------------------------------------a. lexical grammar
-----------------------------------------LEXICAL (REGULAR) GRAMMAR EXAMPLE
nonterminals ::= can be
<Id> ::= <NonUpper_Letter> { <NameChar> }
<VIdent> ::= <UpperLetter_> { <NameChar> }
<NonUpper_Letter> ::= a | b | c | d | ... | z
<UpperLetter_> ::= _ | A | B | C | D | ... | Z
<NameChar> ::= <NonUpper_Letter> | <UpperLetter_>
| <Digit> | + | <Digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
-----------------------------------------What are examples of <Id>s? a
What are examples of <VIdent>s? A
-----------------------------------------CONVENTIONS AND EXTENSIONS (EBNF)
defining the symbols
{ <X> } ::= <Empty> | <X> { <X> }
<X>*
::= <Empty> | <X> <X>*
<X>+
::= <X> | <X> <X>+
[ <X> ] ::= <Empty> | <X>
[ <X> ]... ::= <Empty> | <X> [ <X> ]...
<Empty> ::=
-----------------------------------------b. syntax and context-free grammars
-----------------------------------------BNF (CONTEXT-FREE GRAMMAR) EXAMPLE
<Term> ::= <Term> <TypedCIdent>
|
<TypedCIdent> <Term>
|
<Term> <Term>
|
<TypedId> \ <Term>
|
'[' ']'
|
'[' TermList ']'
|
'[' TermList '|' Term ']'
|
<TypedCIdent>
|
<TypedVar>
|
<TypedId>
|
( <Term> )
<TermList> ::= <Term>
|
<Term> , <TermList>
<TypedCIdent> ::= <Id>
|
<Id> : <Type>
|
( <TypedCIdent> )
<TypedId> ::= <Id>
|
<Id> : <Type>
|
( <TypedId> )
<TypedVar> ::= <VIdent>
|
<VIdent> : <Type>
|
( <TypedVar> )
-----------------------------------------What are examples of <Term>s?
II. Example: learning lambda Prolog
A. motivation
-----------------------------------------MOTIVATIONS FOR LOGIC PROGRAMMING
% rule
good X :- logical X.
For logic programming use:
-----------------------------------------data
syntax
a. compound terms in LambdaProlog
- named entities
<Id>,<Numeric Literal
logic: anything that is not found true, by
- unknown entities <VIdent> i.e. X,Y,Z
default it is false
- relationships
<Id>,<Term>
-----------------------------------------(term is application of one onto another i.e. sorted COMPOUND TERMS
lst)
Items are treated symbolically
(tree Subtree1 Root Subtree2)
-----------------------------------------(((tree Subtree1) Root) Subtree2)
D. means of combination
describe relations between things
%lists:
-----------------------------------------a::b::nil
CONNECTIVES FOR EFFECTIVE SPECIFICATION
(sentence (np (n ron))
Effectively describe L2?
(vp (v gave)
unique things specified by that
(np (art a) (n paper))
- (ordered L2) and (permutation L1 L2)
(compls (prep to)
unique outcome: , (coma)
(np (n sue)))))
event type.
person type.
thing type.
verb type.
type
type
type
type
type
type
type
type
event1 event.
paper thing.
sue person.
ron person.
gave verb.
event2 event.
football thing.
swen person.
-----------------------------------------b. queries
what is the result of the query:
recipient event1 Who. ?
how would you add the fact that in event2,
sue gave the football to swen?
4. Lists
-----------------------------------------% LISTS
sig list.
% this is all built in to lambda Prolog
% but this shows how it would be done.
-----------------------------------------a. declarative
b. Procedural reading (programming language)
6. tracing queries
7. variation on addtoend
-----------------------------------------% VARIATION ON ADDTOEND
module addtoend2.
% signature file
sig lisp_lists.
kind lisp_list
sig addtoend2.
type equals T -> T -> o.
type addtoend (list T) -> T -> (list T).
I. Introduction to Haskell
A. What's interesting about Haskell?
-----------------------------------------FUNCTIONAL PROGRAMMING
type the_empty_list
(lisp_list T).
type cons T -> (lisp_list T) -> (lisp_list T).
t ~ element, lisp_list ~ list, ~ return the - Models computations as expressions
list
- All changing arguments passed explicitly
type to_list
(lisp_list T) -> (list T) ->
(no implicit global state)
o.
- Functions as data allows better abstraction
end
HASKELL FEATURES COMMON TO FUNCTIONAL LANGUAGES
% the module
module lisp_lists.
- Data is treated abstractly (as terms)[1,2,3]
- Powerful pattern matching
% an inductive definition
- All functions take just one argument
to_list the_empty_list nil.
(use tuples to group multiple arguments as one)
to_list (cons Head Tail) (Head::PL) :- to_list Tail - Powerful library
PL.
- Order of definition doesn't matter
end
-----------------------------------------UNIQUE FEATURES OF HASKELL
what are these type decls like in Haskell?
a. example using lists
- Type-based separation of
-----------------------------------------computations with effects from
sig addtoend1.
pure expressions
- I/O actions are data
type addtoend
- Type classes for static overloading
(list T) -> T -> (list T) -> o.
- Lazy evaluation is the default
module addtoend1.
only if its needed will the value be returned
addtoend
- Type expressions resemble values
-----------------------------------------What's the difference between an expression and a
How would we write this?
statement?
5. Interpretations of clauses
expressions have values but statements dont
-----------------------------------------INTERPRETATIONS OF CLAUSES
-----------------------------------------B. Haskell platform mechanics
(h X) :- (b1 Y), (b2 Z), (b3 W).
1. getting in and out
declarative interpretation:
-----------------------------------------procedural interpretation:
( Fact.hs,
II.Fact> fact
<interactive>:3:1:
No instance for (Show (Integer -> Integer))
arising from a use of `print'
Possible fix:
add an instance declaration for (Show (Integer
-> Integer))
In a stmt of an interactive GHCi command: print
it
*Fact>
*Fact> :type fact
fact :: fact :: (Enum a, Num a) => a -> a
*Fact> :set +t
*Fact> fact 4
24
it :: Integer
*Fact> fact 100
9332621544394415268169923885626670049071596826438162
1468592963895217599993229915
6089414639761565182862536979208272237582511852109168
64000000000000000000000000
it :: Integer
*Fact> :edit Fact.hs
*Fact> :reload
*Fact> :type fact
fact :: Integer -> Integer
*Fact> fact 100
9332621544394415268169923885626670049...
Fact>
Fact> :q
Leaving GHCi
-----------------------------------------$ ghci Fact.hs
[...]
[1 of 1] Compiling Fact
( Fact.hs,
interpreted )
Ok, modules loaded: Fact.
*Fact> fact 8
40320
let x = 3
y = 4
in x + y
==>
let {x = 3
;y = 4
}in x + y
Example:
CASE MATTERS
varids:
( Hello.lhs,
conids:
$ ghcii Hello.lhs
start with A-Z
...
Ok, modules loaded: Main.
Examples:
Prelude Main> :type main
Stack, Rational, Typ'_3
main :: IO ()
-----------------------------------------Prelude Main> main
OPERATORS
Hello, world!
Prelude Main> :quit
Examples:
-----------------------------------------+, -, !!, ++, ==, /=
III. lexical matters in Haskell (Thompson 3.7, Davie :, :#
2.14, appendix C.2-3)
A. important and unusual lexical conventions
Notes:
-----------------------------------------IMPORTANT AND UNUSUAL LEXICAL CONVENTIONS
1. Drawn from: !#$%&*+./<=>?@\^|-~:
and unicode symbols and punctuation
<varid> ::= <small> { <idchar> }
<conid> ::= <large> { <idchar> }
2. An operator is a constructor
<idchar> ::= <small> | <large> | <digit> | '
if it starts with :
<small> ::= <ascSmall> | _
<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
3. All operators are infix
<large> ::= <ascLarge>
except unary negation (-)
<ascLarge> ::= A | B | C | D | ... | Z
<ascSmall> ::= a | b | c | d | ... | z
4. Any identifer can be made into
an infix operator using backquotes
- Case matters
3 `div` 4 or mod
identifiers: fact, x y
constructors: Rational, Stack
5. An infix operator can be used as
an identifier when put in parentheses
- Layout (white space) matters:
(if omit braces and semicolons,
(+) 3 4 == 3+4
following "where", "let", "do", and "of")
-----------------------------------------IV. Built-in types of Haskell
A. Fundamental classification of objects
(x,y,z) = (1,2,3) in x
(x,y,z) = (1,2,3) in z
(_,y,_) = (1,2,3) in y
(a:as) = 1:2:3:[] in a
(a:as) = [1,2,3] in as
Write
The general rule (Report, section 3.10) is that
addPairs :: [(Integer, Integer)] -> [Integer]
[e1,e2 .. e3] gives a list of values starting at e1,
which takes a list of pairs
with increment e2-e1 of values not greater than e3
and produces a list of their sums
(assuming e2-e1 is positive)
addPairs[(3,4),(0,5)] == [7,5]
addPairs lst = [i+j | (i,j) lst]
for infinite lists
addPairs :: [(Integer,Integer)] -> [Integer]
[n ..] = [n, n+1, n+2, ...]
addPairs x = [ fst n + snd n | n<-x ]
e.g., [1 .. ] = [1, 2, 3, 4, ...]
-----------------------------------------[7 .. ] = [7, 8, ...]
iv. nested maps
-----------------------------------------[n, p ..] = [n, n + (p-n), ...]
NESTED MAPS
e.g., [2, 4 ..] = [2, 4, 6, 8, ...]
[(a,b) | a <- ex, b <- [1,2]] =
[n .. m] = [n,n+1,n+2,...,m-1,m]
[(2,1), (2,2), (4,1), (4,2), (7,1), (7,2),
(3,1), (3,2), (2,1), (2,2)]
[n, p .. m] = [n, p, p+(p-n), p+2(p-n),...]
last element is less than or equal to m:
-----------------------------------------observe:
2. built-in functions, standard Prelude (go quickly
[1,2..10] -> [1,3,5,7,9]
or skip)
[0,2..10] -> [0,2,4,6,8,10]
a. zip and unzip
b. ++, !!, concat, length, head, last, tail, init
-----------------------------------------c. take, drop
b. .. for infinite lists!
B. explicit recursions.
-----------------------------------------------------------------------------------
reverse_iter([1,2,3], [])
reverse_iter([2,3], [1])
reverse_iter([3], [2,1])
reverse_iter([], [3,2,1])
[3,2,1]
-----------------------------------------|
|
What are some examples we have seen of need
|\\\\\\\\\\\\\\\\\\\\\\\\\ <____
to use tail recursion?
SP (stack pointer)
D. memory management
1. overall memory layout
How do we represent the environment and the
--------------------store in a single
does saved part save information about caller
address space on a conventional computer?
or callee?
-----------------------------------------How would this be used in making a call?
How would this be used in a return?
3. Last call optimization
OVERALL MEMORY LAYOUT
-----------------------------------------LAST CALL OPTIMIZATION
|---------------|
Scheme first language to take advantage of this
|
Code
|
feature.
|---------------|
def: a language implements the
| Static Data
|
*last call optimization* if
| (constants)
|
it reuses the current AR for the last
|---------------|
call made during a function's execution.
| run-time
|
~ Environment
| stack of ARs |
length' ls = length'iter ls 0
|
|
length'iter [] acc = acc
|
|
|
length'iter (_:xs) acc = length'iter xs (acc+1)
|
v
|
\\\\\\\\\\\\\\\\|
Tracing this:
|\\\\\\\\\\\\\\\|
|
(heap)
|
|---------------|
~ Store
length' 1:2:3:[]
= length'iter (1:2:3:[]) 0
length' :: [a] -> Int
length' ls = length'iter ls 0
length'iter :: [a] -> Int -> Int
length'iter [] acc = acc
length'iter (_:xs) acc = length'iter xs (acc+1)
-----------------------------------------What is it useful for?
Does the semantics already to this?
Do C, C++, and Java require this optimization?
Not sure but involves the JIT = Just-In-Time feature
What does that say about using recursion in these
languages?
VI. data-driven recursion (Thompson 14, Davie
sections 3.2, 4.4)
A. data declaration in Haskell
1. example: the natural numbers
-----------------------------------------DATA-DRIVEN RECURSION or
FOLLOW THE GRAMMAR!
Definition of natural numbers:
> data Nat = Zero | Succ Nat deriving Eq
To define a function f :: Nat -> t
define recursively by:
f Zero =
...
-- basis
f (Succ n) = ... -- inductive case
Examples:
> toInteger :: Nat -> Integer
toInteger (Succ (Succ Zero)) == 2
toInteger (Succ Zero) == 1
toInteger Zero == 0
> plus :: Nat -> Nat -> Nat
plus
plus
plus
==