SlideShare a Scribd company logo
1 of 55
Fun with Twelf

Jake Donham, Skydeck Inc.
What's Twelf?

Twelf is a
dependently-typed
higher-order
logic programming language
and also a
proof assistant

useful for proofs about programming languages
Outline

•   Judgments, inference rules, derivations
•   Proof search, logic programming
•   Representing judgments, dependent types
•   Logic programs as proofs
•   Higher-order syntax, logic programming
What is a natural number?

      N nat       N is a natural number
                  • a judgment
                  • defined by inference rules

   ____________
       z nat      zero is a natural number


       N nat      if N is a natural number
   ____________
      s(N) nat    then N + 1 is a natural number
Inference rules

   ____________
       z nat                axiom


       N nat                premise(s)
   ____________
      s(N) nat              conclusion



        capital N is implicitly for all
Examples

    zero is a nat                    2 is a nat

   ___________                          z nat
       z nat                      _____________
                                      s(z) nat
                                  _____________
                                     s(s(z)) nat



                    derivations
Example?

           s(s(xyzzy)) nat?
Example?

                s(s(xyzzy)) nat?

           nope, there is no derivation

               (can we prove it?)
What is addition?

    ___________
      sum z N N       0+N=N


      sum M N P       if M + N = P
   _____________
    sum s(M) N s(P)   then M + 1 + N = P + 1
Examples

     sum 0 2 2     sum 0 3 3
    __________    __________
      sum 1 2 3     sum 1 3 4
    __________    __________
     sum 2 2 4     sum 2 3 5
                  __________
                   sum 3 3 6
                  __________
                   sum 4 3 7
Recap

 o   judgments are things for which we have proof
 o   judgments are defined by inference rules
 o   derivations are proofs of a judgment
 o   natural numbers and addition as judgments




 next: proof search and logic programming
Searching for proof
                             __________
                              sum z N N

                               sum M N P
                            _____________
                             sum s(M) N s(P)

does sum 2 3 5 hold?
can we find a derivation?
Searching for proof
                                               __________
                                                sum z N N

                                                 sum M N P
                                              _____________
                                               sum s(M) N s(P)

does sum 2 3 5 hold?
can we find a derivation?
which rule(s) must the derivation end with?
Searching for proof
                                               __________
                                                sum z N N

                                                 sum M N P
                                              _____________
                                               sum s(M) N s(P)

does sum 2 3 5 hold?
can we find a derivation?
which rule(s) must the derivation end with?




sum 1 3 4
________
sum 2 3 5 ~ sum s(M) N s(P)     [M = 1, N = 3, P = 4]
Searching for proof
                                               __________
                                                sum z N N

                                                 sum M N P
                                              _____________
                                               sum s(M) N s(P)

does sum 2 3 5 hold?
can we find a derivation?
which rule(s) must the derivation end with?


sum 0 3 3
________
sum 1 3 4 ~ sum s(M) N s(P)     [M = 0, N = 3, P = 3]
________
sum 2 3 5 ~ sum s(M) N s(P)     [M = 1, N = 3, P = 4]
Searching for proof
                                               __________
                                                sum z N N

                                                 sum M N P
                                              _____________
                                               sum s(M) N s(P)

does sum 2 3 5 hold?
can we find a derivation?
which rule(s) must the derivation end with?

________
sum 0 3 3 ~ sum z N N           [N = 3]
________
sum 1 3 4 ~ sum s(M) N s(P)     [M = 0, N = 3, P = 3]
________
sum 2 3 5 ~ sum s(M) N s(P)     [M = 1, N = 3, P = 4]
Proof search with unification
                                  __________
                                   sum z N N

                                    sum M N P
                                 _____________
                                  sum s(M) N s(P)

does sum 2 3 P hold for any P?
Proof search with unification
                                               __________
                                                sum z N N

                                                 sum M N P
                                              _____________
                                               sum s(M) N s(P)

does sum 2 3 P hold for any P?



sum 1 3 P'
_________
sum 2 3 P ~ sum s(M) N s(P')       [M=1, N=3, P=s(P')]

need to invent fresh variable P'
Proof search with unification
                                             __________
                                              sum z N N

                                               sum M N P
                                            _____________
                                             sum s(M) N s(P)

does sum 2 3 P hold for any P?

sum 0 3 P''
_________
sum 1 3 P' ~ sum s(M) N s(P'')   [M=0, N=3, P'=s(P'')]
_________
sum 2 3 P ~ sum s(M) N s(P')     [M=1, N=3, P=s(P')]
Proof search with unification
                                                 __________
                                                  sum z N N

                                                sum M N P
                                             _____________
                                              sum s(M) N s(P)

does sum 2 3 P hold for any P?
_________
sum 0 3 P'' ~ sum z N N          [N = 3 = P'']
_________
sum 1 3 P' ~ sum s(M) N s(P'')   [M=0, N=3, P'=s(P'')]
_________
sum 2 3 P ~ sum s(M) N s(P')     [M=1, N=3, P=s(P')]
Proof search with unification
                                                    __________
                                                     sum z N N

                                                      sum M N P
                                                   _____________
                                                    sum s(M) N s(P)

does sum 2 3 P hold for any P?
_________
sum 0 3 P'' ~ sum z N N             [N = 3 = P'']
_________
sum 1 3 P' ~ sum s(M) N s(P'')      [M=0, N=3, P'=s(P'')]
_________
sum 2 3 P ~ sum s(M) N s(P')        [M=1, N=3, P=s(P')]

now substitute to find P = s(P') = s(s(P'')) = 5
Other modes
               __________
                sum z N N

                 sum M N P
              _____________
               sum s(M) N s(P)

sum 2 N 5?
Other modes
                                                __________
                                                 sum z N N

                                                  sum M N P
                                               _____________
                                                sum s(M) N s(P)

sum 2 N 5?
_________
sum 0 N 3 ~ sum z N N            [N = 3]
_________
sum 1 N 4 ~ sum s(M) N s(P)      [M=0, P=3]
_________
sum 2 N 5 ~ sum s(M) N s(P)       [M=1, P=4]

which args are inputs vs. outputs - mode
Branching, backtracking
                           __________
                            sum z N N

                             sum M N P
                          _____________
                           sum s(M) N s(P)

sum M N 2?
unifies with both rules
Branching, backtracking
                                             __________
                                              sum z N N

                                               sum M N P
                                            _____________
                                             sum s(M) N s(P)

sum M N 2?
unifies with both rules
                         [M=2, N=0]
                         _________
[M=1, N=1]               sum M'' N 0 ~ sum z N N
_________                ________
sum M' N 1 ~ sum z N N | sum M' N 1 ~ sum s(M'') N s(P)

[M=0, N=2]
________                ________
sum M N 2 ~ sum z N N | sum M N 2 ~ sum s(M') N s(P)
Recap

 o   interpret judgments as logic programs
 o   proof search gives rise to computation
 o   unification of terms containing variables
 o   viewing a judgment in different modes
 o   branching, backtracking
 o   sum as a logic program




 next: representing judgments in Twelf
Representing syntax
                          ____________
                              z nat


                              N nat
                          ____________
                             s(N) nat
in OCaml:
type nat = Z | S of nat

in Twelf:
nat : type.
z : nat.
s : nat -> nat.
Representing derivations            __________
                                     sum z N N

                                      sum M N P
                                   _____________
                                    sum s(M) N s(P)
in OCaml:
type sum =
  Sum_z of nat
| Sum_s of nat * nat * nat * sum
Representing derivations               __________
                                        sum z N N

                                         sum M N P
                                      _____________
                                       sum s(M) N s(P)
in OCaml:
type sum =
  Sum_z of nat
| Sum_s of nat * nat * nat * sum

but what derivation does
Sum_s(1, 2, 3, Sum_z 4)

represent? type sum is not adequate
Dependent types                     __________
                                     sum z N N

                                      sum M N P
                                   _____________
                                    sum s(M) N s(P)
in Twelf:
sum : nat -> nat -> nat -> type.
sum_z : {N:nat} sum z N N.
sum_s : {M:nat} {N:nat} {P:nat}
sum M N P -> sum (s M) N (s P).
Dependent types                           __________
                                           sum z N N

                                            sum M N P
                                         _____________
                                          sum s(M) N s(P)
in Twelf:
sum : nat -> nat -> nat -> type.
sum_z : {N:nat} sum z N N.
sum_s : {M:nat} {N:nat} {P:nat}
sum M N P -> sum (s M) N (s P).

•   type sum M N P is indexed by M N P
•   dependent type (depends on terms)
•   indices let us express invariant
•   no inadequate terms
Dependent types                            __________
                                            sum z N N

                                             sum M N P
                                          _____________
                                           sum s(M) N s(P)
in Twelf:
sum : nat -> nat -> nat -> type.
sum_z : {N:nat} sum z N N.
sum_s : {M:nat} {N:nat} {P:nat}
sum M N P -> sum (s M) N (s P).

or (with implicit arguments):

sum_z : sum z N N.
sum_s : sum M N P -> sum (s M) N (s P).
Twelf types as logic programs

in Twelf:

sum : nat -> nat -> nat -> type.
sum_z : sum z N N.
sum_s : sum s(M) N s(P)
<- sum M N P.
in OCaml:

let rec sum : (nat * nat) -> nat = function
    | Z, n -> n
    | S m, n -> S (sum (m, n))
Recap

 o   represent syntax by datatypes
 o   represent derivations by datatypes
 o   want adequate representation
 o   OCaml type system is not rich enough
 o   need dependent types
 o   interpret Twelf datatypes as logic programs




 next: logic programs as proofs
A theorem about addition

 addition is commutative
 M+N=N+M
A theorem about addition

 addition is commutative
 M+N=N+M

 but we have not said:
  o sum is a function
 if sum M N P and sum M N P' then P = P'
  o sum is total
 for all M, N there exists P where sum M N P
  o we haven't even defined equality
A theorem about addition

 addition is commutative
 M+N=N+M
A theorem about addition

 addition is commutative
 M+N=N+M

 if sum M N P then sum N M P
A theorem about addition

 addition is commutative
 M+N=N+M

 if sum M N P then sum N M P

 if you give me a derivation of sum M N P
 I will give you a derivation of sum N M P
A theorem about addition

 addition is commutative
 M+N=N+M

 if sum M N P then sum N M P

 if you give me a derivation of sum M N P
 I will give you a derivation of sum N M P

 function of type sum M N P -> sum N M P
 Curry-Howard correspondence
Addition is commutative

proof outline:
 • prove "right-handed" versions of sum rules
        for all N, sum N z N
        if sum M N P then sum M (s N) (s P)
 • recurse down derivation of sum M N P
 • build it back up using right-handed rules
sum : nat -> nat -> nat -> type.
Addition is commutative                   sum_z : sum z N N.
                                          sum_s : sum M N P ->
                                          sum (s M) N (s P).




sum_z' : {N} sum N z N -> type.
- : sum_z' z sum_z.
- : sum_z' (s N') (sum_s D)
<- sum_z' N' D.

sum_s' : sum M N P -> sum M (s N) (s P) -> type.
- : sum_s' sum_z sum_z.
- : sum_s' (sum_s D1) (sum_s D2)
<- sum_s' D1 D2.
sum : nat -> nat -> nat -> type.
Addition is commutative                   sum_z : sum z N N.
                                          sum_s : sum M N P ->
                                          sum (s M) N (s P).




sum_z' : {N} sum N z N -> type.
sum_s' : sum M N P -> sum M (s N) (s P) -> type.

sum_comm : sum M N P -> sum N M P -> type.
- : sum_comm sum_z D
<- sum_z' _ D.
- : sum_comm (sum_s D1) D3
<- sum_comm D1 D2
<- sum_s' D2 D3.
sum : nat -> nat -> nat -> type.
Totality                                     sum_z : sum z N N.
                                             sum_s : sum M N P ->
                                             sum (s M) N (s P).




only a proof if function is total - succeeds on all inputs
totality = coverage + termination

sum_comm : sum M N P -> sum N M P -> type.
%mode sum_comm +D1 -D2.

- : sum_comm sum_z ...
- : sum_comm (sum_s D1) D3
<- sum_comm D1 D2
...
%total (D1) (sum_comm D1 D2).
Recap

 o   programs are proofs
 o   functions from derivations to derivations
 o   need adequacy of representation
 o   need totality
 o   write proofs as Twelf logic programs
 o   we proved that sum is commutative




 next: proofs about programming languages
Programming languages

a tiny programming language:

  N nat                          E1 exp E2 exp
________                       ______________
nat(N) exp                     let x = E1 in E2 exp

E1 exp E2 exp
___________                    x is a bound variable in E2
 E1 + E2 exp
Representing PLs
                                                   N nat
                                                 ________
                                                 nat(N) exp

                                               E1 exp E2 exp
                                               ___________
                                                 E1 + E2 exp
in OCaml:
                                               E1 exp E2 exp
type exp =                                   ______________
| Var of string                              let x = E1 in E2 exp

| Nat of nat
| Plus of exp * exp
| Let of exp * string * exp

• no alpha-equivalence; choice of name matters
• must implement scope, substitution manually
• inadequate: what does Var "x" w/o Let represent?
Representing PLs
                                                          N nat
                                                        ________
                                                        nat(N) exp

                                                      E1 exp E2 exp
                                                      ___________
                                                        E1 + E2 exp
another try in OCaml:
                                                      E1 exp E2 exp
type exp =                                          ______________
| Nat of nat                                        let x = E1 in E2 exp

| Plus of exp * exp
| Let of exp * (exp -> exp)

 • body of let is function that does substitution:
let x = 1 in x + 2 == Let (Nat 1, (fun x -> Plus (x, Nat 2))
 • unbound var inadequacy goes away
 • functions that branch on arg, or raise exception?
Representing PLs
                                                             N nat
                                                           ________
                                                           nat(N) exp

                                                        E1 exp E2 exp
                                                        ___________
                                                          E1 + E2 exp
in Twelf:
                                                        E1 exp E2 exp
exp : type.                                           ______________
nat : nat -> exp.                                     let x = E1 in E2 exp

plus : exp -> exp -> exp.
let : exp -> (exp -> exp) -> exp.

 • body of let is function that does substitution:
let x = 1 in x + 2 == (let (nat 1) ([x] plus x (nat 2)))
 • Twelf functions are very weak, just templates
 • adequate, alpha-equivalence
Higher-order logic programming

count variables uses - e.g. count (let x = 1 in x + x) 2

count : exp -> nat -> type.

count_nat : count (nat _) z.

count_plus : count (plus E1 E2) C
<- count E1 C1
<- count E2 C2
<- sum C1 C2 C.
Higher-order logic programming

count : exp -> nat -> type.

count_let : count (let E1 E2) C
<- count E1 C1
<- ({x:exp}{d:count x (s z)} count (E2 x) C2)
<- sum C1 C2 C.
 • { } indicates scoped axioms (just for enclosed goal)
 • x is a fresh variable
 • substitute E2's bound variable with x
 • when we find x in E2 it gets a count of 1
Recap

•   programming languages have scope and binding
•   want a convenient way to work with it
•   higher-order syntax representation
•   higher-order logic programming
•   only works because Twelf functions are weak
Twelf

this approach scales to realistic programming lanaguages:
semantics and type safety proof for Standard ML:
 Lee, Crary, Harper
 Toward a Mechanized Metatheory of Standard ML
 http://www.cs.cmu.edu/~rwh/papers/tslf/full.pdf

formalized x86 arch. and type-safe assembly language:
 Crary, Sarkar
 Foundational Certified Code in a Metalogical Framework
 http://www.cs.cmu.edu/~crary/papers/2005/mafcc.pdf
Pointers

 Pfenning, Logic Progamming course notes
 http://www.cs.cmu.edu/~fp/courses/lp/

 Pfenning, Computation and Deduction course notes
 http://www.cs.cmu.edu/~fp/courses/comp-ded/
Thanks for listening



questions?

More Related Content

What's hot

A common fixed point theorems in menger space using occationally weakly compa...
A common fixed point theorems in menger space using occationally weakly compa...A common fixed point theorems in menger space using occationally weakly compa...
A common fixed point theorems in menger space using occationally weakly compa...
Alexander Decker
 
Day 10 examples u1w14
Day 10 examples u1w14Day 10 examples u1w14
Day 10 examples u1w14
jchartiersjsd
 
Interval Notation
Interval NotationInterval Notation
Interval Notation
MarkBredin
 
Mathematics Mid Year Form 4 Paper 1 Mathematics
Mathematics Mid Year Form 4 Paper 1 MathematicsMathematics Mid Year Form 4 Paper 1 Mathematics
Mathematics Mid Year Form 4 Paper 1 Mathematics
sue sha
 
91 sequences
91 sequences91 sequences
91 sequences
math126
 
Set theory solutions
Set theory solutionsSet theory solutions
Set theory solutions
Garden City
 

What's hot (20)

A common fixed point theorems in menger space using occationally weakly compa...
A common fixed point theorems in menger space using occationally weakly compa...A common fixed point theorems in menger space using occationally weakly compa...
A common fixed point theorems in menger space using occationally weakly compa...
 
On similarity of fuzzy triangles
On similarity of fuzzy trianglesOn similarity of fuzzy triangles
On similarity of fuzzy triangles
 
Zeros of p(x)
Zeros of p(x)Zeros of p(x)
Zeros of p(x)
 
Number theory
Number theoryNumber theory
Number theory
 
Day 10 examples u1w14
Day 10 examples u1w14Day 10 examples u1w14
Day 10 examples u1w14
 
Interval Notation
Interval NotationInterval Notation
Interval Notation
 
Task 4
Task 4Task 4
Task 4
 
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program CorrectnessCMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
CMSC 56 | Lecture 12: Recursive Definition & Algorithms, and Program Correctness
 
Blackbox task 2
Blackbox task 2Blackbox task 2
Blackbox task 2
 
CMSC 56 | Lecture 9: Functions Representations
CMSC 56 | Lecture 9: Functions RepresentationsCMSC 56 | Lecture 9: Functions Representations
CMSC 56 | Lecture 9: Functions Representations
 
Math task 3
Math task 3Math task 3
Math task 3
 
Mathematics Mid Year Form 4 Paper 1 Mathematics
Mathematics Mid Year Form 4 Paper 1 MathematicsMathematics Mid Year Form 4 Paper 1 Mathematics
Mathematics Mid Year Form 4 Paper 1 Mathematics
 
Set theory - worksheet
Set theory - worksheetSet theory - worksheet
Set theory - worksheet
 
Sets
SetsSets
Sets
 
Discrete-Chapter 01 Sets
Discrete-Chapter 01 SetsDiscrete-Chapter 01 Sets
Discrete-Chapter 01 Sets
 
2 5 zeros of poly fn
2 5 zeros of poly fn2 5 zeros of poly fn
2 5 zeros of poly fn
 
91 sequences
91 sequences91 sequences
91 sequences
 
Set theory solutions
Set theory solutionsSet theory solutions
Set theory solutions
 
Epanaliptiko pros spiros_giannakaros_2021
Epanaliptiko pros spiros_giannakaros_2021Epanaliptiko pros spiros_giannakaros_2021
Epanaliptiko pros spiros_giannakaros_2021
 
Polynomial function
Polynomial functionPolynomial function
Polynomial function
 

Viewers also liked

Viewers also liked (13)

Quale percorso formativo per un Lean Manager?
Quale percorso formativo per un Lean Manager?Quale percorso formativo per un Lean Manager?
Quale percorso formativo per un Lean Manager?
 
Leadership and Culture in Leadership Excellence_November 2016 Issue
Leadership and Culture in Leadership Excellence_November 2016 IssueLeadership and Culture in Leadership Excellence_November 2016 Issue
Leadership and Culture in Leadership Excellence_November 2016 Issue
 
Vortioxetina (Infografia)
Vortioxetina (Infografia)Vortioxetina (Infografia)
Vortioxetina (Infografia)
 
dược lý
dược  lýdược  lý
dược lý
 
FXCM sanction AMF contre Forex Capital Markets Ltd du 26/10/2015
FXCM sanction AMF contre Forex Capital Markets Ltd du 26/10/2015FXCM sanction AMF contre Forex Capital Markets Ltd du 26/10/2015
FXCM sanction AMF contre Forex Capital Markets Ltd du 26/10/2015
 
Beneficial Ownership Transparency, State Revenue, and EITI
Beneficial Ownership Transparency, State Revenue, and EITIBeneficial Ownership Transparency, State Revenue, and EITI
Beneficial Ownership Transparency, State Revenue, and EITI
 
E7. violencia, miedo y condicion
E7. violencia, miedo y condicionE7. violencia, miedo y condicion
E7. violencia, miedo y condicion
 
Js talks lubo
Js talks luboJs talks lubo
Js talks lubo
 
Actividad con pancartas y pintadas - Aprende español callejeando por Madrid
Actividad con pancartas y pintadas - Aprende español callejeando por MadridActividad con pancartas y pintadas - Aprende español callejeando por Madrid
Actividad con pancartas y pintadas - Aprende español callejeando por Madrid
 
Case Study: IoT industry applied to the production of Peruvian native potatoes
Case Study: IoT industry applied to the production of Peruvian native potatoesCase Study: IoT industry applied to the production of Peruvian native potatoes
Case Study: IoT industry applied to the production of Peruvian native potatoes
 
Nervous system
Nervous systemNervous system
Nervous system
 
Southwest Airlines Uses Automation to Accelerate and Ensure SOX Compliance
Southwest Airlines Uses Automation to Accelerate and Ensure SOX ComplianceSouthwest Airlines Uses Automation to Accelerate and Ensure SOX Compliance
Southwest Airlines Uses Automation to Accelerate and Ensure SOX Compliance
 
Matteo meucci Software Security - Napoli 10112016
Matteo meucci   Software Security - Napoli 10112016Matteo meucci   Software Security - Napoli 10112016
Matteo meucci Software Security - Napoli 10112016
 

Similar to Fun With Twelf (6)

Ch08
Ch08Ch08
Ch08
 
Per4 induction
Per4 inductionPer4 induction
Per4 induction
 
Ch7
Ch7Ch7
Ch7
 
Sol80
Sol80Sol80
Sol80
 
Sol80
Sol80Sol80
Sol80
 
3.-SEQUENCES-AND-SERIES-THEORY.hhsssspdf
3.-SEQUENCES-AND-SERIES-THEORY.hhsssspdf3.-SEQUENCES-AND-SERIES-THEORY.hhsssspdf
3.-SEQUENCES-AND-SERIES-THEORY.hhsssspdf
 

Recently uploaded

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Fun With Twelf

  • 1. Fun with Twelf Jake Donham, Skydeck Inc.
  • 2. What's Twelf? Twelf is a dependently-typed higher-order logic programming language and also a proof assistant useful for proofs about programming languages
  • 3. Outline • Judgments, inference rules, derivations • Proof search, logic programming • Representing judgments, dependent types • Logic programs as proofs • Higher-order syntax, logic programming
  • 4. What is a natural number? N nat N is a natural number • a judgment • defined by inference rules ____________ z nat zero is a natural number N nat if N is a natural number ____________ s(N) nat then N + 1 is a natural number
  • 5. Inference rules ____________ z nat axiom N nat premise(s) ____________ s(N) nat conclusion capital N is implicitly for all
  • 6. Examples zero is a nat 2 is a nat ___________ z nat z nat _____________ s(z) nat _____________ s(s(z)) nat derivations
  • 7. Example? s(s(xyzzy)) nat?
  • 8. Example? s(s(xyzzy)) nat? nope, there is no derivation (can we prove it?)
  • 9. What is addition? ___________ sum z N N 0+N=N sum M N P if M + N = P _____________ sum s(M) N s(P) then M + 1 + N = P + 1
  • 10. Examples sum 0 2 2 sum 0 3 3 __________ __________ sum 1 2 3 sum 1 3 4 __________ __________ sum 2 2 4 sum 2 3 5 __________ sum 3 3 6 __________ sum 4 3 7
  • 11. Recap o judgments are things for which we have proof o judgments are defined by inference rules o derivations are proofs of a judgment o natural numbers and addition as judgments next: proof search and logic programming
  • 12. Searching for proof __________ sum z N N sum M N P _____________ sum s(M) N s(P) does sum 2 3 5 hold? can we find a derivation?
  • 13. Searching for proof __________ sum z N N sum M N P _____________ sum s(M) N s(P) does sum 2 3 5 hold? can we find a derivation? which rule(s) must the derivation end with?
  • 14. Searching for proof __________ sum z N N sum M N P _____________ sum s(M) N s(P) does sum 2 3 5 hold? can we find a derivation? which rule(s) must the derivation end with? sum 1 3 4 ________ sum 2 3 5 ~ sum s(M) N s(P) [M = 1, N = 3, P = 4]
  • 15. Searching for proof __________ sum z N N sum M N P _____________ sum s(M) N s(P) does sum 2 3 5 hold? can we find a derivation? which rule(s) must the derivation end with? sum 0 3 3 ________ sum 1 3 4 ~ sum s(M) N s(P) [M = 0, N = 3, P = 3] ________ sum 2 3 5 ~ sum s(M) N s(P) [M = 1, N = 3, P = 4]
  • 16. Searching for proof __________ sum z N N sum M N P _____________ sum s(M) N s(P) does sum 2 3 5 hold? can we find a derivation? which rule(s) must the derivation end with? ________ sum 0 3 3 ~ sum z N N [N = 3] ________ sum 1 3 4 ~ sum s(M) N s(P) [M = 0, N = 3, P = 3] ________ sum 2 3 5 ~ sum s(M) N s(P) [M = 1, N = 3, P = 4]
  • 17. Proof search with unification __________ sum z N N sum M N P _____________ sum s(M) N s(P) does sum 2 3 P hold for any P?
  • 18. Proof search with unification __________ sum z N N sum M N P _____________ sum s(M) N s(P) does sum 2 3 P hold for any P? sum 1 3 P' _________ sum 2 3 P ~ sum s(M) N s(P') [M=1, N=3, P=s(P')] need to invent fresh variable P'
  • 19. Proof search with unification __________ sum z N N sum M N P _____________ sum s(M) N s(P) does sum 2 3 P hold for any P? sum 0 3 P'' _________ sum 1 3 P' ~ sum s(M) N s(P'') [M=0, N=3, P'=s(P'')] _________ sum 2 3 P ~ sum s(M) N s(P') [M=1, N=3, P=s(P')]
  • 20. Proof search with unification __________ sum z N N sum M N P _____________ sum s(M) N s(P) does sum 2 3 P hold for any P? _________ sum 0 3 P'' ~ sum z N N [N = 3 = P''] _________ sum 1 3 P' ~ sum s(M) N s(P'') [M=0, N=3, P'=s(P'')] _________ sum 2 3 P ~ sum s(M) N s(P') [M=1, N=3, P=s(P')]
  • 21. Proof search with unification __________ sum z N N sum M N P _____________ sum s(M) N s(P) does sum 2 3 P hold for any P? _________ sum 0 3 P'' ~ sum z N N [N = 3 = P''] _________ sum 1 3 P' ~ sum s(M) N s(P'') [M=0, N=3, P'=s(P'')] _________ sum 2 3 P ~ sum s(M) N s(P') [M=1, N=3, P=s(P')] now substitute to find P = s(P') = s(s(P'')) = 5
  • 22. Other modes __________ sum z N N sum M N P _____________ sum s(M) N s(P) sum 2 N 5?
  • 23. Other modes __________ sum z N N sum M N P _____________ sum s(M) N s(P) sum 2 N 5? _________ sum 0 N 3 ~ sum z N N [N = 3] _________ sum 1 N 4 ~ sum s(M) N s(P) [M=0, P=3] _________ sum 2 N 5 ~ sum s(M) N s(P) [M=1, P=4] which args are inputs vs. outputs - mode
  • 24. Branching, backtracking __________ sum z N N sum M N P _____________ sum s(M) N s(P) sum M N 2? unifies with both rules
  • 25. Branching, backtracking __________ sum z N N sum M N P _____________ sum s(M) N s(P) sum M N 2? unifies with both rules [M=2, N=0] _________ [M=1, N=1] sum M'' N 0 ~ sum z N N _________ ________ sum M' N 1 ~ sum z N N | sum M' N 1 ~ sum s(M'') N s(P) [M=0, N=2] ________ ________ sum M N 2 ~ sum z N N | sum M N 2 ~ sum s(M') N s(P)
  • 26. Recap o interpret judgments as logic programs o proof search gives rise to computation o unification of terms containing variables o viewing a judgment in different modes o branching, backtracking o sum as a logic program next: representing judgments in Twelf
  • 27. Representing syntax ____________ z nat N nat ____________ s(N) nat in OCaml: type nat = Z | S of nat in Twelf: nat : type. z : nat. s : nat -> nat.
  • 28. Representing derivations __________ sum z N N sum M N P _____________ sum s(M) N s(P) in OCaml: type sum = Sum_z of nat | Sum_s of nat * nat * nat * sum
  • 29. Representing derivations __________ sum z N N sum M N P _____________ sum s(M) N s(P) in OCaml: type sum = Sum_z of nat | Sum_s of nat * nat * nat * sum but what derivation does Sum_s(1, 2, 3, Sum_z 4) represent? type sum is not adequate
  • 30. Dependent types __________ sum z N N sum M N P _____________ sum s(M) N s(P) in Twelf: sum : nat -> nat -> nat -> type. sum_z : {N:nat} sum z N N. sum_s : {M:nat} {N:nat} {P:nat} sum M N P -> sum (s M) N (s P).
  • 31. Dependent types __________ sum z N N sum M N P _____________ sum s(M) N s(P) in Twelf: sum : nat -> nat -> nat -> type. sum_z : {N:nat} sum z N N. sum_s : {M:nat} {N:nat} {P:nat} sum M N P -> sum (s M) N (s P). • type sum M N P is indexed by M N P • dependent type (depends on terms) • indices let us express invariant • no inadequate terms
  • 32. Dependent types __________ sum z N N sum M N P _____________ sum s(M) N s(P) in Twelf: sum : nat -> nat -> nat -> type. sum_z : {N:nat} sum z N N. sum_s : {M:nat} {N:nat} {P:nat} sum M N P -> sum (s M) N (s P). or (with implicit arguments): sum_z : sum z N N. sum_s : sum M N P -> sum (s M) N (s P).
  • 33. Twelf types as logic programs in Twelf: sum : nat -> nat -> nat -> type. sum_z : sum z N N. sum_s : sum s(M) N s(P) <- sum M N P. in OCaml: let rec sum : (nat * nat) -> nat = function | Z, n -> n | S m, n -> S (sum (m, n))
  • 34. Recap o represent syntax by datatypes o represent derivations by datatypes o want adequate representation o OCaml type system is not rich enough o need dependent types o interpret Twelf datatypes as logic programs next: logic programs as proofs
  • 35. A theorem about addition addition is commutative M+N=N+M
  • 36. A theorem about addition addition is commutative M+N=N+M but we have not said: o sum is a function if sum M N P and sum M N P' then P = P' o sum is total for all M, N there exists P where sum M N P o we haven't even defined equality
  • 37. A theorem about addition addition is commutative M+N=N+M
  • 38. A theorem about addition addition is commutative M+N=N+M if sum M N P then sum N M P
  • 39. A theorem about addition addition is commutative M+N=N+M if sum M N P then sum N M P if you give me a derivation of sum M N P I will give you a derivation of sum N M P
  • 40. A theorem about addition addition is commutative M+N=N+M if sum M N P then sum N M P if you give me a derivation of sum M N P I will give you a derivation of sum N M P function of type sum M N P -> sum N M P Curry-Howard correspondence
  • 41. Addition is commutative proof outline: • prove "right-handed" versions of sum rules for all N, sum N z N if sum M N P then sum M (s N) (s P) • recurse down derivation of sum M N P • build it back up using right-handed rules
  • 42. sum : nat -> nat -> nat -> type. Addition is commutative sum_z : sum z N N. sum_s : sum M N P -> sum (s M) N (s P). sum_z' : {N} sum N z N -> type. - : sum_z' z sum_z. - : sum_z' (s N') (sum_s D) <- sum_z' N' D. sum_s' : sum M N P -> sum M (s N) (s P) -> type. - : sum_s' sum_z sum_z. - : sum_s' (sum_s D1) (sum_s D2) <- sum_s' D1 D2.
  • 43. sum : nat -> nat -> nat -> type. Addition is commutative sum_z : sum z N N. sum_s : sum M N P -> sum (s M) N (s P). sum_z' : {N} sum N z N -> type. sum_s' : sum M N P -> sum M (s N) (s P) -> type. sum_comm : sum M N P -> sum N M P -> type. - : sum_comm sum_z D <- sum_z' _ D. - : sum_comm (sum_s D1) D3 <- sum_comm D1 D2 <- sum_s' D2 D3.
  • 44. sum : nat -> nat -> nat -> type. Totality sum_z : sum z N N. sum_s : sum M N P -> sum (s M) N (s P). only a proof if function is total - succeeds on all inputs totality = coverage + termination sum_comm : sum M N P -> sum N M P -> type. %mode sum_comm +D1 -D2. - : sum_comm sum_z ... - : sum_comm (sum_s D1) D3 <- sum_comm D1 D2 ... %total (D1) (sum_comm D1 D2).
  • 45. Recap o programs are proofs o functions from derivations to derivations o need adequacy of representation o need totality o write proofs as Twelf logic programs o we proved that sum is commutative next: proofs about programming languages
  • 46. Programming languages a tiny programming language: N nat E1 exp E2 exp ________ ______________ nat(N) exp let x = E1 in E2 exp E1 exp E2 exp ___________ x is a bound variable in E2 E1 + E2 exp
  • 47. Representing PLs N nat ________ nat(N) exp E1 exp E2 exp ___________ E1 + E2 exp in OCaml: E1 exp E2 exp type exp = ______________ | Var of string let x = E1 in E2 exp | Nat of nat | Plus of exp * exp | Let of exp * string * exp • no alpha-equivalence; choice of name matters • must implement scope, substitution manually • inadequate: what does Var "x" w/o Let represent?
  • 48. Representing PLs N nat ________ nat(N) exp E1 exp E2 exp ___________ E1 + E2 exp another try in OCaml: E1 exp E2 exp type exp = ______________ | Nat of nat let x = E1 in E2 exp | Plus of exp * exp | Let of exp * (exp -> exp) • body of let is function that does substitution: let x = 1 in x + 2 == Let (Nat 1, (fun x -> Plus (x, Nat 2)) • unbound var inadequacy goes away • functions that branch on arg, or raise exception?
  • 49. Representing PLs N nat ________ nat(N) exp E1 exp E2 exp ___________ E1 + E2 exp in Twelf: E1 exp E2 exp exp : type. ______________ nat : nat -> exp. let x = E1 in E2 exp plus : exp -> exp -> exp. let : exp -> (exp -> exp) -> exp. • body of let is function that does substitution: let x = 1 in x + 2 == (let (nat 1) ([x] plus x (nat 2))) • Twelf functions are very weak, just templates • adequate, alpha-equivalence
  • 50. Higher-order logic programming count variables uses - e.g. count (let x = 1 in x + x) 2 count : exp -> nat -> type. count_nat : count (nat _) z. count_plus : count (plus E1 E2) C <- count E1 C1 <- count E2 C2 <- sum C1 C2 C.
  • 51. Higher-order logic programming count : exp -> nat -> type. count_let : count (let E1 E2) C <- count E1 C1 <- ({x:exp}{d:count x (s z)} count (E2 x) C2) <- sum C1 C2 C. • { } indicates scoped axioms (just for enclosed goal) • x is a fresh variable • substitute E2's bound variable with x • when we find x in E2 it gets a count of 1
  • 52. Recap • programming languages have scope and binding • want a convenient way to work with it • higher-order syntax representation • higher-order logic programming • only works because Twelf functions are weak
  • 53. Twelf this approach scales to realistic programming lanaguages: semantics and type safety proof for Standard ML: Lee, Crary, Harper Toward a Mechanized Metatheory of Standard ML http://www.cs.cmu.edu/~rwh/papers/tslf/full.pdf formalized x86 arch. and type-safe assembly language: Crary, Sarkar Foundational Certified Code in a Metalogical Framework http://www.cs.cmu.edu/~crary/papers/2005/mafcc.pdf
  • 54. Pointers Pfenning, Logic Progamming course notes http://www.cs.cmu.edu/~fp/courses/lp/ Pfenning, Computation and Deduction course notes http://www.cs.cmu.edu/~fp/courses/comp-ded/