<br><br><div class="gmail_quote">On Mon, Oct 31, 2011 at 6:52 PM, Paterson, Ross <span dir="ltr">&lt;<a href="mailto:R.Paterson@city.ac.uk">R.Paterson@city.ac.uk</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im"><br>
</div>If you require the circuit to be parametric in the value types, you can limit the types of function you can pass to arr to simple plumbing.<br>
See the netlist example at the end of my &quot;Fun of Programming&quot; slides (<a href="http://www.soi.city.ac.uk/%7Eross/papers/fop.html" target="_blank">http://www.soi.city.ac.uk/~ross/papers/fop.html</a>).<br></blockquote>
<div><br>That&#39;s a neat trick, Ross!  It seems really similar to using parametricity to recover the insides of lambdas in PHOAS metaprogramming:<br><br>-- HOAS expression language<br>data Expr (v :: * -&gt; *) a where<br>
    Ap :: Expr v (a -&gt; b) -&gt; Expr v a -&gt; Expr v b<br>    Var :: v a -&gt; Expr v a<br>    Lam :: (v a -&gt; Expr v b) -&gt; Expr v (a -&gt; b)<br><br>-- some expressions that are paremetric in the variable type<br>
ex_id :: Expr v (a -&gt; a)<br>ex_id = Lam $ \x -&gt; Var x<br><br>ex_const :: Expr v (a -&gt; b -&gt; a)<br>ex_const = Lam $ \x -&gt; Lam $ \y -&gt; Var x<br><br>-- a print function that relies on parametricity to expose the insides of the functions inside &quot;Lam&quot;<br>
printExpr :: (forall v. Expr v a) -&gt; String<br>
printExpr e = pe_helper vars 0 e &quot;&quot; where<br>
   vars = map (:[]) [&#39;a&#39; .. &#39;z&#39;] ++ map (\n -&gt; &quot;t&quot; ++ show n) [1..]<br>
<br>
prec_lam = 1<br>prec_ap = 2<br><br>newtype VarName a = VarName String<br>pe_helper :: [String] -&gt; Expr VarName a -&gt; Int -&gt; ShowS<br>pe_helper fv prec (Var (VarName s)) = showString s<br>pe_helper fv prec (Ap x y) = showParen (prec &gt; prec_ap) (pe_helper fv prec_ap x . showString &quot; &quot; . pe_helper fv (prec_ap+1) y)<br>
pe_helper (v:fv) prec (Lam k) = showParen (prec &gt; prec_lam) (showString &quot;\&quot; . showString v . showString &quot; -&gt; &quot; . pe_helper fv prec_lam e)<br>   where e = k (VarName v)<br><br>-- some test cases<br>
test1 = printExpr ex_const -- &quot;\a -&gt; \b -&gt; a&quot;<br>test2 = printExpr (ex_id `Ap` ex_const) -- &quot;(\a -&gt; a) (\a -&gt; \b -&gt; a)&quot;<br></div></div><br>