Skip to content

Instantly share code, notes, and snippets.

@sdleffler
Created June 20, 2017 15:50
Show Gist options
  • Save sdleffler/734fe400dbcb997b6f99049c55c8aed7 to your computer and use it in GitHub Desktop.
Save sdleffler/734fe400dbcb997b6f99049c55c8aed7 to your computer and use it in GitHub Desktop.
LiquidHaskell interaction with optimized Haskell
-- | Extract the last element of a list, which must be finite and non-empty.
last :: [a] -> a
#ifdef USE_REPORT_PRELUDE
last [x] = x
last (_:xs) = last xs
last [] = errorEmptyList "last"
#else
-- Use foldl to make last a good consumer.
-- This will compile to good code for the actual GHC.List.last.
-- (At least as long it is eta-expaned, otherwise it does not, #10260.)
last xs = foldl (\_ x -> x) lastError xs
{-# INLINE last #-}
-- The inline pragma is required to make GHC remember the implementation via
-- foldl.
lastError :: a
lastError = errorEmptyList "last"
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment