Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping monad IO

Tags:

haskell

One of the things I like best about Haskell is how the compiler locates the side effects via the IO monad in function signatures. However, it seems easy to bypass this type check by importing 2 GHC primitives :

{-# LANGUAGE MagicHash #-}

import GHC.Magic(runRW#)
import GHC.Types(IO(..))

hiddenPrint :: ()
hiddenPrint = case putStrLn "Hello !" of
  IO sideEffect -> case runRW# sideEffect of
    _ -> ()

hiddenPrint is of type unit, but it does trigger a side effect when called (it prints Hello). Is there a way to forbid those hidden IOs (other than trusting no one imports GHC's primitives) ?

like image 977
V. Semeria Avatar asked Dec 11 '22 15:12

V. Semeria


1 Answers

This is the purpose of Safe Haskell. If you add {-# language Safe #-} to the top of your source file, you will only be allowed to import modules that are either inferred safe or labeled {-# language Trustworthy #-}. This also imposes some mild restrictions on overlapping instances.

like image 114
dfeuer Avatar answered Dec 25 '22 00:12

dfeuer