COMP 302: Programming Languages and Paradigms
Week 1: Basic Intro to OCaml Prof. Xujie Si
A brief history of OCaml
Copyright By PowCoder代写 加微信 powcoder
A brief history of OCaml
• Meta-language (ML), 1970s
• Designed for developing theorem provers
• Logic for Computable Functions (LCF) theorem-proving project
• Standard ML, 1980s • Caml, 1980s
• Categorical Abstract Machine Language (CAML)
• Caml Light, 1990s
• New implementation of Caml (based on bytecode interpreter) • Highly portable
• Caml Special Light, 1990s
• Improvement of Caml Light, with a native-code compiler
• Objective Caml, 1990s • OCaml, 2011
A brief history of OCaml
• Meta-language (ML), 1970s
• Designed for developing theorem provers
• Logic for Computable Functions (LCF) theorem-proving project
• Standard ML, 1980s • Caml, 1980s
• Categorical Abstract Machine Language (CAML)
• Caml Light, 1990s
• New implementation of Caml (based on bytecode interpreter) • Highly portable
• Caml Special Light, 1990s
• Improvement of Caml Light, with a native-code compiler
• Objective Caml, 1990s • OCaml, 2011
A brief history of OCaml
• Meta-language (ML), 1970s
• Designed for developing theorem provers
• Logic for Computable Functions (LCF) theorem-proving project
• Standard ML, 1980s • Caml, 1980s
• Categorical Abstract Machine Language (CAML)
• Caml Light, 1990s
• New implementation of Caml (based on bytecode interpreter) • Highly portable
• Caml Special Light, 1990s
• Improvement of Caml Light, with a native-code compiler
• Objective Caml, 1990s • OCaml, 2011
A brief history of OCaml
• Meta-language (ML), 1970s
• Designed for developing theorem provers
• Logic for Computable Functions (LCF) theorem-proving project
• Standard ML, 1980s • Caml, 1980s
• Categorical Abstract Machine Language (CAML)
• Caml Light, 1990s
• New implementation of Caml (based on bytecode interpreter) • Highly portable
• Caml Special Light, 1990s
• Improvement of Caml Light, with a native-code compiler
• Objective Caml, 1990s • OCaml, 2011
A brief history of OCaml
• Meta-language (ML), 1970s
• Designed for developing theorem provers
• Logic for Computable Functions (LCF) theorem-proving project
• Standard ML, 1980s • Caml, 1980s
• Categorical Abstract Machine Language (CAML)
• Caml Light, 1990s
• New implementation of Caml (based on bytecode interpreter) • Highly portable
• Caml Special Light, 1990s
• Improvement of Caml Light, with a native-code compiler
• Objective Caml, 1990s • OCaml, 2011
A brief history of OCaml
• Meta-language (ML), 1970s
• Designed for developing theorem provers
• Logic for Computable Functions (LCF) theorem-proving project
• Standard ML, 1980s • Caml, 1980s
• Categorical Abstract Machine Language (CAML)
• Caml Light, 1990s
• New implementation of Caml (based on bytecode interpreter) • Highly portable
• Caml Special Light, 1990s
• Improvement of Caml Light, with a native-code compiler
• Objective Caml, 1990s • OCaml, 2011
A brief history of OCaml
• Meta-language (ML), 1970s
• Designed for developing theorem provers
• Logic for Computable Functions (LCF) theorem-proving project
• Standard ML, 1980s • Caml, 1980s
• Categorical Abstract Machine Language (CAML)
• Caml Light, 1990s
• New implementation of Caml (based on bytecode interpreter) • Highly portable
• Caml Special Light, 1990s
• Improvement of Caml Light, with a native-code compiler
• Objective Caml, 1990s • OCaml, 2011
Web-based Programming Platform for OCaml
Web-based Programming Platform for OCaml
• Any browser can be your IDE (Yes, offline is completely fine!)
• Immediate Feedback on assignments via autograder
• Playground provides direct access to all the code discussed in class and you can directly interact with it
• Toplevel allows you to execute and test your program via the Read- Eval-Print Loop (REPL)
Web-based Programming Platform for OCaml
• Any browser can be your IDE (Yes, offline is completely fine!)
• Immediate Feedback on assignments via autograder
• Playground provides direct access to all the code discussed in class and you can directly interact with it
• Toplevel allows you to execute and test your program via the Read- Eval-Print Loop (REPL)
Web-based Programming Platform for OCaml
• Any browser can be your IDE (Yes, offline is completely fine!)
• Immediate Feedback on assignments via autograder
• Playground provides direct access to all the code discussed in class and you can directly interact with it
• Toplevel allows you to execute and test your program via the Read- Eval-Print Loop (REPL)
Web-based Programming Platform for OCaml
• Any browser can be your IDE (Yes, offline is completely fine!)
• Immediate Feedback on assignments via autograder
• Playground provides direct access to all the code discussed in class and you can directly interact with it
• Toplevel allows you to execute and test your program via the Read- Eval-Print Loop (REPL)
Web-based Programming Platform for OCaml
• Any browser can be your IDE (Yes, offline is completely fine!)
• Immediate Feedback on assignments via autograder
• Playground provides direct access to all the code discussed in class and you can directly interact with it
• Toplevel allows you to execute and test your program via the Read- Eval-Print Loop (REPL)
a compiler from OCaml bytecode programs to JavaScript.
Create a LearnOCaml Account
https://winter2022-comp302.cs.mcgill.ca/
Your 9-digit student ID
You have to remember this new token!!
You will only need this (secret) token later on
What is a variable in functional programming?
• Just a name for a value & cannot “vary”
• Imagine they were variables in math equations
• Conceptually, there is no memory (thus no mutation)
What is a variable in functional programming?
• Just a name for a value & cannot “vary”
• Imagine they were variables in math equations
• Conceptually, there is no memory (thus no mutation)
What is a variable in functional programming?
• Just a name for a value & cannot “vary”
• Imagine they were variables in math equations
• Conceptually, there is no memory (thus no mutation)
Do x and y vary here?
What is a variable in functional programming?
• Just a name for a value & cannot “vary”
• Imagine they were variables in math equations
• Conceptually, there is no memory (thus no mutation)
y=4-x x=4-y
Do x and y vary here?
What is a variable in functional programming?
• Just a name for a value & cannot “vary”
• Imagine they were variables in math equations
• Conceptually, there is no memory (thus no mutation)
y=4-x x=4-y
Do x and y vary here?
Are these two equations equivalent? Should they?
What is a variable in functional programming?
• Just a name for a value & cannot “vary”
• Imagine they were variables in math equations
• Conceptually, there is no memory (thus no mutation)
y=4-x x=4-y
Do x and y vary here?
Are these two equations equivalent? Should they?
However, in imperative programming languages (e.g., C, C++, Java, Python, etc.), the meaning of these two “equations” are quite different …
What is a variable in functional programming?
• Just a name for a value & cannot “vary”
• Imagine they were variables in math equations
• Conceptually, there is no memory (thus no mutation)
y=4-x x=4-y
Do x and y vary here?
Are these two equations equivalent? Should they?
However, in imperative programming languages (e.g., C, C++, Java, Python, etc.), the meaning of these two “equations” are quite different …
(Informal) quiz: What is Lvalue (Rvalue)? Share your answers on Ed!!
What is a value?
What is a value?
• Can you give some examples of values? • 1, 2, 3
• true, false
• Or even simpler: ()
• How about “1 + 2” or “1 < 2”?
• They are expressions (NOT values), which can be evaluated into values. • Are values expressions?
• How about a function? Is it a value or an expression or something else? • Function is also a value! (aka. functions are first-class citizen!)
What is a value?
• Can you give some examples of values? • 1, 2, 3
• true, false
• Or even simpler: ()
• How about “1 + 2” or “1 < 2”?
• They are expressions (NOT values), which can be evaluated into values. • Are values expressions?
• How about a function? Is it a value or an expression or something else? • Function is also a value! (aka. functions are first-class citizen!)
What is a value?
• Can you give some examples of values? • 1, 2, 3
• true, false
• Or even simpler: ()
• How about “1 + 2” or “1 < 2”?
• They are expressions (NOT values), which can be evaluated into values. • Are values expressions?
• How about a function? Is it a value or an expression or something else? • Function is also a value! (aka. functions are first-class citizen!)
What is a value?
• Can you give some examples of values? • 1, 2, 3
• true, false
• Or even simpler: ()
• How about “1 + 2” or “1 < 2”?
• They are expressions (NOT values), which can be evaluated into values. • Are values expressions?
• How about a function? Is it a value or an expression or something else? • Function is also a value! (aka. functions are first-class citizen!)
What is a value?
• Can you give some examples of values? • 1, 2, 3
• true, false
• Or even simpler: ()
• How about “1 + 2” or “1 < 2”?
• They are expressions (NOT values), which can be evaluated into values. • Are values expressions?
• How about a function? Is it a value or an expression or something else? • Function is also a value! (aka. functions are first-class citizen!)
What is a value?
• Can you give some examples of values? • 1, 2, 3
• true, false
• Or even simpler: ()
• How about “1 + 2” or “1 < 2”?
• They are expressions (NOT values), which can be evaluated into values. • Are values expressions?
• How about a function? Is it a value or an expression or something else? • Function is also a value! (aka. functions are first-class citizen!)
What is a value?
• Can you give some examples of values? • 1, 2, 3
• true, false
• Or even simpler: ()
• How about “1 + 2” or “1 < 2”?
• They are expressions (NOT values), which can be evaluated into values. • Are values expressions?
• How about a function? Is it a value or an expression or something else? • Function is also a value! (aka. functions are first-class citizen!)
What is a value?
• Can you give some examples of values? • 1, 2, 3
• true, false
• Or even simpler: ()
• How about “1 + 2” or “1 < 2”?
• They are expressions (NOT values), which can be evaluated into values. • Are values expressions?
• How about a function? Is it a value or an expression or something else? • Function is also a value! (aka. functions are first-class citizen!)
What is a value?
• Can you give some examples of values? • 1, 2, 3
• true, false
• Or even simpler: ()
• How about “1 + 2” or “1 < 2”?
• They are expressions (NOT values), which can be evaluated into values. • Are values expressions?
• How about a function? Is it a value or an expression or something else? • Function is also a value! (aka. functions are first-class citizen!)
What is a value?
• Can you give some examples of values? • 1, 2, 3
• true, false
• Or even simpler: ()
• How about “1 + 2” or “1 < 2”?
• They are expressions (NOT values), which can be evaluated into values. • Are values expressions?
• How about a function? Is it a value or an expression or something else? • Function is also a value! (aka. functions are first-class citizen!)
(informal) Quiz: What is the difference between a statement and an expression? Please share your answer on Ed!!
Variable binding / Naming values
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
DoesXbind to“3+3”or“6”?
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
DoesXbind to“3+3”or“6”?
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Empty x =10
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Empty x =10
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Empty x =10
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Empty x =10
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Empty x =10
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Empty x =10
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Empty x =10
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Empty x =10
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
Out of scope
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Empty x =10
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
Out of scope
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Empty x =10 x =10
Variable binding / Naming values
• Let-expression
• let
• let x = 3 + 3 ;;
• let y = x + x + x ;;
• let x = 10 in x + y (Note: no double semicolon here)
• Nested let-expressions
• let x = 3 in (let y = x + x + x in x + y)
Out of scope
• Overshadowing (binding stack)
• let x = 10 in (let x = x + x in x + x + x) + x
Empty x =10 x =10
What is a type?
What is a type?
• Examples:
• int, float, bool, char, string
• int -> int, string -> int
• OCaml convention: type names are all in lower case
• Types classify expressions (by their values)
• An expression can only have one type, correct or wrong?
• Statically typed vs dynamically typed
• Types can be inferred before executions
• Types are determined at the moment of being executed
What is a type?
• Examples:
• int, float, bool, char, string
• int -> int, string -> int
• OCaml convention: type names are all in lower case
• Types classify expressions (by their values)
• An expression can only have one type, correct or wrong?
• Statically typed vs dynamically typed
• Types can be inferred before executions
• Types are determined at the moment of being executed
What is a type?
• Examples:
• int, float, bool, char, string
• int -> i
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com