on master help sign in
Continue reorganizing the code-base.

Remove uses of uniplate, introduces a regression in testing the ordering of Defs in let-expressions.

commit721ce4f1921d4d0fd04156790adbed87662b8f98 authorevancz <info@elm-lang.org> dateThu 13 Jun 2013, 22:45 (13 years ago) parent272ecc6
14 files changed+249−233
Elm.cabal−2 compiler/Gen/Cases.hs → compiler/Generate/Cases.hs+23−20 compiler/Gen/GenerateHtml.hs → compiler/Generate/Html.hs+8−8 compiler/Gen/CompileToJS.hs → compiler/Generate/JavaScript.hs+21−18 compiler/Gen/ExtractNoscript.hs → compiler/Generate/Noscript.hs+24−17 compiler/Initialize.hs+7−9 compiler/Transform/Optimize.hs+60−57 compiler/Transform/Substitute.hs+4−4 compiler/Types/Alias.hs+59−54 compiler/Types/Constrain.hs+23−23 compiler/Types/Hints.hs+6−6 compiler/Types/Solver.hs+6−6 compiler/Types/Substitutions.hs+6−7 compiler/Types/Unify.hs+2−2
Elm.cabal−2
@@ -65,7 +65,6 @@ Library
6565
6666 Build-depends: base >=4.2 && <5,
6767 containers >= 0.3,
68− uniplate >= 1.6,
6968 transformers >= 0.2,
7069 mtl >= 2,
7170 deepseq,
@@ -118,7 +117,6 @@ Executable elm
118117
119118 Build-depends: base >=4.2 && <5,
120119 containers >= 0.3,
121− uniplate >= 1.6,
122120 transformers >= 0.2,
123121 mtl >= 2,
124122 deepseq,
compiler/Gen/Cases.hs → compiler/Generate/Cases.hsrenamed+23−20
@@ -1,15 +1,16 @@
11
2−module Cases (caseToMatch, Match (..), Clause (..), matchSubst) where
2+module Generate.Cases (caseToMatch, Match (..), Clause (..), matchSubst) where
33
44import Control.Arrow (first)
55import Control.Monad (liftM,foldM)
66import Data.List (groupBy,sortBy,lookup)
77import Data.Maybe (fromMaybe)
88
9−import Ast
10−import Located
11−import Guid
12−import Substitute
9+import Unique
10+import SourceSyntax.Location
11+import SourceSyntax.Pattern
12+import SourceSyntax.Expression
13+import Transform.Substitute
1314
1415caseToMatch patterns = do
1516 v <- newVar
@@ -18,17 +19,19 @@ caseToMatch patterns = do
1819newVar = do n <- guid
1920 return $ "case" ++ show n
2021
21−data Match = Match String [Clause] Match
22− | Break
23− | Fail
24− | Other CExpr
25− | Seq [Match]
26− deriving Show
22+data Match t v
23+ = Match String [Clause t v] (Match t v)
24+ | Break
25+ | Fail
26+ | Other (LExpr t v)
27+ | Seq [Match t v]
28+ deriving Show
2729
28−data Clause = Clause String [String] Match
29− deriving Show
30+data Clause t v =
31+ Clause String [String] (Match t v)
32+ deriving Show
3033
31−matchSubst :: [(String,String)] -> Match -> Match
34+matchSubst :: [(String,String)] -> Match t v -> Match t v
3235matchSubst _ Break = Break
3336matchSubst _ Fail = Fail
3437matchSubst pairs (Seq ms) = Seq (map (matchSubst pairs) ms)
@@ -45,7 +48,7 @@ isCon _ = False
4548
4649isVar p = not (isCon p)
4750
48−match :: [String] -> [([Pattern],CExpr)] -> Match -> GuidCounter Match
51+match :: [String] -> [([Pattern],LExpr t v)] -> Match t v -> Unique (Match t v)
4952match [] [] def = return def
5053match [] [([],e)] Fail = return $ Other e
5154match [] [([],e)] Break = return $ Other e
@@ -55,14 +58,14 @@ match vs cs def
5558 | all isCon cs = matchCon vs cs def
5659 | otherwise = matchMix vs cs def
5760
58−matchVar :: [String] -> [([Pattern],CExpr)] -> Match -> GuidCounter Match
61+matchVar :: [String] -> [([Pattern],LExpr t v)] -> Match t v -> Unique (Match t v)
5962matchVar (v:vs) cs def = match vs (map subVar cs) def
6063 where
6164 subVar (p:ps, ce@(L t s e)) =
6265 let
6366 loc = L t s
6467 subOnePattern (PVar x) = subst x (Var v) e
65− subOnePattern (PAsVar x p) =
68+ subOnePattern (PAlias x p) =
6669 subst x (Var v) e
6770 subOnePattern PAnything = e
6871 subOnePattern (PRecord fs) =
@@ -70,14 +73,14 @@ matchVar (v:vs) cs def = match vs (map subVar cs) def
7073 in
7174 (ps, L t s $ subOnePattern p)
7275
73−matchCon :: [String] -> [([Pattern],CExpr)] -> Match -> GuidCounter Match
76+matchCon :: [String] -> [([Pattern],LExpr t v)] -> Match t v -> Unique (Match t v)
7477matchCon (v:vs) cs def = (flip (Match v) def) `liftM` mapM toClause css
7578 where css = groupBy (withName (==)) $ sortBy (withName compare) cs
7679 withName f (PData n1 _:_,_) (PData n2 _:_,_) = f n1 n2
7780 toClause cs = let (PData name _ : _ , _) = head cs in
7881 matchClause name (v:vs) cs Break
7982
80−matchClause :: String -> [String] -> [([Pattern],CExpr)] -> Match -> GuidCounter Clause
83+matchClause :: String -> [String] -> [([Pattern],LExpr t v)] -> Match t v -> Unique (Clause t v)
8184matchClause c (v:vs) cs def =
8285 do vs' <- getVars
8386 Clause c vs' `liftM` match (vs' ++ vs) (map flatten cs) def
@@ -85,6 +88,6 @@ matchClause c (v:vs) cs def =
8588 getVars = let (PData _ ps : _, _) = head cs in
8689 mapM (\_ -> newVar) ps
8790
88−matchMix :: [String] -> [([Pattern],CExpr)] -> Match -> GuidCounter Match
91+matchMix :: [String] -> [([Pattern],LExpr t v)] -> Match t v -> Unique (Match t v)
8992matchMix vs cs def = foldM (flip $ match vs) def (reverse css)
9093 where css = groupBy (\p1 p2 -> isCon p1 == isCon p2) cs
compiler/Gen/GenerateHtml.hs → compiler/Generate/Html.hsrenamed+8−8
@@ -1,9 +1,9 @@
11{-# LANGUAGE OverloadedStrings #-}
2−module GenerateHtml (generateHtml,
3− createHtml,
4− JSStyle (..),
5− JSSource (..)
6− ) where
2+module Generate.Html (generateHtml,
3+ createHtml,
4+ JSStyle (..),
5+ JSSource (..)
6+ ) where
77
88import Data.List (intercalate)
99import Text.Blaze (preEscapedToMarkup)
@@ -17,9 +17,9 @@ import qualified Data.ByteString.Lazy.Char8 as BS
1717
1818import Ast
1919import Initialize (buildFromSource)
20−import CompileToJS
21−import ExtractNoscript
22−import Libraries as Libraries
20+import Generate.JavaScript
21+import Generate.Noscript
22+import Metadata.Libraries as Libraries
2323
2424data JSStyle = Minified | Readable
2525data JSSource = Link String | Source JSStyle String
compiler/Gen/CompileToJS.hs → compiler/Generate/JavaScript.hsrenamed+21−18
@@ -1,4 +1,4 @@
1−module CompileToJS (showErr, jsModule) where
1+module Generate.JavaScript (showErr, jsModule) where
22
33import Control.Arrow (first,second)
44import Control.Monad (liftM,(<=<),join,ap)
@@ -9,12 +9,9 @@ import Data.Either (partitionEithers)
99import qualified Text.Pandoc as Pan
1010import Data.Maybe (maybeToList)
1111
12−import Ast
13−import Located
14−import Rename (derename)
15−import Cases
16−import Guid
17−import Rename (deprime)
12+import Unique
13+import Generate.Cases
14+import SourceSyntax.Everything hiding (parens)
1815import Types.Types ( Type(RecordT) )
1916
2017showErr :: String -> String
@@ -119,7 +116,7 @@ jsImport (modul, how) =
119116
120117
121118
122−stmtsToJS :: [Statement] -> String
119+stmtsToJS :: [Declaration t v] -> String
123120stmtsToJS stmts = run $ do program <- mapM toJS (sortBy cmpStmt stmts)
124121 return (concat program)
125122 where
@@ -135,9 +132,9 @@ stmtsToJS stmts = run $ do program <- mapM toJS (sortBy cmpStmt stmts)
135132 TypeAlias _ _ _ -> 0
136133
137134class ToJS a where
138− toJS :: a -> GuidCounter String
135+ toJS :: a -> Unique String
139136
140−instance ToJS Def where
137+instance ToJS (Def t v) where
141138 toJS (FnDef x [] e) = assign x `liftM` toJS' e
142139 toJS (FnDef f as e) = (assign f . wrapper . func) `liftM` toJS' e
143140 where
@@ -151,15 +148,15 @@ instance ToJS Def where
151148 toJS (TypeAnnotation _ _) = return ""
152149
153150
154−instance ToJS Statement where
151+instance ToJS (Declaration t v) where
155152 toJS stmt =
156153 case stmt of
157154 Definition d -> toJS d
158155 Datatype _ _ tcs -> concat `liftM` mapM (toJS . toDef) tcs
159156 where toDef (name,args) =
160157 let vars = map (('a':) . show) [1..length args] in
161− Definition . FnDef name vars . notLocated $
162− Data (derename name) (map (notLocated . Var) vars)
158+ Definition . FnDef name vars . none $
159+ Data (derename name) (map (none . Var) vars)
163160 ImportEvent js base elm _ ->
164161 do v <- toJS' base
165162 return $ concat [ "\nvar " ++ elm ++ "=Elm.Signal(elm).constant(" ++ v ++ ");"
@@ -174,7 +171,7 @@ instance ToJS Statement where
174171 , "document.dispatchEvent(e); return v; })(", elm, ");" ]
175172 TypeAlias n _ t -> return ""
176173
177−toJS' :: CExpr -> GuidCounter String
174+toJS' :: LExpr t v -> Unique String
178175toJS' (L txt span expr) =
179176 case expr of
180177 MultiIf ps -> multiIfToJS span ps
@@ -200,15 +197,21 @@ makeRecord kvs = record `liftM` collect kvs
200197 record kvs = fields . (("_", hidden kvs) :) . Map.toList . Map.map head $ kvs
201198
202199
203−instance ToJS Expr where
204− toJS expr =
205− case expr of
206− Var x -> return $ x
200+instance ToJS Literal where
201+ toJS lit =
202+ case lit of
207203 Chr c -> return $ quoted [c]
208204 Str s -> return $ "_str" ++ parens (quoted s)
209205 IntNum n -> return $ show n
210206 FloatNum n -> return $ show n
211207 Boolean b -> return $ if b then "true" else "false"
208+
209+
210+instance ToJS (Expr t v) where
211+ toJS expr =
212+ case expr of
213+ Var x -> return x
214+ Literal lit -> toJS lit
212215 Range lo hi -> jsRange `liftM` toJS' lo `ap` toJS' hi
213216 Access e x -> access x `liftM` toJS' e
214217 Remove e x -> remove x `liftM` toJS' e
compiler/Gen/ExtractNoscript.hs → compiler/Generate/Noscript.hsrenamed+24−17
@@ -1,24 +1,24 @@
1−module ExtractNoscript (extractNoscript) where
1+module Generate.Noscript (noscript) where
22
3−import Ast
4−import Located
3+import Data.List (isInfixOf)
4+import SourceSyntax.Everything
55import qualified Text.Pandoc as Pan
66
7−extractNoscript :: Module -> String
8−extractNoscript modul = concat (extract modul)
7+noscript :: Module t v -> String
8+noscript modul = concat (extract modul)
99
1010class Extract a where
1111 extract :: a -> [String]
1212
13−instance Extract Module where
13+instance Extract (Module t v) where
1414 extract (Module _ _ _ stmts) =
1515 map (\s -> "<p>" ++ s ++ "</p>") (concatMap extract stmts)
1616
17−instance Extract Statement where
17+instance Extract (Declaration t v) where
1818 extract (Definition d) = extract d
1919 extract _ = []
2020
21−instance Extract Def where
21+instance Extract (Def t v) where
2222 extract (FnDef _ _ e) = extract e
2323 extract (OpDef _ _ _ e) = extract e
2424 extract _ = []
@@ -26,21 +26,24 @@ instance Extract Def where
2626instance Extract e => Extract (Located e) where
2727 extract (L _ _ e) = extract e
2828
29−instance Extract Expr where
29+instance Extract (Expr t v) where
3030 extract expr =
3131 let f = extract in
3232 case expr of
33− Str s -> [s]
33+ Literal (Str s) -> [s]
3434 Binop op e1 e2 -> case (op, f e1, f e2) of
3535 ("++", [s1], [s2]) -> [s1 ++ s2]
3636 (_ , ss1 , ss2 ) -> ss1 ++ ss2
3737 Lambda v e -> f e
38− App (L _ _ (App (L _ _ (Var "link")) src)) txt -> linkExtract src txt
39− App (L _ _ (App (L _ _ (Var "Text.link")) src)) txt -> linkExtract src txt
40− App (L _ _ (Var "header")) e -> tag "h1" e
41− App (L _ _ (Var "bold")) e -> tag "b" e
42− App (L _ _ (Var "italic")) e -> tag "i" e
43− App (L _ _ (Var "monospace")) e -> tag "code" e
38+ App (L _ _ (App (L _ _ (App (L _ _ (Var func)) w)) h)) src
39+ | "image" `isInfixOf` func -> extractImage src
40+ App (L _ _ (App (L _ _ (Var func)) src)) txt
41+ | "link" `isInfixOf` func -> extractLink src txt
42+ App (L _ _ (Var func)) e
43+ | "header" `isInfixOf` func -> tag "h1" e
44+ | "bold" `isInfixOf` func -> tag "b" e
45+ | "italic" `isInfixOf` func -> tag "i" e
46+ | "monospace" `isInfixOf` func -> tag "code" e
4447 App e1 e2 -> f e1 ++ f e2
4548 Let defs e -> concatMap extract defs ++ f e
4649 Var _ -> []
@@ -50,10 +53,14 @@ instance Extract Expr where
5053 Markdown doc -> [ Pan.writeHtmlString Pan.def doc ]
5154 _ -> []
5255
53−linkExtract src txt =
56+extractLink src txt =
5457 case (extract src, extract txt) of
5558 ([s1],[s2]) -> [ "<a href=\"" ++ s1 ++ "\">" ++ s2 ++ "</a>" ]
5659 ( ss1, ss2) -> ss1 ++ ss2
5760
61+extractImage src =
62+ case extract src of
63+ [s] -> ["<img src=\"" ++ s ++ "\">"]
64+ ss -> ss
5865
5966tag t e = map (\s -> concat [ "<", t, ">", s, "</", t, ">" ]) (extract e)
compiler/Initialize.hs+7−9
@@ -5,35 +5,33 @@ import Control.Monad.Error
55import Data.List (lookup,nub)
66import qualified Data.Map as Map
77
8−import Ast
9−import Data.Either (lefts,rights)
8+import SourceSyntax.Everything
109import Data.List (intercalate,partition)
1110import Parse.Parser (parseProgram, parseDependencies)
12−import Rename
13−import qualified Libraries as Libs
11+import qualified Metadata.Libraries as Libs
1412import Types.Types ((-:))
1513import Types.Hints (hints)
1614import Types.Unify (unify)
1715import Types.Alias (dealias, mistakes)
18−import Optimize
16+import Transform.Optimize
1917import System.Exit
2018import System.FilePath
2119
22−checkMistakes :: Module -> Either String Module
20+checkMistakes :: Module t v -> Either String (Module t v)
2321checkMistakes modul@(Module name ex im stmts) =
2422 case mistakes stmts of
2523 m:ms -> Left (unlines (m:ms))
2624 [] -> return modul
2725
28−checkTypes :: Module -> Either String Module
26+checkTypes :: Module t v -> Either String (Module t v)
2927checkTypes modul =
3028 do subs <- unify hints modul
3129 subs `seq` return (optimize (renameModule modul))
3230
33−check :: Module -> Either String Module
31+check :: Module t v -> Either String (Module t v)
3432check = checkMistakes >=> checkTypes
3533
36−buildFromSource :: Bool -> String -> Either String Module
34+buildFromSource :: Bool -> String -> Either String (Module t v)
3735buildFromSource noPrelude src =
3836 let add = if noPrelude then id else Libs.addPrelude in
3937 (check . add) =<< (parseProgram src)
compiler/Transform/Optimize.hs+60−57
@@ -1,10 +1,9 @@
1−module Optimize (optimize) where
1+module Transform.Optimize (optimize) where
22
3−import Ast
4−import Located
3+import SourceSyntax.Everything
54import Control.Arrow (second, (***))
65import Data.Char (isAlpha)
7−import Substitute
6+import Transform.Substitute
87
98optimize (Module name ims exs stmts) =
109 Module name ims exs (map optimizeStmt stmts)
@@ -15,12 +14,12 @@ optimizeStmt stmt = if stmt == stmt' then stmt' else optimizeStmt stmt'
1514class Simplify a where
1615 simp :: a -> a
1716
18−instance Simplify Statement where
17+instance Simplify (Declaration t v) where
1918 simp (Definition def) = Definition (simp def)
2019 simp (ImportEvent js b elm t) = ImportEvent js (simp b) elm t
2120 simp stmt = stmt
2221
23−instance Simplify Def where
22+instance Simplify (Def t v) where
2423 simp (FnDef func args e) = FnDef func args (simp e)
2524 simp (OpDef op a1 a2 e) = OpDef op a1 a2 (simp e)
2625 simp x = x
@@ -28,12 +27,12 @@ instance Simplify Def where
2827instance Simplify e => Simplify (Located e) where
2928 simp (L t s e) = L t s (simp e)
3029
31−instance Simplify Expr where
30+instance Simplify (Expr t v) where
3231 simp expr =
3332 let f = simp in
3433 case expr of
3534 Range e1 e2 -> Range (f e1) (f e2)
36− Binop op e1 e2 -> simp_binop op (f e1) (f e2)
35+ Binop op e1 e2 -> binop op (f e1) (f e2)
3736 Lambda x e -> Lambda x (f e)
3837 Record fs -> Record (map (\(f,as,e) -> (f, as, simp e)) fs)
3938 App (L t s (Lambda x e1)) e2 ->
@@ -51,41 +50,43 @@ instance Simplify Expr where
5150clipBranches [] = []
5251clipBranches (e:es) =
5352 case e of
54− (L _ _ (Boolean True), _) -> [e]
53+ (L _ _ (Literal (Boolean True)), _) -> [e]
5554 _ -> e : clipBranches es
5655
5756
5857isValue e =
59− case e of { IntNum _ -> True
60− ; FloatNum _ -> True
61− ; Chr _ -> True
62− ; Str _ -> True
63− ; Boolean _ -> True
64− ; Var _ -> True
65− ; Data _ _ -> True
66− ; _ -> False }
58+ case e of
59+ Literal _ -> True
60+ Var _ -> True
61+ Data _ _ -> True
62+ _ -> False
6763
68−simp_binop = binop
6964
7065binop op ce1@(L t1 s1 e1) ce2@(L t2 s2 e2) =
71− let c1 = L t1 s1 in
72− let c2 = L t2 s2 in
66+ let c1 = L t1 s1
67+ c2 = L t2 s2
68+ int = Literal . IntNum
69+ str = Literal . Str
70+ bool = Literal . Boolean
71+ in
7372 case (op, e1, e2) of
74− (_, IntNum n, IntNum m) -> case op of
75− { "+" -> IntNum $ (+) n m
76− ; "-" -> IntNum $ (-) n m
77− ; "*" -> IntNum $ (*) n m
78− ; "^" -> IntNum $ n ^ m
79− ; "div" -> IntNum $ div n m
80− ; "mod" -> IntNum $ mod n m
81− ; "<" -> Boolean $ n < m
82− ; ">" -> Boolean $ n < m
83− ; "<=" -> Boolean $ n <= m
84− ; ">=" -> Boolean $ n >= m
85− ; "==" -> Boolean $ n == m
86− ; "/=" -> Boolean $ n /= m
87− ; _ -> Binop op ce1 ce2 }
88−
73+ (_, Literal (IntNum n), Literal (IntNum m)) ->
74+ case op of
75+ { "+" -> int $ (+) n m
76+ ; "-" -> int $ (-) n m
77+ ; "*" -> int $ (*) n m
78+ ; "^" -> int $ n ^ m
79+ ; "div" -> int $ div n m
80+ ; "mod" -> int $ mod n m
81+ ; "rem" -> int $ rem n m
82+ ; "<" -> bool $ n < m
83+ ; ">" -> bool $ n < m
84+ ; "<=" -> bool $ n <= m
85+ ; ">=" -> bool $ n >= m
86+ ; "==" -> bool $ n == m
87+ ; "/=" -> bool $ n /= m
88+ ; _ -> Binop op ce1 ce2 }
89+ {--
8990 -- flip order to move lone integers to the left
9091 ("+", _, IntNum n) -> binop "+" ce2 ce1
9192 ("*", _, IntNum n) -> binop "*" ce2 ce1
@@ -95,47 +96,49 @@ binop op ce1@(L t1 s1 e1) ce2@(L t2 s2 e2) =
9596 binop "+" (c1 $ IntNum (n+m)) ce
9697 ("+", Binop "+" (L _ _ (IntNum n)) ce1'
9798 , Binop "+" (L _ _ (IntNum m)) ce2') ->
98− binop "+" (notLocated $ IntNum (n+m)) (notLocated $ Binop "+" ce1' ce2')
99+ binop "+" (none $ IntNum (n+m)) (none $ Binop "+" ce1' ce2')
99100
100101 ("*", IntNum 0, _) -> e1
101102 ("*", IntNum 1, _) -> e2
102103 ("*", IntNum n, Binop "*" (L _ _ (IntNum m)) ce) ->
103− binop "*" (notLocated $ IntNum (n*m)) ce
104+ binop "*" (none $ IntNum (n*m)) ce
104105 ("*", Binop "*" (L _ _ (IntNum n)) ce1'
105106 , Binop "*" (L _ _ (IntNum m)) ce2') ->
106− binop "*" (notLocated $ IntNum (n*m)) (notLocated $ Binop "*" ce1' ce2')
107+ binop "*" (none $ IntNum (n*m)) (none $ Binop "*" ce1' ce2')
107108
108109 ("-", _, IntNum 0) -> e1
109110 ("/", _, IntNum 1) -> e1
110111 ("div", _, IntNum 1) -> e1
111−
112− (_, Boolean n, Boolean m) -> case op of "&&" -> Boolean $ n && m
113− "||" -> Boolean $ n || m
114− _ -> Binop op ce1 ce2
115−
116− ("&&", Boolean True, _) -> e2
117− ("&&", Boolean False, _) -> Boolean False
118− ("||", Boolean True, _) -> Boolean True
119− ("||", Boolean False, _) -> e2
112+--}
113+ (_, Literal (Boolean n), Literal (Boolean m)) ->
114+ case op of
115+ "&&" -> bool $ n && m
116+ "||" -> bool $ n || m
117+ _ -> Binop op ce1 ce2
118+
119+ ("&&", Literal (Boolean True), _) -> e2
120+ ("&&", Literal (Boolean False), _) -> bool False
121+ ("||", Literal (Boolean True), _) -> bool True
122+ ("||", Literal (Boolean False), _) -> e2
120123
121124 ("::", _, _) -> let (L _ _ e) = cons ce1 ce2 in e
122125
123− ("++", Str s1, Str s2) -> Str $ s1 ++ s2
124− ("++", Str s1, Binop "++" (L _ _ (Str s2)) ce) ->
125− Binop "++" (c1 $ Str $ s1 ++ s2) ce
126− ("++", Binop "++" e (L _ _ (Str s1)), Str s2) ->
127− Binop "++" e (c1 $ Str $ s1 ++ s2)
126+ ("++", Literal (Str s1), Literal (Str s2)) -> str $ s1 ++ s2
127+ ("++", Literal (Str s1), Binop "++" (L _ _ (Literal (Str s2))) ce) ->
128+ Binop "++" (c1 . str $ s1 ++ s2) ce
129+ ("++", Binop "++" e (L _ _ (Literal (Str s1))), Literal (Str s2)) ->
130+ Binop "++" e (c1 . str $ s1 ++ s2)
128131
129132 ("++", Data "Nil" [], _) -> e2
130133 ("++", _, Data "Nil" []) -> e1
131− ("++", Data "Cons" [h,t], _) -> Data "Cons" [h, notLocated $ binop "++" t ce2]
134+ ("++", Data "Cons" [h,t], _) -> Data "Cons" [h, none $ binop "++" t ce2]
132135
133136 ("|>", _, _) -> App ce2 ce1
134137 ("<|", _, _) -> App ce1 ce2
135138 (".", _, _) ->
136− Lambda "x" (notLocated $
137− App ce1 (notLocated $ App ce2 (notLocated $ Var "x")))
139+ Lambda "x" (none $
140+ App ce1 (none $ App ce2 (none $ Var "x")))
138141
139142 _ | isAlpha (head op) || '_' == head op ->
140− App (notLocated $ App (notLocated $ Var op) ce1) ce2
141− | otherwise -> Binop op ce1 ce2
143+ App (none $ App (none $ Var op) ce1) ce2
144+ | otherwise -> Binop op ce1 ce2
compiler/Transform/Substitute.hs+4−4
@@ -1,10 +1,10 @@
1−module Substitute (subst) where
1+module Transform.Substitute (subst) where
22
3−import Ast
4−import Located
3+import SourceSyntax.Expression
4+import SourceSyntax.Location
55import Control.Arrow (second, (***))
66
7−subst :: String -> Expr -> Expr -> Expr
7+subst :: String -> Expr t v -> Expr t v -> Expr t v
88subst old new expr =
99 let f (L t s e) = L t s (subst old new e) in
1010 case expr of
compiler/Types/Alias.hs+59−54
@@ -1,16 +1,15 @@
11{-# LANGUAGE PatternGuards #-}
22module Types.Alias (dealias, get, mistakes) where
33
4−import Ast
5−import Located
4+import SourceSyntax.Everything
65import Control.Arrow (second)
76import Data.List (group,sort)
8−import Data.Maybe (mapMaybe)
97import qualified Data.Map as Map
108import qualified Data.Set as Set
119import Types.Substitutions (subst)
1210import Types.Types
1311import Data.Generics.Uniplate.Data
12+import Data.Data
1413
1514builtins :: [(String,([X],Type))]
1615builtins =
@@ -30,7 +29,7 @@ builtins =
3029 ("LineStyle", ([], makeRecord line))
3130 ]
3231
33−get :: [Statement] -> Map.Map String ([X],Type)
32+get :: [Declaration t v] -> Map.Map String ([X],Type)
3433get stmts = Map.fromList (builtins ++ concatMap getAlias stmts)
3534 where getAlias stmt = case stmt of
3635 TypeAlias alias xs t -> [(alias, (xs,t))]
@@ -47,63 +46,74 @@ dealias aliases t =
4746 RecordT r t -> RecordT (Map.map (map f) r) (f t)
4847 _ -> t
4948
50−mistakes :: [Statement] -> [String]
49+mistakes :: [Declaration t v] -> [String]
5150mistakes stmts = badKinds stmts ++ dups stmts ++ badOrder stmts
5251
53−badKinds :: [Statement] -> [String]
54−badKinds stmts = [msg tname | t <- universeBi stmts, tname <- badT t]
52+badKinds :: [Declaration t v] -> [String]
53+badKinds stmts = map msg (concatMap badS stmts)
5554 where
5655 msg x = "Type Error: Type alias '" ++ x ++
5756 "' was given the wrong number of arguments."
5857
5958 badT :: Type -> [String]
60− badT (ADT name ts)
61− | Just (xs, _) <- Map.lookup name (get stmts),
62− length xs /= length ts = [name]
63− badT _ = []
64−
65−annotation :: Def -> Maybe String
59+ badT t =
60+ case t of
61+ ADT name ts ->
62+ case Map.lookup name (get stmts) of
63+ Just (xs,t) | length xs == length ts -> []
64+ | otherwise -> [name]
65+ Nothing -> concatMap badT ts
66+ LambdaT t u -> badT t ++ badT u
67+ RecordT r t -> badT t ++ concatMap badT (concat (Map.elems r))
68+ _ -> []
69+
70+ badS :: Declaration t v -> [String]
71+ badS s =
72+ case s of
73+ Datatype _ _ tcs -> concatMap badT (concatMap snd tcs)
74+ ExportEvent _ _ tipe -> badT tipe
75+ ImportEvent _ _ _ tipe -> badT tipe
76+ TypeAlias _ _ tipe -> badT tipe
77+ Definition d ->
78+ case d of
79+ TypeAnnotation _ tipe -> badT tipe
80+ _ -> []
81+
82+annotation :: Declaration t v -> [String]
6683annotation s =
6784 case s of
68− TypeAnnotation name _ -> Just name
69− _ -> Nothing
85+ Definition (TypeAnnotation name _) -> [name]
86+ _ -> []
7087
71−definition :: Def -> Maybe String
88+definition :: Declaration t v -> [String]
7289definition s =
7390 case s of
74− FnDef name _ _ -> Just name
75− OpDef name _ _ _ -> Just name
76− _ -> Nothing
77−
78−checkTopLevelAndLets :: [Statement] -> (String -> [Def] -> [a]) -> [a]
79−checkTopLevelAndLets stmts fcheck =
80− fcheck "at top-level" topLevelDefs ++
81− concatMap (fcheck "in let binding") allLetDefs
82− where
83− topLevelDefs = mapMaybe maybeDef stmts
84− maybeDef (Definition d) = Just d
85− maybeDef _ = Nothing
86− allLetDefs = [defList | Let defList _ <- universeBi stmts]
91+ Definition d -> [defName d]
92+ _ -> []
8793
88−dups :: [Statement] -> [String]
89−dups stmts = checkTopLevelAndLets stmts $ \ctxt defs ->
90− let
91− dup :: (Def -> Maybe String) -> [String]
92− dup f = map head . filter ((>1) . length) . group . sort $ mapMaybe f defs
94+defName :: Def t v -> String
95+defName d =
96+ case d of
97+ FnDef n _ _ -> n
98+ OpDef n _ _ _ -> n
99+
100+dups :: [Declaration t v] -> [String]
101+dups stmts = map defMsg (dup definition) ++ map annMsg (dup annotation)
102+ where
103+ --dup :: (Declaration t v -> [String]) -> [String]
104+ dup f = map head . filter ((>1) . length) . group . sort $ concatMap f stmts
93105
94106 msg = "Syntax Error: There can only be one "
95− defMsg x = msg ++ "definition of '" ++ x ++ "' " ++ ctxt ++ "."
96− annMsg x = msg ++ "type annotation for '" ++ x ++ "'" ++ ctxt ++ "."
97− in
98− map defMsg (dup definition) ++ map annMsg (dup annotation)
99−
100−badOrder :: [Statement] -> [String]
101−badOrder stmts = checkTopLevelAndLets stmts $ \ctxt defs ->
102− let
107+ defMsg x = msg ++ "top-level definition of '" ++ x ++ "'."
108+ annMsg x = msg ++ "type annotation for '" ++ x ++ "'."
109+
110+badOrder :: [Declaration t v] -> [String]
111+badOrder stmts = map msg $ missings (sort $ expectedPairs as ds) (sort $ actualPairs stmts)
112+ where
103113 msg x = "Syntax Error: The type annotation for '" ++ x ++
104− "' must be directly above its definition in " ++ ctxt ++ "."
105− as = sort $ mapMaybe annotation defs
106− ds = sort $ mapMaybe definition defs
114+ "' must be directly above its definition."
115+ as = sort $ concatMap annotation stmts
116+ ds = sort $ concatMap definition stmts
107117
108118 expectedPairs :: [String] -> [String] -> [String]
109119 expectedPairs as ds =
@@ -114,14 +124,11 @@ badOrder stmts = checkTopLevelAndLets stmts $ \ctxt defs ->
114124 GT -> expectedPairs (x:xs) ys
115125 ( _ , _ ) -> []
116126
117− actualPairs :: [Def] -> [String]
127+ --actualPairs :: [Declaration t v] -> [String]
118128 actualPairs stmts =
119129 case stmts of
120− TypeAnnotation n _ : rest@(d : _) ->
121− (if Just n == definition d
122− then [n]
123− else []
124− ) ++ actualPairs rest
130+ Definition (TypeAnnotation n _) : Definition d : rest ->
131+ (if n == defName d then [n] else []) ++ actualPairs rest
125132 t:s:rest -> actualPairs (s:rest)
126133 _ -> []
127134
@@ -133,6 +140,4 @@ badOrder stmts = checkTopLevelAndLets stmts $ \ctxt defs ->
133140 EQ -> missings es as
134141 GT -> a : missings (e:es) as
135142 ( [] , _ ) -> actual
136− ( _ , [] ) -> expected
137− in
138− map msg $ missings (sort $ expectedPairs as ds) (sort $ actualPairs defs)
143+ ( _ , [] ) -> expected
compiler/Types/Constrain.hs+23−23
@@ -9,9 +9,8 @@ import Data.List (foldl',sort,group,isPrefixOf,intercalate,isSuffixOf)
99import qualified Data.Map as Map
1010import qualified Data.Set as Set
1111
12−import Ast
13−import Located
14−import Guid
12+import SourceSyntax.Everything
13+import Unique
1514
1615import Types.Types
1716import qualified Types.Substitutions as Subs
@@ -55,7 +54,7 @@ findAmbiguous hints assumptions continue =
5554 _ -> continue
5655
5756mergeSchemes :: [Map.Map String Scheme]
58− -> GuidCounter (TVarMap, Constraints, Map.Map String Scheme)
57+ -> Unique (TVarMap, Constraints, Map.Map String Scheme)
5958mergeSchemes schmss = do (ass,css,sss) <- unzip3 `liftM` mapM split kvs
6059 return (Map.unions ass, concat css, Map.unions sss)
6160 where
@@ -73,7 +72,7 @@ mergeSchemes schmss = do (ass,css,sss) <- unzip3 `liftM` mapM split kvs
7372
7473--constrain :: Map.Map String Scheme
7574-- -> Module
76−-- -> GuidCounter (Either String Constraints)
75+-- -> Unique (Either String Constraints)
7776constrain typeHints (Module _ _ imports stmts) = do
7877 (ass,css,schemess) <- unzip3 `liftM` mapM stmtGen stmts
7978 aliasHints <- getAliases imports `liftM` typeHints
@@ -98,7 +97,7 @@ type Constraints = [Located Constraint]
9897
9998loc e span = L (Just $ show e) span
10099
101−gen :: CExpr -> GuidCounter (TVarMap, Constraints, Type)
100+gen :: LExpr t v -> Unique (TVarMap, Constraints, Type)
102101gen (L _ span expr) =
103102 let loc' = L (Just $ show expr) span in
104103 case expr of
@@ -145,14 +144,14 @@ gen (L _ span expr) =
145144 (ass,css,ts) <- liftM unzip3 $ mapM (caseGen t) cases
146145 return ( unionsA $ as:ass
147146 , let cases' = map snd cases
148− locs = zipWith epos cases' (tail cases')
147+ locs = zipWith merge cases' (tail cases')
149148 csts = zipWith (:=:) ts (tail ts)
150149 cs' = zipWith ($) locs csts
151150 in concat $ cs' : cs : css
152151 , head ts)
153152
154153 Data name es ->
155− gen $ foldl' (\f x -> epos f x $ App f x) (loc' $ Var name) es
154+ gen $ foldl' (\f x -> merge f x $ App f x) (loc' $ Var name) es
156155
157156 Binop op e1 e2 ->
158157 gen $ loc' (App (loc' $ App (loc' $ Var op) e1) e2)
@@ -215,22 +214,23 @@ gen (L _ span expr) =
215214 , c1 ++ c2 ++ [ L t s (t1 :=: bool) ]
216215 , t2 )
217216
218− IntNum _ -> do t <- beta
219− return (Map.empty, [loc' $ t :<: number], t)
220−
221− FloatNum _ -> primitive float
222− Chr _ -> primitive char
223− Str _ -> primitive string
224− Boolean _ -> primitive bool
225217 Markdown _ -> primitive element
218+ Literal lit ->
219+ case lit of
220+ IntNum _ -> do t <- beta
221+ return (Map.empty, [loc' $ t :<: number], t)
222+ FloatNum _ -> primitive float
223+ Chr _ -> primitive char
224+ Str _ -> primitive string
225+ Boolean _ -> primitive bool
226226
227227
228−primitive :: Type -> GuidCounter (TVarMap, Constraints, Type)
228+primitive :: Type -> Unique (TVarMap, Constraints, Type)
229229primitive t = return (Map.empty, [], t)
230230
231231caseGen :: Type
232− -> (Pattern, CExpr)
233− -> GuidCounter (TVarMap, Constraints, Type)
232+ -> (Pattern, LExpr t v)
233+ -> Unique (TVarMap, Constraints, Type)
234234caseGen tipe (p, ce@(L _ span e)) = do
235235 (as ,cs ,t) <- gen ce
236236 (as',cs',_) <- patternGen (loc p span) tipe as p
@@ -240,7 +240,7 @@ patternGen :: (Constraint -> Located Constraint)
240240 -> Type -- Type of e in `case e of ...`
241241 -> TVarMap
242242 -> Pattern
243− -> GuidCounter (TVarMap, Constraints, Type)
243+ -> Unique (TVarMap, Constraints, Type)
244244patternGen loc tipe as pattern =
245245 case pattern of
246246 PAnything -> do b <- beta ; return ( as, [], b )
@@ -250,7 +250,7 @@ patternGen loc tipe as pattern =
250250 let cs = map (loc . (b :=:) . VarT) (Map.findWithDefault [] v as)
251251 return ( Map.delete v as, loc (b :=: tipe) : cs, b )
252252
253− PAsVar v p -> do
253+ PAlias v p -> do
254254 b <- beta
255255 let cs = map (loc . (b :=:) . VarT) (Map.findWithDefault [] v as)
256256 (as', cs', tipe') <- patternGen loc b as p
@@ -279,7 +279,7 @@ patternGen loc tipe as pattern =
279279 , t )
280280
281281
282−defScheme :: Def -> GuidCounter (Map.Map String [X], Scheme)
282+defScheme :: Def t v -> Unique (Map.Map String [X], Scheme)
283283defScheme def = do (as,cs,hint) <- defGen def
284284 return ( as, snd hint )
285285
@@ -305,8 +305,8 @@ defGenHelp name args e = do
305305 return ( as', [], (name, scheme) )
306306
307307
308−stmtGen :: Statement
309− -> GuidCounter (TVarMap, Constraints, Map.Map String Scheme)
308+stmtGen :: Declaration t v
309+ -> Unique (TVarMap, Constraints, Map.Map String Scheme)
310310stmtGen stmt =
311311 case stmt of
312312 Definition def -> do (as,cs,hint) <- defGen def
compiler/Types/Hints.hs+6−6
@@ -4,14 +4,14 @@ import Control.Arrow (first)
44import Control.Monad (liftM)
55import Data.Maybe (catMaybes)
66import qualified Data.Map as Map
7−import Guid
8−import qualified Libraries as Libs
9−import Parse.Library (iParse)
10−import Parse.Types
7+import Unique
8+import qualified Metadata.Libraries as Libs
9+import Parse.Helpers (iParse)
10+import Parse.Type
1111import qualified Types.Substitutions as Subs
1212import Types.Types
1313
14−hints :: GuidCounter [(String, Scheme)]
14+hints :: Unique [(String, Scheme)]
1515hints = liftM catMaybes (mapM toScheme values)
1616 where
1717 values :: [(String, String)]
@@ -20,7 +20,7 @@ hints = liftM catMaybes (mapM toScheme values)
2020 addPrefixes :: [(String,[(String, String)])] -> [(String, String)]
2121 addPrefixes = concatMap (\(m,vs) -> map (first (\n -> m ++ "." ++ n)) vs)
2222
23− toScheme :: (String, String) -> GuidCounter (Maybe (String, Scheme))
23+ toScheme :: (String, String) -> Unique (Maybe (String, Scheme))
2424 toScheme (name, 't':'y':'p':'e':' ':_) = return Nothing
2525 toScheme (name, 'd':'a':'t':'a':' ':_) = return Nothing
2626 toScheme (name, tipeString) =
compiler/Types/Solver.hs+6−6
@@ -1,7 +1,7 @@
11
22module Types.Solver (solver) where
33
4−import Located
4+import SourceSyntax.Everything
55import Control.Arrow (second)
66import Control.Monad (liftM)
77import Data.Either (lefts,rights)
@@ -9,7 +9,7 @@ import Data.List (foldl')
99import Data.Maybe (isJust)
1010import qualified Data.Set as Set
1111import qualified Data.Map as Map
12−import Guid
12+import Unique
1313import Types.Types
1414import Types.Substitutions
1515import Types.Alias (dealias)
@@ -22,7 +22,7 @@ isSolved ss c = False
2222
2323type Aliases = Map.Map String ([X],Type)
2424
25−crush :: Aliases -> Scheme -> GuidCounter (Either String Scheme)
25+crush :: Aliases -> Scheme -> Unique (Either String Scheme)
2626crush aliases forall@(Forall xs cs t) =
2727 do subs <- solver aliases Map.empty cs
2828 return $ do ss' <- subs
@@ -68,11 +68,11 @@ recordConstraints eq fs t fs' t' =
6868 Map.difference fs' fs
6969 ]
7070 where constrain :: Map.Map String [Type] -> Map.Map String [Type]
71− -> GuidCounter [Located Constraint]
71+ -> Unique [Located Constraint]
7272 constrain as bs = liftM concat . sequence . Map.elems $
7373 Map.intersectionWithKey (zipper []) as bs
7474 zipper :: [Located Constraint] -> String -> [Type] -> [Type]
75− -> GuidCounter [Located Constraint]
75+ -> Unique [Located Constraint]
7676 zipper cs k xs ys =
7777 case (xs,ys) of
7878 (a:as, b:bs) -> zipper (eq a b : cs) k as bs
@@ -87,7 +87,7 @@ recordConstraints eq fs t fs' t' =
8787solver :: Aliases
8888 -> Map.Map X Type
8989 -> [Located Constraint]
90− -> GuidCounter (Either String (Map.Map X Type))
90+ -> Unique (Either String (Map.Map X Type))
9191solver _ subs [] = return $ Right subs
9292solver aliases subs (L txt span c : cs) =
9393 let loc = L txt span
compiler/Types/Substitutions.hs+6−7
@@ -7,14 +7,13 @@ module Types.Substitutions (subst,
77 generalize,
88 superize) where
99
10−import Ast
11−import Located
10+import SourceSyntax.Everything
1211import Control.Monad (liftM, liftM2)
1312import Control.Monad.State (runState, State, get, put)
1413import Data.List (nub)
1514import qualified Data.Set as Set
1615import qualified Data.Map as Map
17−import Guid
16+import Unique
1817import Types.Types
1918
2019class Subst a where
@@ -78,18 +77,18 @@ instance FreeVars a => FreeVars [a] where
7877
7978occurs x t = x `elem` freeVars t
8079
81−concretize :: Scheme -> GuidCounter (Type, [Located Constraint])
80+concretize :: Scheme -> Unique (Type, [Located Constraint])
8281concretize (Forall xs cs t) = do
8382 ss <- zip xs `liftM` mapM (\_ -> liftM VarT guid) xs
8483 return (subst ss t, subst ss cs)
8584
86−rescheme :: Scheme -> GuidCounter Scheme
85+rescheme :: Scheme -> Unique Scheme
8786rescheme (Forall xs cs t) = do
8887 xs' <- mapM (const guid) xs
8988 let ss = zip xs (map VarT xs')
9089 return $ Forall xs' (subst ss cs) (subst ss t)
9190
92−generalize :: [X] -> Scheme -> GuidCounter Scheme
91+generalize :: [X] -> Scheme -> Unique Scheme
9392generalize exceptions (Forall xs cs t) = rescheme (Forall (xs ++ frees) cs t)
9493 where allFrees = Set.fromList $ freeVars t ++ concatMap freeVars cs
9594 frees = Set.toList $ Set.difference allFrees (Set.fromList exceptions)
@@ -97,7 +96,7 @@ generalize exceptions (Forall xs cs t) = rescheme (Forall (xs ++ frees) cs t)
9796newtype Superize a = S { runSuper :: State ([X], [X], [X]) a }
9897 deriving (Monad)
9998
100−superize :: String -> Type -> GuidCounter Scheme
99+superize :: String -> Type -> Unique Scheme
101100superize name tipe =
102101 do constraints <- liftM concat $
103102 sequence [ mapM (<: nmbr) (nub ns)
compiler/Types/Unify.hs+2−2
@@ -4,8 +4,8 @@ module Types.Unify (unify) where
44import Control.Monad (liftM)
55import qualified Data.Map as Map
66
7−import Ast
8−import Guid
7+import SourceSyntax.Module
8+import Unique
99import qualified Types.Constrain as Constrain
1010import qualified Types.Solver as Solver
1111import qualified Types.Alias as Alias