Skip to content

Commit 05fc9f7

Browse files
Merge pull request #139 from chrisdone/cd/2026-08-12-add-Enum-and-some-sugar
Add Enum class, instances and [x..] [x..y] sugar
2 parents 4c5c076 + 151f3b7 commit 05fc9f7

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

examples/45-enum.hell

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
main = do
2+
IO.print $ List.take 5 $ Enum.enumFrom 1
3+
IO.print $ Enum.enumFromTo 1 10
4+
IO.print $ List.take 5 $ Enum.enumFrom 'a'
5+
IO.print $ List.take 5 $ Enum.enumFrom Bool.False
6+
IO.print $ List.take 5 $ Enum.enumFrom (Int.toInteger 1)
7+
IO.print $ List.take 5 $ Enum.enumFrom $
8+
Maybe.maybe (Error.error "Bad day.") Function.id $ Day.fromGregorianValid (Int.toInteger 2020) 01 01
9+
10+
-- simple list generators
11+
IO.print [1 .. 3]
12+
IO.print $ List.take 5 ['a' ..]

src/Hell.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,12 @@ instances =
904904
instance0 @Ord @Text,
905905
instance0 @Ord @ByteString,
906906
instance0 @Ord @ExitCode,
907+
instance0 @Enum @Int,
908+
instance0 @Enum @Integer,
909+
instance0 @Enum @Day,
910+
instance0 @Enum @DayOfWeek,
911+
instance0 @Enum @Bool,
912+
instance0 @Enum @Char,
907913
instance0 @Monad @IO,
908914
instance0 @Monad @Maybe,
909915
instance0 @Monad @[],
@@ -1226,6 +1232,15 @@ desugarExp userDefinedTypeAliases globals = go mempty
12261232
squash _ = Left BadDoNotation
12271233
squash stmts >>= go scope
12281234
HSE.RecConstr _ qname fields -> go scope $ makeConstructRecord qname fields
1235+
-- generators sugars
1236+
HSE.EnumFromTo l from to -> do
1237+
let enumFromTo' = HSE.Var l (HSE.Qual l (HSE.ModuleName l "Enum") (HSE.Ident l "enumFromTo"))
1238+
go scope $
1239+
HSE.App l (HSE.App l enumFromTo' from) to
1240+
HSE.EnumFrom l from -> do
1241+
let enumFrom' = HSE.Var l (HSE.Qual l (HSE.ModuleName l "Enum") (HSE.Ident l "enumFrom"))
1242+
go scope $ HSE.App l enumFrom' from
1243+
-- end of generator sugars
12291244
e -> Left $ UnsupportedSyntax $ show e
12301245

12311246
-- | Handles both user-defined case and primitive type case (Maybe, Either, etc.)
@@ -2184,6 +2199,10 @@ polyLits =
21842199
"Ord.lt" (Ord.<) :: forall a. (Ord a) => a -> a -> Bool
21852200
"Ord.gt" (Ord.>) :: forall a. (Ord a) => a -> a -> Bool
21862201

2202+
-- Enum
2203+
"Enum.enumFrom" enumFrom :: forall a. Enum a => a -> [a]
2204+
"Enum.enumFromTo" enumFromTo :: forall a. Enum a => a -> a -> [a]
2205+
21872206
-- Tuples
21882207
"Tuple.(,)" (,) :: forall a b. a -> b -> (a, b)
21892208
"Tuple.(,)" (,) :: forall a b. a -> b -> (a, b)

0 commit comments

Comments
 (0)