From Haskell to Hardware via CCCs Conal Elliott Tabula August, 2014 Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 1 / 57
Tabula Founded by Steve Teig about 10 years ago. Post-FPGA reconfigurable hardware. Spacetime architecture: 3D for shorter paths Implemented by rapid reconfiguration (2GHz) Minkowski spacetime (special relativity) Spacetime layout with causality constraints Very high sustained throughput Tremendous flexibility for moving computations in space & time Program in a non-sequential language: Haskell Compiler developed openly and shared freely Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 2 / 57
Example square :: Num a ñ a Ñ a square a “ a ˚ a sumSquare :: p Functor f , Foldable f , Num a q ñ f a Ñ a sumSquare “ sum ˝ fmap square Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 3 / 57
sumSquare :: Tree 2 Int Ñ Int 32 mul sumSquare “ sum ˝ fmap square 32 32 add 32 32 mul 32 32 32 add Out In 32 32 32 32 mul add 32 32 32 mul Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 4 / 57
sumSquare :: Tree 2 Int Ñ Int module sumSquare-t2 (In_0, In_1, In_2, In_3, Out); input [0:31] In_0; 32 input [0:31] In_1; mul input [0:31] In_2; 32 input [0:31] In_3; 32 output [0:31] Out; wire [0:31] w_mul_I1; wire [0:31] w_mul_I2; add 32 32 mul wire [0:31] w_mul_I3; 32 32 wire [0:31] w_mul_I4; 32 wire [0:31] w_add_I5; add Out In wire [0:31] w_add_I6; 32 32 wire [0:31] w_add_I7; assign w_mul_I1 = In_0 * In_0; 32 32 mul assign w_mul_I2 = In_1 * In_1; add assign w_mul_I3 = In_2 * In_2; assign w_mul_I4 = In_3 * In_3; 32 assign w_add_I5 = w_mul_I1 + w_mul_I2; 32 assign w_add_I6 = w_mul_I3 + w_mul_I4; 32 assign w_add_I7 = w_add_I5 + w_add_I6; mul assign Out = w_add_I7; endmodule Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 5 / 57
sumSquare :: Tree 3 Int Ñ Int 32 32 mul 32 32 add 32 mul 32 32 32 32 mul 32 add 32 add 32 32 mul 32 32 32 add Out 32 In 32 32 32 mul 32 32 add 32 add 32 mul 32 32 32 32 mul add 32 32 32 mul Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 6 / 57
sumSquare :: Tree 4 Int Ñ Int 32 mul 32 32 32 add 32 mul 32 32 32 mul 32 32 32 add add 32 32 mul 32 32 32 mul 32 32 32 32 add 32 mul 32 32 add add 32 32 32 mul add 32 32 32 32 mul 32 add Out 32 32 In 32 32 mul 32 32 32 32 add 32 32 mul 32 add 32 add 32 32 32 mul add 32 32 32 mul 32 32 32 mul 32 32 add 32 add 32 32 mul 32 32 32 mul 32 add 32 32 mul Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 7 / 57
How it works Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 8 / 57
Overall plan Convert Haskell to Core (GHC). Monomorphize. Convert to abstract vocabulary. Interpret as circuits. Synthesize & optimize with existing HDL machinery. Initial simplifications: Shape-typed data Combinational Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 9 / 57
GHC Core data Expr b -- “b” for the type of binders, “ Var Id | Lit Literal | App p Expr b q p Expr b q | Lam b p Expr b q | Let p Bind b q p Expr b q | Case p Expr b q b Type r Alt b s | Cast p Expr b q Coercion | Type Type type Alt b “ p AltCon , r b s , Expr b q data AltCon “ DataAlt DataCon | LitAlt Literal | DEFAULT data Bind b “ NonRec b p Expr b q | Rec rp b , Expr b qs Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 10 / 57
Overloading lambda Powerful abstraction mechanisms: Lambda/application Type classes Can we use type classes to generalize lambda & application? Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 11 / 57
(Bi-)Cartesian closed categories Category : identity and composition Cartesian : products Co-Cartesian : coproducts (“sums”) Closed : exponentials (arrows as “values”) Suffices for translating typed lambda calculus (J. Lambek, 1980). Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 12 / 57
Category Interface: class Category p ❀ q where id :: a ❀ a p˝q :: p b ❀ c q Ñ p a ❀ b q Ñ p a ❀ c q Laws: id ˝ f ” f g ˝ id ” g p h ˝ g q ˝ f ” h ˝ p g ˝ f q Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 13 / 57
Products class Category p ❀ q ñ ProductCat p ❀ q where type a ˆ ❀ b exl :: p a ˆ ❀ b q ❀ a exr :: p a ˆ ❀ b q ❀ b p Ÿ q :: p a ❀ c q Ñ p a ❀ d q Ñ p a ❀ p c ˆ ❀ d qq p ˆ q :: p a ❀ c q Ñ p b ❀ d q Ñ pp a ˆ ❀ b q ❀ p c ˆ ❀ d qq f ˆ g “ p f ˝ exl q Ÿ p g ˝ exr q Laws: exl ˝ p f Ÿ g q ” f p f ˆ g q ˝ p h Ÿ k q ” p f ˝ h q Ÿ p g ˝ k q exr ˝ p f Ÿ g q ” g id ˆ id ” id exl ˝ h Ÿ exr ˝ h ” h p f ˆ g q ˝ p h ˆ k q ” p f ˝ h qˆp g ˝ k q exl Ÿ exr ” id p f Ÿ g q ˝ h ” p f ˝ h q Ÿ p g ˝ h q Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 14 / 57
Coproducts class Category p ❀ q ñ CoproductCat p ❀ q where type a ` ❀ b inl :: a ❀ p a ` ❀ b q inr :: b ❀ p a ` ❀ b q p Ź q :: p a ❀ c q Ñ p b ❀ c q Ñ pp a ` ❀ b q ❀ c q p ` q :: p a ❀ c q Ñ p b ❀ d q Ñ pp a ` ❀ b q ❀ p c ` ❀ d qq f ` g “ p inl ˝ f q Ź p inr ˝ g q Laws (dual to product): p f Ź g q ˝ inl ” f p h Ź k q ˝ p f ` g q ” p h ˝ f q Ź p k ˝ g q p f Ź g q ˝ inr ” g id ` id ” id h ˝ inl Ź h ˝ inr ” h p h ` k q ˝ p f ` g q ” p h ˝ f q`p k ˝ g q ” id h ˝ p f Ź g q ” p h ˝ f q Ź p h ˝ g q inl Ź inr Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 15 / 57
Exponentials class ProductCat p ❀ q ñ ClosedCat p ❀ q where type a ñ ❀ b apply :: pp a ñ ❀ b q ˆ ❀ a q ❀ b :: pp a ˆ ❀ b q ❀ c q Ñ p a ❀ p b ñ ❀ c qq curry uncurry :: p a ❀ p b ñ ❀ c qq Ñ p a ˆ ❀ b q ❀ c Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 16 / 57
Lambda terms data E :: ˚ Ñ ˚ where Var :: V a Ñ E a Const :: Prim a Ñ E a :: E p a Ñ b q Ñ E a Ñ E b App Lam :: Pat a Ñ E b Ñ E p a Ñ b q data Pat :: ˚ Ñ ˚ where UnitPat :: Pat pq VarPat :: V a Ñ Pat a PairPat :: Pat a Ñ Pat b Ñ Pat p a ˆ b q Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 17 / 57
Lambda to CCC p λ p Ñ k q -- v not free in k ��� const k p λ p Ñ v q -- accessor ��� ... p λ p Ñ u v q ��� apply ˝ pp λ p Ñ u q Ÿ p λ p Ñ v qq p λ p Ñ λ q Ñ u q ��� curry p λ p p , q q Ñ u q Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 18 / 57
Lambda to CCC p λ p Ñ k q -- v not free in k ��� const k p λ p Ñ v q -- accessor ��� ... p λ p Ñ u v q ��� apply ˝ pp λ p Ñ u q Ÿ p λ p Ñ v qq p λ p Ñ λ q Ñ u q ��� curry p λ p p , q q Ñ u q convert :: CCC p ❀ q ñ Pat a Ñ E b Ñ p a ❀ b q convert p Const x q “ constArrow x convert p p Var v q “ convertVar p v convert p p App u v q “ apply ˝ p convert p u Ÿ convert p v q convert p p Lam q e q “ curry p convert p PairPat p q q e q Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 18 / 57
Circuit CCC data Comp “ @ a b . Comp p Prim a b q p Buses a q p Buses b q type CircuitM “ WriterT p Seq Comp q p State BusSupply q newtype a b “ Circ p Buses a Ñ CircuitM p Buses b qq instance Category p q where ... instance ProductCat p q where ... p q where ... instance ClosedCat Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 19 / 57
Examples Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 20 / 57
sumSquare :: Tree 2 Int Ñ Int 32 mul sumSquare “ sum ˝ fmap square 32 32 add 32 32 mul 32 32 32 add Out In 32 32 32 32 mul add 32 32 32 mul Conal Elliott (Tabula) From Haskell to Hardware via CCCs August, 2014 21 / 57
Recommend
More recommend