Circuit timing analysis, linear maps, and semantic morphisms Conal Elliott Tabula IFIP WG 2.8, Nov. 2012 Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 1 / 29
Outline Timing analysis Linear transformations Semantics and implementation Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 2 / 29
Timing analysis Timing analysis Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 3 / 29
Timing analysis Simple timing analysis Computation with a time delay: Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 4 / 29
Timing analysis Trivial timings Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 5 / 29
Timing analysis Sequential composition Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 6 / 29
Timing analysis Parallel composition Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 7 / 29
Timing analysis But ... Oops: Same circuit ( f × g ), different timings. Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 8 / 29
Timing analysis Multi-path analysis ◮ Max delay for each input/output pair ◮ How do delays compose ? Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 9 / 29
Timing analysis How do delays compose ? Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 10 / 29
Timing analysis How do delays compose ? V ⊙ U = W , where W i , k = Max ( U i , j + V j , k ) j Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 11 / 29
Timing analysis How do delays compose ? V ⊙ U = W , where W i , k = Max ( U i , j + V j , k ) j Look familiar? Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 11 / 29
Timing analysis How do delays compose ? V ⊙ U = W , where W i , k = Max ( U i , j + V j , k ) j Look familiar? Matrix multiplication? Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 11 / 29
Timing analysis MaxPlus algebra type Delay = MaxPlus Double data MaxPlus a = MP a instance Ord a ⇒ AdditiveGroup ( MaxPlus a ) where MP a ˆ + MP b = MP ( a ‘ max ‘ b ) instance ( Ord a , Num a ) ⇒ VectorSpace ( MaxPlus a ) where type Scalar ( MaxPlus a ) = a a · MP b = MP ( a + b ) Oops – We also need a zero. VectorSpace is overkill. Module over a semi-ring suffices. Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 12 / 29
Timing analysis MaxPlus algebra type Delay = MaxPlus Double data MaxPlus a = −∞ | Fi a instance Ord a ⇒ AdditiveGroup ( MaxPlus a ) where 0 = −∞ MP a ˆ + MP b = MP ( a ‘ max ‘ b ) − ∞ ˆ + = −∞ ˆ + −∞ = −∞ instance ( Ord a , Num a ) ⇒ VectorSpace ( MaxPlus a ) where type Scalar ( MaxPlus a ) = a a · MP b = MP ( a + b ) · −∞ = −∞ Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 13 / 29
Linear transformations Linear transformations Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 14 / 29
Linear transformations Representation? How might we represent linear maps/transformations a ⊸ b ? Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 15 / 29
Linear transformations Representation? How might we represent linear maps/transformations a ⊸ b ? ◮ Matrices ◮ Functions ◮ What else? Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 15 / 29
Linear transformations Matrices a 11 · · · a 1m . . ... . . . . a n1 · · · a nm Static typing? Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 16 / 29
Linear transformations Statically sized matrices type Mat m n a = Vec m ( Vec n a ) ( ◦ ) :: ( IsNat m , IsNat o ) ⇒ Mat n o D → Mat m n D → Mat o m D no ◦ mn = crossF dot ( transpose no ) mn crossF :: ( IsNat m , IsNat o ) ⇒ ( a → b → c ) → Vec o a → Vec m b → Mat o m c crossF f as bs = ( λ a → f a < $ > bs ) < $ > as dot :: ( Ord a , Num a ) ⇒ Vec n a → Vec n a → a u ‘ dot ‘ v = sum ( zipWithV ( ∗ ) u v ) Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 17 / 29
Linear transformations Generalizing type Mat m n a = m ( n a ) ( ◦ ) :: ( Functor m , Applicative n , Traversable n , Applicative o ) ⇒ Mat n o D → Mat m n D → Mat o m D no ◦ mn = crossF dot ( sequenceA no ) mn crossF :: ( Functor m , Functor o ) ⇒ ( a → b → c ) → o a → m b → Mat o m c crossF f as bs = ( λ a → f a < $ > bs ) < $ > as dot :: ( Foldable n , Applicative n , Ord a , Num a ) ⇒ n a → n a → a u ‘ dot ‘ v = sum ( liftA2 ( ∗ ) u v ) Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 18 / 29
Linear transformations Represent via type family (old) class VectorSpace v ⇒ HasBasis v where type Basis v :: ∗ coord :: v → ( Basis v → Scalar v ) Linear map as memoized function from basis: newtype a ⊸ b = L ( Basis a → M b ) See Beautiful differentiation (ICFP 2009). Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 19 / 29
Linear transformations Represent as GADT data a ⊸ b where Dot :: InnerSpace b ⇒ b → ( b ⊸ Scalar b ) (: △ ) :: VS 3 a c d ⇒ -- vector spaces with same scalar field ( a ⊸ c ) → ( a ⊸ d ) → ( a ⊸ c × d ) Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 20 / 29
Semantics and implementation Semantics and implementation Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 21 / 29
Semantics and implementation Semantics [ [ · ] ] :: ( a ⊸ b ) → ( a → b ) [ [ Dot b ] ] = dot b [ [ f : △ g ] ] = [ [ f ] ] △ [ [ g ] ] where, on functions, ( f △ g ) a = ( f a , g a ) Recall: data a ⊸ b where Dot :: InnerSpace b ⇒ b → ( b ⊸ Scalar b ) (: △ ) :: VS 3 a c d ⇒ ( a ⊸ c ) → ( a ⊸ d ) → ( a ⊸ c × d ) Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 22 / 29
Semantics and implementation Semantic type class morphisms Category instance specification: Arrow instance specification: [ [ id ] ] ≡ id [ [ f △ g ] ] ≡ [ [ f ] ] △ [ [ g ] ] [ [ g ◦ f ] ] ≡ [ [ g ] ] ◦ [ [ f ] ] [ [ f × g ] ] ≡ [ [ f ] ] × [ [ g ] ] where ( △ ) :: Arrow ( ❀ ) ⇒ ( a ❀ c ) → ( a ❀ d ) → ( a ❀ c × d ) ( × ) :: Arrow ( ❀ ) ⇒ ( a ❀ c ) → ( b ❀ d ) → ( a × b ❀ c × d ) The Category and Arrow laws then follow. Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 23 / 29
Semantics and implementation Deriving a Category instance One case: [ [( f : △ g ) ◦ h ] ] ≡ ([ [ f ] ] △ [ [ g ] ]) ◦ [ [ h ] ] ≡ [ [ f ] ] ◦ [ [ h ] ] △ [ [ g ] ] ◦ [ [ h ] ] ≡ [ [ f ◦ h △ g ◦ h ] ] (where f ◦ h △ g ◦ h ≡ ( f ◦ h ) △ ( g ◦ h )). Uses: ( f △ g ) ◦ h ≡ f ◦ h △ g ◦ h Implementation: ( f : △ g ) ◦ h = f ◦ h : △ g ◦ h Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 24 / 29
Semantics and implementation Deriving a Category instance [ [ Dot ( a , b ) ◦ ( f : △ g )] ] [ [ Dot s ◦ Dot b ] ] ≡ dot ( a , b ) ◦ ([ [ f ] ] △ [ [ g ] ]) ≡ dot s ◦ dot b ≡ add ◦ ( dot a ◦ [ [ f ] ] △ dot b ◦ [ [ g ] ]) ≡ dot ( s · b ) ] ˆ ≡ dot a ◦ [ [ f ] + dot b ◦ [ [ g ] ] ≡ [ [ Dot ( s · b )] ] [ Dot a ◦ f ˆ ≡ [ + Dot b ◦ g ] ] Uses: dot ( a , b ) ≡ add ◦ ( dot a × dot b ) ( k × h ) ◦ ( f △ g ) ≡ k ◦ f △ h ◦ g [ f ˆ ] ˆ [ + g ] ] ≡ [ [ f ] +[ [ g ] ] Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 25 / 29
Semantics and implementation Deriving an Arrow instance [ [ f × g ] ] [ [ f △ g ] ] ≡ [ [ f ] ] × [ [ g ] ] ≡ [ [ f ] ] △ [ [ g ] ] ≡ [ [ f ] ] ◦ fst △ [ [ g ] ] ◦ snd ≡ [ [ f : △ g ] ] ≡ [ [ compFst f ] ] △ [ [ compSnd g ] ] ≡ [ [ compFst f : △ compSnd g ] ] assuming [ [ compFst f ] ] ≡ [ [ f ] ] ◦ fst [ [ compSnd g ] ] ≡ [ [ g ] ] ◦ snd Conal Elliott (Tabula) Circuit timing analysis, linear maps, and semantic morphisms IFIP WG 2.8, Nov. 2012 26 / 29
Recommend
More recommend