SlideShare a Scribd company logo
Shape Safety in Tensor Programming is Easy for a
Theorem Prover
Project: https://github.com/tribbloid/shapesafe
Content: https://github.com/tribbloid/shapesafe-demo
-- Peng Cheng - tribbloid@{github | twitter}
1
About Me
Maintainer of DataPassports, Computing Engine ... 2015 ~
Only read Scala 'cause of Apache Spark
Only read type theory 'cause math seems more stable than programming
Not a Python fan, but only use PyTorch 'cause it is not TensorFlow
Maintainer of splain plugin ... 2021 ~
Partially integrated with scala compiler 2.13.6+ -Vimplicit -Vtype-diff
2
Overview
Why type-safe linear algebra?
How to push it to extreme?
Why scala?
What's next?
3
Why type-safe linear algebra?
4
5
Designed For Human (who make mistakes)
6
OR is it?
(source: https://arxiv.org/abs/2102.06599)
7
How to push it to extreme?
8
Curry-Howard(-Lambek) isomorphism
proof system
functional programming 

(since 1930)
Proposition
type P <: Any (in MLTT) 

type P <: Prop (in CiC)
Proposition type P[_ <: A] , a: {type P}
Inductive Proposition type ::[HEAD, TAIL <: P] <: P
9
... with quantiifiers
proof system functional programming (since 1970)
def p[AS <: A with Singleton](a: AS): P[AS] , 

def p[AS <: A with Singleton](a: AS): AS#P , 

def p(a: A): a.P
def p[B]: P {val b <: B, type P}
Axiom def axiom[X](.): P[X]{.}
Theorem def theorem[X](., lemma1: X => X#L1): P[X]{.}
10
... in shapesafe impl
proof system functional programming (after -expansion)
def p[AS <: A with Singleton]: AS |- P[AS] , 

def p[AS <: A with Singleton]: AS |- AS#P
def p[B]: P {val b <: B, type P}
Axiom def axiom[X]: X |- P[X]{.}
Theorem def theorem[X](lemma1: X => X#L1): X |- P[X]{.}
11
Scala 2.10 - 2015
class Matrix[A,B](val mat: DenseMatrix[Double]) {

def *[C](other: Matrix[B,C]): Matrix[A,C] = new Matrix[A,C](mat*other.mat)

}

class N

class D

class K

val x = new Matrix[N,D](DenseMatrix.rand(100,10))

val y = new Matrix[N,K](DenseMatrix.rand(100,2))

val z1 = x * x //Does not compile!

val z2 = x.t * y //Compiles! Returns a Matrix[D,K]

12
Scala 3.1 - 2021
import scala.compiletime.ops.int._

class Matrix[A, B]():

def *[C](other: Matrix[B, C]): Matrix[A, C] = new Matrix[A, C]()

def conv[C, D](other: Matrix[C, D]): Matrix[A - C + 1, B - D + 1] =

new Matrix[A - C + 1, B - D + 1]()

val x = new Matrix[100, 100]()

val y = new Matrix[3, 3]()

val w = x.conv(y)

val z1 = w * new Matrix[100, 1]() //Does not compile!

val z2 = w * new Matrix[98, 1]() //Compiles!

13
Extreme 1
Weird operands (Seriously, who is going to write compile-time type evaluation for
einsum?)
type EinSum[Matrix[N1 -> D1, N2 -> D2]] = {

if (N1 =:= N2) {

if (D1 =:= D2) {

Matrix[N1 -> D1]

}

else {

error(...)

}

}

else {

Matrix[N1 -> D1, N2 -> D2]

}

}

14
Extreme 2
Symbolic reasoning ( p.apply + =:= can only go in 1 direction)
class Matrix[A, B]():

def +(other: Matrix[A, B]): Matrix[A, B]

def transpose: Matrix[B, A]

def flatten: Matrix[A * B, 1]

type A <: Int

type B <: Int

val m = new Matrix[A, B]()

m.flatten + (m.transpose.flatten) // Oops

15
Learn from
forward mode
lemma and_is_comm (p q: Prop) (h: p ∧ q): q ∧ p := 

and.intro (h.right) (h.left)

tactic mode
lemma and_is_comm' (p q: Prop) (h: p ∧ q): q ∧ p := 

begin

apply and.intro,

exact h.right,

exact h.left,

end

(source: LEAN for Scala programmers - Part 4 - Juan Pablo Romero Méndez) 16
Full auto mode
lemma and_is_comm' (p q: Prop) (h: p ∧ q): q ∧ p := 

begin

assumption,

assumption,

assumption,

end

=:=
begin

assumption | ? => q ∧ p | .intro: (q, p)? => q ∧ p

assumption | p ∧ q => ? | .right: p ∧ q => p?

assumption | p ∧ q => ? | .left: p ∧ q => q?

end

17
... in Scala!
(source: Oron Port's response to "Principles for Implicits in Scala 3" by Odersky)
18
Curry-Howard isomorphism, rewritten
proof system functional programming (full auto!)
def p[AS <: A with Singleton]: AS |- P[AS] , 

def p[AS <: A with Singleton]: AS |- AS#P
def p[BS <: B](implicit b: BS): BS |- BS#P
Axiom def axiom[X]: X |- P[X]{.}
Theorem
def theorem[X](implicit lemma1: X => X#L1): X |-
P[X]{.}
19
<Life Coding>
20
Why Scala
-- Specifically, why scala 2?
21
Stack of AI-HPC-Hardware co-evolution
/ More Abstract
Distributed Computing --- Apache {Spark | Flink}
[ SOME Deep Learning Library? ]
IR/Multi-stage Compilation --- reflection, LMS
Hardware Design --- CHISEL / SpinalHDL
/ More Concrete
22
But try to avoid ...
Diverging implicit
Circular reference def theorem[A](using a: A): A
Expanding reference def theorem[A <: F[_], B](using a: F[A, B]): A
Summoning proof on long algebraic data type
SquashByName[ / ]#On[

SquashByName[ / ]#On[

SquashByName[ + ]#On[

SquashByName[ - ]#On[

SquashByName[ / ]#On[

SquashByName[ + ]#On[

SquashByName[ - ]#On[

SquashByName[ / ]#On[

SquashByName[ + ]#On[

SquashByName[ - ]#On[

1024 >< 1024 |<<- (i >< j) >< (3 >< 3 |<<- (i >< j))...
23
What's next?
Gradually typed ( UncheckedShape ), but still no symbolic reasoning
Fairly stable, but not released or in production
Not a computing library! Cannot empower ML engineers directly, but serve as a
bridge to get there
Need Scala 3 support!
24
Moving to Scala 3?
Pros
Implicit search tree traversal is aggressively cached
No more unpredictable diverging implicit recovery
Theoretically sound resolving of <:< and =:=
Cons
No type projection (may be added back)
No shapeless Record type

(may be integrated into Programmatic Structural Types)

(can be expressed in match type in a very inefficient way)
25
The End Game?
.

.

.

.

.

.

.

.

.

.

.

.
26
Proudly Supported By
™, the Data Privacy & Trust Company
We are hiring! Looking for a director of engineering
Splain Open-Source Team
Looking for scala compiler guru / type theorist as technical advisor
1.0.0RC is close, looking for test users
27

More Related Content

PPT
Directed Acyclic Graph
PPT
Matlab Nn Intro
ZIP
.Net 4.0 Threading and Parallel Programming
PPT
Application of Stacks
PDF
Contravariant functors in scala
DOCX
Cs6660 compiler design november december 2016 Answer key
DOCX
Cs6660 compiler design may june 2017 answer key
PPTX
Dag representation of basic blocks
Directed Acyclic Graph
Matlab Nn Intro
.Net 4.0 Threading and Parallel Programming
Application of Stacks
Contravariant functors in scala
Cs6660 compiler design november december 2016 Answer key
Cs6660 compiler design may june 2017 answer key
Dag representation of basic blocks

What's hot (20)

PPT
Circular queues
PDF
Cs6503 theory of computation november december 2015 be cse anna university q...
PPT
multi threaded and distributed algorithms
PPT
Infix prefix postfix
DOCX
CS2303 Theory of computation April may 2015
PDF
Introduction to Gura Programming Language
PDF
Cs6503 theory of computation may june 2016 be cse anna university question paper
PDF
Cs2303 theory of computation november december 2015
DOCX
Cs6503 theory of computation november december 2016
PDF
Cs2303 theory of computation all anna University question papers
PDF
A peek on numerical programming in perl and python e christopher dyken 2005
PPTX
Crystal Ball Event Prediction and Log Analysis with Hadoop MapReduce and Spark
PPTX
Tensorflow in practice by Engineer - donghwi cha
PDF
Big picture of category theory in scala with deep dive into contravariant and...
PPTX
My lecture infix-to-postfix
DOC
Infix to-postfix examples
PDF
Infix to Prefix (Conversion, Evaluation, Code)
PPTX
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
PDF
PDF
Python for Scientific Computing -- Ricardo Cruz
Circular queues
Cs6503 theory of computation november december 2015 be cse anna university q...
multi threaded and distributed algorithms
Infix prefix postfix
CS2303 Theory of computation April may 2015
Introduction to Gura Programming Language
Cs6503 theory of computation may june 2016 be cse anna university question paper
Cs2303 theory of computation november december 2015
Cs6503 theory of computation november december 2016
Cs2303 theory of computation all anna University question papers
A peek on numerical programming in perl and python e christopher dyken 2005
Crystal Ball Event Prediction and Log Analysis with Hadoop MapReduce and Spark
Tensorflow in practice by Engineer - donghwi cha
Big picture of category theory in scala with deep dive into contravariant and...
My lecture infix-to-postfix
Infix to-postfix examples
Infix to Prefix (Conversion, Evaluation, Code)
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
Python for Scientific Computing -- Ricardo Cruz
Ad

Similar to Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021 (20)

PDF
Demystifying Shapeless
PDF
Towards typesafe deep learning in scala
PDF
Scala jargon cheatsheet
KEY
Pontificating quantification
PPTX
Scala Back to Basics: Type Classes
PDF
Scala or functional programming from a python developer's perspective
PDF
Programming in Scala - Lecture Three
PDF
Monads and Monoids by Oleksiy Dyagilev
PPTX
Scala 3 Is Coming: Martin Odersky Shares What To Know
PDF
Type classes 101 - classification beyond inheritance
PPTX
Monads and friends demystified
PDF
There's a Prolog in your Scala!
PDF
Large-scale computation without sacrificing expressiveness
PDF
Property based Testing - generative data & executable domain rules
PDF
Functional Operations - Susan Potter
PDF
Formal methods 4 - Z notation
PDF
Oh, All the things you'll traverse
PDF
pure-functional-programming.pdf
PDF
Generic Functional Programming with Type Classes
PDF
Power of functions in a typed world
Demystifying Shapeless
Towards typesafe deep learning in scala
Scala jargon cheatsheet
Pontificating quantification
Scala Back to Basics: Type Classes
Scala or functional programming from a python developer's perspective
Programming in Scala - Lecture Three
Monads and Monoids by Oleksiy Dyagilev
Scala 3 Is Coming: Martin Odersky Shares What To Know
Type classes 101 - classification beyond inheritance
Monads and friends demystified
There's a Prolog in your Scala!
Large-scale computation without sacrificing expressiveness
Property based Testing - generative data & executable domain rules
Functional Operations - Susan Potter
Formal methods 4 - Z notation
Oh, All the things you'll traverse
pure-functional-programming.pdf
Generic Functional Programming with Type Classes
Power of functions in a typed world
Ad

Recently uploaded (20)

PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
PPT on Performance Review to get promotions
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPT
Project quality management in manufacturing
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Digital Logic Computer Design lecture notes
PDF
composite construction of structures.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
web development for engineering and engineering
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
Sustainable Sites - Green Building Construction
PPTX
Construction Project Organization Group 2.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPT on Performance Review to get promotions
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Project quality management in manufacturing
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Digital Logic Computer Design lecture notes
composite construction of structures.pdf
OOP with Java - Java Introduction (Basics)
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Foundation to blockchain - A guide to Blockchain Tech
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
web development for engineering and engineering
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Internet of Things (IOT) - A guide to understanding
Sustainable Sites - Green Building Construction
Construction Project Organization Group 2.pptx

Shape Safety in Tensor Programming is Easy for a Theorem Prover -SBTB 2021

  • 1. Shape Safety in Tensor Programming is Easy for a Theorem Prover Project: https://github.com/tribbloid/shapesafe Content: https://github.com/tribbloid/shapesafe-demo -- Peng Cheng - tribbloid@{github | twitter} 1
  • 2. About Me Maintainer of DataPassports, Computing Engine ... 2015 ~ Only read Scala 'cause of Apache Spark Only read type theory 'cause math seems more stable than programming Not a Python fan, but only use PyTorch 'cause it is not TensorFlow Maintainer of splain plugin ... 2021 ~ Partially integrated with scala compiler 2.13.6+ -Vimplicit -Vtype-diff 2
  • 3. Overview Why type-safe linear algebra? How to push it to extreme? Why scala? What's next? 3
  • 4. Why type-safe linear algebra? 4
  • 5. 5
  • 6. Designed For Human (who make mistakes) 6
  • 7. OR is it? (source: https://arxiv.org/abs/2102.06599) 7
  • 8. How to push it to extreme? 8
  • 9. Curry-Howard(-Lambek) isomorphism proof system functional programming (since 1930) Proposition type P <: Any (in MLTT) type P <: Prop (in CiC) Proposition type P[_ <: A] , a: {type P} Inductive Proposition type ::[HEAD, TAIL <: P] <: P 9
  • 10. ... with quantiifiers proof system functional programming (since 1970) def p[AS <: A with Singleton](a: AS): P[AS] , def p[AS <: A with Singleton](a: AS): AS#P , def p(a: A): a.P def p[B]: P {val b <: B, type P} Axiom def axiom[X](.): P[X]{.} Theorem def theorem[X](., lemma1: X => X#L1): P[X]{.} 10
  • 11. ... in shapesafe impl proof system functional programming (after -expansion) def p[AS <: A with Singleton]: AS |- P[AS] , def p[AS <: A with Singleton]: AS |- AS#P def p[B]: P {val b <: B, type P} Axiom def axiom[X]: X |- P[X]{.} Theorem def theorem[X](lemma1: X => X#L1): X |- P[X]{.} 11
  • 12. Scala 2.10 - 2015 class Matrix[A,B](val mat: DenseMatrix[Double]) { def *[C](other: Matrix[B,C]): Matrix[A,C] = new Matrix[A,C](mat*other.mat) } class N class D class K val x = new Matrix[N,D](DenseMatrix.rand(100,10)) val y = new Matrix[N,K](DenseMatrix.rand(100,2)) val z1 = x * x //Does not compile! val z2 = x.t * y //Compiles! Returns a Matrix[D,K] 12
  • 13. Scala 3.1 - 2021 import scala.compiletime.ops.int._ class Matrix[A, B](): def *[C](other: Matrix[B, C]): Matrix[A, C] = new Matrix[A, C]() def conv[C, D](other: Matrix[C, D]): Matrix[A - C + 1, B - D + 1] = new Matrix[A - C + 1, B - D + 1]() val x = new Matrix[100, 100]() val y = new Matrix[3, 3]() val w = x.conv(y) val z1 = w * new Matrix[100, 1]() //Does not compile! val z2 = w * new Matrix[98, 1]() //Compiles! 13
  • 14. Extreme 1 Weird operands (Seriously, who is going to write compile-time type evaluation for einsum?) type EinSum[Matrix[N1 -> D1, N2 -> D2]] = { if (N1 =:= N2) { if (D1 =:= D2) { Matrix[N1 -> D1] } else { error(...) } } else { Matrix[N1 -> D1, N2 -> D2] } } 14
  • 15. Extreme 2 Symbolic reasoning ( p.apply + =:= can only go in 1 direction) class Matrix[A, B](): def +(other: Matrix[A, B]): Matrix[A, B] def transpose: Matrix[B, A] def flatten: Matrix[A * B, 1] type A <: Int type B <: Int val m = new Matrix[A, B]() m.flatten + (m.transpose.flatten) // Oops 15
  • 16. Learn from forward mode lemma and_is_comm (p q: Prop) (h: p ∧ q): q ∧ p := and.intro (h.right) (h.left) tactic mode lemma and_is_comm' (p q: Prop) (h: p ∧ q): q ∧ p := begin apply and.intro, exact h.right, exact h.left, end (source: LEAN for Scala programmers - Part 4 - Juan Pablo Romero Méndez) 16
  • 17. Full auto mode lemma and_is_comm' (p q: Prop) (h: p ∧ q): q ∧ p := begin assumption, assumption, assumption, end =:= begin assumption | ? => q ∧ p | .intro: (q, p)? => q ∧ p assumption | p ∧ q => ? | .right: p ∧ q => p? assumption | p ∧ q => ? | .left: p ∧ q => q? end 17
  • 18. ... in Scala! (source: Oron Port's response to "Principles for Implicits in Scala 3" by Odersky) 18
  • 19. Curry-Howard isomorphism, rewritten proof system functional programming (full auto!) def p[AS <: A with Singleton]: AS |- P[AS] , def p[AS <: A with Singleton]: AS |- AS#P def p[BS <: B](implicit b: BS): BS |- BS#P Axiom def axiom[X]: X |- P[X]{.} Theorem def theorem[X](implicit lemma1: X => X#L1): X |- P[X]{.} 19
  • 21. Why Scala -- Specifically, why scala 2? 21
  • 22. Stack of AI-HPC-Hardware co-evolution / More Abstract Distributed Computing --- Apache {Spark | Flink} [ SOME Deep Learning Library? ] IR/Multi-stage Compilation --- reflection, LMS Hardware Design --- CHISEL / SpinalHDL / More Concrete 22
  • 23. But try to avoid ... Diverging implicit Circular reference def theorem[A](using a: A): A Expanding reference def theorem[A <: F[_], B](using a: F[A, B]): A Summoning proof on long algebraic data type SquashByName[ / ]#On[ SquashByName[ / ]#On[ SquashByName[ + ]#On[ SquashByName[ - ]#On[ SquashByName[ / ]#On[ SquashByName[ + ]#On[ SquashByName[ - ]#On[ SquashByName[ / ]#On[ SquashByName[ + ]#On[ SquashByName[ - ]#On[ 1024 >< 1024 |<<- (i >< j) >< (3 >< 3 |<<- (i >< j))... 23
  • 24. What's next? Gradually typed ( UncheckedShape ), but still no symbolic reasoning Fairly stable, but not released or in production Not a computing library! Cannot empower ML engineers directly, but serve as a bridge to get there Need Scala 3 support! 24
  • 25. Moving to Scala 3? Pros Implicit search tree traversal is aggressively cached No more unpredictable diverging implicit recovery Theoretically sound resolving of <:< and =:= Cons No type projection (may be added back) No shapeless Record type (may be integrated into Programmatic Structural Types) (can be expressed in match type in a very inefficient way) 25
  • 27. Proudly Supported By ™, the Data Privacy & Trust Company We are hiring! Looking for a director of engineering Splain Open-Source Team Looking for scala compiler guru / type theorist as technical advisor 1.0.0RC is close, looking for test users 27