<div>In  <a href="https://github.com/ekmett/rounded/blob/master/src/Numeric/Rounded/Precision.hs">https://github.com/ekmett/rounded/blob/master/src/Numeric/Rounded/Precision.hs</a> I borrow GHC&#39;s type level naturals for use directly as the reflection of a number of bits of precision.</div>
<div><br></div><div>That lets you work with `Rounded TowardZero Double` for the type of a number that offers 53 bits of mantissa and a known rounding mode or `Rounded TowardZero 512` to get a number with 512 bits of mantissa or you can use</div>
<div><br></div><div>reifyPrecision :: Int -&gt; (<span class="n" style="margin:0px;padding:0px;border:0px;color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)">forall</span><span style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="p" style="margin:0px;padding:0px;border:0px;color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)">(</span><span class="n" style="margin:0px;padding:0px;border:0px;color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)">p</span><span style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="ow" style="margin:0px;padding:0px;border:0px;font-weight:bold;color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)">::</span><span style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="o" style="margin:0px;padding:0px;border:0px;font-weight:bold;color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)">*</span><span class="p" style="margin:0px;padding:0px;border:0px;color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)">)</span><span class="o" style="margin:0px;padding:0px;border:0px;font-weight:bold;color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)">.</span><span style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="kt" style="margin:0px;padding:0px;border:0px;color:rgb(68,85,136);font-weight:bold;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)">Precision</span><span style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="n" style="margin:0px;padding:0px;border:0px;color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)">p</span><span style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="ow" style="margin:0px;padding:0px;border:0px;font-weight:bold;color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)">=&gt;</span><span style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="kt" style="margin:0px;padding:0px;border:0px;color:rgb(68,85,136);font-weight:bold;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)">Proxy</span><span style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="n" style="margin:0px;padding:0px;border:0px;color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)">p</span><span style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="ow" style="margin:0px;padding:0px;border:0px;font-weight:bold;color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)">-&gt;</span><span style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)"> </span><span class="n" style="margin:0px;padding:0px;border:0px;color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)">a</span><span class="p" style="margin:0px;padding:0px;border:0px;color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;white-space:pre;background-color:rgb(255,255,255)">)</span><span style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px"> </span><span class="ow" style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;margin:0px;padding:0px;border:0px;font-weight:bold">-&gt;</span><span style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px"> </span><span class="n" style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;margin:0px;padding:0px;border:0px">a</span></div>
<div><span class="n" style="color:rgb(51,51,51);font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;font-size:11.818181991577148px;line-height:14.545454025268555px;margin:0px;padding:0px;border:0px"><br></span></div>
<div>to constructo a type p that you can use for `Rounded TowardZero p`.</div><div><br></div><div>This is how I piggyback on GHC&#39;s type-nats support. It&#39;ll get nicer once the actual solver is available and you can compute meaningfully with type level naturals.</div>
<div><br></div><div>-Edward</div><div><br><div class="gmail_quote">On Fri, Nov 16, 2012 at 12:33 PM, Nicolas Frisby <span dir="ltr">&lt;<a href="mailto:nicolas.frisby@gmail.com" target="_blank">nicolas.frisby@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>When wren&#39;s email moved this thread to the top of my inbox, I noticed that I never sent this draft I wrote. It gives some concrete code along the line of Wren&#39;s suggestion.</div>
<div><br></div><div>-----</div>

<div><br></div><div>The included code uses a little of this (singleton types) and a little of that (implicit configurations).</div><div><br></div><div>  <a href="http://www.cis.upenn.edu/~eir/papers/2012/singletons/paper.pdf" target="_blank">http://www.cis.upenn.edu/~eir/papers/2012/singletons/paper.pdf</a><br>


</div><div><br></div><div>  <a href="http://www.cs.rutgers.edu/~ccshan/prepose/prepose.pdf" target="_blank">http://www.cs.rutgers.edu/~ccshan/prepose/prepose.pdf</a></div><div><br></div><div>As is so often the case with Haskell discussions, I&#39;m not sure if this is overkill for your actual needs. There might be simpler possible solutions, but this idea at least matches my rather literal interpretation of your email. </div>


<div><br></div><div>Also, I apologize if this approach is what you meant by &quot;(Or, more or less equivalently, one can&#39;t write dependently typed functions, and trying to fake it at the type-level leads to the original problem.)&quot;</div>


<div><br></div><div>&gt; {-# LANGUAGE GADTs #-}</div><div>&gt; {-# LANGUAGE DataKinds #-}</div><div>&gt; {-# LANGUAGE KindSignatures #-}</div><div>&gt; {-# LANGUAGE Rank2Types #-}</div><div><br></div><div>&gt; module STandIC where</div>


<div><br></div><div>A data type for naturals; I&#39;m assuming you have something like a useful Arbitrary instance for these.</div><div><br></div><div>&gt; data Nat = Z | S Nat</div><div><br></div><div>The corresponding singleton type.</div>


<div><br></div><div>&gt; data Nat1 :: Nat -&gt; * where</div><div>&gt;   Z1 :: Nat1 Z</div><div>&gt;   S1 :: Nat1 n -&gt; Nat1 (S n)</div><div><br></div><div>Reification of a Nat; cf implicit configurations (ie prepose.pdf).</div>


<div><br></div><div>&gt; reifyNat :: Nat -&gt; (forall n. Nat1 n -&gt; a) -&gt; a<br></div><div>&gt; reifyNat Z k = k Z1</div><div>&gt; reifyNat (S n) k = reifyNat n $ k . S1</div><div><br></div><div>Here&#39;s my questionable assumption:</div>


<div><br></div><div>  If the code you want to use arbitrary type-level naturals with</div><div>  works for all type-level naturals, then it should be possible to</div><div>  wrap it in a function that fits as the second argument to reifyNat.</div>


<div><br></div><div>That may be tricky to do. I&#39;m not sure it&#39;s necessarily always possible in general; hence &quot;questionable assumption&quot;. But maybe it&#39;ll work for you.</div><div><br></div><div>HTH. And I apologize if it&#39;s overkill; as Pedro was suggesting, there might be simpler ways.</div>
<div class="HOEnZb"><div class="h5">

<div><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Nov 16, 2012 at 12:52 AM, wren ng thornton <span dir="ltr">&lt;<a href="mailto:wren@freegeek.org" target="_blank">wren@freegeek.org</a>&gt;</span> wrote:<br>


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>On 11/5/12 6:23 PM, Eric M. Pashman wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I&#39;ve been playing around with the idea of writing a genetic programming implementation based on the ideas behind HList. Using type-level programming, it&#39;s fairly straighforward to put together a program representation that&#39;s strongly typed, that supports polymorphism, and that permits only well-typed programs by construction. This has obvious advantages over vanilla GP implementations. But it&#39;s impossible to generate arbitrary programs in such a representation using standard Haskell.<br>



<br>
Imagine that you have an HList-style heterogenous list of arbitrarily typed Haskell values. It would be nice to be able to pull values from this collection at random and use them to build up random programs. But that&#39;s not possible because one can&#39;t write a function that outputs a value of arbitrary type. (Or, more or less equivalently, one can&#39;t write dependently typed functions, and trying to fake it at the type-level leads to the original problem.)<br>



</blockquote>
<br></div>
Actually, you can write functions with the necessary &quot;dependent&quot; types using an old trick from Chung-chieh Shan and Oleg Kiselyov:<br>
<br>
    <a href="http://www.cs.rutgers.edu/~ccshan/prepose/prepose.pdf" target="_blank">http://www.cs.rutgers.edu/~<u></u>ccshan/prepose/prepose.pdf</a><br>
<br>
The first trick is to reify runtime values at the type level, which the above paper explains how to do, namely: type class hackery.<br>
<br>
The second trick is to overcome the issue you raised about not actually having dependent types in Haskell. The answer to this is also given in the paper, but I&#39;ll cut to the chase. The basic idea is that we just need to be able to hide our dependent types from the compiler. That is, we can&#39;t define:<br>



<br>
    reifyInt :: (n::Int) -&gt; ...n...<br>
<br>
but only because we&#39;re not allowed to see that pesky n. And the reason we&#39;re not allowed to see it is that it must be some particular fixed value only we don&#39;t know which one. But, if we can provide a function eliminating n, and that function works for all n, then it doesn&#39;t matter what the actual value is since we&#39;re capable of eliminating all of them:<br>



<br>
    reifyInt :: Int -&gt; (forall n. ReflectNum n =&gt; n -&gt; a) -&gt; a<br>
<br>
This is just the standard CPS trick we also use for dealing with existentials and other pesky types we&#39;re not allowed to see. Edward Kmett has a variation of this theme already on Hackage:<br>
<br>
    <a href="http://hackage.haskell.org/package/reflection" target="_blank">http://hackage.haskell.org/<u></u>package/reflection</a><br>
<br>
It doesn&#39;t include the implementation of type-level numbers, so you&#39;ll want to look at the paper to get an idea about that, but the reflection package does generalize to non-numeric types as well.<span><font color="#888888"><br>



<br>
-- <br>
Live well,<br>
~wren</font></span><div><div><br>
<br>
______________________________<u></u>_________________<br>
Glasgow-haskell-users mailing list<br>
<a href="mailto:Glasgow-haskell-users@haskell.org" target="_blank">Glasgow-haskell-users@haskell.<u></u>org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/glasgow-haskell-users" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/glasgow-<u></u>haskell-users</a><br>
</div></div></blockquote></div><br></div>
</div></div><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" target="_blank">http://www.haskell.org/mailman/listinfo/glasgow-haskell-users</a><br>
<br></blockquote></div><br></div>