Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you want sounds similar to "refinement types" à la <a href="http://goto.ucsd.edu/~rjhala/liquid/haskell/blog/blog/2013/01/01/refinement-types-101.lhs/">Liquid Haskell</a>. This is an external tool that allows you to "refine" your Haskell types by specifying additional predicates that hold over your types. To check that these hold, you use an <a href="https://en.wikipedia.org/wiki/Satisfiability_Modulo_Theories">SMT solver</a> to verify all the constraints have been satisfied. </p> <p>The following code snippets are taken from their introductory blog post.</p> <p>For example, you could write the type that <code>zero</code> is <code>0</code>:</p> <pre><code>{-@ zero :: { v : Int | v = 0 } @-} zero :: Int zero = 0 </code></pre> <p>You'll notice that the syntax for types looks just like set notation for math--you're defining a new type as a subset of the old on. In this case, you're defining the type of <code>Int</code>s that are equal to 0.</p> <p>You can use this system to write a safe divide function:</p> <pre><code>{-@ divide :: Int -&gt; { v : Int | v != 0 } -&gt; Int @-} divide :: Int -&gt; Int -&gt; Int divide n 0 = error "Cannot divide by 0." divide n d = n `div` d </code></pre> <p>When you actually try to compile this program, Liquid Haskell will see that having 0 as the denominator violates the predicate and so the call to <code>error</code> cannot happen. Moreover, when you try to use <code>divide</code>, it will check that the argument you pass in cannot be 0.</p> <p>Of course, to make this useful, you have to be able to add information about the postconditions of your functions, not just the preconditions. You can just do this by refining the result type of the function; for example, you can imagine the following type for <code>abs</code>:</p> <pre><code>{-@ abs :: Int -&gt; { v : Int | 0 &lt;= v } @-} </code></pre> <p>Now the type system knows that the result of calling <code>abs</code> will never be negative, and it can take advantage of this fact when it needs to verify your program.</p> <p>Like other people mentioned, using this sort of type system means you will have to have proofs in your code. The advantage of Liquid Haskell is that it uses an SMT solver to generate the proof for you automatically--you just have to write the assertions. </p> <p>Liquid Haskell is still a research project, and it's limited by what can reasonably be done with an SMT solver. I haven't used it myself, but it looks really awesome and seems to be exactly what you want. One thing I'm not sure about is how it interacts with custom types and <code>IO</code>--something you might want to look into yourself.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload