Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to found most of the errors in programs written in Haskell by adding a new feature, something I call it "subtype system", to Haskell?
    primarykey
    data
    text
    <p>I have learned Haskell for about one year, and came up with a question that could the talented compiler writers add a new feature called "subset" by me to enhance the Haskell's type system to catch many errors including IOExceptions in compiling stage. I'm novice of Theory of types, and forgive my wishful thinking.</p> <p>My initial purpose is not how to solve the problem but to know whether there exists a related solution but because of some reasons the solution is not introduced to Haskell.</p> <p>Haskell is nearly perfect in my mind except for some little things, and I will express my wish to Haskell of the future in the following lines.</p> <p>The following is the major one:</p> <p>If we can define a type, which is just a "subset" of <code>Int</code> assuming Haskell allows us to do that, like bellow:</p> <pre><code>data IntNotZero = Int {except `0`} -- certainly it is not legal in Haskell, but I just assume that Haskell allows us to define a type as a "subset" of an already existing type. I'm novice of Theory of types, and forgive me. </code></pre> <p>And If a function needs a parameter of <code>Int</code>, a variable of <code>IntNotZero</code>, which is just a "subset" of <code>Int</code>, can also be a parameter of the function. But, If a function needs a <code>IntNotZero</code>, then a <code>Int</code> is illegal.</p> <p>For example:</p> <pre><code>div' :: Int -&gt; IntNotZero -&gt; Int div' = div aFunction :: Int -&gt; Int -&gt; Int --If we casually write it, then the compiler will complain for type conflict. aFunction = div' aFunction2 :: Int -&gt; Int -&gt; Int --we have to distinguish between `Int` and `IntNotZero`. aFunction2 m n = type n of --An assumed grammar like `case ... of` to separate "subset" from its complement. `case ...of` only works on different patterns. IntNotZero -&gt; m `div` n otherwise -&gt; m + n </code></pre> <p>For a more useful example:</p> <pre><code>data HandleNotClosed = Handle {not closed} --this type infers a Handle not closed hGetContents' :: HandleNotClosed -&gt; IO String --this function needs a HandleNotClosed and a Handle will make a type conflict. hGetContents' = hGetContents wrongMain = do ... h &lt;- openFile "~/xxx/aa" ReadMode ... -- we do many tasks with h and we may casually closed h contents &lt;- hGetContents' h --this will raise a type conflict, because h has type of Handle not HandleNotClosed. ... rightMain = do ... h &lt;- openFile "~/xxx/aa" ReadMode ... -- we do many tasks with h and we may casually closed h type h of -- the new grammar. HandleNotClosed -&gt; do contents &lt;- hGetContents' h ... otherwise -&gt; ... </code></pre> <p>If we combine ordinary IO with Exception to a new "supset", then we may get free of IOErrors.</p>
    singulars
    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.
 

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