Skip to content

Commit

Permalink
add internal library for unsafe base functions
Browse files Browse the repository at this point in the history
  • Loading branch information
larskuhtz committed Jul 2, 2024
1 parent d12f3c2 commit 2c990c9
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
51 changes: 51 additions & 0 deletions lib/unsafe/src/Data/Foldable/Unsafe.hs
Original file line number Diff line number Diff line change
@@ -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

89 changes: 89 additions & 0 deletions lib/unsafe/src/Data/List/Unsafe.hs
Original file line number Diff line number Diff line change
@@ -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

16 changes: 16 additions & 0 deletions pact.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 2c990c9

Please sign in to comment.