on ebda137 help sign in
elk/src/Transform/Declaration.hs
168 lines 6.0 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module Transform.Declaration (combineAnnotations, toExpr) where
4import Control.Applicative ((<$>))
6import qualified AST.Annotation as A
7import qualified AST.Declaration as D
8import qualified AST.Expression.General as E
9import qualified AST.Expression.Source as Source
10import qualified AST.Expression.Valid as Valid
11import qualified AST.Expression.Canonical as Canonical
12import qualified AST.Module as Module
13import qualified AST.Pattern as P
14import qualified AST.Type as T
15import qualified AST.Variable as Var
17import qualified Transform.Expression as Expr
18import qualified Transform.Definition as Def
21combineAnnotations :: [D.SourceDecl] -> Either String [D.ValidDecl]
22combineAnnotations =
23 go
24 where
25 errorMessage kind name =
26 "Syntax Error: The type annotation for " ++ kind ++ " '" ++
27 name ++ "' must be directly above its definition."
29 exprCombineAnnotations =
30 Expr.crawlLet Def.combineAnnotations
32 go decls =
33 case decls of
34 -- simple cases, pass them through with no changes
35 [] ->
36 return []
38 D.Datatype name tvars ctors : rest ->
39 (:) (D.Datatype name tvars ctors) <$> go rest
41 D.TypeAlias name tvars alias : rest ->
42 (:) (D.TypeAlias name tvars alias) <$> go rest
44 D.Fixity assoc prec op : rest ->
45 (:) (D.Fixity assoc prec op) <$> go rest
47 -- combine definitions
48 D.Definition def : defRest ->
49 case def of
50 Source.Definition pat expr ->
51 do expr' <- exprCombineAnnotations expr
52 let def' = Valid.Definition pat expr' Nothing
53 (:) (D.Definition def') <$> go defRest
55 Source.TypeAnnotation name tipe ->
56 case defRest of
57 D.Definition (Source.Definition pat@(P.Var name') expr) : rest
58 | name == name' ->
59 do expr' <- exprCombineAnnotations expr
60 let def' = Valid.Definition pat expr' (Just tipe)
61 (:) (D.Definition def') <$> go rest
63 _ -> Left (errorMessage "value" name)
65 -- combine wires
66 D.Wire wire : wireRest ->
67 case wire of
68 D.InputAnnotation name tipe ->
69 (:) (D.Wire (D.Input name tipe)) <$> go wireRest
71 D.OutputDefinition name _ ->
72 Left (errorMessage "output" name)
74 D.OutputAnnotation name tipe ->
75 case wireRest of
76 D.Wire (D.OutputDefinition name' expr) : rest | name == name' ->
77 do expr' <- exprCombineAnnotations expr
78 (:) (D.Wire (D.Output name expr' tipe)) <$> go rest
80 _ ->
81 Left (errorMessage "output" name)
83 D.LoopbackDefinition name _tipe ->
84 Left (errorMessage "loopback" name)
86 D.LoopbackAnnotation name tipe ->
87 case wireRest of
88 D.Wire (D.LoopbackDefinition name' expr) : rest | name == name' ->
89 do expr' <- exprCombineAnnotations expr
90 (:) (D.Wire (D.Loopback name (Just expr') tipe)) <$> go rest
92 _ ->
93 (:) (D.Wire (D.Loopback name Nothing tipe)) <$> go wireRest
96toExpr :: Module.Name -> [D.CanonicalDecl] -> [Canonical.Def]
97toExpr moduleName =
98 concatMap (toDefs moduleName)
101toDefs :: Module.Name -> D.CanonicalDecl -> [Canonical.Def]
102toDefs moduleName decl =
103 let typeVar = Var.Canonical (Var.Module moduleName) in
104 case decl of
105 D.Definition def ->
106 [def]
108 D.Datatype name tvars constructors ->
109 concatMap toDefs' constructors
110 where
111 toDefs' (ctor, tipes) =
112 let vars = take (length tipes) arguments
113 tbody = T.App (T.Type (typeVar name)) (map T.Var tvars)
114 body = A.none . E.Data ctor $ map (A.none . E.localVar) vars
115 in
116 [ definition ctor (buildFunction body vars) (foldr T.Lambda tbody tipes) ]
118 D.TypeAlias name tvars tipe@(T.Record fields ext) ->
119 [ definition name (buildFunction record vars) (foldr T.Lambda result args) ]
120 where
121 result =
122 T.Aliased (typeVar name) (zip tvars (map T.Var tvars)) tipe
124 args =
125 map snd fields ++ maybe [] (:[]) ext
127 var = A.none . E.localVar
128 vars = take (length args) arguments
130 efields = zip (map fst fields) (map var vars)
131 record =
132 case ext of
133 Nothing -> A.none $ E.Record efields
134 Just _ ->
135 foldl (\r (f,v) -> A.none $ E.Insert r f v) (var $ last vars) efields
137 -- Type aliases must be added to an extended equality dictionary,
138 -- but they do not require any basic constraints.
139 D.TypeAlias _ _ _ ->
140 []
142 D.Wire wire ->
143 case wire of
144 D.Output name expr@(A.A s _) tipe ->
145 [ definition name (A.A s $ E.Output name tipe expr) tipe ]
146 D.Input name tipe ->
147 [ definition name (A.none $ E.Input name tipe) tipe ]
148 D.Loopback name maybeExpr tipe ->
149 [ definition name (A.none $ E.Loopback name tipe maybeExpr) tipe ]
151 -- no constraints are needed for fixity declarations
152 D.Fixity _ _ _ ->
153 []
156arguments :: [String]
157arguments =
158 map (:[]) ['a'..'z'] ++ map (\n -> "_" ++ show (n :: Int)) [1..]
161buildFunction :: Canonical.Expr -> [String] -> Canonical.Expr
162buildFunction body@(A.A s _) vars =
163 foldr (\p e -> A.A s (E.Lambda p e)) body (map P.Var vars)
166definition :: String -> Canonical.Expr -> T.CanonicalType -> Canonical.Def
167definition name expr tipe =
168 Canonical.Definition (P.Var name) expr (Just tipe)