<div dir="ltr">I believe you can use unidirectional pattern synonyms, and only export the patterns, not the constructors.<div><br></div><div>{-# LANGUAGE PatternSynonyms #-}</div><div>module Tee (T, t1, t2, pattern T1, pattern T2) where</div><div><br></div><div>data T = MkT1 Bool | MkT2 Int | …</div><div>pattern T1 a <- MkT1 a</div><div>pattern T2 a <- MkT2 a</div><div>…</div><div>t1 :: Bool -> T</div><div>t2 :: Int -> T</div><div>…</div><div><br></div><div>You can pattern-match on T1 just fine, but if you try to use it as a constructor you’ll get “non-bidirectional pattern synonym ‘T1’ used in an expression”.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, May 19, 2017 at 1:43 AM, Andrew Butterfield <span dir="ltr"><<a href="mailto:Andrew.Butterfield@scss.tcd.ie" target="_blank">Andrew.Butterfield@scss.tcd.ie</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">All,<br>
<br>
 is there any way in Haskell to export a *data* type so that importing modules can pattern match,<br>
but not use the constructors to build anything?<br>
<br>
My use case is an AST with invariant - I want the convenience of pattern matching<br>
with the safety of having to build using functions exported by the model rather than the constructors directly.<br>
<br>
 e.g<br>
<br>
given<br>
<br>
data T = T1 Bool | T2 Int | TT T T<br>
t1 :: Bool -> T<br>
t2 :: Int -> T<br>
tt :: T -> T -> T<br>
<br>
from outside I can write<br>
<br>
f(T1 False) =  tt (t1 True) (t2 42)<br>
<br>
but not<br>
<br>
f(T1 False) = TT (T1 True) (T2 42) ?<br>
<br>
<br>
Regards,<br>
<br>
Andrew Butterfield<br>
School of Computer Science & Statistics<br>
Trinity College<br>
Dublin 2, Ireland<br>
<br>
______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div><br></div>