diff --git a/examples/45-enum.hell b/examples/45-enum.hell new file mode 100644 index 0000000..1439d61 --- /dev/null +++ b/examples/45-enum.hell @@ -0,0 +1,12 @@ +main = do + IO.print $ List.take 5 $ Enum.enumFrom 1 + IO.print $ Enum.enumFromTo 1 10 + IO.print $ List.take 5 $ Enum.enumFrom 'a' + IO.print $ List.take 5 $ Enum.enumFrom Bool.False + IO.print $ List.take 5 $ Enum.enumFrom (Int.toInteger 1) + IO.print $ List.take 5 $ Enum.enumFrom $ + Maybe.maybe (Error.error "Bad day.") Function.id $ Day.fromGregorianValid (Int.toInteger 2020) 01 01 + + -- simple list generators + IO.print [1 .. 3] + IO.print $ List.take 5 ['a' ..] diff --git a/src/Hell.hs b/src/Hell.hs index 9e04c61..9bfdcf5 100644 --- a/src/Hell.hs +++ b/src/Hell.hs @@ -904,6 +904,12 @@ instances = instance0 @Ord @Text, instance0 @Ord @ByteString, instance0 @Ord @ExitCode, + instance0 @Enum @Int, + instance0 @Enum @Integer, + instance0 @Enum @Day, + instance0 @Enum @DayOfWeek, + instance0 @Enum @Bool, + instance0 @Enum @Char, instance0 @Monad @IO, instance0 @Monad @Maybe, instance0 @Monad @[], @@ -1226,6 +1232,15 @@ desugarExp userDefinedTypeAliases globals = go mempty squash _ = Left BadDoNotation squash stmts >>= go scope HSE.RecConstr _ qname fields -> go scope $ makeConstructRecord qname fields + -- generators sugars + HSE.EnumFromTo l from to -> do + let enumFromTo' = HSE.Var l (HSE.Qual l (HSE.ModuleName l "Enum") (HSE.Ident l "enumFromTo")) + go scope $ + HSE.App l (HSE.App l enumFromTo' from) to + HSE.EnumFrom l from -> do + let enumFrom' = HSE.Var l (HSE.Qual l (HSE.ModuleName l "Enum") (HSE.Ident l "enumFrom")) + go scope $ HSE.App l enumFrom' from + -- end of generator sugars e -> Left $ UnsupportedSyntax $ show e -- | Handles both user-defined case and primitive type case (Maybe, Either, etc.) @@ -2184,6 +2199,10 @@ polyLits = "Ord.lt" (Ord.<) :: forall a. (Ord a) => a -> a -> Bool "Ord.gt" (Ord.>) :: forall a. (Ord a) => a -> a -> Bool + -- Enum + "Enum.enumFrom" enumFrom :: forall a. Enum a => a -> [a] + "Enum.enumFromTo" enumFromTo :: forall a. Enum a => a -> a -> [a] + -- Tuples "Tuple.(,)" (,) :: forall a b. a -> b -> (a, b) "Tuple.(,)" (,) :: forall a b. a -> b -> (a, b)