The course will be based off the textbook LACI by Professor Ragde
Racket knowledge is a prerequisite of this course
Here are some grammar rules:
Each of these rules has a conclusions that is drawn from some premises.
Such rules are called inference rules
We can abbreviate by using a horizontal line where the top is the premise and below is the conclusion
, ,
We can chain inferences to form a tree such that many premises lead to eventually one conclusion.
The formulas we study will use logical connectives. They resemble arithmetic expressions.
we also use propsitional variables, which represent statements or atomic propositions
(assertions that must be true or false). Together with the logical connectives they make the language of propositional logic
can be represented with
Here is the grammar of propositional formulas we will use for now (will change as we go).
T = X
| (T ∧ T)
| (T ∨ T)
| (¬ T)
| (T → T)
For formulas, the priority goes, Negation -> conjuction/disjunction (left-associativity) -> implication
We will use 1 and 0 to represent the "true" and "false" values known as Boolean Values which form Boolean algebra.
We will use a Boolean interpretation by assigning our logical variables to Boolean values and considering the formula to be true,
when any possible Boolean assignment evaluates to true.
\begin{align*} \langle X \rangle_v &= v(X), ~X\mbox{ a variable}\\ \langle T \land V \rangle_v &= \langle T \rangle_v \land \langle V \rangle_v \\ \langle T \lor V \rangle_v &= \langle T \rangle_v \lor \langle V \rangle_v \\ \langle T \ra V \rangle_v &= \langle T \rangle_v \ra \langle V \rangle_v \\ \langle \lnot T \rangle_v &= \lnot \langle T \rangle_v \end{align*}
For connectives with the recursive definition for which is the truth value assigned to formula by valuation .
To check whether a formula is true we have to try every possible combination of values, which results in exponential number of calculations.
So we will continue to develop the idea of proof this chapter to prove formulas without having to go through a full truth table.
There are two ways that proof and truth are related.
We start by restricting the language of formulas to only propositional variables and implication. T = X | (T -> T)
We write to mean "From the assumption , we can prove .
We will use a system called natural deduction introduced by Gerhard Gentzen
First rule in our set of inference rules for natural deduction:
Second rule
Assumptions:
We want to interpret logical connectives in terms of what a proof of a formula using that connective should look like.
This idea is the basis of the BHK interpretation which we will be using thruoghout the rest of the course.
The BHK interpretation for implication is phrased as "A proof of is a construction which permits us to
transform a proof of into a proof of ".
We change our sequent notation to include proof terms. A sequent is now a three way relation ,
this reads as " proves with proof "
Here is the rule for implication introduction:
we can write the proof term for as
and for :
Quote notation in racket:
'(1 x 2)
(list 1 x 2)
'*Here's an example of an interpreter
(define (interp exp)
(cond
[(number? exp) exp]
[(symbol=? (first exp) '*)
(* (interp (second exp))
(interp (third exp)))]
[(symbol=? (first exp) '+)
(+ (interp (second exp))
(interp (third exp)))]))
with (interp '(* (+ 1 2) (+ 3 4))) evaluating to 21
we can achieve this using Quasiquoute abbreviated with a single open quote `
(define x 3)
`(a b ,(+ 2 x))
=> '(a b 5)
we can use unquote-splicing to splice a list of elements:
(define x '(c d))
`(a b ,@x)
=> '(a b c d)
A useful pattern-matching speciam form match is available in full Racket.
cond but there is an initial expression which is reduced to a value and the questiosn(match exp
[pattern answer]
...)
(define (f x)
(match x
[y (+ 3 y)]))
(f 2) => 5
(f 5) => 8
(define (f x)
(match x
[3 "three"]
["four" 4]
[y false]))
(f 3) => "three"
(f "four") => 4
(f 5) => false
We can form compound patterns by applying list to a sequence of several patterns.
(define (f x)
(match x
[(list x y) (+ x y)]
[(list x y rst ...)
(list (- x y) rst)]
[(list 1) false]))
(f '(1 2)) => 3
(f '(1 2 3 4)) => '(-1 (3 4))
(f '(1)) => false
Identifiers can be repeated in a pattern indicating a reptition of a value in whatever matches.
(define (g x)
(match x
[(list x y x) (+ x y)]
[(list (list x y) z ) (- x z)]
['(1 (2 3)) true]))
(g '(1 3 1)) => 4
(g '((4 8) 3)) => 1
(g '(1 (2 3))) => true
Patterns can use cons and list, an _ underscore can be used to indicate the presence of a value that we don't need to a give a name
structures can also be used in patterns.
(struct posn (x y))
(define (h x)
(match x
[(cons 1 (list y z)) (+ y z)]
[(list p _) (sqr p)]
[(posn p q)
(lsit p q)]))
(h '(1 2 3)) => 5
(h '(2 3)) => 4
(h (posn 7 8)) => (list 7 8)
Quasiquote also works in pattern matching.
(define (h x)
(match x
[`(1 ,y ,z) (+ y z)]
[`(,(posn p q) ,r)
(list p q r)]
[`(,p ,_) (sqr p)]))
(h '(1 2 3)) => 5
(h (list (posn 7 8) 9)) => '(7 8 9)
(h ('3 4)) => 9
Here's an example of our interpreter using match,
;; original (without using match)
(define (interp exp)
(cond
[(number? exp) exp]
[(symbol=? (first exp) '*)
(* (interp (second exp))
(interp (third exp)))]
[(symbol=? (first exp) '+)
(+ (interp (second exp))
(interp (third exp)))]))
;; with match
(define (interp exp)
(match exp
[(? number? n) n]
[`(* ,x ,y) (* (interp x) (interp y))]
[`(+ ,x ,y) (+ (interp x) (interp y))]))
number? to the valueOur goal is write a Racket function that validates a sequent
We start with the goal of verifying the sequent, let's write this goal as
and call it type checking.
The other task we have is when we only have two of the pieces, the context and the term
and from them we need to figure out the type . Let's write this goal as and call it
type snythesis.
Here are the modified inference rules:
We can turn from checking to synthesis using the following rule,
or
We introduce a grammar rule for terms with a type annotation (which helps us check things that need synthesis)
t = x
| ( x.t)
| (t t)
| (t : T)
Here is the grammar for types and expressions (terms) in Prous: expr = ( x => expr) | (expr expr) | (expr : type) | x
type = (type -> type) | X
Here are the Racket structs for each grammar rule
(struct Lam (var body) #:transparent)
(struct App (rator rand) #:transparent)
(struct Ann (expr type) #:transparent)
(struct Arrow (domain range) #:transparent)
The parser:
;; parse-expr : sexp -> Expr
;; sexp = symbolic expression
(define (parse-expr s)
(match s
[`(λ ,(? symbol? x) => ,e) (Lam x (parse-expr e))]
[`(,e1, ,e2) (App (parse-expr e1) (parse-expr e2))]
[(? symbol? x) x]
[`(,e : ,t) (Ann (parse-expr e) (parse-type t))]
[else (error 'parse "bad syntax: ~a" s)]))
;; parse-type : sexp -> Type
(define (parse-type t)
(match t
[`(,t1 -> ,t2) (Arrow (parse-type t1) (parse-type t2))]
[(? symbol? X) X]
[else (error 'parse "bad syntax: ~a\n" t)]))
Here are lines to add which allow for left-associative function application and right-associative arrows.
;; added to the end of parse-expr
[`(,e1 ,e2 ,e3 ,r ...) (parse-expr `((,e1 ,e2) ,e3 ,@r))]
;; added to parse-type
[`(,t1 -> ,t2 -> ,r ...) (Arrow (parse-type t1) (parse-type `(,t2 -> ,@r)))]
Here are functions which create readable strings for humans from the abstract structures:
; pretty-print-expr : Expr -> String
(define (pretty-print-expr e)
(match e
[(Lam x b) (format "(λ ~a => ~a)" x (pretty-print-expr b))]
[(App e1 e2) (format "(~a ~a)" (pretty-print-expr e1)
(pretty-print-expr e2))]
[(? symbol? x) (format "~a" x)]
[(Ann e t) (format "(~a : ~a)" (pretty-print-expr e)
(pretty-print-type t))]))
; pretty-print-type : Type -> String
(define (pretty-print-type t)
(match t
[(Arrow t1 t2) (format "(~a -> ~a)" (pretty-print-type t1)
(pretty-print-type t2))]
[else (symbol->string t)]))
; type-check : Context Expr Type -> boolean
; produces true if expr has type t in context ctx (or error if not)
(define (type-check ctx expr type)
(match expr
[(Lam x t)
(match type
[(Arrow tt tw) (type-check (cons `(,x ,tt) ctx) t tw)]
[else (cannot-check ctx expr type)])]
[else (if (equal? (type-synth ctx expr) type) true (cannot-check ctx expr type))]))
; type-synth : Context Expr -> Type
; produces type of expr in context ctx (or error if can't)
; all failing cases spelled out rather than put into "else" at end
(define (type-synth ctx expr)
(match expr
[(Lam _ _) (cannot-synth ctx expr)]
[(Ann e t) (type-check ctx e t) t]
[(App f a)
(define tf (type-synth ctx f))
(match tf
[(Arrow tt tw) #:when (type-check ctx a tt) tw]
[else (cannot-synth ctx expr)])]
[(? symbol? x)
(cond
[(assoc x ctx) => second]
[else (cannot-synth ctx expr)])]))
We can check tautologies (formulas that can be proved with an empty context) by using the type-synth with an empty context
(require test-engine/racket-tests)
(define (check-proof p) (type-synth empty (parse-expr p)) true)
(check-expect (check-proof '((λ x => (λ y => x)) : (A -> (B -> A))))
true)
We introduce the notion of holes (or goals) which are unfinished parts of the proof represented by ?
conjunction type checking:
conjunction type synthesis:
IMO, taking notes for this course isn't beneficial as the course textbook is already well-written and does not benefit from being summarized.