Hi Eric,<br><br>I&#39;m not sure I fully understand what you want to do, so let me ask a basic question:<br>have you considered just using an Arbitrary instance of HsExpr? How would that<br>compare to what you are trying to achieve?<br>

<br><br>Cheers,<br>Pedro<br><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Nov 5, 2012 at 11:23 PM, Eric M. Pashman <span dir="ltr">&lt;<a href="mailto:eric.pashman@gmail.com" target="_blank">eric.pashman@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">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>


<br>
One can, however, write a bit of Template Haskell to construct a random HNat. Something like,<br>
<br>
    hNat 0 = [| hZero |]<br>
    hNat n = [| hSucc $(hNat (n - 1)) |]<br>
<br>
Call that with a random positive integer inside a splice and Template Haskell will give you a random HNat that you can use to index an HList. There&#39;s your arbitrary Haskell value.<br>
<br>
But this technique is of little practical value since it only works at compile-time. I&#39;ve fiddled around with the hint library, trying to interpret the Template Haskell to get random HNats at run-time. This works, sort of, but there&#39;s no way to &quot;liberate&quot; the generated HNat because hint&#39;s `interpret` function requires a type witness. A look-alike string from the `eval` function is as close as you can get.<br>


<br>
Is there some dirty trick provided by the GHC API that would allow me to bind the generated HNat to a name and pass it around in a way similar to what GHCi seems to do? I realize that I probably have a grossly optimistic idea of what magic GHCi is performing when it interprets user input in a way that seems to allow this sort of thing.<br>


<br>
To summarize, I&#39;m basically trying to wrap up the following process programmatically:<br>
<br>
    ghci&gt; n &lt;- randomInt       -- A random Int<br>
    ghci&gt; :load SomeModule.hs  -- Contains `hNat` as above<br>
    ghci&gt; let hn = $(hNat n)   -- Voila, a random HNat<br>
    ghci&gt; ...                  -- Do stuff with `hn`, then repeat<br>
<br>
Is something along the lines of what I&#39;m imagining possible? If not, can I get close enough for my purposes?<br>
<br>
General insights and commentary are welcome as well.<br>
<br>
Regards,<br>
<br>
Eric<br>
<br>
<br>
_______________________________________________<br>
Glasgow-haskell-users mailing list<br>
<a href="mailto:Glasgow-haskell-users@haskell.org">Glasgow-haskell-users@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/glasgow-haskell-users" target="_blank">http://www.haskell.org/mailman/listinfo/glasgow-haskell-users</a><br>
</blockquote></div><br></div>