<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1498" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT size=2>Hi,</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>I have run into problems trying to implement a program, and 
think that I might be traipsing along the edge of ghc design.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>I have a NL system which uses a lexicon of words annotated 
with types and semantics.&nbsp; The grammer uses the type information to 
correctly structure the semantic nodes.&nbsp; The grammer uses simple 
combinators to manipulate the semantics, thus I have implemented my system as an 
untyped lambda calculus extended with a single data type.&nbsp; So I 
have</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>&gt; data Term a = Var String | Lam Var (Term a) | App (Term 
a) (App Term a) | Sem a -- Dynamic a</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>A correct sentence will yield a term with only App and 
Sem.&nbsp; Because I have used the (user-supplied) type information, I know that 
application of Sem to Sem will be correct (given correct type 
information).</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>Thus I could have a term </FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>&gt;&nbsp;&nbsp; App (Sem f) (Sem g)</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>where f is of type: s / np</FONT></DIV>
<DIV><FONT size=2>and g is of type:&nbsp;&nbsp; np</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>I want to harness the power of haskell in my semantic terms, 
so currently I am implementing the member of (Sem a) in haskell and loading 
dynamically with hs-plugins.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>I have a type</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>&gt; data SemF a = ZSem (SST a)</FONT></DIV>
<DIV><FONT size=2>&gt;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; |&nbsp; USem (SST a -&gt; SST 
a)</FONT></DIV>
<DIV><FONT 
size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
etc...</FONT></DIV>
<DIV><FONT size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </FONT></DIV>
<DIV><FONT size=2>This works well, except for the fact that in english some 
words take up to five arguments, and there are any number of possible 
combinations of argument types, so a particular word such as 
probably</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>probably&nbsp;=&nbsp;(s\np)/(s\np)</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>has a type (* -&gt; *) -&gt; (* -&gt; *)</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>so I need a separate data constructor</FONT></DIV>
<DIV><FONT size=2>&gt;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; | U22Sem ((SST a -&gt; SST a) -&gt; (SST a 
-&gt; SST a))</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>There are a huge number of possible types, and doing it this 
way requires instantiating a data constructor for each type.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>Modification 1:</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>Someone suggested I use GADTs, so I tried implementing 
with:</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>&gt; data SemG where<BR>&gt;&nbsp;&nbsp;&nbsp; ZSemG :: 
SST&nbsp;SNode&nbsp;-&gt; SemG<BR>&gt;&nbsp;&nbsp;&nbsp; USemG :: (Typeable b) 
=&gt; (b -&gt; SST SNode) -&gt; SemG<BR></FONT>&nbsp;</DIV>
<DIV><FONT size=2>(so USem represents functions of type * -&gt; *, (* -&gt; *) 
-&gt; *, ...</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>but when I go to use the function I can't, because trying to 
cast gives an ambiguous type variable (I think the technical term is monomorphic 
restriction):</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>applySem :: SemG -&gt; SemG -&gt; SemG<BR>applySem (USemG a) 
(ZSemG b) = ZSemG $ <BR>&nbsp;&nbsp;&nbsp; ((unJust $ cast a) :: SST 
SNode&nbsp;-&gt; SST SNode)<BR>&nbsp;&nbsp;&nbsp; ((unJust $ cast b) :: SST 
SNode)</FONT></DIV>
<DIV><FONT size=2>applySem (BSemG a) (ZSemG b) = USemG $<BR>&nbsp;&nbsp;&nbsp; 
((unJust $ cast a) :: Typeable a =&gt;&nbsp;SST SNode&nbsp;-&gt; a -&gt; SST 
SNode) -- this a here is problematic<BR>&nbsp;&nbsp;&nbsp; ((unJust $ cast b) :: 
SST SNode)<BR></FONT></DIV>
<DIV><FONT size=2>This code (applySem) is compiled and linked, whereas the 
functions which are the data in this case are compiled and loaded at 
run-time.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>This is frustrating, I&nbsp;want to harness ghcs power without 
reimplementing.&nbsp; (The reason I want to use haskell in the semantic terms is 
that I need access to IORefs and other features which would be non-trivial to 
implement by hand in a lambda interpreter).&nbsp; Why reinvent the 
wheel?</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>First, am I missing something?</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>If not...</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>Suggestion.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>The problem occurs at the location of the cast, I am applying 
two terms, I know they are correct, given that the user supplied syntactic type 
is correct, but to give each a type annotation by hand in the applySem code 
would require, again, &gt; 5^4 possibilities.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>The type information of the semantic term is available at 
compile time (which in this case is while the main program is 
running).</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>What I propose is this:</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>two additions, one to template haskell, the other to the 
language as a primitive.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>1) The ability in Template Haskell to provide the type of an 
expression as a first class value.&nbsp; This would require a hook into the 
compiler and datatypes to express the type.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>2) a castWithType function which, given a type expression and 
a (Typeable a) expression will attempt to perform the cast to (Maybe 
a).</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>This would solve my problem, because then when I compile the 
semantic expressions (while the main program is running) I can 
include&nbsp;their types as part of the expression.&nbsp; Then, when I go to use 
the semantic term, I can give the castWithType function the type information and 
the function to cast.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>I realise that this is in essence what the typeable class is 
supposed to provide.&nbsp; My problem is that I need a 'general' casting code, 
(part of the main run-time) which is capable of applying semantic functions with 
many possible different types.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>In summary, in order to avoid hand coding thousands of 
possible function types, I want to use dynamic casts.&nbsp; I have tried GADTs 
(which I may not have understood correctly :-).&nbsp; It appears that what I 
want to do is not possible at the moment.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>I think that this modification is worthwhile.&nbsp; The 
reflection abilities it provides (along with Template Haskell) would allow 
extension of Haskell as opposed to reimplementation.<BR></FONT></DIV>
<DIV><FONT size=2>Cheers,</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>Vivian</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>P.S. If I have missed something, please could you let me know, 
I'm stumped.&nbsp; In which case I apologise for the rant.</FONT></DIV>
<DIV><FONT size=2>&nbsp;</DIV></FONT></BODY></HTML>