<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="font-family:Noto Sans;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">Sir/Madam, </span></span></span></p>
<br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">I have familiarity with Haskell and have recently returned to using it on a small project, and have just taken a crack at using type families for the first time. I've used functional dependencies more in the past. My example below is a greatly simplified version of what I'm trying to do; for context, the overall purpose is a clean representation of logical forms across languages for abstract case analysis for theorem-proving for certain multimodal logics. The idea represented here is a unified representation for sentential forms and labelled sentential forms. The instance for the latter would be defined in terms of the instance for the former, but to keep it as simple as possible, the snippet here just has a separate (nonsense) implementation of three functions f1,f2,f3 that represent the pattern. </span></span></p>
<br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">I understand that that I'm running into overlapping instances but am unsure about how to correct it, and I couldn't find anything to address this in a search of the haskell-cafe archive (which is not to say that it isn't there, just that I couldn't find and/or recognise it). I'm sure it will be a simple matter to point me in the right direction here for people with more experience. </span></span></p>
<br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">Thank you to anyone in advance who can help; I do really appreciate it. </span></span></p>
<br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">-------------------------------------------------------------------------------</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">{-# LANGUAGE TypeFamilies, TypeFamilyDependencies, MultiParamTypeClasses, FlexibleInstances #-} </span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">-- A simplified model for sentences.</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">data S a = SA a (S a)</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> | SB a (S a)</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> | SC String</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> deriving (Eq, Show)</span></span></p>
<br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">-- A simplified model for what will be abstract case analysis:</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">data Form a b = FA a b</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> | FB</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> deriving (Eq, Show)</span></span></p>
<br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">-- A simplified a model for the case analysis class:</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">class (F a ~ b) => Formable a b where</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> type F a = t | t -> a</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f1 :: b -> Form a b</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f2 :: b -> b</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f3 :: b -> Bool</span></span></p>
<br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">-- Simple test instance of Formable for S:</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">instance Formable a (S a) where</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> type F a = S a</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f1 (SA a y) = FA a y</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f1 (SB a y) = FA a y</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f1 (SC x) = FB</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f2 (SA a y) = SA a y</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f2 (SB a y) = SA a y</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f2 (SC x) = SC "nothing"</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f3 (SA a y) = False</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f3 (SB a y) = False</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f3 (SC _) = True</span></span></p>
<br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">-- Some test instances all work fine:</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">-- > f1 (SA "a" (SB "b" (SC "c")))</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">-- > f2 (SA "a" (SB "b" (SC "c")))</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">-- > f3 (SA "a" (SB "b" (SC "c")))</span></span></p>
<br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">-- A model of a wrapper for sentences to iadd integer labels:</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">data W a = W Int (S a)</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> deriving (Eq, Show)</span></span></p>
<br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">-- Test instance modelling an instance for labelled sentences:</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">instance Formable a (W a) where</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> type F a = W a</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f1 (W k (SA a y)) = FA a (W k y)</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f1 (W k (SB a y)) = FA a (W k y)</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f1 (W k (SC x)) = FB</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f2 (W k x) = W (1+k) x</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f3 (W k (SA a y)) = False</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f3 (W k (SB a y)) = False</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> f3 (W k (SC _)) = True</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> -- Intend to re-implement f1,f2,f3 in terms of the instance for S once</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> -- I can convince GHC to let both instances stand. </span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">------------------------------------------------------------------------------- </span></span></p>
<br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">Everything is dandy if either of the two instances, for S and for W, are provided, but GHC is not at all impressed with the idea of having both. GHCi reports the following error.</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">Test.hs:29:10: error:</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> Conflicting family instance declarations:</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> F a = S a -- Defined at Test.hs:29:10</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;"> F a = W a -- Defined at Test.hs:51:10 </span></span></p>
<br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">Regards,</span></span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;"><span style="background-color:#1c1c1c;"><span style="color:#ebebeb;">D.</span></span></p>
</body>
</html>