21 Dec 05:37
Smart Constructor Puzzle
From: Ronald Guida <ronguida <at> mindspring.com>
Subject: Smart Constructor Puzzle
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2007-12-21 04:39:42 GMT
Subject: Smart Constructor Puzzle
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2007-12-21 04:39:42 GMT
I'm playing around with smart constructors, and I have encountered a weird puzzle. My goal is to do vector arithmetic. I'm using smart constructors so that I can store a vector as a list and use the type system to staticly enforce the length of a vector. So my first step is to define Peano numbers at the type level. > data PZero = PZero deriving (Show) > data PSucc a = PSucc a deriving (Show) > > type P1 = PSucc PZero > type P2 = PSucc P1 > type P3 = PSucc P2 > -- etc Next, I define a vector type and tag it with a Peano number. > data Vec s t = Vec [t] deriving (Eq, Ord, Show, Read) Now I can define a few smart constructors. > vec0 :: Vec PZero t > vec0 = Vec [] > > vec1 :: t -> Vec P1 t > vec1 x = Vec [x] > > vec2 :: t -> t -> Vec P2 t(Continue reading)
RSS Feed