The Comonad.Reader » Higher-Order Abstract Syntax à la Carte
You may recall buffets in south lake tahoe the definition for an exponential functor from my previous entry, which can also be viewed, I suppose, as functors in the category of right-invertible functions in Haskell. buffets in south lake tahoe class ExpFunctor f where xmap :: ( a -> b ) -> ( b -> a ) -> f a -> f b
Clarifying the above, an instance of ExpFunctor should satisfy the slightly generalized version of the Functor laws from Control.Monad: xmap id id = id xmap f g . xmap f' g' = xmap ( f . f' ) ( g' . g )
Since we like to apply xmap to a pair of functions such that f . g = id, as in the Fegaras/Sheard case, we get: xmap f g . xmap g f = xmap ( f . g ) ( f . g ) -- by second xmap law = xmap id id -- by f . g = id = id -- by first xmap law
Swierstra's main definition looks something like: data ( f :+: g ) e = Inl ( f e ) | Inr ( g e ) buffets in south lake tahoe instance ( Functor f, Functor g ) => Functor ( f :+: g ) where fmap f ( Inl e ) = Inl ( fmap f e ) fmap f ( Inr e ) = Inr ( fmap f e )
This permits the obvious analogue: instance buffets in south lake tahoe ( ExpFunctor f, ExpFunctor g ) => ExpFunctor ( f :+: g ) where xmap f g ( Inl e ) = Inl ( xmap f g e ) xmap f g ( Inr e ) = Inr ( xmap f g e )
With this we can quickly buffets in south lake tahoe encode the untyped lambda calculus: newtype Lam a = Lam ( a -> a ) instance ExpFunctor buffets in south lake tahoe Lam where xmap f g ( Lam k ) = Lam ( f . k . g ) data App a = App a a instance Functor App where fmap f ( App a b ) = App ( f a ) ( f b ) instance buffets in south lake tahoe ExpFunctor App where xmap = const . fmap buffets in south lake tahoe
From there we can define a fairly generic pretty printing framework. class ShowAlgebra f where showAlgebra :: f ( [ String ] -> String ) -> [ String ] -> String instance buffets in south lake tahoe ( ShowAlgebra f, ShowAlgebra buffets in south lake tahoe g ) => ShowAlgebra ( f :+: g ) where showAlgebra ( Inl e ) = showAlgebra e showAlgebra ( Inr e ) = showAlgebra e instance ( Cata f ( ForAll t ) , ShowAlgebra f ) => Show ( ForAll t ) where show x = cata showAlgebra ( runForAll x ) vars
And then we can derive particular instances for the untyped buffets in south lake tahoe lambda calculus we slapped together above: instance ShowAlgebra Lam where showAlgebra ( Lam k ) ( v:vars ) = "( \\ \\ " buffets in south lake tahoe ++ v ++ ". " ++ k ( const v ) vars ++ ")" instance ShowAlgebra App where showAlgebra ( App a b ) vars = "(" ++ a vars ++ " buffets in south lake tahoe " ++ b vars ++ ")"
Then using the combinators from the other day: type Term a = Elim ( Lam :+: App ) a type Expr = ForAll Term app_id_id :: Term a app_id_id = app ( lam id ) ( lam id ) -- for suitable definitions of app and lam a la the a la Carte paper. main = putStrLn . show $ safe app_id_id
Looks like the syntax highlighter is cutting comments contents on the right (at least in firefox on win32). Even so, the comment text is still there for selecting. Belva Guilford Says: February 17th, 2012 at 10:21 pm
This web site seems to receive a little bit of comment spam, can I suggest you set up the spam free plugin – it has helped safeguard a selection of my personal web pages with great success Leave a Reply
Archived Entry Post Date : Wednesday, Mar 26th, 2008 at 12:11 am Category : Category Theory and Haskell Do More : You can leave a response , or trackback buffets in south lake tahoe from your own site. Wiki Old Journal Source Harmless Algorithms Tag Cloud Search
Design based on www.vanillamist.com
You may recall buffets in south lake tahoe the definition for an exponential functor from my previous entry, which can also be viewed, I suppose, as functors in the category of right-invertible functions in Haskell. buffets in south lake tahoe class ExpFunctor f where xmap :: ( a -> b ) -> ( b -> a ) -> f a -> f b
Clarifying the above, an instance of ExpFunctor should satisfy the slightly generalized version of the Functor laws from Control.Monad: xmap id id = id xmap f g . xmap f' g' = xmap ( f . f' ) ( g' . g )
Since we like to apply xmap to a pair of functions such that f . g = id, as in the Fegaras/Sheard case, we get: xmap f g . xmap g f = xmap ( f . g ) ( f . g ) -- by second xmap law = xmap id id -- by f . g = id = id -- by first xmap law
Swierstra's main definition looks something like: data ( f :+: g ) e = Inl ( f e ) | Inr ( g e ) buffets in south lake tahoe instance ( Functor f, Functor g ) => Functor ( f :+: g ) where fmap f ( Inl e ) = Inl ( fmap f e ) fmap f ( Inr e ) = Inr ( fmap f e )
This permits the obvious analogue: instance buffets in south lake tahoe ( ExpFunctor f, ExpFunctor g ) => ExpFunctor ( f :+: g ) where xmap f g ( Inl e ) = Inl ( xmap f g e ) xmap f g ( Inr e ) = Inr ( xmap f g e )
With this we can quickly buffets in south lake tahoe encode the untyped lambda calculus: newtype Lam a = Lam ( a -> a ) instance ExpFunctor buffets in south lake tahoe Lam where xmap f g ( Lam k ) = Lam ( f . k . g ) data App a = App a a instance Functor App where fmap f ( App a b ) = App ( f a ) ( f b ) instance buffets in south lake tahoe ExpFunctor App where xmap = const . fmap buffets in south lake tahoe
From there we can define a fairly generic pretty printing framework. class ShowAlgebra f where showAlgebra :: f ( [ String ] -> String ) -> [ String ] -> String instance buffets in south lake tahoe ( ShowAlgebra f, ShowAlgebra buffets in south lake tahoe g ) => ShowAlgebra ( f :+: g ) where showAlgebra ( Inl e ) = showAlgebra e showAlgebra ( Inr e ) = showAlgebra e instance ( Cata f ( ForAll t ) , ShowAlgebra f ) => Show ( ForAll t ) where show x = cata showAlgebra ( runForAll x ) vars
And then we can derive particular instances for the untyped buffets in south lake tahoe lambda calculus we slapped together above: instance ShowAlgebra Lam where showAlgebra ( Lam k ) ( v:vars ) = "( \\ \\ " buffets in south lake tahoe ++ v ++ ". " ++ k ( const v ) vars ++ ")" instance ShowAlgebra App where showAlgebra ( App a b ) vars = "(" ++ a vars ++ " buffets in south lake tahoe " ++ b vars ++ ")"
Then using the combinators from the other day: type Term a = Elim ( Lam :+: App ) a type Expr = ForAll Term app_id_id :: Term a app_id_id = app ( lam id ) ( lam id ) -- for suitable definitions of app and lam a la the a la Carte paper. main = putStrLn . show $ safe app_id_id
Looks like the syntax highlighter is cutting comments contents on the right (at least in firefox on win32). Even so, the comment text is still there for selecting. Belva Guilford Says: February 17th, 2012 at 10:21 pm
This web site seems to receive a little bit of comment spam, can I suggest you set up the spam free plugin – it has helped safeguard a selection of my personal web pages with great success Leave a Reply
Archived Entry Post Date : Wednesday, Mar 26th, 2008 at 12:11 am Category : Category Theory and Haskell Do More : You can leave a response , or trackback buffets in south lake tahoe from your own site. Wiki Old Journal Source Harmless Algorithms Tag Cloud Search
Design based on www.vanillamist.com
No comments:
Post a Comment