10 May 14:28
Type unions
From: Eric Stansifer <eric.stansifer <at> gmail.com>
Subject: Type unions
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2008-05-10 12:28:58 GMT
Subject: Type unions
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2008-05-10 12:28:58 GMT
I have been trying to write a DSL for Povray (see www.povray.org) in Haskell, using the technique of: http://okmij.org/ftp/papers/tagless-final-APLAS.pdf with some inspiration taken from http://okmij.org/ftp/Haskell/DSLSharing.hs The Povray Scene Description Language is a very declarative language, with few high level constructs (even loops take a bit of work) -- which is why I'm putting it in Haskell. At one point, I needed a "varargs" function for the DSL, a function f :: b -> a -> b dressed up to take a variable number of 'a's, known at compile time. This was easy enough: > data Nil a = Nil > data Cons b a = a ::: b a > infixr 1 ::: > > class VarArgs v where > apply_args :: (s -> a -> s) -> s -> v a -> s > > instance VarArgs Nil where > apply_args _ start _ = start > > instance VarArgs b => VarArgs (Cons b) where > apply_args f start (a ::: b) = apply_args f (f start a) b The solution is quite workable: I can simply write the following, and I believe the summation is expanded out at compile-time:(Continue reading)
RSS Feed