SlideShare a Scribd company logo
1 of 25
Download to read offline
FµN
FROM DEFINITION TO EXECUTION
20 septembre 2017
Software Craftsmanship Toulouse
&@dplaindoux @fcabestre
1
LIV
E
CO
D
E
LIV
E
CO
D
E
LIV
E
CO
D
E
LIV
E
CO
D
E
LIV
E
CO
D
E
FµN
inside
LANGUAGE DEFINITION
λ-Calculus: variable, abstraction and application
Constant: number and string
De nition: function and constant
Native: function and constant
2
LANGUAGE GRAMMAR
LL(1)?
exp ::= sexp sexp*
sexp ::= { (IDENT+ ->)? exp } -- à la Swift or Kotlin
| ( exp? )
| $ exp -- Infix application
| IDENT
| NUMBER
| STRING
| native STRING
def ::= def IDENT sexp
| exp
s0 ::= def*
3
SYNTAX HIGHLIGHTS
a (b (c d)) ≡ a $ b $ c d In x Application
{ a -> { b -> ... } } ≡ { a b -> ... } Curri ed form
{ a -> f a } ≡ { f _ } Implicit argument
{ f a } ≢ ( f a ) Abstraction vs. Application
{ a -> { b -> f a } } ≢ { { f _ } } Implicit variable scope
4
EXAMPLE :: FACTORIAL
def leq { l r t f in native "leq" } // l <= r ? t : f
def mult { l r -> native "mult" }
def minus { l r -> native "minus" }
def cond { c t f -> c t f () }
def fact { a ->
cond (leq a 1)
{ _ -> 1 }
{ _ -> mult a (fact (minus a 1)) }
}
fact 12
5
EXAMPLE :: FACTORIAL CONT'D
def leq { l r t f -> native "leq" } // l <= r ? t : f
def mult { l r -> native "mult" }
def minus { l r -> native "minus" }
def cond { c t f -> c t f () }
def fact { a ->
cond (leq a 1)
{ 1 }
{ mult a $ fact $ minus a 1 }
}
fact 12
6
EXAMPLE :: DSL
def do { f -> then () f }
def then { v f -> { v c -> c v } $ f v }
def yield { v f -> f v }
do { plus 20 2 }
then { minus _ 1 }
yield { mult _ 2 } // 42
7
ANALYZE
Sequence char → Try Ast
8
(PARSER CHAR TOKEN).CHAIN(PARSER TOKEN AST)
Generic lexer cf.
LL(1) or when k > 1 with try parser i.e. backtrack
Parser Combinators
OCaml Genlex
9
FROM CONCRETE SYNTAX TO ABSTRACT SYNTAX
[_] :: Sequence Token → Ast
[{ a -> b }] = Ast.abstraction(a, [b]))
[a b] = Ast.application([a],[b])
[i] = Ast.ident(i) if i ∈ IDENT
[n] = Ast.constant(n) if n ∈ NUMBER ∪ STRING
[native s] = Ast.native(s)
[( a )] = [a]
[$ a] = [a]
[()] = Ast.constant(unit)
10
TRANSFORM
Ast → AstDB
11
DE BRUIJN INDEX
Invariant with respect to α-conversion
Direct access in an environment
12
DETERMINE DE BRUIJN INDEXES
[_] :: Ast → List String → AstDB
[Ast.constant(c)]e = AstDB.constant(c)
[Ast.native(c)]e = AstDB.native(c)
[Ast.application(a,b)]e = AstDB.application([a]e, [b]e)
[Ast.abstraction(a,b)]e = AstDB.abstraction([b](a::e))
[Ast.ident(n)]e = AstDB.ident(n) if ∀ i / e[i]≠n
[Ast.ident(n)]e = AstDB.variable(i) if ∃ i / e[i]=n
∀ j<i / e[j]≠n
13
INTERPRET
AstDB → Result
14
ADVANCED INTERPRETER
No β-reduction based on term subtitution
Application interpretation is not tail recursive
type Result = NUMBER | STRING | (AstDB,Env)
and Env = List Result
[_] :: AstDB → Env → Result
[AstDB.constant(c)]e = c
[AstDB.variable(i)]e = e[i]
[AstDB.abstraction(b)]e = (b,e)
[AstDB.application(a,b)]e = [c]d::e' when [a]e =* (c,e')
and [b]e =* d
15
COMPILE
AstDB → Objcode
16
ABSTRACT MACHINE
J-L. Krivine's machine
X. Leroy's ZINC abstract machine
17
COMPILATION PROCESS
Standard call-by-value
[_] :: AstDB → Objcode
[AstDB.variable(i)] = Objcode.access(i)
[AstDB.application(a,b)] = [a];[b];Objcode.apply
[AstDB.abstraction(b)] = Objcode.closure([b];Objcode.returns)
[AstDB.ident(n)] = Objcode.ident(n)
[AstDB.constant(c)] = Objcode.constant(c)
[AstDB.native(c)] = Objcode.native(c)
18
EXECUTE
Objcode → Try Result
19
EXECUTION PROCESS :: CORE
type Result = NUMBER | STRING | (Objcode,Env)
and Env = List Result
and Stack = List (Result | Env | Objcode)
[_] :: Objcode → Env → Stack → Result
[Objcode.constant(n);c]e s = [c]e n::s
[Objcode.access(i);c]e s = [c]e e[i]::s
[Objcode.closure(c');c]e s = [c]e (c',e)::s
[Objcode.apply;c]e v::(c',e')::s = [c']v::e' c::e::s
[Objcode.returns;c]e v::c'::e'::s = [c']e' v::s
...
20
EXECUTION PROCESS :: EXTENSION
...
definition :: String -> Result
native :: String -> Env -> Result
[Objcode.ident(n);c]e s = [c]e (definition n)::s
[Objcode.native(n);c]e s = [c]e (native n e)::s
21
REPL
Sequence char → Try Result
22
S => PRINT(ANALYZE(S).MAP(TRANSFORM).MAP.(COMPILE).FLATMAP(EXECUTE))
23
FµN IN A BROWSER
fact <i id='val'>?</i> = <i id='res'>?</i>
<script type="application/mfun">
def compute_then_display { v ->
do { changeDom "val" v }
then { changeDom "res" $ fact v }
yield { () }
}
compute_then_display 100
</script>
24
WHAT'S NEXT?
Call-by-Name
Tail recursive
Type checker
G-Machine, SECD-Machine etc.
25

More Related Content

What's hot (19)

Revision1 C programming
Revision1 C programmingRevision1 C programming
Revision1 C programming
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
 
C questions
C questionsC questions
C questions
 
Lecture 3 c++
Lecture 3 c++Lecture 3 c++
Lecture 3 c++
 
Fp in scala with adts
Fp in scala with adtsFp in scala with adts
Fp in scala with adts
 
Swift the implicit parts
Swift the implicit partsSwift the implicit parts
Swift the implicit parts
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
 
Tipos de funciones
Tipos de funcionesTipos de funciones
Tipos de funciones
 
Strings and pointers
Strings and pointersStrings and pointers
Strings and pointers
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
VERILOG CODE FOR Adder
VERILOG CODE FOR AdderVERILOG CODE FOR Adder
VERILOG CODE FOR Adder
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
Linked list2
Linked list2Linked list2
Linked list2
 
C test
C testC test
C test
 

Similar to Compiling fµn language

Scope Graphs: A fresh look at name binding in programming languages
Scope Graphs: A fresh look at name binding in programming languagesScope Graphs: A fresh look at name binding in programming languages
Scope Graphs: A fresh look at name binding in programming languagesEelco Visser
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with StatixEelco Visser
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryDatabricks
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryDatabricks
 
Fp in scala with adts part 2
Fp in scala with adts part 2Fp in scala with adts part 2
Fp in scala with adts part 2Hang Zhao
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
 
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionCompiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionEelco Visser
 
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsQuark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsJohn De Goes
 
Calculus- Basics
Calculus- BasicsCalculus- Basics
Calculus- BasicsRabin BK
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Languagevsssuresh
 
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate CompilersFunctional Thursday
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)stasimus
 
Indefinite Integration One shot Revision
Indefinite Integration One shot Revision Indefinite Integration One shot Revision
Indefinite Integration One shot Revision CHANDHINIJAYARAMAN
 

Similar to Compiling fµn language (20)

Scope Graphs: A fresh look at name binding in programming languages
Scope Graphs: A fresh look at name binding in programming languagesScope Graphs: A fresh look at name binding in programming languages
Scope Graphs: A fresh look at name binding in programming languages
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love Story
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love Story
 
Fp in scala with adts part 2
Fp in scala with adts part 2Fp in scala with adts part 2
Fp in scala with adts part 2
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
Scala by Luc Duponcheel
Scala by Luc DuponcheelScala by Luc Duponcheel
Scala by Luc Duponcheel
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
Model answer of compilers june spring 2013
Model answer of compilers june spring 2013Model answer of compilers june spring 2013
Model answer of compilers june spring 2013
 
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionCompiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint Resolution
 
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsQuark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
 
Calculus- Basics
Calculus- BasicsCalculus- Basics
Calculus- Basics
 
Compilers Final spring 2013 model answer
 Compilers Final spring 2013 model answer Compilers Final spring 2013 model answer
Compilers Final spring 2013 model answer
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
 
Ch8a
Ch8aCh8a
Ch8a
 
Indefinite Integration One shot Revision
Indefinite Integration One shot Revision Indefinite Integration One shot Revision
Indefinite Integration One shot Revision
 
Syntaxdirected
SyntaxdirectedSyntaxdirected
Syntaxdirected
 

Recently uploaded

Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 

Recently uploaded (20)

Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 

Compiling fµn language

  • 1. FµN FROM DEFINITION TO EXECUTION 20 septembre 2017 Software Craftsmanship Toulouse &@dplaindoux @fcabestre 1 LIV E CO D E LIV E CO D E LIV E CO D E LIV E CO D E LIV E CO D E FµN inside
  • 2. LANGUAGE DEFINITION λ-Calculus: variable, abstraction and application Constant: number and string De nition: function and constant Native: function and constant 2
  • 3. LANGUAGE GRAMMAR LL(1)? exp ::= sexp sexp* sexp ::= { (IDENT+ ->)? exp } -- à la Swift or Kotlin | ( exp? ) | $ exp -- Infix application | IDENT | NUMBER | STRING | native STRING def ::= def IDENT sexp | exp s0 ::= def* 3
  • 4. SYNTAX HIGHLIGHTS a (b (c d)) ≡ a $ b $ c d In x Application { a -> { b -> ... } } ≡ { a b -> ... } Curri ed form { a -> f a } ≡ { f _ } Implicit argument { f a } ≢ ( f a ) Abstraction vs. Application { a -> { b -> f a } } ≢ { { f _ } } Implicit variable scope 4
  • 5. EXAMPLE :: FACTORIAL def leq { l r t f in native "leq" } // l <= r ? t : f def mult { l r -> native "mult" } def minus { l r -> native "minus" } def cond { c t f -> c t f () } def fact { a -> cond (leq a 1) { _ -> 1 } { _ -> mult a (fact (minus a 1)) } } fact 12 5
  • 6. EXAMPLE :: FACTORIAL CONT'D def leq { l r t f -> native "leq" } // l <= r ? t : f def mult { l r -> native "mult" } def minus { l r -> native "minus" } def cond { c t f -> c t f () } def fact { a -> cond (leq a 1) { 1 } { mult a $ fact $ minus a 1 } } fact 12 6
  • 7. EXAMPLE :: DSL def do { f -> then () f } def then { v f -> { v c -> c v } $ f v } def yield { v f -> f v } do { plus 20 2 } then { minus _ 1 } yield { mult _ 2 } // 42 7
  • 9. (PARSER CHAR TOKEN).CHAIN(PARSER TOKEN AST) Generic lexer cf. LL(1) or when k > 1 with try parser i.e. backtrack Parser Combinators OCaml Genlex 9
  • 10. FROM CONCRETE SYNTAX TO ABSTRACT SYNTAX [_] :: Sequence Token → Ast [{ a -> b }] = Ast.abstraction(a, [b])) [a b] = Ast.application([a],[b]) [i] = Ast.ident(i) if i ∈ IDENT [n] = Ast.constant(n) if n ∈ NUMBER ∪ STRING [native s] = Ast.native(s) [( a )] = [a] [$ a] = [a] [()] = Ast.constant(unit) 10
  • 12. DE BRUIJN INDEX Invariant with respect to α-conversion Direct access in an environment 12
  • 13. DETERMINE DE BRUIJN INDEXES [_] :: Ast → List String → AstDB [Ast.constant(c)]e = AstDB.constant(c) [Ast.native(c)]e = AstDB.native(c) [Ast.application(a,b)]e = AstDB.application([a]e, [b]e) [Ast.abstraction(a,b)]e = AstDB.abstraction([b](a::e)) [Ast.ident(n)]e = AstDB.ident(n) if ∀ i / e[i]≠n [Ast.ident(n)]e = AstDB.variable(i) if ∃ i / e[i]=n ∀ j<i / e[j]≠n 13
  • 15. ADVANCED INTERPRETER No β-reduction based on term subtitution Application interpretation is not tail recursive type Result = NUMBER | STRING | (AstDB,Env) and Env = List Result [_] :: AstDB → Env → Result [AstDB.constant(c)]e = c [AstDB.variable(i)]e = e[i] [AstDB.abstraction(b)]e = (b,e) [AstDB.application(a,b)]e = [c]d::e' when [a]e =* (c,e') and [b]e =* d 15
  • 17. ABSTRACT MACHINE J-L. Krivine's machine X. Leroy's ZINC abstract machine 17
  • 18. COMPILATION PROCESS Standard call-by-value [_] :: AstDB → Objcode [AstDB.variable(i)] = Objcode.access(i) [AstDB.application(a,b)] = [a];[b];Objcode.apply [AstDB.abstraction(b)] = Objcode.closure([b];Objcode.returns) [AstDB.ident(n)] = Objcode.ident(n) [AstDB.constant(c)] = Objcode.constant(c) [AstDB.native(c)] = Objcode.native(c) 18
  • 20. EXECUTION PROCESS :: CORE type Result = NUMBER | STRING | (Objcode,Env) and Env = List Result and Stack = List (Result | Env | Objcode) [_] :: Objcode → Env → Stack → Result [Objcode.constant(n);c]e s = [c]e n::s [Objcode.access(i);c]e s = [c]e e[i]::s [Objcode.closure(c');c]e s = [c]e (c',e)::s [Objcode.apply;c]e v::(c',e')::s = [c']v::e' c::e::s [Objcode.returns;c]e v::c'::e'::s = [c']e' v::s ... 20
  • 21. EXECUTION PROCESS :: EXTENSION ... definition :: String -> Result native :: String -> Env -> Result [Objcode.ident(n);c]e s = [c]e (definition n)::s [Objcode.native(n);c]e s = [c]e (native n e)::s 21
  • 22. REPL Sequence char → Try Result 22
  • 24. FµN IN A BROWSER fact <i id='val'>?</i> = <i id='res'>?</i> <script type="application/mfun"> def compute_then_display { v -> do { changeDom "val" v } then { changeDom "res" $ fact v } yield { () } } compute_then_display 100 </script> 24
  • 25. WHAT'S NEXT? Call-by-Name Tail recursive Type checker G-Machine, SECD-Machine etc. 25