Skip to content

Commit 18350b9

Browse files
committed
Type.Infer: binops: refactor
1 parent d513933 commit 18350b9

File tree

1 file changed

+53
-67
lines changed

1 file changed

+53
-67
lines changed

src/Nix/Type/Infer.hs

Lines changed: 53 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE MultiWayIf #-}
12
{-# LANGUAGE CPP #-}
23
{-# LANGUAGE AllowAmbiguousTypes #-}
34
{-# LANGUAGE ConstraintKinds #-}
@@ -33,6 +34,7 @@ import Control.Arrow
3334
import Control.Monad.Catch
3435
import Control.Monad.Except
3536
#if !MIN_VERSION_base(4,13,0)
37+
import Prelude hiding ( fail )
3638
import Control.Monad.Fail
3739
#endif
3840
import Control.Monad.Logic
@@ -302,77 +304,61 @@ unops u1 op =
302304
]
303305

304306
binops :: Type -> NBinaryOp -> [Constraint]
305-
binops u1 = \case
306-
NApp -> mempty -- this is handled separately
307-
308-
-- Equality tells you nothing about the types, because any two types are
309-
-- allowed.
310-
NEq -> mempty
311-
NNEq -> mempty
312-
313-
NGt -> inequality
314-
NGte -> inequality
315-
NLt -> inequality
316-
NLte -> inequality
317-
318-
NAnd -> [EqConst u1 (typeFun [typeBool, typeBool, typeBool])]
319-
NOr -> [EqConst u1 (typeFun [typeBool, typeBool, typeBool])]
320-
NImpl -> [EqConst u1 (typeFun [typeBool, typeBool, typeBool])]
321-
322-
NConcat -> [EqConst u1 (typeFun [typeList, typeList, typeList])]
323-
324-
NUpdate ->
325-
[ EqConst
326-
u1
327-
(TMany
328-
[ typeFun [typeSet, typeSet, typeSet]
329-
, typeFun [typeSet, typeNull, typeSet]
330-
, typeFun [typeNull, typeSet, typeSet]
331-
]
332-
)
333-
]
334-
335-
NPlus ->
336-
[ EqConst
337-
u1
338-
(TMany
339-
[ typeFun [typeInt, typeInt, typeInt]
340-
, typeFun [typeFloat, typeFloat, typeFloat]
341-
, typeFun [typeInt, typeFloat, typeFloat]
342-
, typeFun [typeFloat, typeInt, typeFloat]
343-
, typeFun [typeString, typeString, typeString]
344-
, typeFun [typePath, typePath, typePath]
345-
, typeFun [typeString, typeString, typePath]
346-
]
347-
)
348-
]
349-
NMinus -> arithmetic
350-
NMult -> arithmetic
351-
NDiv -> arithmetic
307+
binops u1 op =
308+
if
309+
-- NApp in fact is handled separately
310+
-- Equality tells nothing about the types, because any two types are allowed.
311+
| op `elem` [ NApp , NEq , NNEq ] -> mempty
312+
| op `elem` [ NGt , NGte , NLt , NLte ] -> inequality
313+
| op `elem` [ NAnd , NOr , NImpl ] -> gate
314+
| op == NConcat -> concatenation
315+
| op `elem` [ NMinus, NMult, NDiv ] -> arithmetic
316+
| op == NUpdate -> rUnion
317+
| op == NPlus -> addition
318+
| otherwise -> fail "GHC so far can not infer that this pattern match is full, so make it happy."
319+
352320
where
321+
322+
gate = eqCnst [typeBool, typeBool, typeBool]
323+
concatenation = eqCnst [typeList, typeList, typeList]
324+
325+
eqCnst l = [EqConst u1 (typeFun l)]
326+
353327
inequality =
354-
[ EqConst
355-
u1
356-
(TMany
357-
[ typeFun [typeInt, typeInt, typeBool]
358-
, typeFun [typeFloat, typeFloat, typeBool]
359-
, typeFun [typeInt, typeFloat, typeBool]
360-
, typeFun [typeFloat, typeInt, typeBool]
361-
]
362-
)
363-
]
328+
eqCnstMtx
329+
[ [typeInt , typeInt , typeBool]
330+
, [typeFloat, typeFloat, typeBool]
331+
, [typeInt , typeFloat, typeBool]
332+
, [typeFloat, typeInt , typeBool]
333+
]
364334

365335
arithmetic =
366-
[ EqConst
367-
u1
368-
(TMany
369-
[ typeFun [typeInt, typeInt, typeInt]
370-
, typeFun [typeFloat, typeFloat, typeFloat]
371-
, typeFun [typeInt, typeFloat, typeFloat]
372-
, typeFun [typeFloat, typeInt, typeFloat]
373-
]
374-
)
375-
]
336+
eqCnstMtx
337+
[ [typeInt , typeInt , typeInt ]
338+
, [typeFloat, typeFloat, typeFloat]
339+
, [typeInt , typeFloat, typeFloat]
340+
, [typeFloat, typeInt , typeFloat]
341+
]
342+
343+
rUnion =
344+
eqCnstMtx
345+
[ [typeSet , typeSet , typeSet]
346+
, [typeSet , typeNull, typeSet]
347+
, [typeNull, typeSet , typeSet]
348+
]
349+
350+
addition =
351+
eqCnstMtx
352+
[ [typeInt , typeInt , typeInt ]
353+
, [typeFloat , typeFloat , typeFloat ]
354+
, [typeInt , typeFloat , typeFloat ]
355+
, [typeFloat , typeInt , typeFloat ]
356+
, [typeString, typeString, typeString]
357+
, [typePath , typePath , typePath ]
358+
, [typeString, typeString, typePath ]
359+
]
360+
361+
eqCnstMtx mtx = [EqConst u1 (TMany (typeFun <$> mtx))]
376362

377363
liftInfer :: Monad m => m a -> InferT s m a
378364
liftInfer = InferT . lift . lift . lift

0 commit comments

Comments
 (0)