Fun with Semirings. A functional pearl on the abuse of linear algebra. Stephen Dolan

Similar documents
Fun with Semirings. A functional pearl on the abuse of linear algebra. Stephen Dolan

Introduction to Kleene Algebras

1) 2) Algebra (3-2) Solving Inequalities with Additon and Subtraction

Section Summary. Sequences. Recurrence Relations. Summations Special Integer Sequences (optional)

Definition 8.1 Two inequalities are equivalent if they have the same solution set. Add or Subtract the same value on both sides of the inequality.

x y = x + y. For example, the tropical sum 4 9 = 4, and the tropical product 4 9 = 13.

Introduction. Foundations of Computing Science. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Discrete Mathematics Review

Semirings for Breakfast

Notes on Inductive Sets and Induction

Kleene Algebras and Algebraic Path Problems

Relations Graphical View

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Maximum sum contiguous subsequence Longest common subsequence Matrix chain multiplication All pair shortest path Kna. Dynamic Programming

Pure Mathematics P1

Control of Real-Time Discrete Event Systems * Guillaume Brat and Vijay K. Garg. The University of Texas at Austin. Austin, TX 78712, USA

Polynomial and Synthetic Division

Axioms of Kleene Algebra

Notes. Relations. Introduction. Notes. Relations. Notes. Definition. Example. Slides by Christopher M. Bourke Instructor: Berthe Y.

CA320 - Computability & Complexity

A. Incorrect! Apply the rational root test to determine if any rational roots exist.

correlated to the Washington D.C. Public Schools Learning Standards Algebra I

With Question/Answer Animations. Chapter 2

A. Incorrect! This inequality is a disjunction and has a solution set shaded outside the boundary points.

CS100: DISCRETE STRUCTURES. Lecture 3 Matrices Ch 3 Pages:

Reteach Simplifying Algebraic Expressions

bc7f2306 Page 1 Name:

CLEP College Algebra - Problem Drill 21: Solving and Graphing Linear Inequalities

Lecture 4. Algebra, continued Section 2: Lattices and Boolean algebras

Solving Polynomial and Rational Inequalities Algebraically. Approximating Solutions to Inequalities Graphically

Tribhuvan University Institute of Science and Technology Micro Syllabus

BOOLEAN ALGEBRA INTRODUCTION SUBSETS

Chapter 1 Review of Equations and Inequalities

Solving Linear and Rational Inequalities Algebraically. Definition 22.1 Two inequalities are equivalent if they have the same solution set.

REVIEW QUESTIONS. Chapter 1: Foundations: Sets, Logic, and Algorithms

Kleene modules and linear languages

Discrete Math. Instructor: Mike Picollelli. Day 10

Solving Linear Equations

Functional Programming and Non-Distributivity in Pathfinding problems

Numerical Methods I Solving Square Linear Systems: GEM and LU factorization

R1: Sets A set is a collection of objects sets are written using set brackets each object in onset is called an element or member

Lesson 7.1 Polynomial Degree and Finite Differences

Automata Theory and Formal Grammars: Lecture 1

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

An Overview of Residuated Kleene Algebras and Lattices Peter Jipsen Chapman University, California. 2. Background: Semirings and Kleene algebras

Tropical Islands. Jan Verschelde

Chapter 4, Logic using Propositional Calculus Handout

Regular Expressions and Language Properties

Sets are one of the basic building blocks for the types of objects considered in discrete mathematics.

Packet #1: Logic & Proofs. Applied Discrete Mathematics

1. Introduction to commutative rings and fields

Equational Logic and Term Rewriting: Lecture I

Quantile Textbook Report

ABSOLUTE VALUE EQUATIONS AND INEQUALITIES

Automata Theory for Presburger Arithmetic Logic

CMPSCI 250: Introduction to Computation. Lecture #29: Proving Regular Language Identities David Mix Barrington 6 April 2012

Lecture 2: Syntax. January 24, 2018

Linear Algebra Basics

Unit 4 Systems of Equations Systems of Two Linear Equations in Two Variables

Logic, Sets, and Proofs

2 Metric Spaces Definitions Exotic Examples... 3

Math 10850, fall 2017, University of Notre Dame

Algebra Revision Guide

Chapter 8 Dynamic Programming

Fast Inference with Min-Sum Matrix Product

Propositional Logic, Predicates, and Equivalence

B.E./B.Tech. DEGREE EXAMINATION, NOVEMBER/DECEMBER Fifth Semester. Computer Science and Engineering MA 2265 DISCRETE MATHEMATICS

CME Project, Geometry 2009 Correlated to: Kentucky Core Content for Mathematics Assessment 4.1 (High School, Grade 11)

Dependent ( ) Independent (1 or Ø) These lines coincide so they are a.k.a coincident.

Chapter 2 - Basics Structures

ZEROS OF POLYNOMIAL FUNCTIONS ALL I HAVE TO KNOW ABOUT POLYNOMIAL FUNCTIONS

1. Introduction to commutative rings and fields

XMA2C011, Annual Examination 2012: Worked Solutions

Grade 8 Alignment of CMP with Andover Benchmarks

Unit 1: Equations & Inequalities in One Variable

UNIT II REGULAR LANGUAGES

Prentice Hall PreCalculus, 3rd Edition 2007, (Blitzer)

Lecture 4. 1 Circuit Complexity. Notes on Complexity Theory: Fall 2005 Last updated: September, Jonathan Katz

1. a. Give the converse and the contrapositive of the implication If it is raining then I get wet.

2.6 algebraic proofs. September 13, algebraic proofs ink.notebook. Page 71. Page 72 Page 70. Page 73. Standards. 2.

Groups. 3.1 Definition of a Group. Introduction. Definition 3.1 Group

Prentice Hall Intermediate Algebra, 5th Edition 2009 (Martin-Gay)

Chapter 8 Dynamic Programming

Disturbance Decoupling of Timed Event Graphs by Output Feedback Controller

Secondary Math 3 Honors - Polynomial and Polynomial Functions Test Review

Finite Automata Part Two

PROBLEM SET 3: PROOF TECHNIQUES

Causal Dataflow Analysis for Concurrent Programs

Algebra. Robert Taggart

A. Incorrect! Replacing is not a method for solving systems of equations.

Background on Linear Algebra - Lecture 2

Generating Function Notes , Fall 2005, Prof. Peter Shor

Applications of Regular Algebra to Language Theory Problems. Roland Backhouse February 2001

About closed-loop control and observability of max-plus linear systems: Application to manufacturing systems

Kleene Algebra and Arden s Theorem. Anshul Kumar Inzemamul Haque

Section Summary. Sequences. Recurrence Relations. Summations. Examples: Geometric Progression, Arithmetic Progression. Example: Fibonacci Sequence

586 Index. vertex, 369 disjoint, 236 pairwise, 272, 395 disjoint sets, 236 disjunction, 33, 36 distributive laws

Homework. Basic properties of real numbers. Adding, subtracting, multiplying and dividing real numbers. Solve one step inequalities with integers.

A Weak Bisimulation for Weighted Automata

Problem One: Order Relations i. What three properties does a binary relation have to have to be a partial order?

Transcription:

A functional pearl on the abuse of linear algebra Computer Laboratory University of Cambridge stephen.dolan@cl.cam.ac.uk September 25, 2013

Linear algebra is magic If your problem can be expressed as vectors and matrices, it is essentially already solved. Linear algebra works with fields, like the real or complex numbers: sets with a notion of addition, multiplication, subtraction and division.

We don t have fields CS has many structures with multiplication and addition : conjunction and disjunction sequencing and choice intersection and union product type and sum type But very few with a sensible division or subtraction. What we have are semirings, not fields.

Semirings A closed semiring is a set with some notion of addition and multiplication as well as a unary operation, where: a + b = b + a a + (b + c) = (a + b) + c a + 0 = a a (b c) = (a b) c a 1 = 1 a = a a 0 = 0 a = 0 (+, 0) is a commutative monoid (, 1) is a monoid, with zero a (b + c) = a b + a c distributes over + (a + b) c = a c + b c a = 1 + a a closure operation Daniel J. Lehmann, Algebraic Structures for Transitive Closure, 1977.

Closed semirings A closed semiring has a closure operation, where a = 1 + a a = 1 + a a Intuitively, we can often think of closure as: a = 1 + a + a 2 + a 3 +...

Closed semirings as a Haskell typeclass i n f i x l 9 @. i n f i x l 8 @+ class Semiring r where zero, one : : r closure : : r > r (@+), (@.) : : r > r > r instance Semiring Bool where zero = False one = True closure x = True (@+) = ( ) (@.) = (&&)

Adjacency matrices Directed graphs are represented as matrices of Booleans. G 2 gives the two-hop paths through G. 1 2 3 1 2 3 = 1 2 3 0 1 0 0 0 1 0 0 0 0 1 0 0 0 1 = 0 0 0 0 0 1 0 0 0 0 0 0 (AB) ij = k A ik B kj = k such that A ik B kj

Closure of an adjacency matrix The closure of an adjacency matrix gives us the reflexive transitive closure of the graph. 1 2 3 = 1 2 3 0 1 0 0 0 1 0 0 0 = 1 1 1 0 1 1 0 0 1 A = 1 + A A = 1 + A + A 2 + A 3 +...

A semiring of matrices A matrix is represented by a list of lists of elements. data Matrix a = Matrix [ [ a ] ] instance Semiring a => Semiring ( Matrix a) where... Matrix addition and multiplication is as normal, and Lehmann gives an imperative algorithm for calculating the closure of a matrix.

Closure of a matrix The correctness proof of the closure algorithm states: ( ) A B If M = C D ( then M A = + B C B ) C where B = A B, C = C A and = D + C A B.

Block matrices We can split a matrix into blocks, and join them back together. type BlockMatrix a = ( Matrix a, Matrix a, Matrix a, Matrix a) msplit : : Matrix a > BlockMatrix a mjoin : : BlockMatrix a > Matrix a

Closure of a matrix The algorithm is imperative, but the correctness proof gives a recursive functional implementation: closure (Matrix [ [ x ] ] ) = Matrix [ [ closure x ] ] closure m = mjoin ( first @+ top @. rest @. left, top @. rest, rest @. left, rest ) where ( first, top, left, rest ) = msplit m first = closure f i r s t top = first @. top left = l e f t @. first rest = closure ( rest @+ left @. top)

Shortest distances in a graph Distances form a semiring, with as addition and + as choosing the shorter. The closure algorithm then finds shortest distances. data ShortestDistance = Distance Int Unreachable instance Semiring ShortestDistance where zero = Unreachable one = Distance 0 closure x = one x @+ Unreachable = x Unreachable @+ x = x Distance a @+ Distance b = Distance (min a b) x @. Unreachable = Unreachable Unreachable @. x = Unreachable Distance a @. Distance b = Distance (a + b)

Shortest paths in a graph We can also recover the actual path: data ShortestPath n = Path Int [( n, n)] NoPath instance Ord n => Semiring ( ShortestPath n) where zero = NoPath one = Path 0 [ ] closure x = one x @+ NoPath = x NoPath @+ x = x Path a p @+ Path a p a < a = Path a p a == a && p < p = Path a p otherwise = Path a p x @. NoPath = NoPath NoPath @. x = NoPath Path a p @. Path a p = Path (a + a ) (p ++ p )

Solving linear equations If we have a linear equation like: then a b is a solution: x = a x + b a b = (a a + 1) b = a (a b) + b If we have a system of linear equations like: x 1 = A 11 x 1 + A 12 x 2 +... A 1n x n + B 1. x n = A n1 x 1 + A n2 x 2 +... A nn x n + B n then A B is a solution (for a matrix A and vector B of coefficients) which can be found using closure.

Regular expressions and state machines A state machine can be described by a regular grammar: q A y q C z x q B A xb B ya + zc C 1 The regular grammar is a system of linear equations, and the regular expression describing it can be found by closure.

Solving linear equations Reconstructing regular expressions Solving equations in the free semiring rebuilds regular expressions from a state machine. Dataflow analysis Solving equations in the semiring of sets of variables does live variables analysis (among others).

Linear recurrences and power series Suppose the next value in a sequence is a linear combination of previous values: F (0) = 0 F (1) = 1 F (n) = F (n 2) + F (n 1) We represent these as formal power series: F = x + x 2 + 2x 3 + 3x 4 + 5x 5 + 8x 6... Multiplying by x shifts the sequence one place, so: F = 1 + (x 2 + x) F

Power series are a semiring We represent power series as lists: a::p is a + px. instance Semiring r => Semiring [ r ] where zero = [ ] one = [one] Addition is pointwise: [ ] @+ y = y x @+ [ ] = x (x : xs) @+ (y : ys) = (x @+ y ):( xs @+ ys)

Multiplying power series Multiplying power series works like this: In Haskell: (a + px)(b + qx) = ab + (aq + pb + pqx)x [ ] @. = [ ] @. [ ] = [ ] (a:p) @. (b:q) = (a @. b):(map (a @.) q @+ map (@. b) p @+ (zero :(p @. q))) This is convolution, without needing indices. M. Douglas McIlroy. Power series, power serious. Journal of Functional Programming, 1999.

Closure of a power series The closure of a + px must satisfy: (a + px) = 1 + (a + px) (a + px) This has a solution satisfying: (a + px) = a (1 + px (a + px) ) which translates neatly into (lazy!) Haskell: closure [ ] = one closure (a:p) = r where r = [ closure a] @. (one:(p @. r ))

Fibonacci, again F = 1 + (x + x 2 )F = (x + x 2 ) fib = closure [0,1,1] Any linear recurrence can be solved with closure.

Knapsacks Suppose we are trying to fill our baggage allowance with: Knuth books: weight 10, value 100 Haskell books: weight 7, value 80 Java books: weight 9, value 3 The best value we can have with weight n is: best n = max(100 + best n 10, 80 + best n 7, 3 + best n 9 ) In the (max, +)-semiring, that reads: best n = 100 best n 10 + 80 best n 7 + 3 best n 9 which is a linear recurrence.

Thank you! Questions? Many problems are linear, for a suitable notion of linear. stephen.dolan@cl.cam.ac.uk

Live variables analysis Many dataflow analyses are just linear equations in a semiring. This live variables analysis uses the semiring of sets of variables. IN A = OUT A {x} A B x := 1 while x < y: IN B = OUT B {x, y} IN C = OUT C {x} IN D = OUT D {x} C D x := x * 2 return x OUT A = IN B OUT B = IN C IN D OUT C = IN B OUT D =

Petri nets Timed event graphs (a form of Petri net with a notion of time) can be viewed as linear systems, in the (max, +)-semiring 5 This transition fires for the nth time after all of its inputs have fired for the nth time. The nth token is available from this place 5 time units after then (n 3)th token is available from its input. G. Cohen, P. Moller, J.P. Quadrat, M. Viot, Linear system theory for discrete event systems, 1984.