on ba0b721 help sign in
elk/src/Type/Constraint.hs
171 lines 3.1 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Type.Constraint
4 ( Constraint(..)
5 , exists
6 , Expected(..)
7 , Context(..)
8 , SubContext(..)
9 , MaybeName(..)
10 , Category(..)
11 , PExpected(..)
12 , PContext(..)
13 , PCategory(..)
14 , typeReplace
15 , ptypeReplace
16 )
17 where
20import qualified Data.Map.Strict as Map
22import qualified AST.Canonical as Can
23import qualified Data.Index as Index
24import qualified Elm.Name as N
25import qualified Reporting.Annotation as A
26import qualified Reporting.Region as R
27import Type.Type (Type, Variable)
31-- CONSTRAINTS
34data Constraint
35 = CTrue
36 | CSaveTheEnvironment
37 | CEqual R.Region Category Type (Expected Type)
38 | CLocal R.Region N.Name (Expected Type)
39 | CForeign R.Region N.Name Can.Annotation (Expected Type)
40 | CPattern R.Region PCategory Type (PExpected Type)
41 | CAnd [Constraint]
42 | CBranch
43 { _a :: Type
44 , _b :: Type
45 , _eq :: Constraint
46 , _neq :: IO Constraint
47 }
48 | CLet
49 { _rigidVars :: [Variable]
50 , _flexVars :: [Variable]
51 , _header :: Map.Map N.Name (A.Located Type)
52 , _headerCon :: Constraint
53 , _bodyCon :: Constraint
54 }
57exists :: [Variable] -> Constraint -> Constraint
58exists flexVars constraint =
59 CLet [] flexVars Map.empty constraint CTrue
63-- EXPRESSION EXPECTATIONS
66data Expected tipe
67 = NoExpectation tipe
68 | FromContext R.Region Context tipe
69 | FromAnnotation N.Name Int SubContext tipe
72data Context
73 = ListEntry Index.ZeroBased
74 | Negate
75 | OpLeft N.Name
76 | OpRight N.Name
77 | IfCondition
78 | IfBranch Index.ZeroBased
79 | CaseBranch Index.ZeroBased
80 | CallArg MaybeName Index.ZeroBased
81 | TooManyArgs MaybeName Int
82 | RecordAccess N.Name
83 | RecordUpdate
84 | Destructure
87data SubContext
88 = TypedIfBranch Index.ZeroBased
89 | TypedCaseBranch Index.ZeroBased
90 | TypedBody
93data MaybeName
94 = FuncName N.Name
95 | CtorName N.Name
96 | OpName N.Name
97 | NoName
100data Category
101 = List
102 | Number
103 | Float
104 | String
105 | Char
106 | If
107 | Case
108 | CallResult MaybeName
109 | Lambda
110 | Accessor N.Name
111 | Access N.Name
112 | Record
113 | Tuple
114 | Unit
115 | Shader
116 | Effects
120-- PATTERN EXPECTATIONS
123data PExpected tipe
124 = PNoExpectation tipe
125 | PFromContext R.Region PContext tipe
128data PContext
129 = PTypedArg N.Name Index.ZeroBased
130 | PCaseMatch Index.ZeroBased
131 | PCtorArg N.Name Index.ZeroBased
132 | PListEntry Index.ZeroBased
133 | PTail
136data PCategory
137 = PRecord
138 | PUnit
139 | PTuple
140 | PList
141 | PCtor N.Name
142 | PInt
143 | PStr
144 | PChr
148-- HELPERS
151typeReplace :: Expected Type -> Can.Type -> Expected Can.Type
152typeReplace expectation tipe =
153 case expectation of
154 NoExpectation _ ->
155 NoExpectation tipe
157 FromContext region context _ ->
158 FromContext region context tipe
160 FromAnnotation name arity context _ ->
161 FromAnnotation name arity context tipe
164ptypeReplace :: PExpected Type -> Can.Type -> PExpected Can.Type
165ptypeReplace expectation tipe =
166 case expectation of
167 PNoExpectation _ ->
168 PNoExpectation tipe
170 PFromContext region context _ ->
171 PFromContext region context tipe