<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta name="robots" content="index,nofollow"> <title>AST - MLton Standard ML Compiler (SML Compiler)</title> <link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="common.css"> <link rel="stylesheet" type="text/css" charset="iso-8859-1" media="screen" href="screen.css"> <link rel="stylesheet" type="text/css" charset="iso-8859-1" media="print" href="print.css"> <link rel="Start" href="Home"> </head> <body lang="en" dir="ltr"> <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"> </script> <script type="text/javascript"> _uacct = "UA-833377-1"; urchinTracker(); </script> <table bgcolor = lightblue cellspacing = 0 style = "border: 0px;" width = 100%> <tr> <td style = " border: 0px; color: darkblue; font-size: 150%; text-align: left;"> <a class = mltona href="Home">MLton MLTONWIKIVERSION</a> <td style = " border: 0px; font-size: 150%; text-align: center; width: 50%;"> AST <td style = " border: 0px; text-align: right;"> <table cellspacing = 0 style = "border: 0px"> <tr style = "vertical-align: middle;"> </table> <tr style = "background-color: white;"> <td colspan = 3 style = " border: 0px; font-size:70%; text-align: right;"> <a href = "Home">Home</a> <a href = "TitleIndex">Index</a> </table> <div id="content" lang="en" dir="ltr"> AST is the <a href="IntermediateLanguage">IntermediateLanguage</a> produced by the <a href="FrontEnd">FrontEnd</a> and translated by <a href="Elaborate">Elaborate</a> to <a href="CoreML">CoreML</a>. <h2 id="head-55f8ebc805e65b5b71ddafdae390e3be2bcd69af">Description</h2> <p> The abstract syntax tree produced by the <a href="FrontEnd">FrontEnd</a>. </p> <h2 id="head-8781d615fd77be9578225c40ac67b9471394cced">Implementation</h2> <a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mlton/tags/on-MLTONWIKIVERSION-release/mlton/ast/ast-programs.sig?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">ast-programs.sig</a> <a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mlton/tags/on-MLTONWIKIVERSION-release/mlton/ast/ast-programs.fun?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">ast-programs.fun</a><br> <a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mlton/tags/on-MLTONWIKIVERSION-release/mlton/ast/ast-modules.sig?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">ast-modules.sig</a> <a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mlton/tags/on-MLTONWIKIVERSION-release/mlton/ast/ast-modules.fun?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">ast-modules.fun</a><br> <a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mlton/tags/on-MLTONWIKIVERSION-release/mlton/ast/ast-core.sig?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">ast-core.sig</a> <a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mlton/tags/on-MLTONWIKIVERSION-release/mlton/ast/ast-core.fun?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">ast-core.fun</a><br> <a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mlton/tags/on-MLTONWIKIVERSION-release/mlton/ast"><img src="moin-www.png" alt="[WWW]" height="11" width="11">ast</a> <h2 id="head-a6de4b535fedeaa8890bd890b60fe09f87b5f96a">Type Checking</h2> <p> The AST <a href="IntermediateLanguage">IntermediateLanguage</a> has no independent type checker. Type inference is performed on an AST program as part of <a href="Elaborate">Elaborate</a>. </p> <h2 id="head-35ec00231a68203708e39f0e2cc10b50c6bf62de">Details and Notes</h2> <ul> <li> <p> Source locations. MLton makes use of a relatively clean method for annotating the abstract syntax tree with source location information. Every source program phrase is "wrapped" with the <tt>WRAPPED</tt> interface: <pre class=code><B><FONT COLOR="#0000FF">signature</FONT></B> WRAPPED = <B><FONT COLOR="#0000FF">sig</FONT></B> <B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> node' </FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> obj </FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> dest: obj -> node' * Region.t <B><FONT COLOR="#A020F0">val</FONT></B> makeRegion': node' * SourcePos.t * SourcePos.t -> obj <B><FONT COLOR="#A020F0">val</FONT></B> makeRegion: node' * Region.t -> obj <B><FONT COLOR="#A020F0">val</FONT></B> node: obj -> node' <B><FONT COLOR="#A020F0">val</FONT></B> region: obj -> Region.t <B><FONT COLOR="#0000FF">end</FONT></B> </PRE> The key idea is that <tt>node'</tt> is the type of an unannotated syntax phrase and <tt>obj</tt> is the type of its annotated counterpart. In the implementation, every <tt>node'</tt> is annotated with a <tt>Region.t</tt> (<a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mlton/tags/on-MLTONWIKIVERSION-release/mlton/control/region.sig?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">region.sig</a>,<a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mlton/tags/on-MLTONWIKIVERSION-release/mlton/control/region.sml?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">region.sml</a>), which describes the syntax phrase's left source position and right source position, where <tt>SourcePos.t</tt> (<a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mlton/tags/on-MLTONWIKIVERSION-release/mlton/control/source-pos.sig?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">source-pos.sig</a>,<a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mlton/tags/on-MLTONWIKIVERSION-release/mlton/control/source-pos.sml?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">source-pos.sml</a>) denotes a particular file, line, and column. A typical use of the <tt>WRAPPED</tt> interface is illustrated by the following code: <pre class=code> <B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> node </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">App</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> Longcon.t * t </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Const</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> Const.t </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Constraint</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> t * Type.t </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">FlatApp</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> t vector </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Layered</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> {constraint: Type.t option, fixop: Fixop.t, pat: t, var: Var.t} </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">List</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> t vector </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Record</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> {flexible: bool, items: (Record.Field.t * Item.t) vector} </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Tuple</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> t vector </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Var</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> {fixop: Fixop.t, name: Longvid.t} </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Wild</FONT> </FONT></B><B><FONT COLOR="#0000FF">include</FONT></B> WRAPPED <B><FONT COLOR="#0000FF">sharing</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> node' </FONT></B>=<B><FONT COLOR="#228B22"> node </FONT></B><B><FONT COLOR="#0000FF">sharing</FONT></B> <B><FONT COLOR="#0000FF">type</FONT></B><B><FONT COLOR="#228B22"> obj </FONT></B>=<B><FONT COLOR="#228B22"> t </FONT></B></PRE> Thus, AST nodes are cleanly separated from source locations. By way of contrast, consider the approach taken by <a href="SMLNJ">SML/NJ</a> (and also by the <a href="CKitLibrary">CKit Library</a>). Each datatype denoting a syntax phrase dedicates a special constructor for annotating source locations: <pre class=code> <B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> pat </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">WildPat</FONT> <I><FONT COLOR="#B22222">(* empty pattern *)</FONT></I> </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">AppPat</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> {constr:pat,argument:pat} <I><FONT COLOR="#B22222">(* application *)</FONT></I> </FONT></B>|<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">MarkPat</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> pat * region <I><FONT COLOR="#B22222">(* mark a pattern *)</FONT></I> </FONT></B></PRE> The main drawback of this approach is that static type checking is not sufficient to guarantee that the AST emitted from the front-end is properly annotated. </p> </li> </ul> </div> <p> <hr> Last edited on 2010-05-13 19:55:19 by <span title="fenrir.cs.rit.edu"><a href="MatthewFluet">MatthewFluet</a></span>. </body></html>