Skip to content

Commit fac6125

Browse files
committed
Add simplification module
1 parent 6d5cc57 commit fac6125

File tree

3 files changed

+526
-0
lines changed

3 files changed

+526
-0
lines changed

src/Language/Elm/Expression.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Language.Elm.Expression where
1111
import Protolude
1212

1313
import Bound
14+
import Bound.Var (unvar)
1415
import Data.Bifoldable
1516
import Data.Eq.Deriving
1617
import Data.Ord.Deriving
@@ -104,6 +105,25 @@ if_ bool_ true false =
104105
tuple :: Expression v -> Expression v -> Expression v
105106
tuple e1 e2 = apps "Basics.," [e1, e2]
106107

108+
lets :: Eq b => [(b, Expression v)] -> Scope b Expression v -> Expression v
109+
lets =
110+
go (panic "Language.Elm.Expression.lets unbound var") identity
111+
where
112+
go :: Eq b => (b -> v') -> (v -> v') -> [(b, Expression v)] -> Scope b Expression v -> Expression v'
113+
go boundVar freeVar bindings scope =
114+
case bindings of
115+
[] ->
116+
unvar boundVar freeVar <$> fromScope scope
117+
118+
(v, e):bindings' ->
119+
Let (freeVar <$> e) $
120+
toScope $
121+
go
122+
(\b -> if b == v then B () else F $ boundVar b)
123+
(F . freeVar)
124+
bindings'
125+
scope
126+
107127
foldMapGlobals
108128
:: Monoid m
109129
=> (Name.Qualified -> m)

src/Language/Elm/Name.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
55
{-# LANGUAGE NoImplicitPrelude #-}
66
{-# LANGUAGE OverloadedStrings #-}
7+
{-# LANGUAGE ViewPatterns #-}
78
module Language.Elm.Name where
89

910
import Protolude
1011

1112
import Data.String
13+
import qualified Data.Char as Char
1214
import qualified Data.Text as Text
1315

1416
type Module = [Text]
@@ -21,6 +23,21 @@ newtype Local = Local Text
2123
data Qualified = Qualified Module Text
2224
deriving (Eq, Ord, Show, Generic, Hashable)
2325

26+
isConstructor :: Qualified -> Bool
27+
isConstructor name =
28+
case name of
29+
"List.::" ->
30+
True
31+
32+
"Basics.," ->
33+
True
34+
35+
Qualified _ (Text.uncons -> Just (firstChar, _)) ->
36+
Char.isUpper firstChar
37+
38+
_ ->
39+
False
40+
2441
instance IsString Qualified where
2542
fromString s =
2643
case unsnoc $ Text.splitOn "." $ fromString s of

0 commit comments

Comments
 (0)