on 2deedfb help sign in
elk/src/Type/Constraint.hs
170 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 , FuncName(..)
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 | CLookup R.Region N.Name (Expected Type)
39 | CInstance R.Region FuncName 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 (Maybe FuncName) Index.ZeroBased
81 | BadArity
82 | RecordAccess N.Name
83 | RecordUpdate
84 | Destructure
87data SubContext
88 = TypedIfBranch Index.ZeroBased
89 | TypedCaseBranch Index.ZeroBased
90 | TypedBody
93data FuncName
94 = FuncName N.Name
95 | OpName N.Name
98data Category
99 = List
100 | Number
101 | Float
102 | String
103 | Char
104 | Binop
105 | If
106 | Case
107 | CallResult (Maybe FuncName)
108 | Lambda
109 | Accessor N.Name
110 | Access N.Name
111 | Record
112 | Tuple
113 | Unit
114 | Shader
115 | Effects
119-- PATTERN EXPECTATIONS
122data PExpected tipe
123 = PNoExpectation tipe
124 | PFromContext R.Region PContext tipe
127data PContext
128 = PTypedArg N.Name Index.ZeroBased
129 | PCaseMatch Index.ZeroBased
130 | PCtorArg N.Name Index.ZeroBased
131 | PListEntry Index.ZeroBased
132 | PTail
135data PCategory
136 = PRecord
137 | PUnit
138 | PTuple
139 | PList
140 | PCtor N.Name
141 | PInt
142 | PStr
143 | PChr
147-- HELPERS
150typeReplace :: Expected Type -> Can.Type -> Expected Can.Type
151typeReplace expectation tipe =
152 case expectation of
153 NoExpectation _ ->
154 NoExpectation tipe
156 FromContext region context _ ->
157 FromContext region context tipe
159 FromAnnotation name arity context _ ->
160 FromAnnotation name arity context tipe
163ptypeReplace :: PExpected Type -> Can.Type -> PExpected Can.Type
164ptypeReplace expectation tipe =
165 case expectation of
166 PNoExpectation _ ->
167 PNoExpectation tipe
169 PFromContext region context _ ->
170 PFromContext region context tipe