<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
        {mso-style-priority:99;
        mso-style-link:"Balloon Text Char";
        margin:0cm;
        margin-bottom:.0001pt;
        font-size:8.0pt;
        font-family:"Tahoma","sans-serif";}
span.BalloonTextChar
        {mso-style-name:"Balloon Text Char";
        mso-style-priority:99;
        mso-style-link:"Balloon Text";
        font-family:"Tahoma","sans-serif";}
p.emailquote, li.emailquote, div.emailquote
        {mso-style-name:emailquote;
        mso-margin-top-alt:auto;
        margin-right:0cm;
        mso-margin-bottom-alt:auto;
        margin-left:1.0pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
span.EmailStyle20
        {mso-style-type:personal;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.EmailStyle21
        {mso-style-type:personal;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.EmailStyle22
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-GB" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US">The id you are getting is a monomorphic id, with a type like a->a, not the polymorphic forall a. a->a. You don’t want to go round
arbitrarily creating a new Id with the same unique but a different type. I have no idea what would happen then.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US">It’s hard for me to understand just what you code is trying to do. I think you are making bindig<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US"> it = <some expr><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US">and then you want the type of “it”. Maybe something like<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US"> (binds’, gbl_env) <- tcLocalBinds (..your bindin..)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US"> poly_id <- setGblEnv gbl_env (tcLooupId the_id_name)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US">But I’m not totally sure.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US">S<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif"">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif""> p.k.f.holzenspies@utwente.nl [mailto:p.k.f.holzenspies@utwente.nl]
<br>
<b>Sent:</b> 03 September 2013 14:18<br>
<b>To:</b> Simon Peyton-Jones; glasgow-haskell-users@haskell.org<br>
<b>Subject:</b> RE: Question about correct GHC-API use for type checking (or zonking, or tidying)<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Dear Simon, et al,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">I had a chance to try it now. The strange thing is that when I use the lines:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">zonked_id <- TcMType.zonkId id<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">say $ “zonked idType: “ ++ pp (idType zonked_id)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">that is still some unresolved type variable (i.e. prints as “b_i”). Since I already have the intended target-type (considering the code by which
it is produced), is it safe to do what TcMType.zonkId does and manually set it? In other words, I now do this:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">zonked_ty <- zonkTcType all_expr_ty<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">return (setIdType id zonked_ty)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Will this bite me later?<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Regards,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Philip<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span lang="EN-US" style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> Simon Peyton-Jones [<a href="mailto:simonpj@microsoft.com">mailto:simonpj@microsoft.com</a>]
<br>
<b>Sent:</b> maandag 2 september 2013 13:34<br>
<b>To:</b> Holzenspies, P.K.F. (EWI); <a href="mailto:glasgow-haskell-users@haskell.org">
glasgow-haskell-users@haskell.org</a><br>
<b>Subject:</b> RE: Question about correct GHC-API use for type checking (or zonking, or tidying)<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><span lang="NL"><o:p> </o:p></span></p>
<p class="MsoNormal">Does this mean that for the idType to come out correctly I should also zonk (AND BIND) the Id-value I return?<o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US">Yes, zonk the type and grab the type that comes back.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US"><br>
S<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif"">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif"">
<a href="mailto:p.k.f.holzenspies@utwente.nl">p.k.f.holzenspies@utwente.nl</a> [<a href="mailto:p.k.f.holzenspies@utwente.nl">mailto:p.k.f.holzenspies@utwente.nl</a>]
<br>
<b>Sent:</b> 30 August 2013 17:49<br>
<b>To:</b> Simon Peyton-Jones; <a href="mailto:glasgow-haskell-users@haskell.org">
glasgow-haskell-users@haskell.org</a><br>
<b>Subject:</b> Re: Question about correct GHC-API use for type checking (or zonking, or tidying)<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">I feel so unbelievably ignorant now. I thought with all the IORefs in the type checking process that zonking did this in these refs. Somehow I started thinking that some of these remained in SDocs, not thinking showSDoc is pure and results
in a String, which holds no IORefs.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Does this mean that for the idType to come out correctly I should also zonk (AND BIND) the Id-value I return?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Ph.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal"><span style="font-size:9.0pt;color:#575757">Sent from Samsung Mobile<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
<br>
<br>
-------- Original message --------<br>
From: Simon Peyton-Jones <<a href="mailto:simonpj@microsoft.com">simonpj@microsoft.com</a>>
<br>
Date: 30/08/2013 18:25 (GMT+01:00) <br>
To: "Holzenspies, P.K.F. (EWI)" <<a href="mailto:p.k.f.holzenspies@utwente.nl">p.k.f.holzenspies@utwente.nl</a>>,glasgow-haskell-users@haskell.org
<br>
Subject: RE: Question about correct GHC-API use for type checking (or zonking, or tidying)
<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:10.0pt">Haskell is a *functional* language. Consider<br>
<br>
say $ " pre-zonk: " ++ pp all_expr_ty<br>
zonkTcType all_expr_ty<br>
say $ " post-zonk: " ++ pp all_expr_ty<br>
<br>
pp is a pure function; it is given the same input both times, so of course it produces the same output.<br>
<br>
If you collect the result of zonkTcType you might have better luck, thus:<br>
<br>
say $ " pre-zonk: " ++ pp all_expr_ty<br>
zonked_expr_ty <- zonkTcType all_expr_ty<br>
say $ " post-zonk: " ++ pp zonked_expr_ty<br>
<br>
Zonking walks over a type, returning a new type in which unification variables are replaced by the types they unified to.<br>
<br>
Hope this helps<br>
<br>
Simon<br>
<br>
| -----Original Message-----<br>
| From: Glasgow-haskell-users [mailto:glasgow-haskell-users-<br>
| <a href="mailto:bounces@haskell.org">bounces@haskell.org</a>] On Behalf Of <a href="mailto:p.k.f.holzenspies@utwente.nl">
p.k.f.holzenspies@utwente.nl</a><br>
| Sent: 29 August 2013 14:42<br>
| To: <a href="mailto:glasgow-haskell-users@haskell.org">glasgow-haskell-users@haskell.org</a><br>
| Subject: Question about correct GHC-API use for type checking (or<br>
| zonking, or tidying)<br>
| <br>
| Dear GHC-ers,<br>
| <br>
| I'm working on building an interactive environment around the<br>
| composition of expressions. Users can type in (i.e. give strings of)<br>
| expressions and can then use these expressions to produce other<br>
| expressions. I'm close to having a working GHC-API binding for this. The<br>
| resulting types, however, still contain some things I don't quite<br>
| understand. Any help would be appreciated.<br>
| <br>
| Below, I've included the function exprFromString which should parse,<br>
| rename and typecheck strings to Id-things and give their type (although,<br>
| ideally, the idType of said Id-thing should be the same as the type<br>
| returned). This function lives in the IA (InterActive) monad; a monad<br>
| that is a GhcMonad and can lift monadic computations in TcM into itself<br>
| using liftTcM (which uses the initTcPrintErrors and<br>
| setInteractiveContext functions similarly to TcRnDriver.tcRnExpr).<br>
| <br>
| Near the end of the function, debugging output is produced. This output<br>
| confuses me slightly. Here is the output for the three inputs "map (+1)<br>
| [1..10]", "5" and "<a href="file:///\\x%20-%3e%20x">\\x -> x</a>":<br>
| <br>
| <br>
| map (+1) [1..10]<br>
| pre-zonk: forall b. (GHC.Enum.Enum b_i, GHC.Num.Num b_i) => [b_i]<br>
| post-zonk: forall b. (GHC.Enum.Enum b_i, GHC.Num.Num b_i) => [b_i]<br>
| idType: [b_c]<br>
| tidied: forall b. (GHC.Enum.Enum b_i, GHC.Num.Num b_i) => [b_i]<br>
| 5<br>
| pre-zonk: forall a. GHC.Num.Num a_d => t_c<br>
| post-zonk: forall a. GHC.Num.Num a_d => t_c<br>
| idType: a_b<br>
| tidied: forall a. GHC.Num.Num a_d => t_c<br>
| \x -> x<br>
| pre-zonk: forall t. t_e<br>
| post-zonk: forall t. t_e<br>
| idType: forall t. t -> t<br>
| tidied: forall t. t_e<br>
| <br>
| <br>
| The zonking and tidying part of the type-checking process are still a<br>
| bit unclear to me and I suspect the problems arise there. It looks to me<br>
| that the type variables in the quantifications are different ones from<br>
| those in the pi/rho-types. I had expected the types to only contain the<br>
| variables over which they are quantified, so e.g. in the map-example, I<br>
| had expected "forall b . (GHC.Enum.Enum b, GHC.Num.Num b) => [b]"<br>
| <br>
| Can anyone explain what I'm missing?<br>
| <br>
| Regards,<br>
| Philip<br>
| <br>
| <br>
| <br>
| <br>
| <br>
| exprFromString :: String -> IA (Id,Type)<br>
| exprFromString str = do<br>
| dfs <- getDynFlags<br>
| let pp = showSDoc dfs . ppr<br>
| pst <- mkPState dfs buf <$> newRealSrcLoc<br>
| <br>
| {- Parse -}<br>
| (loc,rdr_expr) <- case unP parseStmt pst of<br>
| PFailed span err -> throwOneError (mkPlainErrMsg dfs span err)<br>
| POk pst' (Just (L l (ExprStmt rdr_expr _ _ _))) -> do<br>
| logWarningsReportErrors (getMessages pst')<br>
| return (l,rdr_expr)<br>
| POk pst' thing -> throw $ maybe EmptyParse (const<br>
| NonExpressionParse) thing<br>
| liftTcM $ do<br>
| fresh_it <- freshName loc str<br>
| <br>
| {- Rename -}<br>
| (rn_expr, fvs) <- checkNoErrs $ rnLExpr rdr_expr<br>
| <br>
| {- Typecheck -}<br>
| let binds = mkBinds fresh_it rn_expr fvs<br>
| <br>
| (((_bnds,((_tc_expr,res_ty),id)),untch),lie) <- captureConstraints .<br>
| captureUntouchables $<br>
| tcLocalBinds binds ((,) <$> tcInferRho rn_expr <*> tcLookupId<br>
| fresh_it)<br>
| ((qtvs, dicts, _bool, _evbinds), lie_top) <- captureConstraints $<br>
| simplifyInfer True False [(fresh_it, res_ty)] (untch,lie)<br>
| <br>
| let all_expr_ty = mkForAllTys qtvs (mkPiTypes dicts res_ty)<br>
| say str<br>
| say $ " pre-zonk: " ++ pp all_expr_ty<br>
| zonkTcType all_expr_ty<br>
| say $ " post-zonk: " ++ pp all_expr_ty<br>
| say $ " idType: " ++ pp (idType id)<br>
| say $ " tidied: " ++ pp (tidyTopType all_expr_ty)<br>
| <br>
| return (id,all_expr_ty)<br>
| where<br>
| say = liftIO . putStrLn<br>
| buf = stringToStringBuffer str<br>
| freshName loc str = (\u -> mkInternalName u name loc) <$> newUnique<br>
| where<br>
| name = mkOccNameFS varName $ fsLit $ "it" ++ show (lineOf loc)<br>
| isVarChar c = isAlphaNum c || c == '_' || c == '\''<br>
| lineOf (RealSrcSpan s) = srcSpanStartLine s<br>
| lineOf _ = -1<br>
| <br>
| mkBinds :: Name -> LHsExpr Name -> FreeVars -> HsLocalBinds Name<br>
| mkBinds nm e@(L l _) fvs = HsValBinds $ ValBindsOut [(NonRecursive,<br>
| unitBag the_bind)] []<br>
| where<br>
| the_bind = L l (mkTopFunBind (L l nm) [mkMatch [] e<br>
| emptyLocalBinds]) { bind_fvs = fvs }<br>
| <br>
| <br>
| <br>
| _______________________________________________<br>
| Glasgow-haskell-users mailing list<br>
| <a href="mailto:Glasgow-haskell-users@haskell.org">Glasgow-haskell-users@haskell.org</a><br>
| <a href="http://www.haskell.org/mailman/listinfo/glasgow-haskell-users">http://www.haskell.org/mailman/listinfo/glasgow-haskell-users</a><o:p></o:p></span></p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>