Skip to content

Commit

Permalink
Merge pull request #121 from opencompl/paper-examples
Browse files Browse the repository at this point in the history
feat: add examples from paper as a project
  • Loading branch information
goens authored Oct 26, 2023
2 parents 1bf57f0 + f04071c commit c536947
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 5 deletions.
1 change: 1 addition & 0 deletions SSA.lean
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import SSA.Projects.Tensor1D.Tensor1D
import SSA.Projects.Tensor2D.Tensor2D
import SSA.Projects.Holor.Holor
import SSA.Projects.DCE.DCE
import SSA.Projects.PaperExamples.PaperExamples


-- EXPERIMENTAL
Expand Down
5 changes: 4 additions & 1 deletion SSA/Core/ErasedContext.lean
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ def Valuation (Γ : Ctxt Ty) : Type :=
def Valuation.eval {Γ : Ctxt Ty} (VAL : Valuation Γ) ⦃t : Ty⦄ (v : Γ.Var t) : toType t :=
VAL v

instance : Inhabited (Ctxt.Valuation (∅ : Ctxt Ty)) := ⟨fun _ v => v.emptyElim⟩
/-- Make a valuation for the empty context. -/
def Valuation.nil : Ctxt.Valuation (∅ : Ctxt Ty) := fun _ v => v.emptyElim

instance : Inhabited (Ctxt.Valuation (∅ : Ctxt Ty)) := ⟨Valuation.nil⟩

/-- Make a valuation for `Γ.snoc t` from a valuation for `Γ` and an element of `t.toType`. -/
def Valuation.snoc {Γ : Ctxt Ty} {t : Ty} (s : Γ.Valuation) (x : toType t) :
Expand Down
8 changes: 4 additions & 4 deletions SSA/Core/Framework.lean
Original file line number Diff line number Diff line change
Expand Up @@ -1162,16 +1162,16 @@ leaving behind a bare Lean level proposition to be proven.
macro "simp_peephole" "[" ts: Lean.Parser.Tactic.simpLemma,* "]" "at" ll:ident : tactic =>
`(tactic|
(
try simp only [Com.denote, Expr.denote, HVector.denote, Var.zero_eq_last, Var.succ_eq_toSnoc,
Ctxt.snoc, Ctxt.Valuation.snoc_last, Ctxt.ofList, Ctxt.Valuation.snoc_toSnoc,
try simp (config := {decide := false}) only [Com.denote, Expr.denote, HVector.denote, Var.zero_eq_last, Var.succ_eq_toSnoc,
Ctxt.empty, Ctxt.snoc, Ctxt.Valuation.nil, Ctxt.Valuation.snoc', Ctxt.Valuation.snoc_last, Ctxt.ofList, Ctxt.Valuation.snoc_toSnoc,
HVector.map, HVector.toPair, HVector.toTuple, OpDenote.denote, Expr.op_mk, Expr.args_mk, $ts,*]
generalize $ll { val := 0, property := _ } = a;
generalize $ll { val := 1, property := _ } = b;
generalize $ll { val := 2, property := _ } = c;
generalize $ll { val := 3, property := _ } = d;
generalize $ll { val := 4, property := _ } = e;
generalize $ll { val := 5, property := _ } = f;
simp [Goedel.toType] at a b c d e f;
try simp (config := {decide := false}) [Goedel.toType] at a b c d e f;
try clear f;
try clear e;
try clear d;
Expand Down Expand Up @@ -1217,7 +1217,7 @@ inductive ExOp : Type
| add : ExOp
| beq : ExOp
| cst : ℕ → ExOp
deriving DecidableEq
deriving DecidableEq, Repr

instance : OpSignature ExOp ExTy where
signature
Expand Down
209 changes: 209 additions & 0 deletions SSA/Projects/PaperExamples/PaperExamples.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
import Mathlib.Logic.Function.Iterate
import SSA.Core.Framework
import SSA.Core.Util

set_option pp.proofs false
set_option pp.proofs.withType false

open Std (BitVec)
open Ctxt(Var)

namespace ToyNoRegion

inductive Ty
| int
deriving DecidableEq, Repr

@[reducible]
instance : Goedel Ty where
toType
| .int => BitVec 32

inductive Op : Type
| add : Op
| const : (val : ℤ) → Op
deriving DecidableEq, Repr

instance : OpSignature Op Ty where
signature
| .const _ => ⟨[], [], .int⟩
| .add => ⟨[.int, .int], [], .int⟩

@[reducible]
instance : OpDenote Op Ty where
denote
| .const n, _, _ => BitVec.ofInt 32 n
| .add, .cons (a : BitVec 32) (.cons (b : BitVec 32) .nil), _ => a + b

def cst {Γ : Ctxt _} (n : ℤ) : Expr Op Γ .int :=
Expr.mk
(op := .const n)
(ty_eq := rfl)
(args := .nil)
(regArgs := .nil)

def add {Γ : Ctxt _} (e₁ e₂ : Var Γ .int) : Expr Op Γ .int :=
Expr.mk
(op := .add)
(ty_eq := rfl)
(args := .cons e₁ <| .cons e₂ .nil)
(regArgs := .nil)

attribute [local simp] Ctxt.snoc

/-- x + 0 -/
def lhs : Com Op (Ctxt.ofList [.int]) .int :=
-- %c0 = 0
Com.lete (cst 0) <|
-- %out = %x + %c0
Com.lete (add ⟨1, by simp [Ctxt.snoc]⟩ ⟨0, by simp [Ctxt.snoc]⟩ ) <|
-- return %out
Com.ret ⟨0, by simp [Ctxt.snoc]⟩

/-- x -/
def rhs : Com Op (Ctxt.ofList [.int]) .int :=
Com.ret ⟨0, by simp⟩

def p1 : PeepholeRewrite Op [.int] .int :=
{ lhs := lhs, rhs := rhs, correct :=
by
rw [lhs, rhs]
/-
Com.denote
(Com.lete (cst 0)
(Com.lete (add { val := 1, property := _ } { val := 0, property := _ })
(Com.ret { val := 0, property := ex1.proof_3 }))) =
Com.denote (Com.ret { val := 0, property := _ })
-/
funext Γv
simp_peephole [add, cst] at Γv
/- ⊢ ∀ (a : BitVec 32), a + BitVec.ofInt 32 0 = a -/
intros a
ring
/- goals accomplished 🎉 -/
sorry
}

def ex1' : Com Op (Ctxt.ofList [.int]) .int := rewritePeepholeAt p1 1 lhs


theorem EX1' : ex1' = (
-- %c0 = 0
Com.lete (cst 0) <|
-- %out_dead = %x + %c0
Com.lete (add ⟨1, by simp [Ctxt.snoc]⟩ ⟨0, by simp [Ctxt.snoc]⟩ ) <| -- %out = %x + %c0
-- ret %c0
Com.ret ⟨2, by simp [Ctxt.snoc]⟩)
:= by rfl

end ToyNoRegion

namespace ToyRegion

inductive Ty
| int
deriving DecidableEq, Repr

@[reducible]
instance : Goedel Ty where
toType
| .int => BitVec 32

inductive Op : Type
| add : Op
| const : (val : ℤ) → Op
| iterate (k : ℕ) : Op
deriving DecidableEq, Repr

instance : OpSignature Op Ty where
signature
| .const _ => ⟨[], [], .int⟩
| .add => ⟨[.int, .int], [], .int⟩
| .iterate _k => ⟨[.int], [([.int], .int)], .int⟩

@[reducible]
instance : OpDenote Op Ty where
denote
| .const n, _, _ => BitVec.ofInt 32 n
| .add, .cons (a : BitVec 32) (.cons (b : BitVec 32) .nil), _ => a + b
| .iterate k, (.cons (x : BitVec 32) .nil), (.cons (f : _ → BitVec 32) .nil) =>
let f' (v : BitVec 32) : BitVec 32 := f (Ctxt.Valuation.nil.snoc' v)
k.iterate f' x
-- let f_k := Nat.iterate f' k
-- f_k x

def cst {Γ : Ctxt _} (n : ℤ) : Expr Op Γ .int :=
Expr.mk
(op := .const n)
(ty_eq := rfl)
(args := .nil)
(regArgs := .nil)

def add {Γ : Ctxt _} (e₁ e₂ : Var Γ .int) : Expr Op Γ .int :=
Expr.mk
(op := .add)
(ty_eq := rfl)
(args := .cons e₁ <| .cons e₂ .nil)
(regArgs := .nil)

def iterate {Γ : Ctxt _} (k : Nat) (input : Var Γ Ty.int) (body : Com Op [.int] .int) : Expr Op Γ .int :=
Expr.mk
(op := .iterate k)
(ty_eq := rfl)
(args := .cons input .nil)
(regArgs := HVector.cons body HVector.nil)

attribute [local simp] Ctxt.snoc

/-- running `f(x) = x + x` 0 times is the identity. -/
def lhs : Com Op [.int] .int :=
Com.lete (iterate (k := 0) ⟨0, by simp[Ctxt.snoc]⟩ (
Com.lete (add ⟨0, by simp[Ctxt.snoc]⟩ ⟨0, by simp[Ctxt.snoc]⟩) -- fun x => (x + x)
<| Com.ret ⟨0, by simp[Ctxt.snoc]⟩
)) <|
Com.ret ⟨0, by simp[Ctxt.snoc]⟩

def rhs : Com Op [.int] .int :=
Com.ret ⟨0, by simp[Ctxt.snoc]⟩

attribute [local simp] Ctxt.snoc

@[simp]
theorem Ctxt.Valuation.snoc_last [Goedel Ty] {ty : Ty} (Γ : Ctxt Ty) (V : Γ.Valuation) (v : Goedel.toType ty):
(Ctxt.Valuation.snoc V v) (Ctxt.Var.last _ _) = v := rfl
set_option pp.proofs false in
set_option pp.proofs.withType false in
def p1 : PeepholeRewrite Op [.int] .int:=
{ lhs := lhs, rhs := rhs, correct := by
rw [lhs, rhs]
funext Γv
/-
Com.denote
(Com.lete
(iterate 0 { val := 0, property := lhs.proof_1 }
(Com.lete (add { val := 0, property := lhs.proof_1 } { val := 0, property := lhs.proof_1 })
(Com.ret { val := 0, property := lhs.proof_2 })))
(Com.ret { val := 0, property := lhs.proof_2 }))
Γv =
Com.denote (Com.ret { val := 0, property := rhs.proof_1 }) Γv
-/
simp_peephole [add, iterate] at Γv
/- ∀ (a : BitVec 32), (fun v => v + v)^[0] a = a -/
simp [Function.iterate_zero]
done
}

/-
def ex1' : Com Op (Ctxt.ofList [.int]) .int := rewritePeepholeAt p1 1 lhs
theorem EX1' : ex1' = (
-- %c0 = 0
Com.lete (cst 0) <|
-- %out_dead = %x + %c0
Com.lete (add ⟨1, by simp [Ctxt.snoc]⟩ ⟨0, by simp [Ctxt.snoc]⟩ ) <| -- %out = %x + %c0
-- ret %c0
Com.ret ⟨2, by simp [Ctxt.snoc]⟩)
:= by rfl
-/

end ToyRegion
5 changes: 5 additions & 0 deletions SSA/Projects/PaperExamples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Paper Examples

This folder has the simple examples we use in the paper to motivate
the framework.

0 comments on commit c536947

Please sign in to comment.