1{-# OPTIONS_GHC -Wall #-}
2module Type.Constrain.Declaration where
4import qualified SourceSyntax.Annotation as A
5import qualified SourceSyntax.Declaration as D
6import qualified SourceSyntax.Expression as E
7import qualified SourceSyntax.Pattern as P
8import qualified SourceSyntax.Type as T
10toExpr :: [D.Declaration] -> [E.Def]
11toExpr = concatMap toDefs
13toDefs :: D.Declaration -> [E.Def]
16 D.Definition def -> [def]
18 D.Datatype name tvars constructors -> concatMap toDefs' constructors
20 toDefs' (ctor, tipes) =
21 let vars = take (length tipes) arguments
22 tbody = T.Data name $ map T.Var tvars
23 body = A.none . E.Data ctor $ map (A.none . E.rawVar) vars
24 in [ definition ctor (buildFunction body vars) (foldr T.Lambda tbody tipes) ]
26 D.TypeAlias name _ tipe@(T.Record fields ext) ->
27 [ definition name (buildFunction record vars) (foldr T.Lambda tipe args) ]
29 args = map snd fields ++ maybe [] (\x -> [T.Var x]) ext
31 var = A.none . E.rawVar
32 vars = take (length args) arguments
34 efields = zip (map fst fields) (map var vars)
36 Nothing -> A.none $ E.Record efields
37 Just _ -> foldl (\r (f,v) -> A.none $ E.Insert r f v) (var $ last vars) efields
39 -- Type aliases must be added to an extended equality dictionary,
40 -- but they do not require any basic constraints.
41 D.TypeAlias _ _ _ -> []
45 D.Out name expr@(A.A s _) tipe ->
46 [ definition name (A.A s $ E.PortOut name tipe expr) tipe ]
48 [ definition name (A.none $ E.PortIn name tipe) tipe ]
50 -- no constraints are needed for fixity declarations
55arguments = map (:[]) ['a'..'z'] ++ map (\n -> "_" ++ show (n :: Int)) [1..]
57buildFunction :: E.Expr -> [String] -> E.Expr
58buildFunction body@(A.A s _) vars =
59 foldr (\p e -> A.A s (E.Lambda p e)) body (map P.Var vars)
61definition :: String -> E.Expr -> T.Type -> E.Def
62definition name expr tipe = E.Definition (P.Var name) expr (Just tipe)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list