Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My experience is that sugaring every term intuitively seems unattractive, but is worth pursuing.</p> <p>An interest in persistent storage has led me by a circuitous route to consider the problems of mixing expression and atomic values. To support this, I decided to separate them completely in the type system; thus Int, Char etc. are the type constructors only for integer and character values. Expression types are formed with the polymorphic type constructor Exp; e.g. Exp Int refers to a value which reduces in one step to Int.</p> <p>The relevance of this to your question arises when we consider evaluation. At an underlying level, there are primitives which require atomic values: COND, addInt etc. Some people refer to this as forcing an expression, I prefer to see it simply as a cast between values of different types.</p> <p>The challenge is to see if this can be done without requiring explicit reduction directives. One solution is exactly as you suggest: i.e. to treat coercion as a form of overloading.</p> <p>Say we have an input script: <code>foo x</code></p> <p>Then, after sugaring, this becomes: <code>(coerce foo) (coerce x)</code> </p> <p>Where, informally:</p> <pre><code>coerce :: a -&gt; b coerce x = REDUCE (cast x) if a and b are incompatible x otherwise </code></pre> <p>Thus <em>coerce</em> is either identity or an application of <em>cast</em> where <em>b</em> is the return type for a given context.</p> <p><em>cast</em> can now be treated as a type class method, e.g.</p> <pre><code>class Cast a, b where {cast :: a -&gt; b }; -- ¬:: is an operator, literally meaning: don’t cast --(!) is the reduction operator. Perform one stage of reduction. -- Reduce on demand instance Cast Exp c, c where { inline cast = ¬::(!)(\x::(Exp c) -&gt; ¬::(!)x) }; </code></pre> <p>The <code>¬::</code> annotations are used to suppress the coerce syntactic sugaring.</p> <p>The intention is that other instances of <code>Cast</code> can be introduced to extend the range of conversions, although I haven't explored this. As you say, overlapping instances seem necessary.</p>
 

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