Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Control.Applicative.Alternative capabilities, and introduce Alternative.optional field #86

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/32-optparse.hell
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
-- Includes example of Semigroup.
data Opts = Opts {
quiet :: Bool,
filePath :: Text
filePath :: Maybe Text
}
options =
(\quiet path -> Main.Opts { quiet = quiet, filePath = path })
<$> Options.switch (Flag.long "quiet" <> Flag.help "Be quiet?")
<*> Options.strOption (Option.long "path" <> Option.help "The filepath to export")
<*> (Alternative.optional $ Options.strOption (Option.long "path" <> Option.help "The filepath to export"))
main = do
opts <- Options.execParser (Options.info (Main.options <**> Options.helper) Options.fullDesc)
Text.putStrLn $ Record.get @"filePath" opts
Text.putStrLn $ Maybe.maybe "No file path" Function.id (Record.get @"filePath" opts)
Text.putStrLn $ Show.show @Bool $ Record.get @"quiet" opts
12 changes: 12 additions & 0 deletions src/Hell.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import Data.Aeson (Value)
import qualified Data.Aeson as Json
import qualified Data.Aeson.KeyMap as KeyMap
import Data.Bifunctor
import Control.Applicative (Alternative (..), optional)
import qualified Data.Bool as Bool
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
Expand Down Expand Up @@ -481,6 +482,7 @@ data Forall where
OrdEqShow :: (forall (a :: Type). (Ord a, Eq a, Show a) => TypeRep a -> Forall) -> Forall
Monoidal :: (forall m. (Monoid m) => TypeRep m -> Forall) -> Forall
Applicable :: (forall (m :: Type -> Type). (Applicative m) => TypeRep m -> Forall) -> Forall
Alternable :: (forall (m :: Type -> Type). (Alternative m) => TypeRep m -> Forall) -> Forall
Monadic :: (forall (m :: Type -> Type). (Monad m) => TypeRep m -> Forall) -> Forall
GetOf ::
TypeRep (k :: Symbol) ->
Expand Down Expand Up @@ -651,6 +653,11 @@ tc (UForall _ forallLoc _ _ fall _ _ reps0) _env = go reps0 fall
Just Type.HRefl <- Type.eqTypeRep either' (typeRep @Either) ->
go reps (f rep)
| otherwise -> problem fa $ "type doesn't have enough instances " ++ show rep
go (SomeTypeRep rep : reps) fa@(Alternable f) =
if
| Just Type.HRefl <- Type.eqTypeRep rep (typeRep @Options.Parser) -> go reps (f rep)
| Just Type.HRefl <- Type.eqTypeRep rep (typeRep @Maybe) -> go reps (f rep)
| otherwise -> problem fa $ "type doesn't have enough instances " ++ show rep
go (SomeTypeRep rep : reps) fa@(Monoidal f) =
if
| Type.App either' _ <- rep,
Expand Down Expand Up @@ -690,6 +697,7 @@ showR = \case
OrdEqShow {} -> "forall a. (Ord a, Eq a, Show a)"
Monadic {} -> "forall a. Monad a"
Applicable {} -> "forall a. Applicative a"
Alternable {} -> "forall a. Alternative a"
Monoidal {} -> "forall a. Monoid a"
GetOf {} -> "<record getter>"
SetOf {} -> "<record setter>"
Expand Down Expand Up @@ -1414,6 +1422,7 @@ polyLits =
-- Applicative, we should add a Functor class or
-- this will try to raise it to an Applicative.
applicables = Set.fromList [''Functor, ''Applicative]
alternables = Set.fromList [''Functor, ''Applicative, ''Alternative]
monoidals = Set.fromList [''Semigroup, ''Monoid]
finalExpr =
if
Expand Down Expand Up @@ -1457,6 +1466,7 @@ polyLits =
| Set.isSubsetOf constraints' ordEqShow -> 'OrdEqShow
| Set.isSubsetOf constraints' monadics -> 'Monadic
| Set.isSubsetOf constraints' applicables -> 'Applicable
| Set.isSubsetOf constraints' alternables -> 'Alternable
| Set.isSubsetOf constraints' monoidals -> 'Monoidal
_ -> error "I'm not sure what to do with this variable."
)
Expand Down Expand Up @@ -1529,6 +1539,8 @@ polyLits =
"<*>" (<*>) :: forall f a b. Applicative f => f (a -> b) -> f a -> f b
"<$>" (<$>) :: forall f a b. Functor f => (a -> b) -> f a -> f b
"<**>" (Options.<**>) :: forall f a b. Applicative f => f a -> f (a -> b) -> f b
-- Alternative operations
"Alternative.optional" (optional) :: forall f a. Alternative f => f a -> f (Maybe a)
-- Monadic operations
"Monad.mapM_" mapM_ :: forall a m. (Monad m) => (a -> m ()) -> [a] -> m ()
"Monad.forM_" forM_ :: forall a m. (Monad m) => [a] -> (a -> m ()) -> m ()
Expand Down
Loading