packages feed

parentheses-less (empty) → 0.0.1

raw patch · 5 files changed

+337/−0 lines, 5 filesdep +basedep +mtldep +parentheses-less

Dependencies added: base, mtl, parentheses-less, transformers

Files

+ LICENSE view
@@ -0,0 +1,32 @@+This module is under this "3 clause" BSD license:++Copyright (c) 2025-2025, Daniil Iaitskov+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are+met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * The names of the contributors may not be used to endorse or+      promote products derived from this software without specific+      prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ changelog.md view
@@ -0,0 +1,4 @@+# parentheses-less changelog++## Version 0.0.1 2026-11-30+  * init
+ parentheses-less.cabal view
@@ -0,0 +1,115 @@+cabal-version: 3.0+name:          parentheses-less+version:       0.0.1++synopsis:      left associative dollar operator+description:+    Built-in+    <https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Prelude.html#v:-36- $>+    (dollar operator) in Haskell has the lowest priotity possible - 0 and+    this requires using parentheses in situations like follows, because even+    a custom comparison operator cannot go lower than 0.+    +    > tests =+    >   [ pure (3 + 2) == Just 5+    >   , runReader (pure 3) 0 == 3+    >   , runReader ask (1 + 2) == 3+    >   , runReader ((+4) <$> ask) 1 + 2 == 7+    >   , s3 (8 `div` 3) (2 * 2) (2 + 2) == 10+    >   , s4 (11 `mod` 3) (8 `div` 3) (2 * 2) (2 + 2) == 12+    >   ]+    +    Flipping associativity of the operator from right to left helps with+    reducing parentheses:+    +    > import ParenthesesLess+    >+    > go = (==)+    > infixl 0 go+    >+    > tests =+    >   [ s3 % 8 `div` 3 % 2 * 2 % 2 + 2 `go` 10+    >   , s3 % 8 `div` 3 + 1 % 2 * 2 % 2 + 2 `go` 11+    >   , s4 % 11 `mod` 3 % 8 `div` 3 % 2 * 2 % 2 + 2 `go` 12 +    >   ]+    +    === Unicode comma-like operator+    #unicode-comma-like-operator#+    +    Percent is easy to type, but it mixes with arithmetic expressions it is+    coined to separate. A Unicode comma-like symbol, such as cedilla, offers+    better visual perception.+    +    > import ParenthesesLess+    >+    > go = (==)+    > infixl 1 go+    >+    > tests =+    >   [ s3 ¸ 8 `div` 3 ¸ 2 * 2 ¸ 2 + 2 `go` 10+    >   , s3 ¸ 8 `div` 3 + 1 ¸ 2 * 2 ¸ 2 + 2 `go` 11+    >   , s4 ¸ 11 `mod` 3 ¸ 8 `div` 3 ¸ 2 * 2 ¸ 2 + 2 `go` 12+    >   +    >   , runExcept ¸ when ¸¸ 3 < 4 ¸¸ throwE () `go` Left ()+    >   , runExcept ¸ when ¸¸ 3 > 4 ¸¸ throwE () `go` Right ()+    >   , runWriter ¸ tell [True] >> tell [False] `go` ((), [True, False])+    >   , runReader ¸ runWriterT ¸¸ tell [True] >>> lift ask ¸ 3 `go` (3, [True])+    >   +    >   ]+    +    == Development environment+    #development-environment#+    +    HLS should be available inside the default dev shell.+    +    > $ nix develop+    > $ cabal build+    > $ cabal test+homepage:      http://github.com/yaitskov/parentheses-less+license:       BSD-3-Clause+license-file:  LICENSE+author:        Daniil Iaitskov+maintainer:    dyaitskov@gmail.com+copyright:     Daniil Iaitkov 2026+category:      System+build-type:    Simple+bug-reports:   https://github.com/yaitskov/parentheses-less/issues+extra-doc-files:+  changelog.md+tested-with:+  GHC == 9.14.1++source-repository head+  type:+    git+  location:+    https://github.com/yaitskov/parentheses-less.git++common base+  default-language: GHC2024+  ghc-options: -Wall+  default-extensions:+    DefaultSignatures+    NoImplicitPrelude+    OverloadedStrings+  build-depends:+    , base >=4.7 && < 5++library+  import: base+  hs-source-dirs: src+  exposed-modules:+    ParenthesesLess+  build-depends:++test-suite test+  import: base+  type: exitcode-stdio-1.0+  main-is: Driver.hs+  hs-source-dirs:+    test+  ghc-options: -Wall -Wno-missing-signatures -rtsopts -threaded -main-is Driver+  build-depends:+    , parentheses-less+    , transformers+    , mtl
+ src/ParenthesesLess.hs view
@@ -0,0 +1,85 @@+module ParenthesesLess where++import Control.Monad ( Monad((>>=), (>>)) ) +import Prelude (($))++-- | Left-associative application with precedence 1, equivalent to `Prelude.$`.+--+-- It removes parentheses from nested applications:+--+-- >>> import Prelude+-- >>> import ParenthesesLess+-- >>> let s3 a b c = a + b + c+-- >>> s3 % 8 `div` 3 % 2 * 2 % 2 + 2 :: Int+-- 10+-- >>> s3 (8 `div` 3) (2 * 2) (2 + 2) :: Int+-- 10+--+-- A custom left-associative comparison can follow the chain:+--+-- >>> let { go = (==); infixl 1 `go` } in s3 % 8 `div` 3 % 2 * 2 % 2 + 2 `go` 10+-- True+(%) :: (a -> b) -> a -> b+(%) = ($)+infixl 0 %++(%%) :: (a -> b) -> a -> b+(%%) = ($)+infixl 1 %%++(%%%) :: (a -> b) -> a -> b+(%%%) = ($)+infixl 2 %%%++(%%%%) :: (a -> b) -> a -> b+(%%%%) = ($)+infixl 3 %%%%++-- | Unicode comma-like symbol alternative to `(%)`.+--+-- Cedilla symbol offers better visual perception.+--+-- >>> import Prelude+-- >>> import ParenthesesLess+-- >>> let s3 a b c = a + b + c+-- >>> s3 ¸ 8 `div` 3 ¸ 2 * 2 ¸ 2 + 2 :: Int+-- 10+-- >>> s3 (8 `div` 3) (2 * 2) (2 + 2) :: Int+-- 10+--+-- A custom left-associative comparison can follow the chain:+--+-- >>> let { go = (==); infixl 1 `go` } in s3 ¸ 8 `div` 3 ¸ 2 * 2 ¸ 2 + 2 `go` 10+-- True+(¸) :: (a -> b) -> a -> b+(¸) = ($)+infixl 0 ¸++(¸¸) :: (a -> b) -> a -> b+(¸¸) = ($)+infixl 1 ¸¸++(¸¸¸) :: (a -> b) -> a -> b+(¸¸¸) = ($)+infixl 2 ¸¸¸++(¸¸¸¸) :: (a -> b) -> a -> b+(¸¸¸¸) = ($)+infixl 3 ¸¸¸¸++(>>>) :: Monad m => m a -> m b -> m b+(>>>) = (>>)+infixl 2 >>>++(>>>=) :: Monad m => m a -> (a -> m b) -> m b+(>>>=) = (>>=)+infixl 2 >>>=++(>>>>) :: Monad m => m a -> m b -> m b+(>>>>) = (>>)+infixl 3 >>>>++(>>>>=) :: Monad m => m a -> (a -> m b) -> m b+(>>>>=) = (>>=)+infixl 3 >>>>=+  
+ test/Driver.hs view
@@ -0,0 +1,101 @@+{-# OPTIONS_GHC -Wno-type-defaults #-}+module Driver where++import Control.Exception+import Control.Monad+import Control.Monad.Reader+import Control.Monad.Writer+import Control.Monad.Except+import Control.Monad.Trans.Except+import ParenthesesLess+import Data.Maybe+import Data.List (intercalate)+import Prelude qualified+import Prelude as X hiding (($))++($) :: (a -> b) -> a -> b+($) = (Prelude.$)+infixr 1 $++($$) :: (a -> b) -> a -> b+($$) = (Prelude.$)+infixr 2 $$++($$$) :: (a -> b) -> a -> b+($$$) = (Prelude.$)+infixr 3 $$$++($$$$) :: (a -> b) -> a -> b+($$$$) = (Prelude.$)+infixr 4 $$$$++default (Int)++data Case = forall a. (Show a, Eq a) => Case { got :: a, expected :: a }++instance Show Case where+  show Case {expected, got} = "exp: " <> show expected <> "; got:" <> show got <> ";"++cmp :: (Show a, Eq a) => a -> a -> Maybe Case+cmp a b | a == b = Nothing+        | otherwise = Just $ Case a b+infixr 0 `cmp`++runTests :: [ Maybe Case ] -> IO ()+runTests t+  | null got = pure ()+  | otherwise =+    throwIO . AssertionFailed $ "Failed:\n" <> intercalate "\n" (fmap show got)+  where+    got = catMaybes . fmap sequenceA $ zip [0..] t++go :: (Show a, Eq a) => a -> a -> Maybe Case+go = cmp+infixl 0 `go`++tests :: [Maybe Case]+tests =+  [ pure $ 3 + 2 `cmp` Just 5 -- built-in dollar+  , runReader $$ pure 3 $ 0 `cmp` 3+  , runReader $$ ask $ 1 + 2 `cmp` 3+  , runReader $$ (+4) <$> ask $ 1 + 2 `cmp` 7+  , s3 $$$ 8 `div` 3 $$ 2 * 2 $ 2 + 2 `cmp` 10+  , s4 $$$$ 11 `mod` 3 $$$ 8 `div` 3 $$ 2 * 2 $ 2 + 2 `cmp` 12+  -- cedilla+  , pure ¸ 3 + 2 `go` Just 5+  , runReader ¸ pure 3 ¸ 0 `go` 3+  , runReader ¸ ask ¸ 1 + 2 `go` 3+  , runReader ¸ (+4) <$> ask ¸ 1 + 2 `go` 7+  , s3 ¸ 8 `div` 3 ¸ 2 * 2 ¸ 2 + 2 `go` 10+  , s3 ¸ 8 `div` 3 + 1 ¸ 2 * 2 ¸ 2 + 2 `go` 11+  , s4 ¸ 11 `mod` 3 ¸ 8 `div` 3 ¸ 2 * 2 ¸ 2 + 2 `go` 12+  --+  , pure % 3 + 2 `go` Just 5+  , runReader % pure 3 % 0 `go` 3+  , runReader % ask % 1 + 2 `go` 3+  , runReader % (+4) <$> ask % 1 + 2 `go` 7+  , s3 % 8 `div` 3 % 2 * 2 % 2 + 2 `go` 10+  , s3 % 8 `div` 3 + 1 % 2 * 2 % 2 + 2 `go` 11+  , s4 % 11 `mod` 3 % 8 `div` 3 % 2 * 2 % 2 + 2 `go` 12++  , runExcept ¸ when ¸¸ 3 < 4 ¸¸ throwE () `go` Left ()+  , runExcept ¸ when ¸¸ 3 > 4 ¸¸ throwE () `go` Right ()+  , runWriter ¸ tell [True] >> tell [False] `go` ((), [True, False])+  , runReader ¸ runWriterT ¸¸ tell [True] >>> lift ask ¸ 3 `go` (3, [True])+  -- parentheses+  , pure (3 + 2) `go` Just 5+  , runReader (pure 3) 0 `go` 3+  , runReader ask (1 + 2) `go` 3+  , runReader ((+4) <$> ask) 1 + 2 `go` 7+  , s3 (8 `div` 3) (2 * 2) (2 + 2) `go` 10+  , s4 (11 `mod` 3) (8 `div` 3) (2 * 2) (2 + 2) `go` 12+  -- amp doesn't fit+  -- , 3 & 2 & (+) `go` 5+  -- , 2 + 2 & 2 * 2 & 8 `div` 3 & s3 `go` 10+  ]+  where+    s3 a b c = a + b + c+    s4 a b c d = s3 a b c + d++main :: IO ()+main = runTests tests