<!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> </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> </DIV>
<DIV><FONT size=2>I have a NL system which uses a lexicon of words annotated
with types and semantics. The grammer uses the type information to
correctly structure the semantic nodes. 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. So I
have</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>> 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> </DIV>
<DIV><FONT size=2>A correct sentence will yield a term with only App and
Sem. 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> </DIV>
<DIV><FONT size=2>Thus I could have a term </FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>> App (Sem f) (Sem g)</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>where f is of type: s / np</FONT></DIV>
<DIV><FONT size=2>and g is of type: np</FONT></DIV>
<DIV><FONT size=2></FONT> </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> </DIV>
<DIV><FONT size=2>I have a type</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>> data SemF a = ZSem (SST a)</FONT></DIV>
<DIV><FONT size=2>>
| USem (SST a -> SST
a)</FONT></DIV>
<DIV><FONT
size=2>
etc...</FONT></DIV>
<DIV><FONT size=2>
</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> </DIV>
<DIV><FONT size=2>probably = (s\np)/(s\np)</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>has a type (* -> *) -> (* -> *)</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>so I need a separate data constructor</FONT></DIV>
<DIV><FONT size=2>>
| U22Sem ((SST a -> SST a) -> (SST a
-> SST a))</FONT></DIV>
<DIV><FONT size=2></FONT> </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> </DIV>
<DIV><FONT size=2>Modification 1:</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>Someone suggested I use GADTs, so I tried implementing
with:</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>> data SemG where<BR>> ZSemG ::
SST SNode -> SemG<BR>> USemG :: (Typeable b)
=> (b -> SST SNode) -> SemG<BR></FONT> </DIV>
<DIV><FONT size=2>(so USem represents functions of type * -> *, (* -> *)
-> *, ...</FONT></DIV>
<DIV><FONT size=2></FONT> </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> </DIV>
<DIV><FONT size=2>applySem :: SemG -> SemG -> SemG<BR>applySem (USemG a)
(ZSemG b) = ZSemG $ <BR> ((unJust $ cast a) :: SST
SNode -> SST SNode)<BR> ((unJust $ cast b) :: SST
SNode)</FONT></DIV>
<DIV><FONT size=2>applySem (BSemG a) (ZSemG b) = USemG $<BR>
((unJust $ cast a) :: Typeable a => SST SNode -> a -> SST
SNode) -- this a here is problematic<BR> ((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> </DIV>
<DIV><FONT size=2>This is frustrating, I want to harness ghcs power without
reimplementing. (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). Why reinvent the
wheel?</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>First, am I missing something?</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>If not...</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>Suggestion.</FONT></DIV>
<DIV><FONT size=2></FONT> </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, > 5^4 possibilities.</FONT></DIV>
<DIV><FONT size=2></FONT> </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> </DIV>
<DIV><FONT size=2>What I propose is this:</FONT></DIV>
<DIV><FONT size=2></FONT> </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> </DIV>
<DIV><FONT size=2>1) The ability in Template Haskell to provide the type of an
expression as a first class value. This would require a hook into the
compiler and datatypes to express the type.</FONT></DIV>
<DIV><FONT size=2></FONT> </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> </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 their types as part of the expression. 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> </DIV>
<DIV><FONT size=2>I realise that this is in essence what the typeable class is
supposed to provide. 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> </DIV>
<DIV><FONT size=2>In summary, in order to avoid hand coding thousands of
possible function types, I want to use dynamic casts. I have tried GADTs
(which I may not have understood correctly :-). It appears that what I
want to do is not possible at the moment.</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>I think that this modification is worthwhile. 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> </DIV>
<DIV><FONT size=2>Vivian</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>P.S. If I have missed something, please could you let me know,
I'm stumped. In which case I apologise for the rant.</FONT></DIV>
<DIV><FONT size=2> </DIV></FONT></BODY></HTML>