I'm not certain but I think this will still fail for exactly the piece that you ignored, which is the crux of the problem.<br><br><div><span class="gmail_quote">On 6/8/06, <b class="gmail_sendername">Greg Buchholz</b> <
<a href="mailto:haskell@sleepingsquirrel.org">haskell@sleepingsquirrel.org</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Christophe Poucet wrote:<br>> The idea however is that MonoType is going to be used in a recursive<br>> way. For instance:<br>><br>> newtype FMT = FMT MonoType FMT<br>><br>> instance FMT where...<br><br>
Er, I'll ignore this part.<br>><br>> And this definition will have to reside on recursive definitions. In the<br>> style of how HasVars was instantiated:<br>><br>> instance HasVars a => HasVars (MonoType a) where
<br>> freeVars (TyVar x) = [x]<br>> freeVars (TyConst _ xs) = nub . concatMap freeVars $ xs<br>> occurs x (TyVar y) = x == y<br>> occurs x (TyConst _ xs) = or . map (occurs x) $ xs<br>><br>> So for Type<br>
><br>> instance Type a => Type (MonoType a) where<br>> ...<br>><br>> That's where it becomes rather troublesome.<br><br> Yeah, after a certain point of complexity with type classes, it<br>starts to look like C++ templates. How about something like...
<br><br><br>{-# OPTIONS -fglasgow-exts #-}<br>{-# OPTIONS -fallow-undecidable-instances #-}<br>import List<br><br>type Var = String<br>type Const = String<br><br>data MonoType mt = TyVar Var<br> | TyConst Const [mt] deriving (Eq, Show)
<br><br>data PolyType mt = TyPoly [Var] mt deriving (Show)<br><br>class Type a b where<br> toType :: b -> a b<br> fromType :: a b -> b<br> freeVars :: a b -> [Var]<br> occurs :: Var -> a b -> Bool
<br><br>data Nil = Nil<br><br>instance Type MonoType Nil where<br> freeVars (TyVar x) = [x]<br> freeVars (TyConst _ xs) = ["???"]<br><br>instance (Type a b) => Type MonoType (a b) where<br> freeVars (TyVar x) = [x]
<br> freeVars (TyConst _ xs) = nub . concatMap freeVars $ xs<br> occurs x (TyVar y) = x == y<br> occurs x (TyConst _ xs) = or . map (occurs x) $ xs<br><br>main = print $ freeVars $<br> TyConst "foo" [TyConst "bar" [Nil],
<br> TyConst "baz" [Nil],<br> TyVar "quux" ]<br><br>_______________________________________________<br>Haskell-Cafe mailing list<br><a href="mailto:Haskell-Cafe@haskell.org">
Haskell-Cafe@haskell.org</a><br><a href="http://www.haskell.org/mailman/listinfo/haskell-cafe">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br></blockquote></div><br>