Skip to content

Bool guards can break linearity

Summary

This code compiles with GHC HEAD 9.1.20201223:

{-# LANGUAGE LinearTypes #-}

notL :: Bool %1 -> Bool
notL True = False
notL False = True

z :: Bool %1 -> Bool
z x | notL x = True
z x | otherwise = notL x

-- And this doesn't compile, as expected:
--
-- z :: Bool %1 -> Bool
-- z x = if notL x then True else notL x

Expected behavior

I expected that z should not compile, because it uses its argument twice — evaluates the first guard, possibly fails, and evaluates the second guard.

I tried -dcore-lint and nothing changed.

Environment

  • GHC version used: GHC HEAD 9.1.20201223

Optional:

  • Operating System: macOS Catalina
  • System Architecture: x86_64
Edited by Artyom Kazak
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information