<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>weaselhat &#187; Formal Methods</title>
	<atom:link href="http://www.weaselhat.com/category/formal/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.weaselhat.com</link>
	<description></description>
	<lastBuildDate>Wed, 01 Feb 2012 20:18:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>QuickRedex</title>
		<link>http://www.weaselhat.com/2012/02/01/quickredex/</link>
		<comments>http://www.weaselhat.com/2012/02/01/quickredex/#comments</comments>
		<pubDate>Wed, 01 Feb 2012 20:18:01 +0000</pubDate>
		<dc:creator>Michael Greenberg</dc:creator>
				<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.weaselhat.com/?p=321</guid>
		<description><![CDATA[Following Robby Findler&#8217;s excellent presentation on PLT Redex at POPL (check out the paper and presentation!), I hacked up something similar in Haskell. Naturally, it&#8217;s all manual, and there&#8217;s none of the publication or visualization support, but the essence is there. Here&#8217;s the code for the untyped lambda calculus: Haskell [Show Styled Code]: module Untyped [...]]]></description>
			<content:encoded><![CDATA[<p>Following <a href="http://eecs.northwestern.edu/~robby/" title="I am the walrus.">Robby Findler&#8217;s</a> excellent presentation on <a href="http://redex.plt-scheme.org/">PLT Redex</a> at POPL (check out the <a href="http://eecs.northwestern.edu/~robby/lightweight-metatheory/">paper and presentation!</a>), I hacked up something similar in Haskell.  Naturally, it&#8217;s all manual, and there&#8217;s none of the publication or visualization support, but the essence is there.  Here&#8217;s the code for the untyped lambda calculus:</p>
<div class="synthi_code" style="display:none;" id ="plain_synthi_4f2d1b5f0ca00">
<div class="synthi_header" style="font-weight:bold;"> Haskell <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_4f2d1b5f0ca00').style.display='block';document.getElementById('plain_synthi_4f2d1b5f0ca00').style.display='none';return false">Show Styled Code</a>]:</span></div>
<pre style="width:100%;overflow:auto;">
module Untyped where

import Control.Monad
import Data.Maybe
import Test.QuickCheck

data Expr =
    Var Int
  | Lambda Expr
  | App Expr Expr

showExpr :: Int -> Expr -> String
showExpr _b (Var n) = &#034;var&#034; ++ show n
showExpr b (Lambda e) = &#034;lambda. &#034; ++ showExpr (b+1) e
showExpr b (App e1 e2) = &#034;(&#034; ++ showExpr b e1 ++ &#034; &#034; ++ showExpr b e2 ++ &#034;)&#034;

instance Show Expr where
  show e = showExpr 0 e

size :: Expr -> Int
size (Var _) = 1
size (Lambda e) = 1 + size e
size (App e1 e2) = size e1 + size e2

wellLexed :: Expr -> Bool
wellLexed = wellLexedAux 0
  where wellLexedAux :: Int -> Expr -> Bool
        wellLexedAux b (Var n) = 0 <= n &#038;&#038; n < b
        wellLexedAux b (Lambda e) = wellLexedAux (b+1) e
        wellLexedAux b (App e1 e2) = wellLexedAux b e1 &#038;&#038; wellLexedAux b e2

arbitraryExpr :: Int -> Int -> Gen Expr
arbitraryExpr n 0 = oneof [return $ Lambda $ Var 0] -- base case
arbitraryExpr n max = do
  oneof $
    (if n < max
     then [arbitraryExpr (n+1) (max-1) >>= return . Lambda]
     else []) ++
    (if n > 0
     then [choose (0,n-1) >>= return . Var]
     else []) ++
    [do { e1 <- arbitraryExpr n (max `div` 2);
          e2 <- arbitraryExpr n (max `div` 2);
          return $ App e1 e2 }]

instance Arbitrary Expr where
  arbitrary = sized $ \max -> arbitraryExpr 0 max

-- All the terms we generate should be well-lexed
prop_WellLexed e = collect (size e) $ wellLexed e

subst :: Expr -> Int -> Expr -> Expr
subst e n (Var n')
  | n == n' = e
  | otherwise = (Var n')
subst e n (Lambda e') = Lambda $ subst e (n+1) e'
subst e n (App e1 e2) = App (subst e n e1) (subst e n e2)

shift :: Int -> Expr -> Expr
shift i (Var n) = if n < i then Var n else Var (n-1)
shift i (Lambda e) = Lambda $ shift (i+1) e
shift i (App e1 e2) = App (shift i e1) (shift i e2)

value :: Expr -> Bool
value (Lambda e) = True
value _ = False

-- we can run this with m = Maybe or m = List
step :: MonadPlus m => Expr -> m Expr
step (Var _) = mzero -- unbound variable
step (Lambda e) = mzero -- found a term
step (App e1@(Lambda e11) e2)
  | value e2 = return $ shift 0 $ subst e2 0 e11
  | otherwise = do
    e2' <- step e2
    return $ App e1 e2'
step (App e1 e2) = do
  e1' <- step e1
  return $ App e1' e2

-- if we can step, we'd better preserve scope
prop_StepWellLexed e = isJust next ==> wellLexed (fromJust next)
  where next = step e

-- verify progress
prop_Progress e = (classify isValue &#034;value&#034;) $ (classify (not isValue) &#034;step&#034;) $ value e || isJust (step e)
  where isValue = value e

-- use the List monad to ensure determinacy
prop_Deterministic e = nextStates > 0 ==> nextStates == 1
  where nextStates = length $ step e
</pre>
</div>
<div class="synthi_code" style="display:block;" id ="styled_synthi_4f2d1b5f0ca00">
<div class="synthi_header" style="font-weight:bold;"> Haskell <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_4f2d1b5f0ca00').style.display='block';document.getElementById('styled_synthi_4f2d1b5f0ca00').style.display='none';return false">Show Plain Code</a>]:</span></div>
<div class="haskell" style="font-family: monospace;">
<ol>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #06c; font-weight: bold;">module</span> Untyped <span style="color: #06c; font-weight: bold;">where</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #06c; font-weight: bold;">import</span> Control.<span style="color: #060;">Monad</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #06c; font-weight: bold;">import</span> <span style="color: #06c; font-weight: bold;">Data</span>.<span style="color: #060;">Maybe</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #06c; font-weight: bold;">import</span> Test.<span style="color: #060;">QuickCheck</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #06c; font-weight: bold;">data</span> Expr =</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Var Int</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; | Lambda Expr</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; | App Expr Expr</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">showExpr :: Int -&gt; Expr -&gt; String</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">showExpr _b <span style="color: #6c6;">&#40;</span>Var n<span style="color: #6c6;">&#41;</span> = <span style="color: #3cb371;">&quot;var&quot;</span> ++ show n</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">showExpr b <span style="color: #6c6;">&#40;</span>Lambda e<span style="color: #6c6;">&#41;</span> = <span style="color: #3cb371;">&quot;lambda. &quot;</span> ++ showExpr <span style="color: #6c6;">&#40;</span>b<span style="color: #c6c;">+1</span><span style="color: #6c6;">&#41;</span> e</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">showExpr b <span style="color: #6c6;">&#40;</span>App e1 e2<span style="color: #6c6;">&#41;</span> = <span style="color: #3cb371;">&quot;(&quot;</span> ++ showExpr b e1 ++ <span style="color: #3cb371;">&quot; &quot;</span> ++ showExpr b e2 ++ <span style="color: #3cb371;">&quot;)&quot;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #06c; font-weight: bold;">instance</span> Show Expr <span style="color: #06c; font-weight: bold;">where</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; show e = showExpr <span style="color: #c6c;">0</span> e</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">size :: Expr -&gt; Int</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">size <span style="color: #6c6;">&#40;</span>Var _<span style="color: #6c6;">&#41;</span> = <span style="color: #c6c;">1</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">size <span style="color: #6c6;">&#40;</span>Lambda e<span style="color: #6c6;">&#41;</span> = <span style="color: #c6c;">1</span> + size e</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">size <span style="color: #6c6;">&#40;</span>App e1 e2<span style="color: #6c6;">&#41;</span> = size e1 + size e2</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">wellLexed :: Expr -&gt; Bool</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">wellLexed = wellLexedAux <span style="color: #c6c;">0</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #06c; font-weight: bold;">where</span> wellLexedAux :: Int -&gt; Expr -&gt; Bool</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; wellLexedAux b <span style="color: #6c6;">&#40;</span>Var n<span style="color: #6c6;">&#41;</span> = <span style="color: #c6c;">0</span> &lt;= n &amp;&amp; n &lt; b</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; wellLexedAux b <span style="color: #6c6;">&#40;</span>Lambda e<span style="color: #6c6;">&#41;</span> = wellLexedAux <span style="color: #6c6;">&#40;</span>b<span style="color: #c6c;">+1</span><span style="color: #6c6;">&#41;</span> e</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; wellLexedAux b <span style="color: #6c6;">&#40;</span>App e1 e2<span style="color: #6c6;">&#41;</span> = wellLexedAux b e1 &amp;&amp; wellLexedAux b e2</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">arbitraryExpr :: Int -&gt; Int -&gt; Gen Expr</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">arbitraryExpr n <span style="color: #c6c;">0</span> = oneof <span style="color: #6c6;">&#91;</span><span style="color: #06c; font-weight: bold;">return</span> $ Lambda $ Var <span style="color: #c6c;">0</span><span style="color: #6c6;">&#93;</span> <span style="">&#8211; base case</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">arbitraryExpr n max = <span style="color: #06c; font-weight: bold;">do</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; oneof $ </div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">if</span> n &lt; max </div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">then</span> <span style="color: #6c6;">&#91;</span>arbitraryExpr <span style="color: #6c6;">&#40;</span>n<span style="color: #c6c;">+1</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>max<span style="color: #c6c;">-1</span><span style="color: #6c6;">&#41;</span> &gt;&gt;= <span style="color: #06c; font-weight: bold;">return</span> . <span style="color: #060;">Lambda</span><span style="color: #6c6;">&#93;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">else</span> <span style="color: #6c6;">&#91;</span><span style="color: #6c6;">&#93;</span><span style="color: #6c6;">&#41;</span> ++ </div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">if</span> n &gt; <span style="color: #c6c;">0</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">then</span> <span style="color: #6c6;">&#91;</span>choose <span style="color: #6c6;">&#40;</span><span style="color: #c6c;">0</span>,n<span style="color: #c6c;">-1</span><span style="color: #6c6;">&#41;</span> &gt;&gt;= <span style="color: #06c; font-weight: bold;">return</span> . <span style="color: #060;">Var</span><span style="color: #6c6;">&#93;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">else</span> <span style="color: #6c6;">&#91;</span><span style="color: #6c6;">&#93;</span><span style="color: #6c6;">&#41;</span> ++</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #6c6;">&#91;</span><span style="color: #06c; font-weight: bold;">do</span> <span style="color: #6c6;">&#123;</span> e1 &lt;- arbitraryExpr n <span style="color: #6c6;">&#40;</span>max `div` <span style="color: #c6c;">2</span><span style="color: #6c6;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e2 &lt;- arbitraryExpr n <span style="color: #6c6;">&#40;</span>max `div` <span style="color: #c6c;">2</span><span style="color: #6c6;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #06c; font-weight: bold;">return</span> $ App e1 e2 <span style="color: #6c6;">&#125;</span><span style="color: #6c6;">&#93;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #06c; font-weight: bold;">instance</span> Arbitrary Expr <span style="color: #06c; font-weight: bold;">where</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; arbitrary = sized $ \max -&gt; arbitraryExpr <span style="color: #c6c;">0</span> max</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="">&#8211; All the terms we generate should be well-lexed</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">prop_WellLexed e = collect <span style="color: #6c6;">&#40;</span>size e<span style="color: #6c6;">&#41;</span> $ wellLexed e</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">subst :: Expr -&gt; Int -&gt; Expr -&gt; Expr</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">subst e n <span style="color: #6c6;">&#40;</span>Var n&#8217;<span style="color: #6c6;">&#41;</span> </div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; | n == n&#8217; = e</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; | <span style="color: #06c; font-weight: bold;">otherwise</span> = <span style="color: #6c6;">&#40;</span>Var n&#8217;<span style="color: #6c6;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">subst e n <span style="color: #6c6;">&#40;</span>Lambda e&#8217;<span style="color: #6c6;">&#41;</span> = Lambda $ subst e <span style="color: #6c6;">&#40;</span>n<span style="color: #c6c;">+1</span><span style="color: #6c6;">&#41;</span> e&#8217;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">subst e n <span style="color: #6c6;">&#40;</span>App e1 e2<span style="color: #6c6;">&#41;</span> = App <span style="color: #6c6;">&#40;</span>subst e n e1<span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>subst e n e2<span style="color: #6c6;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">shift :: Int -&gt; Expr -&gt; Expr</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">shift i <span style="color: #6c6;">&#40;</span>Var n<span style="color: #6c6;">&#41;</span> = <span style="color: #06c; font-weight: bold;">if</span> n &lt; i <span style="color: #06c; font-weight: bold;">then</span> Var n <span style="color: #06c; font-weight: bold;">else</span> Var <span style="color: #6c6;">&#40;</span>n<span style="color: #c6c;">-1</span><span style="color: #6c6;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">shift i <span style="color: #6c6;">&#40;</span>Lambda e<span style="color: #6c6;">&#41;</span> = Lambda $ shift <span style="color: #6c6;">&#40;</span>i<span style="color: #c6c;">+1</span><span style="color: #6c6;">&#41;</span> e</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">shift i <span style="color: #6c6;">&#40;</span>App e1 e2<span style="color: #6c6;">&#41;</span> = App <span style="color: #6c6;">&#40;</span>shift i e1<span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>shift i e2<span style="color: #6c6;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">value :: Expr -&gt; Bool</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">value <span style="color: #6c6;">&#40;</span>Lambda e<span style="color: #6c6;">&#41;</span> = <span style="color: #06c; font-weight: bold;">True</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">value _ = <span style="color: #06c; font-weight: bold;">False</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="">&#8211; we can run this with m = Maybe or m = List</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">step :: MonadPlus m =&gt; Expr -&gt; m Expr</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">step <span style="color: #6c6;">&#40;</span>Var _<span style="color: #6c6;">&#41;</span> = mzero <span style="">&#8211; unbound variable</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">step <span style="color: #6c6;">&#40;</span>Lambda e<span style="color: #6c6;">&#41;</span> = mzero <span style="">&#8211; found a term</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">step <span style="color: #6c6;">&#40;</span>App e1@<span style="color: #6c6;">&#40;</span>Lambda e11<span style="color: #6c6;">&#41;</span> e2<span style="color: #6c6;">&#41;</span> </div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; | value e2 = <span style="color: #06c; font-weight: bold;">return</span> $ shift <span style="color: #c6c;">0</span> $ subst e2 <span style="color: #c6c;">0</span> e11</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; | <span style="color: #06c; font-weight: bold;">otherwise</span> = <span style="color: #06c; font-weight: bold;">do</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; e2&#8242; &lt;- step e2</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #06c; font-weight: bold;">return</span> $ App e1 e2&#8242;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">step <span style="color: #6c6;">&#40;</span>App e1 e2<span style="color: #6c6;">&#41;</span> = <span style="color: #06c; font-weight: bold;">do</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; e1&#8242; &lt;- step e1</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #06c; font-weight: bold;">return</span> $ App e1&#8242; e2</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="">&#8211; if we can step, we&#8217;d better preserve scope</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">prop_StepWellLexed e = isJust next ==&gt; wellLexed <span style="color: #6c6;">&#40;</span>fromJust next<span style="color: #6c6;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #06c; font-weight: bold;">where</span> next = step e</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="">&#8211; verify progress</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">prop_Progress e = <span style="color: #6c6;">&#40;</span>classify isValue <span style="color: #3cb371;">&quot;value&quot;</span><span style="color: #6c6;">&#41;</span> $ <span style="color: #6c6;">&#40;</span>classify <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">not</span> isValue<span style="color: #6c6;">&#41;</span> <span style="color: #3cb371;">&quot;step&quot;</span><span style="color: #6c6;">&#41;</span> $ value e || isJust <span style="color: #6c6;">&#40;</span>step e<span style="color: #6c6;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #06c; font-weight: bold;">where</span> isValue = value e</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="">&#8211; use the List monad to ensure determinacy</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">prop_Deterministic e = nextStates &gt; <span style="color: #c6c;">0</span> ==&gt; nextStates == <span style="color: #c6c;">1</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #06c; font-weight: bold;">where</span> nextStates = length $ step e </div>
</li>
</ol>
</div>
</div>
<p>One of the trickiest things here was making sure I was generating well lexed lambda terms that were small enough to be tractable.  It would have been even harder, I think, with a more explicit representation of variables or binding.  Thoughts, variations?  Or, perhaps, complaints that I should have done this with GADTS or in Coq?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.weaselhat.com/2012/02/01/quickredex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Towards a core calculus for implicitly migration-capable applications</title>
		<link>http://www.weaselhat.com/2011/10/25/migration/</link>
		<comments>http://www.weaselhat.com/2011/10/25/migration/#comments</comments>
		<pubDate>Tue, 25 Oct 2011 16:38:31 +0000</pubDate>
		<dc:creator>Michael Greenberg</dc:creator>
				<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Submissions]]></category>

		<guid isPermaLink="false">http://www.weaselhat.com/?p=291</guid>
		<description><![CDATA[Yitzhak Mandelbaum and I have been thinking about language support for program migration. We submitted a short paper, Towards a core calculus for implicitly migration-capable applications, to PEPM&#8217;12 summarizing what we&#8217;ve done so far and the direction we&#8217;re headed. Here&#8217;s the abstract: Mobile computational devices, like smartphones, tablets and laptops, have become a standard part [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www2.research.att.com/~yitzhak/">Yitzhak Mandelbaum</a> and I have been thinking about language support for program migration.  We submitted a short paper, <a class="paper" href="http://www.cis.upenn.edu/~mgree/papers/pepm2012_submission.pdf">Towards a core calculus for implicitly migration-capable applications</a>, to <a href="http://www.program-transformation.org/PEPM12">PEPM&#8217;12</a> summarizing what we&#8217;ve done so far and the direction we&#8217;re headed.  Here&#8217;s the abstract:</p>
<blockquote><p>
Mobile computational devices, like smartphones, tablets and laptops, have become a standard part of the computing landscape. Moreover, many users regularly interact with an assortment of devices, including mobile ones. Therefore, the ability to migrate UI-enabled applications is becoming increasingly important. We describe a design-pattern for applications to simplify support for user-session migration and provide an overview of a lambda calculus for which significant elements of the design pattern can be implemented automatically.
</p></blockquote>
<p>We&#8217;d appreciate any ideas, comments, or questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.weaselhat.com/2011/10/25/migration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ESOP 2011 Papers</title>
		<link>http://www.weaselhat.com/2011/01/04/esop-2011-papers/</link>
		<comments>http://www.weaselhat.com/2011/01/04/esop-2011-papers/#comments</comments>
		<pubDate>Tue, 04 Jan 2011 15:30:01 +0000</pubDate>
		<dc:creator>Michael Greenberg</dc:creator>
				<category><![CDATA[Papers]]></category>
		<category><![CDATA[Programming Languages]]></category>

		<guid isPermaLink="false">http://www.weaselhat.com/?p=237</guid>
		<description><![CDATA[I&#8217;m happy to announce the final versions of two ESOP 2011 papers. Polymorphic Contracts was work done with Jo&#227;o Belo, Atsushi Igarashi, and my advisor, Benjamin Pierce. Measure Transformer Semantics for Bayesian Machine Learning was work done at my internship at MSR Cambridge this summer; the real heroes of this story are Johannes Borgstr&#246;m, Andy [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m happy to announce the final versions of two ESOP 2011 papers.  <a class="paper" href="http://www.cis.upenn.edu/~mgree/papers/esop2011_fh.pdf">Polymorphic Contracts</a> was work done with <a href="http://pwp.net.ipl.pt/cc.isel/jbelo/">Jo&atilde;o Belo</a>, <a href="http://www.sato.kuis.kyoto-u.ac.jp/~igarashi/index.html.en">Atsushi Igarashi</a>, and my advisor, <a href="http://www.cis.upenn.edu/~bcpierce/">Benjamin Pierce</a>.  <a class="paper" href="http://www.cis.upenn.edu/~mgree/papers/esop2011_mts.pdf">Measure Transformer Semantics for Bayesian Machine Learning</a> was work done at my internship at MSR Cambridge this summer; the real heroes of this story are Johannes Borgstr&ouml;m, <a href="http://research.microsoft.com/en-us/um/people/adg/">Andy Gordon</a>, James Margetson, and <a href="http://research.microsoft.com/en-us/people/jvangael/">Jurgen Van Gael</a>.</p>
<p>See you in Saarbr&uuml;cken?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.weaselhat.com/2011/01/04/esop-2011-papers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Polymorphic Contracts</title>
		<link>http://www.weaselhat.com/2010/10/27/polymorphic-contracts/</link>
		<comments>http://www.weaselhat.com/2010/10/27/polymorphic-contracts/#comments</comments>
		<pubDate>Wed, 27 Oct 2010 15:27:00 +0000</pubDate>
		<dc:creator>Michael Greenberg</dc:creator>
				<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Submissions]]></category>

		<guid isPermaLink="false">http://www.weaselhat.com/?p=226</guid>
		<description><![CDATA[Jo&#227;o Belo, Atsushi Igarashi, Benjamin Pierce, and I submitted a paper, Polymorphic Contracts, to ESOP&#8217;11. Here&#8217;s the abstract: Manifest contracts track precise properties by refining types with predicates&#8212;e.g., {x:Int &#124; x > 0} denotes the positive integers. Contracts and polymorphism make a natural combination: programmers can give abstract types strong contracts, precisely stating pre- and [...]]]></description>
			<content:encoded><![CDATA[<p>Jo&atilde;o Belo, <a href="http://www.sato.kuis.kyoto-u.ac.jp/~igarashi/index.html.en">Atsushi Igarashi</a>, <a href="http://www.cis.upenn.edu/~bcpierce/">Benjamin Pierce</a>, and I submitted a paper, <a class="paper" href="http://www.seas.upenn.edu/~mgree/papers/esop2011sub_fh.pdf">Polymorphic Contracts</a>, to <a href="http://software.imdea.org/~gbarthe/esop11/">ESOP&#8217;11</a>.  Here&#8217;s the abstract:</p>
<blockquote><p>
Manifest contracts track precise properties by refining types with predicates&mdash;e.g., <tt>{x:Int | x > 0}</tt> denotes the positive integers. Contracts and polymorphism make a natural combination: programmers can give abstract types strong contracts, precisely stating pre- and post-conditions while hiding implementation details&mdash;for example, an abstract type of stacks might specify that the pop operation has input type <tt>{x:&alpha; Stack | not (empty x)}</tt>. We formalize this combination by defining FH, a polymorphic calculus with manifest contracts, and establishing its fundamental properties, including type soundness and relational parametricity. Our development relies on a significant technical improvement over earlier presentations of contracts: instead of introducing a denotational model to break a problematic circularity between typing, subtyping, and evaluation, we develop the metatheory of contracts in a completely syntactic fashion, omitting subtyping from the core system and recovering it post facto as a derived property.
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.weaselhat.com/2010/10/27/polymorphic-contracts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Resurgence of Parallelism</title>
		<link>http://www.weaselhat.com/2010/05/29/the-resurgence-of-parallelism/</link>
		<comments>http://www.weaselhat.com/2010/05/29/the-resurgence-of-parallelism/#comments</comments>
		<pubDate>Sat, 29 May 2010 09:17:20 +0000</pubDate>
		<dc:creator>Michael Greenberg</dc:creator>
				<category><![CDATA[Programming Languages]]></category>

		<guid isPermaLink="false">http://www.weaselhat.com/?p=222</guid>
		<description><![CDATA[There&#8217;s an article in the CACM trying to restore historical context to research in parallel computation: The Resurgence of Parallelism. (One of) the answer(s), it seems, is functional programming: Parallelism is not new; the realization that it is essential for continued progress in high-performance computing is. Parallelism is not yet a paradigm, but may become [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s an article in the CACM trying to restore historical context to research in parallel computation: <a href="http://cacm.acm.org/magazines/2010/6/92479-the-resurgence-of-parallelism/fulltext">The Resurgence of Parallelism</a>.  (One of) the answer(s), it seems, is functional programming:</p>
<blockquote><p>
Parallelism is not new; the realization that it is essential for continued progress in high-performance computing is. Parallelism is not yet a paradigm, but may become so if enough people adopt it as the standard practice and standard way of thinking about computation.</p>
<p>The new era of research in parallel processing can benefit from the results of the extensive research in the 1960s and 1970s, avoiding rediscovery of ideas already documented in the literature: shared memory multiprocessing, determinacy, functional programming, and virtual memory.
</p></blockquote>
<p>Via <a href="http://lambda-the-ultimate.org/node/3963">LtU</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.weaselhat.com/2010/05/29/the-resurgence-of-parallelism/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nested functions in GCC</title>
		<link>http://www.weaselhat.com/2010/03/03/nested-functions-in-gcc/</link>
		<comments>http://www.weaselhat.com/2010/03/03/nested-functions-in-gcc/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 16:06:40 +0000</pubDate>
		<dc:creator>Michael Greenberg</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.weaselhat.com/?p=208</guid>
		<description><![CDATA[GCC supports &#8220;nested functions&#8221; using the -fnested-functions flag. When I first saw this, I was excited: closures in C! In the famous words of Admiral Ackbar, &#8220;it&#8217;s a trap!&#8221; C [Show Styled Code]: #include typedef int (*fptr)(int); fptr f(int arg) { int nested_function(int nested_arg) { return arg + nested_arg; } return &#038;nested_function; } void smash(int [...]]]></description>
			<content:encoded><![CDATA[<p>GCC supports &#8220;nested functions&#8221; using the <tt>-fnested-functions</tt> flag.  When I first saw this, I was excited: closures in C!  In the famous words of Admiral Ackbar, &#8220;it&#8217;s a trap!&#8221;</p>
<div class="synthi_code" style="display:none;" id ="plain_synthi_4f2d1b5f35a4b">
<div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_4f2d1b5f35a4b').style.display='block';document.getElementById('plain_synthi_4f2d1b5f35a4b').style.display='none';return false">Show Styled Code</a>]:</span></div>
<pre style="width:100%;overflow:auto;">
#include <stdio.h>

typedef int (*fptr)(int);

fptr f(int arg) {
  int nested_function(int nested_arg) {
    return arg + nested_arg;
  }

  return &#038;nested_function;
}

void smash(int arg) {
  return;
}

int main(void) {
  fptr g = f(10);
  printf(&#034;%d\n&#034;, (*g)(5));
  smash(12);
  // printf(&#034;%d\n&#034;, (*g)(5));
  fptr h = f(12);
  printf(&#034;%d\n&#034;, (*g)(5));
  printf(&#034;%d\n&#034;, (*h)(5));

  return 0;
}
</pre>
</div>
<div class="synthi_code" style="display:block;" id ="styled_synthi_4f2d1b5f35a4b">
<div class="synthi_header" style="font-weight:bold;"> C <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_4f2d1b5f35a4b').style.display='block';document.getElementById('styled_synthi_4f2d1b5f35a4b').style.display='none';return false">Show Plain Code</a>]:</span></div>
<div class="c" style="font-family: monospace;">
<ol>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">int</span> <span style="color: #66cc66;">&#40;</span>*fptr<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">fptr f<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> arg<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #993333;">int</span> nested_function<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> nested_arg<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #b1b100;">return</span> arg + nested_arg;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #b1b100;">return</span> &amp;nested_function;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">void</span> smash<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> arg<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #b1b100;">return</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">int</span> main<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">void</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; fptr g = f<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, <span style="color: #66cc66;">&#40;</span>*g<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; smash<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">// printf(&quot;%d\n&quot;, (*g)(5));</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; fptr h = f<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, <span style="color: #66cc66;">&#40;</span>*g<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, <span style="color: #66cc66;">&#40;</span>*h<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
<p>Try compiling (<tt>gcc -fnested-functions</tt>).  What does the second call to <tt>g</tt> produce&#8212;15 or 17?  Try uncommenting line 21.  What happens?  Does commenting out line 20 affect this?  What if line 19 is commented out, but lines 20 and 21 are uncommented?</p>
<p>I&#8217;m not sure this feature is worth it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.weaselhat.com/2010/03/03/nested-functions-in-gcc/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>POPL 2010</title>
		<link>http://www.weaselhat.com/2010/01/22/popl-2010/</link>
		<comments>http://www.weaselhat.com/2010/01/22/popl-2010/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 15:22:37 +0000</pubDate>
		<dc:creator>Michael Greenberg</dc:creator>
				<category><![CDATA[Presentations]]></category>
		<category><![CDATA[Programming Languages]]></category>

		<guid isPermaLink="false">http://www.weaselhat.com/?p=197</guid>
		<description><![CDATA[POPL 2010 has been a blast, with lots of great papers and conversation. I presented Contracts Made Manifest at 10:30am today, and the follow-up discussions contained enough ideas for years of research. (So stay tuned!)]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.cse.psu.edu/popl/10/">POPL 2010</a> has been a blast, with lots of great papers and conversation.  I presented <a class="paper" href="http://www.cis.upenn.edu/~mgree/papers/popl2010_contracts_presentation.pdf">Contracts Made Manifest</a> at 10:30am today, and the follow-up discussions contained enough ideas for years of research.  (So stay tuned!)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.weaselhat.com/2010/01/22/popl-2010/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Contracts Made Manifest: final version</title>
		<link>http://www.weaselhat.com/2009/11/03/contracts-made-manifest-final-version/</link>
		<comments>http://www.weaselhat.com/2009/11/03/contracts-made-manifest-final-version/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 17:03:30 +0000</pubDate>
		<dc:creator>Michael Greenberg</dc:creator>
				<category><![CDATA[Papers]]></category>
		<category><![CDATA[Programming Languages]]></category>

		<guid isPermaLink="false">http://www.weaselhat.com/?p=167</guid>
		<description><![CDATA[We&#8217;ve sent off the final version of Contracts Made Manifest. There have been quite a few improvements since submission, the most important of which is captured by Figure 1 from our paper: Our submission only addressed lax &#955;C, where we had an inexact translation &#966; into &#955;H and an exact translation &#968; out of &#955;H. [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve sent off the final version of <a class="paper" href="http://www.cis.upenn.edu/~mgree/papers/popl2010_contracts.pdf" title="I think the link works, this time!">Contracts Made Manifest</a>.  There have been quite a few improvements since submission, the most important of which is captured by Figure 1 from our paper:</p>
<p><center><img src="http://www.weaselhat.com/wp-content/uploads/2009/11/axis.png" alt="The axis of blame" title="The axis of blame" width="369" height="134" class="size-full wp-image-168" /></center></p>
<p>Our submission only addressed lax &lambda;<sub>C</sub>, where we had an inexact translation &phi; into &lambda;<sub>H</sub> and an exact translation &psi; out of &lambda;<sub>H</sub>.  We show a dual situation for picky &lambda;<sub>C</sub>, where &phi; is exact and &psi; is inexact.  Intuitively, languages farther to the right on the &#8220;axis of blame&#8221; are pickier.  Translating from right to left preserves behavior exactly, but left-to-right translations generate terms that can blame more than their pre-images.  (There are examples in the paper.)  I should note that lax and picky &lambda;<sub>C</sub> seem to be the extremes of the axis of blame: I don&#8217;t see a natural way to be laxer than lax &lambda;<sub>C</sub> or pickier than picky &lambda;<sub>C</sub>.</p>
<p>We also show that restricting these calculi to first-order dependency leaves them exactly equivalent; before, we could only show an exact equivalence by eliminating all dependency.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.weaselhat.com/2009/11/03/contracts-made-manifest-final-version/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Locally installing LLVM with Ocaml bindings</title>
		<link>http://www.weaselhat.com/2009/09/24/llvm-ocaml-loca/</link>
		<comments>http://www.weaselhat.com/2009/09/24/llvm-ocaml-loca/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 19:44:31 +0000</pubDate>
		<dc:creator>Michael Greenberg</dc:creator>
				<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.weaselhat.com/?p=145</guid>
		<description><![CDATA[We can&#8217;t install software into the /usr tree at my office, so I end up having local installs of lots of software. Some things, like GODI, play well with this. I had some trouble finding the right way to get LLVM&#8216;s Ocaml bindings to work, so I figured I&#8217;d share the wealth. The following instructions [...]]]></description>
			<content:encoded><![CDATA[<p>We can&#8217;t install software into the /usr tree at my office, so I end up having local installs of lots of software.  Some things, like <a href="http://godi.camlcity.org/godi/index.html">GODI</a>, play well with this.  I had some trouble finding the right way to get <a href="http://llvm.org">LLVM</a>&#8216;s Ocaml bindings to work, so I figured I&#8217;d share the wealth.  The following instructions will put an install into the directory <tt>$PREFIX/llvm-install</tt>.</p>
<p>Here are the steps; they&#8217;re followed by a plain English explanation.</p>
<div class="synthi_code" style="display:none;" id ="plain_synthi_4f2d1b5f5ac19">
<div class="synthi_header" style="font-weight:bold;"> Bash <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_4f2d1b5f5ac19').style.display='block';document.getElementById('plain_synthi_4f2d1b5f5ac19').style.display='none';return false">Show Styled Code</a>]:</span></div>
<pre style="width:100%;overflow:auto;">
cd $PREFIX
svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
wget http://llvm.org/releases/2.5/llvm-gcc4.2-2.5-x86-linux-RHEL4.tar.gz
tar xzf llvm-gcc4.2-2.5-x86-linux-RHEL4.tar.gz
mkdir llvm-objects llvm-install
cd llvm-objects
../llvm/configure --with-llvmgccdir=$PREFIX/llvm-gcc4.2-2.5-x86-linux-RHEL4 --enable-optimized --enable-jit --prefix=$PREFIX/llvm-install --with-ocaml-libdir=$GODI_PATH/lib/ocaml/std-lib
make
make install
</pre>
</div>
<div class="synthi_code" style="display:block;" id ="styled_synthi_4f2d1b5f5ac19">
<div class="synthi_header" style="font-weight:bold;"> Bash <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_4f2d1b5f5ac19').style.display='block';document.getElementById('styled_synthi_4f2d1b5f5ac19').style.display='none';return false">Show Plain Code</a>]:</span></div>
<div class="bash" style="font-family: monospace;">
<ol>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066;">cd</span> <span style="color: #0000ff;">$PREFIX</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">wget http://llvm.org/releases/<span style="color: #cc66cc;">2.5</span>/llvm-gcc4<span style="color: #cc66cc;">.2</span><span style="color: #cc66cc;">-2.5</span>-x86-linux-RHEL4.tar.gz</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">tar xzf llvm-gcc4<span style="color: #cc66cc;">.2</span><span style="color: #cc66cc;">-2.5</span>-x86-linux-RHEL4.tar.gz</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">mkdir llvm-objects llvm-install</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066;">cd</span> llvm-objects</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">../llvm/configure &#8211;with-<span style="color: #0000ff;">llvmgccdir=</span><span style="color: #0000ff;">$PREFIX</span>/llvm-gcc4<span style="color: #cc66cc;">.2</span><span style="color: #cc66cc;">-2.5</span>-x86-linux-RHEL4 &#8211;enable-optimized &#8211;enable-jit &#8211;<span style="color: #0000ff;">prefix=</span><span style="color: #0000ff;">$PREFIX</span>/llvm-install &#8211;with-ocaml-<span style="color: #0000ff;">libdir=</span><span style="color: #0000ff;">$GODI_PATH</span>/lib/ocaml/std-lib</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">make</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">make install </div>
</li>
</ol>
</div>
</div>
<p>My <tt>PREFIX</tt> is my home directory, and <tt>GODI_PATH = ~/godi</tt>.  First, we checkout the latest LLVM from SVN (step 2).  Then we download and extract the <a href="http://llvm.org/releases/download.html#2.5">latest release (2.5, as of writing) of LLVM-gcc</a> (steps 3 and 4).  (I couldn&#8217;t get the SVN version of LLVM-gcc to work with the SVN version of LLVM.)  Notably, LLVM does <i>not</i> support in-place builds, so we create the <tt>llvm-objects</tt> directory to actually build LLVM; we&#8217;ll install it into <tt>llvm-install</tt> (step 5).  We configure the software <i>from the <tt>llvm-objects</tt> directory</i> (steps 6 and 7).  The long configure is necessary; the only optional item is <tt>--enable-jit</tt>.  You may have to adjust your <tt>--with-ocaml-libdir</tt> to point to wherever your Ocaml libraries live.  Then make and make install (steps 8 and 9).  Voila!</p>
<p>To test it out, we can use the &#8220;Hello, World!&#8221; program written by <a href="http://lists.cs.uiuc.edu/pipermail/llvmdev/2007-October/010996.html">Gordon Henrikson</a>.  I had to change it a little to bring it up to date with the latest APIs (in particular, the global context had to be added).  You can download it as <a href="downloads/llvm_test.ml"><tt>llvm_test.ml</tt></a>.</p>
<div class="synthi_code" style="display:none;" id ="plain_synthi_4f2d1b5f5daf8">
<div class="synthi_header" style="font-weight:bold;"> OCaml <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_4f2d1b5f5daf8').style.display='block';document.getElementById('plain_synthi_4f2d1b5f5daf8').style.display='none';return false">Show Styled Code</a>]:</span></div>
<pre style="width:100%;overflow:auto;">
open Printf
open Llvm

let main filename =
   let c = create_context () in

   let i8_t  = i8_type c in
   let i32_t = i32_type c in

   let m = create_module c filename in

   (* @greeting = global [14 x i8] c&#034;Hello, world!\00&#034; *)
   let greeting =
     define_global &#034;greeting&#034; (const_string c &#034;Hello, world!\000&#034;) m in

   (* declare i32 @puts(i8* ) *)
   let puts =
     declare_function &#034;puts&#034;
       (function_type i32_t [|pointer_type i8_t|]) m in

   (* define i32 @main() { entry: *)
   let main = define_function &#034;main&#034; (function_type i32_t [| |]) m in
   let at_entry = builder_at_end c (entry_block main) in

   (* %tmp = getelementptr [14 x i8]* @greeting, i32 0, i32 0 *)
   let zero = const_int i32_t 0 in
   let str = build_gep greeting [| zero; zero |] &#034;tmp&#034; at_entry in

   (* call i32 @puts( i8* %tmp ) *)
   ignore (build_call puts [| str |] &#034;&#034; at_entry);

   (* ret void *)
   ignore (build_ret (const_null i32_t) at_entry);

   (* write the module to a file *)
   if not (Llvm_bitwriter.write_bitcode_file m filename) then exit 1;
   dispose_module m

let () = match Sys.argv with
  | [|_; filename|] -> main filename
  | _ -> main &#034;a.out&#034;
</pre>
</div>
<div class="synthi_code" style="display:block;" id ="styled_synthi_4f2d1b5f5daf8">
<div class="synthi_header" style="font-weight:bold;"> OCaml <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_4f2d1b5f5daf8').style.display='block';document.getElementById('styled_synthi_4f2d1b5f5daf8').style.display='none';return false">Show Plain Code</a>]:</span></div>
<div class="ocaml" style="font-family: monospace;">
<ol>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #06c; font-weight: bold;">open</span> <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Printf.html"><span style="">Printf</span></a></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #06c; font-weight: bold;">open</span> Llvm</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #06c; font-weight: bold;">let</span> main filename =</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">let</span> c = create_context <span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span> <span style="color: #06c; font-weight: bold;">in</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">let</span> i8_t&nbsp; = i8_type c <span style="color: #06c; font-weight: bold;">in</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">let</span> i32_t = i32_type c <span style="color: #06c; font-weight: bold;">in</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">let</span> m = create_module c filename <span style="color: #06c; font-weight: bold;">in</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #5d478b; font-style: italic;">(* @greeting = global [14 x i8] c&quot;Hello, world!\00&quot; *)</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">let</span> greeting =</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;define_global <span style="color: #3cb371;">&quot;greeting&quot;</span> <span style="color: #6c6;">&#40;</span>const_string c <span style="color: #3cb371;">&quot;Hello, world!\000&quot;</span><span style="color: #6c6;">&#41;</span> m <span style="color: #06c; font-weight: bold;">in</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #5d478b; font-style: italic;">(* declare i32 @puts(i8* ) *)</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">let</span> puts =</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;declare_function <span style="color: #3cb371;">&quot;puts&quot;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #6c6;">&#40;</span>function_type i32_t <span style="color: #6c6;">&#91;</span>|pointer_type i8_t|<span style="color: #6c6;">&#93;</span><span style="color: #6c6;">&#41;</span> m <span style="color: #06c; font-weight: bold;">in</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #5d478b; font-style: italic;">(* define i32 @main() { entry: *)</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">let</span> main = define_function <span style="color: #3cb371;">&quot;main&quot;</span> <span style="color: #6c6;">&#40;</span>function_type i32_t <span style="color: #6c6;">&#91;</span>| |<span style="color: #6c6;">&#93;</span><span style="color: #6c6;">&#41;</span> m <span style="color: #06c; font-weight: bold;">in</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">let</span> at_entry = builder_at_end c <span style="color: #6c6;">&#40;</span>entry_block main<span style="color: #6c6;">&#41;</span> <span style="color: #06c; font-weight: bold;">in</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #5d478b; font-style: italic;">(* %tmp = getelementptr [14 x i8]* @greeting, i32 0, i32 0 *)</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">let</span> zero = const_int i32_t <span style="color: #c6c;">0</span> <span style="color: #06c; font-weight: bold;">in</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">let</span> str = build_gep greeting <span style="color: #6c6;">&#91;</span>| zero; zero |<span style="color: #6c6;">&#93;</span> <span style="color: #3cb371;">&quot;tmp&quot;</span> at_entry <span style="color: #06c; font-weight: bold;">in</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #5d478b; font-style: italic;">(* call i32 @puts( i8* %tmp ) *)</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#VALignore"><span style="">ignore</span></a> <span style="color: #6c6;">&#40;</span>build_call puts <span style="color: #6c6;">&#91;</span>| str |<span style="color: #6c6;">&#93;</span> <span style="color: #3cb371;">&quot;&quot;</span> at_entry<span style="color: #6c6;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #5d478b; font-style: italic;">(* ret void *)</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#VALignore"><span style="">ignore</span></a> <span style="color: #6c6;">&#40;</span>build_ret <span style="color: #6c6;">&#40;</span>const_null i32_t<span style="color: #6c6;">&#41;</span> at_entry<span style="color: #6c6;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #5d478b; font-style: italic;">(* write the module to a file *)</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #06c; font-weight: bold;">if</span> <span style="color: #06c; font-weight: bold;">not</span> <span style="color: #6c6;">&#40;</span>Llvm_bitwriter.<span style="color: #060;">write_bitcode_file</span> m filename<span style="color: #6c6;">&#41;</span> <span style="color: #06c; font-weight: bold;">then</span> <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#VALexit"><span style="">exit</span></a> <span style="color: #c6c;">1</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;dispose_module m</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #06c; font-weight: bold;">let</span> <span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span> = <span style="color: #06c; font-weight: bold;">match</span> <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Sys.html"><span style="">Sys</span></a>.<span style="color: #060;">argv</span> <span style="color: #06c; font-weight: bold;">with</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; | <span style="color: #6c6;">&#91;</span>|_; filename|<span style="color: #6c6;">&#93;</span> -&gt; main filename</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; | _ -&gt; main <span style="color: #3cb371;">&quot;a.out&quot;</span> </div>
</li>
</ol>
</div>
</div>
<p>Now we can compile:</p>
<div class="synthi_code" style="display:none;" id ="plain_synthi_4f2d1b5f657f8">
<div class="synthi_header" style="font-weight:bold;"> Bash <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_4f2d1b5f657f8').style.display='block';document.getElementById('plain_synthi_4f2d1b5f657f8').style.display='none';return false">Show Styled Code</a>]:</span></div>
<pre style="width:100%;overflow:auto;">
ocamlopt -cc g++ llvm.cmxa llvm_bitwriter.cmxa llvm_test.ml -o llvm_test
./llvm_test hello.bc # generates bitcode
$PREFIX/llvm-install/bin/llvm-dis hello.bc # disassembles bitcode into hello.ll
$PREFIX/llvm-install/bin/lli hello.bc # outputs &#034;Hello, world!&#034;
</pre>
</div>
<div class="synthi_code" style="display:block;" id ="styled_synthi_4f2d1b5f657f8">
<div class="synthi_header" style="font-weight:bold;"> Bash <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_4f2d1b5f657f8').style.display='block';document.getElementById('styled_synthi_4f2d1b5f657f8').style.display='none';return false">Show Plain Code</a>]:</span></div>
<div class="bash" style="font-family: monospace;">
<ol>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ocamlopt -cc g++ llvm.cmxa llvm_bitwriter.cmxa llvm_test.ml -o llvm_test</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">./llvm_test hello.bc <span style="color: #808080; font-style: italic;"># generates bitcode</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">$PREFIX</span>/llvm-install/bin/llvm-dis hello.bc <span style="color: #808080; font-style: italic;"># disassembles bitcode into hello.ll</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">$PREFIX</span>/llvm-install/bin/lli hello.bc <span style="color: #808080; font-style: italic;"># outputs &quot;Hello, world!&quot; </span></div>
</li>
</ol>
</div>
</div>
<p>If interpretation via <tt>lli</tt> isn&#8217;t your bag, you can also compile to native code:</p>
<div class="synthi_code" style="display:none;" id ="plain_synthi_4f2d1b5f6773d">
<div class="synthi_header" style="font-weight:bold;"> Bash <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_4f2d1b5f6773d').style.display='block';document.getElementById('plain_synthi_4f2d1b5f6773d').style.display='none';return false">Show Styled Code</a>]:</span></div>
<pre style="width:100%;overflow:auto;">
$PREFIX/llvm-install/bin/llc hello.bc # generates assembly, hello.s
gcc -o hello hello.s
./hello # outputs &#034;Hello, world!&#034;
</pre>
</div>
<div class="synthi_code" style="display:block;" id ="styled_synthi_4f2d1b5f6773d">
<div class="synthi_header" style="font-weight:bold;"> Bash <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_4f2d1b5f6773d').style.display='block';document.getElementById('styled_synthi_4f2d1b5f6773d').style.display='none';return false">Show Plain Code</a>]:</span></div>
<div class="bash" style="font-family: monospace;">
<ol>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">$PREFIX</span>/llvm-install/bin/llc hello.bc <span style="color: #808080; font-style: italic;"># generates assembly, hello.s</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">gcc -o hello hello.s</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">./hello <span style="color: #808080; font-style: italic;"># outputs &quot;Hello, world!&quot; </span></div>
</li>
</ol>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.weaselhat.com/2009/09/24/llvm-ocaml-loca/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Flapjax: A Programming Language for Ajax Applications</title>
		<link>http://www.weaselhat.com/2009/08/13/flapjax-2/</link>
		<comments>http://www.weaselhat.com/2009/08/13/flapjax-2/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 16:00:29 +0000</pubDate>
		<dc:creator>Michael Greenberg</dc:creator>
				<category><![CDATA[Flapjax]]></category>
		<category><![CDATA[Papers]]></category>
		<category><![CDATA[Programming Languages]]></category>

		<guid isPermaLink="false">http://www.weaselhat.com/?p=122</guid>
		<description><![CDATA[I am immensely pleased to report that our paper on Flapjax was accepted to OOPSLA 2009. This paper presents Flapjax, a language designed for contemporary Web applications. These applications communicate with servers and have rich, interactive interfaces. Flapjax provides two key features that simplify writing these applications. First, it provides event streams, a uniform abstraction [...]]]></description>
			<content:encoded><![CDATA[<p>I am immensely pleased to report that <a href="http://www.cis.upenn.edu/~mgree/papers/oopsla2009_flapjax.pdf">our paper on Flapjax</a> was accepted to <a href="http://www.oopsla.org/oopsla2009/" title="My favorite conference name to say, perhaps my least favorite to type.  OOPSPLA, every time.">OOPSLA 2009</a>.</p>
<blockquote><p>
This paper presents Flapjax, a language designed for contemporary Web applications. These applications communicate with servers and have rich, interactive interfaces. Flapjax provides two key features that simplify writing these applications. First, it provides event streams, a uniform abstraction for communication within a program as well as with external Web services. Second, the language itself is reactive: it automatically tracks data dependencies and propagates updates along those data?ows. This allows developers to write reactive interfaces in a declarative and compositional style.</p>
<p>Flapjax is built on top of JavaScript. It runs on unmodi?ed browsers and readily interoperates with existing JavaScript code. It is usable as either a programming language (that is compiled to JavaScript) or as a JavaScript library, and is designed for both uses. This paper presents the language, its design decisions, and illustrative examples drawn from several working Flapjax applications.
</p></blockquote>
<p>The real heroes of this story are my co-authors.  Leo, Arjun, and Greg were there for the initial, heroic-effort-based implementation.  Jacob and Aleks wrote incredible applications with our dog food.  Shriram, of course, saw the whole thing through.  Very few of my contributions remain: the original compiler is gone (thank goodness); my <a href="http://www.cis.upenn.edu/~mgree/papers/ugrad_thesis.pdf">thesis work</a> is discussed briefly in <i>How many DOMs?</i> on page 15.  Here&#8217;s to a great team and a great experience (and a great language)!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.weaselhat.com/2009/08/13/flapjax-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

