Download presentation
Presentation is loading. Please wait.
Published byCameron Garrett Modified over 8 years ago
1
1 ML fun x -> e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors let f x = e variables These slides were originally created by Prof. Sungwoo Park at POSTECH.
2
2 What lies in the core of ML? fun x -> e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors let f x = e variables ?
3
3 Candidates? booleans and if/then/else integers lists variables functions and function applications datatypes patterns structures functors...
4
4 Core of ML fun x -> e e 1 e 2 x
5
5 calculus
6
-Calculus Kangwon National University 임현승 Programming Languages
7
7 Outline Brief history Abstract syntax of the -calculus Operational semantics of the -calculus Substitutions Programming in the -calculus
8
Liebniz’s Ideal Gottfried Wilhelm von Leibniz (1646-1716) 8 1)Create a “universal language” in which all possible problems can be stated. 2)Find a decision method to solve all the problem stated in the universal language.
9
9 Can one solve all problems formulated in the universal language? Need a formalization of the notion of “decidable” or “computable”
10
Two Models of Computation -Calculus (Church 1936) Turing machine (Turing 1937) 10
11
Calculus (1936) Alonzo Church (1903-1995) 11 Minimalist Three expression types: x [variables] x.e [anonymous function] e 1 e 2 [function application] Foundations of functional languages: Lisp, ML, Haskell, etc.
12
Turing Machine (1937) Alan Turing (1912-1954) 12 A hypothetical device that manipulates symbols on a strip of tape according to a table of rules The first accepted definition of a general-purpose computer Foundations of imperative languages: Java, C/C++, C#, Fortran, Pascal, assembler languages, etc.
13
13 calculus is Turing-complete fun x -> e e 1 e 2 x Turing machine calculus =
14
Everywhere Not only in FPLs… C++ 11 C# Java 8 Javascript PHP Python Scala Apple Swift 14
15
15 Outline Brief history V Abstract syntax of the -calculus Operational semantics of the -calculus Substitutions Programming in the -calculus
16
16 Syntax for a Programming Language Concrete syntax program = string of characters specifies rules for parsing. –operator precedence –associativity –keywords,... 1 + 2 * 3 1 + (2 * 3) 1 + (2 * (3)) Abstract syntax abstracts away from details of parsing. focuses on the high-level structure of programs. suitable for studying the semantics
17
17 x –variable –z, s, t, f, arg, accum,... x. e – -abstraction – x = formal argument, e = body – fun x -> e e 1 e 2 –application –left-associative (as in OCaml): e 1 e 2 e 3 = (e 1 e 2 ) e 3 e 1 e 2 e 3 e 1 (e 2 e 3 ) Abstract Syntax of the -Calculus
18
18 Examples
19
19 Outline Brief history V Abstract syntax of the -calculus V Operational semantics of the -calculus Substitutions Programming in the -calculus
20
20 Semantics of Languages Answers "what is the meaning of a given program?" –ML has a formal semantics. –What about C? Three styles –denotational semantics –axiomatic semantics –operational semantics The 1990s saw the renaissance of operational semantics.
21
21 Operational Semantics Specifies how to transform a program into a value via a sequence of operations Program Value P2P2 operation PnPn... let rec fac = function 1 -> 1 | n -> n * fac (n - 1) in fac 4 24
22
22 Operational Semantics of -Calculus Specifies how to transform an expression into a value via a sequence of reductions Expr Value E2E2 reduction EnEn...
23
23 Values and Reductions
24
24 Reductions redex = reducible expression : -reduction
25
25 _____ = Redex
26
26 _____ = Redex
27
27 Reduction Not Unique So we need a reduction strategy.
28
28 Reduction Not Unique So we need a reduction strategy.
29
29 Call-by-name Call-by-value
30
30 Call-by-name Call-by-value
31
31
32
32
33
33 Call-by-name Call-by-value Used in Haskell Lazy or non-strict functional languages The implementation uses call-by-need. Superb! Used in OCaml Eager or strict functional languages Superb! (fn x => 0)
34
34 Outline Brief history V Abstract syntax of the -calculus V Operational semantics of the -calculus V Substitutions Programming in the -calculus
35
35 Values and Reductions redex = reducible expression : -reduction
36
36 Call-by-name Call-by-value
37
37 [e' / x] e Informally "substitute e' for every occurrence of x in e." Examples
38
38 Easy Cases First
39
39 Two Remaining Cases
40
40 First (stupid) attempt Second attempt But wait:
41
OCaml Analogy 41 let f = fun x -> fun x -> x ? fun x -> fun y -> y ≡ Example f 3 4 → ([3/x](fun x -> x)) 4 ≡ (fun x -> x) 4 → 4
42
42 Names of bound variables do not matter. Hence –for a fresh variable y, Bound Variables
43
43 One Remaining Case
44
44 A Naive Attempt An anomaly: something for y
45
45 Free Variables Variables that are bound nowhere FV(e) = set of free variables in e
46
46 Free Variables Remain Free From the point of view of an outside observer, a free variable remains free until it is explicitly replaced. outside observer ? variable capture
47
47 Capture-Avoiding Substitution What happens if –the free variable y is captured and becomes a bound variable. –To an outside observer, it suddenly disappears!
48
48 Substitution Completed
49
49 We have to rename bound variables as necessary. Capture-Avoiding Substitution in Action
50
50 Renaming bound variables when necessary Okay because the names of bound variables do not matter. Examples
51
51
52
52 Last case of substitution
53
53 Outline Brief history V Abstract syntax of the -calculus V Operational semantics of the -calculus V Substitutions V Programming in the -calculus
54
54 A boolean value –"Give me two options and I will choose one for you!" Syntactic sugar Booleans
55
55 Examples Under the call-by-name strategy,
56
56 Logical Operators
57
57 Natural Numbers A natural number n –has the capability to repeat a given process n times. –"Give me a function f and I will return f n = f o f... f o f "
58
58 Church Numerals
59
59 Addition Key observation:
60
60 Multiplication Key observation: Alternatively
61
61 Recursive Functions in -Calculus Plan of attack 1.Assume a recursive function construct 2.Rewrite it as an expression in the -calculus i.e., show that it is syntactic sugar. fun f x. e
62
62 Plan of attack 1.Assume a recursive function construct 2.Rewrite it as an expression in the -calculus i.e., show that it is syntactic sugar. Example: Factorial
63
63 fac to FAC
64
64 FAC produces a new function from f
65
65 Now we only need to find a fixed point of:
66
66 Fixed Point Fixed point of function f V such that V = f (V) Ex. f (x) = 2 - x fixed point of f = 1 1 = f (1)
67
67 Magic Revealed Fixed point combinator / Y combinator (call-by-value) fix F returns a fixed point of F.
68
68 Now we only need to find a fixed point of: Now we only need to compute:
69
69 Answer
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.