diff --git a/lib/unsafe/src/Data/Foldable/Unsafe.hs b/lib/unsafe/src/Data/Foldable/Unsafe.hs new file mode 100644 index 000000000..cb7086878 --- /dev/null +++ b/lib/unsafe/src/Data/Foldable/Unsafe.hs @@ -0,0 +1,51 @@ +{-# OPTIONS_GHC -fno-warn-x-partial #-} + +-- | +-- Module: unsafe.Data.Foldable.Unsafe +-- Copyright: Copyright © 2024 Kadena LLC. +-- License: MIT +-- Maintainer: Pact Team +-- Stability: experimental +-- +-- This module provides unsafe versions for all functions in "Data.Foldable" +-- that are either partial or return a 'Maybe' value. +-- +module Data.Foldable.Unsafe +( +-- * Unsafe versions of partial functions + unsafeMaximum +, unsafeMaximumBy +, unsafeMinimum +, unsafeMinimumBy + +-- * Unsafe versions of functions that return 'Maybe' values +, unsafeFind +) where + +import Data.Foldable + +import GHC.Stack + +-- -------------------------------------------------------------------------- -- +-- Unsafe versions of partial functions + +unsafeMaximum :: HasCallStack => Foldable t => Ord a => t a -> a +unsafeMaximum = maximum + +unsafeMaximumBy :: HasCallStack => Foldable t => (a -> a -> Ordering) -> t a -> a +unsafeMaximumBy = maximumBy + +unsafeMinimum :: HasCallStack => (Foldable t, Ord a) => t a -> a +unsafeMinimum = minimum + +unsafeMinimumBy :: HasCallStack => Foldable t => (a -> a -> Ordering) -> t a -> a +unsafeMinimumBy = minimumBy + +-- -------------------------------------------------------------------------- -- +-- Unsafe versions of functions that return Maybe + +unsafeFind :: HasCallStack => Foldable t => (a -> Bool) -> t a -> a +unsafeFind a b = case find a b of + Nothing -> error "Data.List.Unsafe.unsafeFind: not found" + Just x -> x + diff --git a/lib/unsafe/src/Data/List/Unsafe.hs b/lib/unsafe/src/Data/List/Unsafe.hs new file mode 100644 index 000000000..d43b757a9 --- /dev/null +++ b/lib/unsafe/src/Data/List/Unsafe.hs @@ -0,0 +1,89 @@ +{-# OPTIONS_GHC -fno-warn-x-partial #-} + +-- | +-- Module: Data.List.Unsafe +-- Copyright: Copyright © 2024 Kadena LLC. +-- License: MIT +-- Maintainer: Pact Team +-- Stability: experimental +-- +-- This module provides unsafe versions for all functions in "Data.List" that +-- are either partial or return a 'Maybe' value. +-- +module Data.List.Unsafe +( +-- * Unsafe versions of partial functions + unsafeHead +, unsafeLast +, unsafeTail +, unsafeInit +, unsafeIndex +, unsafeGenericIndex + +-- * Unsafe versions of functions that return 'Maybe' values +, unsafeUncons +, unsafeUnsnoc +, unsafeLookup +, unsafeElemIndex +, unsafeFindIndex +, unsafeStripPrefix +) where + +import Data.List + +import GHC.Stack + +-- -------------------------------------------------------------------------- -- +-- Unsafe versions of partial functions + +unsafeHead :: HasCallStack => [a] -> a +unsafeHead = head + +unsafeLast :: HasCallStack => [a] -> a +unsafeLast = last + +unsafeTail :: HasCallStack => [a] -> [a] +unsafeTail = tail + +unsafeInit :: HasCallStack => [a] -> [a] +unsafeInit = init + +unsafeIndex :: HasCallStack => [a] -> Int -> a +unsafeIndex = (!!) + +unsafeGenericIndex :: Integral i => [a] -> i -> a +unsafeGenericIndex = genericIndex + +-- -------------------------------------------------------------------------- -- +-- Unsafe versions of functions that return Maybe + +unsafeUncons :: HasCallStack => [a] -> (a, [a]) +unsafeUncons a = case uncons a of + Nothing -> error "Data.List.Unsafe.unsafeUncons: empty list" + Just x -> x + +unsafeUnsnoc :: [a] -> ([a], a) +unsafeUnsnoc a = case unsnoc a of + Nothing -> error "Data.List.Unsafe.unsafeUnsnoc: empty list" + Just x -> x + +unsafeLookup :: HasCallStack => Eq a => a -> [(a,b)] -> b +unsafeLookup a b = case lookup a b of + Nothing -> error "Data.List.Unsafe.unsafeLookup: not found" + Just x -> x + +unsafeElemIndex :: Eq a => a -> [a] -> Int +unsafeElemIndex a b = case elemIndex a b of + Nothing -> error "Data.List.Unsafe.unsafeElemIndex: not found" + Just x -> x + +unsafeFindIndex :: (a -> Bool) -> [a] -> Int +unsafeFindIndex a b = case findIndex a b of + Nothing -> error "Data.List.Unsafe.unsafeFindIndex: not found" + Just x -> x + +unsafeStripPrefix :: Eq a => [a] -> [a] -> [a] +unsafeStripPrefix a b = case stripPrefix a b of + Nothing -> error "Data.List.Unsafe.unsafeStripPrefix: not found" + Just x -> x + diff --git a/pact.cabal b/pact.cabal index 1621101e2..dcd3b5ed4 100644 --- a/pact.cabal +++ b/pact.cabal @@ -64,6 +64,22 @@ library pact-prettyprinter , ansi-terminal >=0.4 , prettyprinter >= 1.7 +-- -------------------------------------------------------------------------- -- +-- Internal: unsafe functions from base +-- +-- This is to avoid cluttering production code with +-- `{-# OPTIONS_GHC -fno-warn-x-partial #-}` pragmas with base >= 4.20 + +library unsafe + visibility: public + hs-source-dirs: lib/unsafe/src + default-language: Haskell2010 + exposed-modules: + Data.Foldable.Unsafe + Data.List.Unsafe + build-depends: + , base >= 4.5 && < 5 + -- -------------------------------------------------------------------------- -- -- Pact library