basics of programming languages (pre lecture) · basics of programming languages (pre lecture) dr....

34
Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2021 Dantam (Mines CSCI-400) Basics Spring 2021 1 / 34

Upload: others

Post on 13-Oct-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Basics of Programming Languages (Pre Lecture)

Dr. Neil T. Dantam

CSCI-400, Colorado School of Mines

Spring 2021

Dantam (Mines CSCI-400) Basics Spring 2021 1 / 34

Page 2: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Introduction

PL BasicsI Common features across

programming languages:I ExpressionsI VariablesI FunctionsI Etc.

I Mathematical basis:Lambda Calculus

I A simple/clean/elegant abstraction

OutcomesI Know definitions of expressions,

functions, common constructs

I Understand (basic) evaluation andtype checking

I Understand how to define anonymousfunctions (lambdas)

Dantam (Mines CSCI-400) Basics Spring 2021 2 / 34

Page 3: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Outline

Expressions

Functions

Dantam (Mines CSCI-400) Basics Spring 2021 3 / 34

Page 4: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Expressions

Definition (Expression)

An expression is a finite combination of symbols which may bereduced (rewritten) to obtain a simpler expression or a result.

Example

1 + 2︸ ︷︷ ︸expression

3︸︷︷︸also an expression

Rewrite

Dantam (Mines CSCI-400) Basics Spring 2021 4 / 34

Page 5: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Expressions in Imperative and Functional Programming

Use of Expressions

I Primary building block of functional programs

I Imperative languages: some expressions and somestatements/commands (not expressions)

I (Purely) Functional Languages: Everything is an expression

Example (C Expressions)

I 1+2

I 3

I x ? 1 : 2

Counterexample (C Statements)

I i f ( x ) { y =1; } e l s e { y =2; }I whi le ( i ) { i −−;}

Dantam (Mines CSCI-400) Basics Spring 2021 5 / 34

Page 6: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Syntax and Semantics

Expression Syntax

I Is the expression a valid combinationof symbols?

C Syntax

I Valid: (1 + 2) * 3

I Invalid: (1 + 2 * 3

Expression Semantics

I Type-checking rules(static semantics):Produce a type or fail with an error

I Evaluation rules(dynamic semantics):Produce a value, exception, or infiniteloop

We will precisely define syntax and semantics over this course.

Dantam (Mines CSCI-400) Basics Spring 2021 6 / 34

Page 7: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Values

Definition (Value)

A value is an expression that does not need further evaluation.

Illustration

Values Expressions

Example

1 + 2︸ ︷︷ ︸expression

3︸︷︷︸value, expression

Rewrite

Dantam (Mines CSCI-400) Basics Spring 2021 7 / 34

Page 8: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Notation: Evaluation and Typeing

Evaluation

“α evaluates to β”or “α rewrites to β”

α β

Example

I 1 + 2 3

I ¬true false

I ¬¬true ¬false true

Typing

“α has type τ”or “α is in τ”

α : τ or α ∈ τ

Example

I 3 : int

I false : bool

I ¬true : bool

Dantam (Mines CSCI-400) Basics Spring 2021 8 / 34

Page 9: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

If Expressionsaka Conditionals

If Expression

if e1︸︷︷︸test

then e2︸︷︷︸then clause

else e3︸︷︷︸else clause

EvaluationI When e1 true and e2 v2,

if e1 then e2 else e2 v2

I When e1 false and e3 v3,if e1 then e2 else e2 v3

Type checking

When

I e1 : bool

I e2 : τ

I e3 : τ

(if e1 then e2 else e3) : τ

Dantam (Mines CSCI-400) Basics Spring 2021 9 / 34

Page 10: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Type inference and annotation

Type Inference

I We can infer the type of an if expression fromthe types of its constituent expressions.

I type inference is generally possible in functionallanguages and is performed by compilers

I If types cannot be inferred, compilation failswith a type error

I May add annotations to test/diagnose:Replace e with e:t

If Expression Type

(if e1 then e2 else e3) : τ

where

I e1 : bool

I e2 : τ

I e3 : τ

Capability: “If it compiles, it (probably) works.”

Dantam (Mines CSCI-400) Basics Spring 2021 10 / 34

Page 11: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Example: If Evaluation and Type-checking

if true then 1 else 2

1

if

Evaluation

if true then 1 else 2

if (true : bool) then (1 : int) else (2 : int)

if (true : bool) then (1 : int) else (2 : int) : int

subexpressions

if

Type Checking

Dantam (Mines CSCI-400) Basics Spring 2021 11 / 34

Page 12: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Example: Type Errors

if 1 then false else true

if (1 : int) then (false : bool) else (true : bool)

ERROR

if true then 1 else false

if (true : bool) then (1 : int) else (false : bool)

ERROR

Dantam (Mines CSCI-400) Basics Spring 2021 12 / 34

Page 13: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Definitions

Definition (definition)

A definition gives a name to a value.

IllustrationI Definitions are disjoint from expressions

I But syntactically, definitions contain expressions

Values Expressions Definitions

Dantam (Mines CSCI-400) Basics Spring 2021 13 / 34

Page 14: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Variables

DefinitionI A Variable is a symbol representing an expression.

I Bound variables refer to some expression.

I Free variables DO NOT not refer to some expression (areunbound).

I Immutable variables CANNOT be changed (are constant).

I Mutable variables CAN be changed (reassigned).

Dantam (Mines CSCI-400) Basics Spring 2021 14 / 34

Page 15: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Let ExpressionLet Expression

let x︸︷︷︸identifier

← e1︸︷︷︸binding exp.

in e2︸︷︷︸body exp.

EvaluationI Evaluate e1 v1

I Substitute v1 for x in e2, yielding newexpression e ′2

I Implementation: there is a memory locationnamed x that contains/references v

I Evaluate: e ′2 v2

I Result:let x ← e1 in e2 v2

Type checkingI e1 : τ1

I x : τ1

I e2 : τ2

I let x ← e1 in e2 : τ2

Dantam (Mines CSCI-400) Basics Spring 2021 15 / 34

Page 16: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Example: Expression Evaluation

let x ← true in if x then 1 else 2

if true then 1 else 2

1

let

if

let x ← if false then 1 else 2 in x

let x ← 2 in x

2

if

let

Dantam (Mines CSCI-400) Basics Spring 2021 16 / 34

Page 17: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Exercise: Expression Evaluation

if let x ← true in ¬x then false else true

if ¬true then false else true

if false then false else true

true

let

¬

if

if if true then false else true then 1 else 2

if false then 1 else 2

2

if

if

Dantam (Mines CSCI-400) Basics Spring 2021 17 / 34

Page 18: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Example: Simple Type Propagation (0)

let x ← true in if x then 1 else 2

let x ← (true : bool) in if x then (1 : int) else (2 : int)

let (x : bool)← (true : bool) in if (x : bool) then (1 : int) else (2 : int)

let (x : bool)← (true : bool) in (if (x : bool) then (1 : int) else (2 : int) : int)

let (x : bool)← (true : bool) in (if (x : bool) then (1 : int) else (2 : int) : int) : int

literals

let binding

if

let body

Dantam (Mines CSCI-400) Basics Spring 2021 18 / 34

Page 19: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Example: Simple Type Propagation (1)

let x ← 1 in if x then false else true

let x ← (1 : int) in if x then (false : bool) else (true : bool)

let (x : int)← (1 : int) in if (x : int) then (false : bool) else (true : bool)

ERROR!

literals

let binding

if

Dantam (Mines CSCI-400) Basics Spring 2021 19 / 34

Page 20: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Exercise: Simple Type Propagation (0)

if let x ← true in ¬x then false else true

if let x ← (true : bool) in ¬x then (false : bool) else (true : bool)

if let (x : bool)← (true : bool) in ¬(x : bool) then (false : bool) else (true : bool)

if let (x : bool)← (true : bool) in (¬(x : bool) : bool) then (false : bool) else (true : bool)

if (let (x : bool)← (true : bool) in (¬(x : bool) : bool) : bool) then (false : bool) else (true : bool)

if (let (x : bool)← (true : bool) in (¬(x : bool) : bool) : bool) then (false : bool) else (true : bool) : bool

literal

let binding

not (¬)

let body

if

Dantam (Mines CSCI-400) Basics Spring 2021 20 / 34

Page 21: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Expressions

Exercise: Simple Type Propagation (1)

if if true then false else true then 1 else 2

if if (true : bool) then (false : bool) else (true : bool) then (1 : int) else (2 : int)

if (if (true : bool) then (false : bool) else (true : bool) : bool) then (1 : int) else (2 : int)

if (if (true : bool) then (false : bool) else (true : bool) : bool) then (1 : int) else (2 : int) : int

literal

if

if

Dantam (Mines CSCI-400) Basics Spring 2021 21 / 34

Page 22: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Functions

Outline

Expressions

Functions

Dantam (Mines CSCI-400) Basics Spring 2021 22 / 34

Page 23: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Functions

Lambda

Function Definition

λ

formal parameter︷︸︸︷x . α︸︷︷︸

body exp.

I Create an anonymous (unnamed) function.

I Do not evaluate the body until applied (called)

I The function is itself a value.

Function Application

function︷︸︸︷f z︸︷︷︸

actual parameter

I Bind actual parameter z tothe formal parameter of f .

I Evaluate the body of f .

Example

(λa . 1 + a) 2 1 + 2 3a = 2 +

Dantam (Mines CSCI-400) Basics Spring 2021 23 / 34

Page 24: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Functions

Notation Reorientation

Grade-school Algebra: Implicit Multiply

a b

“multiply a times b”

readas

Lambda Calculus: Function Call

a b

“apply function a to parameter b”

readas

Importance of abstract syntax:What operation is specified?

Dantam (Mines CSCI-400) Basics Spring 2021 24 / 34

Page 25: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Functions

Contrasting Notations

Function Definition

Other notation

function(x) {return 1 + x}

Lambda Calculus

λx . 1 + x

Function Application

Other notation

f (2)

Lambda Calculus

f 2

Minimalist.

Dantam (Mines CSCI-400) Basics Spring 2021 25 / 34

Page 26: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Functions

Lambda: Very Important!

I Lambda can performany possible computation!

I Trivial with Lambda:I N-ary functionsI Variable binding (let)I Statements (;)I Named functionsI OOP

I Also possible with Lambda:I RecursionI Conditionals (if)I ListsI StructsI Arithmetic (+, −, etc.)

λ“I’m kind of a big deal.”

Dantam (Mines CSCI-400) Basics Spring 2021 26 / 34

Page 27: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Functions

Function Call AssociativityEvaluate Left to Right

(a0a1a2a3) = ((((a0a1)a2)a3)

call

call

call

a0

fun.

a1arg.

fun.

a2

arg.

fun.

a3

arg.

Dantam (Mines CSCI-400) Basics Spring 2021 27 / 34

Page 28: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Functions

Function Call ParenthesizationUse parenthesis to change evaluation order

(a0 a1) (a2 a3)

call

call

a0

fun.

a1

arg.

fun.

call

a2

fun.

a3

arg.

arg.

Dantam (Mines CSCI-400) Basics Spring 2021 28 / 34

Page 29: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Functions

Functions are values

Bind variables to functions

Pass one function as the parameter to another

Return one function as the result of another function

Dantam (Mines CSCI-400) Basics Spring 2021 29 / 34

Page 30: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Functions

Example: Binding and Passing Functions

Example (Binding)

let f ← (λa . 1 + a) in f 2

(λa . 1 + a) 2

1+2

3

let

a=2

+

Example (Passing)

(λf . f 2) (λa . 1 + a)

(λa . 1 + a) 2

1+2

3

f = λa . 1+a

a=2

+

Notice the similarity?Dantam (Mines CSCI-400) Basics Spring 2021 30 / 34

Page 31: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Functions

Example: Returning Functions

Example (Returning)

(λb . λa . b + a) 1 2

(λa . 1 + a) 2

1+2

3

b = 1

a=2

+

Dantam (Mines CSCI-400) Basics Spring 2021 31 / 34

Page 32: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Functions

Exercise: Lambda Evaluation

I(λx . x) y

y

I(λx . x) (λy . y)

λy . y

I(λx . (λy . xy)) z

λy . zy

I(λx . x x) (λx . x x)

(λx . x x) (λx . x x) (λx . x x) (λx . x x)

. . .

Dantam (Mines CSCI-400) Basics Spring 2021 32 / 34

Page 33: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Functions

Summary

I Everything is an expressionI Evaluate itI Type Check it

I lambda defines anonymous functions(which are also expressions) λ

“I’ll be back.”

Dantam (Mines CSCI-400) Basics Spring 2021 33 / 34

Page 34: Basics of Programming Languages (Pre Lecture) · Basics of Programming Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-400, Colorado School of Mines Spring 2020 Dantam (Mines CSCI-400)BasicsSpring

Functions

References

I Clarkson. Functional Programming in OCaml.I Ch 2 The basics of OCaml

I Pierce. Type Systems and Programming Languages.I Ch 5 The Untyped Lambda-Calculus

I Hennessy. The Semantics of Programming Languages.I Ch 1.1 Preliminaries: IntroductionI Ch 2.1-2.3 Arithmetic Expressions: Concrete Operational Semantics, Evaluation Semantics,

Computation SemanticI Ch 3: A Simple Functional Language

Dantam (Mines CSCI-400) Basics Spring 2021 34 / 34