Skip to content

Commit be7d90e

Browse files
committed
Remove Expression.If constructor
We can just use Case and pretty-print it as an if expression when it's on the appropriate form.
1 parent 2ae6404 commit be7d90e

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/Language/Elm/Expression.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ data Expression v
3030
| Record [(Name.Field, Expression v)]
3131
| Proj Name.Field
3232
| Case (Expression v) [(Pattern Int, Scope Int Expression v)]
33-
| If (Expression v) (Expression v) (Expression v)
3433
| List [Expression v]
3534
| String !Text
3635
| Int !Integer
@@ -50,7 +49,6 @@ instance Monad Expression where
5049
Record fs >>= f = Record [(fname, e >>= f) | (fname, e) <- fs]
5150
Proj f >>= _ = Proj f
5251
Case e brs >>= f = Case (e >>= f) [(pat, s >>>= f) | (pat, s) <- brs]
53-
If e e1 e2 >>= f = If (e >>= f) (e1 >>= f) (e2 >>= f)
5452
List es >>= f = List ((>>= f) <$> es)
5553
String s >>= _ = String s
5654
Int n >>= _ = Int n
@@ -81,6 +79,13 @@ appsView = go mempty
8179
_ ->
8280
(expr, args)
8381

82+
if_ :: Expression v -> Expression v -> Expression v -> Expression v
83+
if_ bool_ true false =
84+
Case bool_
85+
[ (Pattern.Con "Basics.True" [], Scope $ pure $ pure true)
86+
, (Pattern.Con "Basics.False" [], Scope $ pure $ pure false)
87+
]
88+
8489
(|>) :: Expression v -> Expression v -> Expression v
8590
(|>) e1 e2 = apps "Basics.|>" [e1, e2]
8691

@@ -133,9 +138,6 @@ foldMapGlobals f expr =
133138
(bifoldMap (Pattern.foldMapGlobals f) (foldMapGlobals f . Bound.fromScope))
134139
branches
135140

136-
If e e1 e2 ->
137-
foldMapGlobals f e <> foldMapGlobals f e1 <> foldMapGlobals f e2
138-
139141
List es ->
140142
foldMap (foldMapGlobals f) es
141143

src/Language/Elm/Pretty.hs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,17 @@ expression env prec expr =
457457
Expression.Proj f ->
458458
"." <> field f
459459

460+
Expression.Case bool_
461+
[ (Pattern.Con "Basics.True" [], unusedScope -> Just true)
462+
, (Pattern.Con "Basics.False" [], unusedScope -> Just false)
463+
] ->
464+
parensWhen (prec > ifPrec) $
465+
"if" <+> expression env 0 bool_ <+> "then" <> line <>
466+
indent 4 (expression env 0 true) <> line <>
467+
line <>
468+
"else" <> line <>
469+
indent 4 (expression env 0 false)
470+
460471
Expression.Case expr' branches ->
461472
parensWhen (prec > casePrec) $
462473
"case" <+> expression env 0 expr' <+> "of" <> line <>
@@ -472,14 +483,6 @@ expression env prec expr =
472483
]
473484
)
474485

475-
Expression.If expr' true false ->
476-
parensWhen (prec > ifPrec) $
477-
"if" <+> expression env 0 expr' <+> "then" <> line <>
478-
indent 4 (expression env 0 true) <> line <>
479-
line <>
480-
"else" <> line <>
481-
indent 4 (expression env 0 false)
482-
483486
Expression.List exprs ->
484487
list $ expression env 0 <$> exprs
485488

@@ -663,3 +666,7 @@ casePrec = 0
663666
ifPrec = 0
664667
funPrec = 0
665668
projPrec = 11
669+
670+
unusedScope :: (Monad f, Traversable f) => Bound.Scope b f a -> Maybe (f a)
671+
unusedScope =
672+
traverse (Bound.unvar (const Nothing) pure) . Bound.fromScope

0 commit comments

Comments
 (0)