title: F# generic constraint union type
body: <p>I have been working with F# for a few months but did not find any satisfying solution for my problem.
  I would like to describe a succession of operations as a discriminated union of values, or operations on these values.
  In this way, my type Val&lt;'o> is defined as follow:</p>
  
  <pre><code>type Val&lt;'o&gt; =
      | Val of 'o
      | Func1 of ('a-&gt;'o) * Val&lt;'a&gt;
      | Func2 of ('a-&gt;'b-&gt;'o) * Val&lt;'a&gt; * Val&lt;'b&gt;
  </code></pre>
  
  <p>The type Val&lt;'o> can be converted into a 'o type by recursively applying all operations, and still keep the list of operations.</p>
  
  <p>But I can't define the generic types 'a and 'b and their constraints if I don't use Val&lt;'a,'b,'o>.
  If I do, I have to define the sub-Val generic types, which I want to stay generic:</p>
  
  <pre><code>type Val&lt;'a, 'b, 'o&gt; =
      | Val of 'o
      | Func1 of ('a-&gt;'o) * Val&lt;?, ?, 'a&gt;
      | Func2 of ('a-&gt;'b-&gt;'o) * Val&lt;?, ?, 'a&gt; * Val&lt;?, ?, 'b&gt;
  </code></pre>
  
  <p>Is there any F# structure that could be adapted for this issue?</p>
  
  <p>Many thanks</p>
  
  <p>[edit]</p>
  
  <p>To describe my problem further, I am trying to get an exhaustive representation of a FRP structure (but the genericity problem is the same for events/signals that for values).<br/>
  The representation could be either serialized for database storage, translated into text for display and <strong>user edit</strong> or evaluated to get a result:</p>
  
  <pre><code>"Func (x -&gt; x²) (Val(3.4))" &lt;--&gt; representation &lt;--&gt; 11.56
               |
              user 
  </code></pre>
  
  <p>I made a prototype working quite well using a <code>PrimitiveValue</code> union type, and functions strings compiled at runtime into generic <code>obj[] -&gt; obj</code> functions, but evaluation is very heavy on type checking and casting (especially because I am also using arrays and options in <code>PrimitiveValue</code>), so I was looking for a more elegant and strongly-typed solution.</p>