9
9
{-# LANGUAGE CPP #-}
10
10
{-# LANGUAGE NamedFieldPuns #-}
11
11
{-# LANGUAGE OverloadedStrings #-}
12
+ {-# LANGUAGE QuasiQuotes #-}
12
13
{-# LANGUAGE TemplateHaskell #-}
13
14
14
15
module Clash.Core.Util where
@@ -23,6 +24,7 @@ import Data.List
23
24
import Data.List.Extra (nubOrd )
24
25
import Data.Maybe
25
26
(fromJust , isJust , mapMaybe , catMaybes )
27
+ import qualified Data.String.Interpolate as I
26
28
import qualified Data.Text as T
27
29
import Data.Text.Prettyprint.Doc (line )
28
30
#if !MIN_VERSION_base(4,11,0)
@@ -39,7 +41,7 @@ import Clash.Core.Name
39
41
import Clash.Core.Pretty (ppr , showPpr )
40
42
import Clash.Core.Subst
41
43
(extendTvSubst , mkSubst , mkTvSubst , substTy , substTyWith ,
42
- substTyInVar , extendTvSubstList )
44
+ substTyInVar , extendTvSubstList , aeqType )
43
45
import Clash.Core.Term
44
46
(LetBinding , Pat (.. ), PrimInfo (.. ), Term (.. ), Alt , WorkInfo (.. ),
45
47
TickInfo (.. ), collectArgs )
@@ -327,7 +329,8 @@ applyTypeToArgs e m opTy args = go opTy args
327
329
-- Do not iterate 'piResultTy', because it's inefficient to substitute one
328
330
-- variable at a time; instead use 'piResultTys'
329
331
piResultTy
330
- :: TyConMap
332
+ :: HasCallStack
333
+ => TyConMap
331
334
-> Type
332
335
-> Type
333
336
-> Type
@@ -340,15 +343,26 @@ piResultTy m ty arg = case piResultTyMaybe m ty arg of
340
343
-- Do not iterate 'piResultTyMaybe', because it's inefficient to substitute one
341
344
-- variable at a time; instead use 'piResultTys'
342
345
piResultTyMaybe
343
- :: TyConMap
346
+ :: HasCallStack
347
+ => TyConMap
344
348
-> Type
345
349
-> Type
346
350
-> Maybe Type
347
351
piResultTyMaybe m ty arg
348
352
| Just ty' <- coreView1 m ty
349
353
= piResultTyMaybe m ty' arg
350
- | FunTy _ res <- tyView ty
351
- = Just res
354
+ | FunTy a res <- tyView ty
355
+ = if debugIsOn && not (aeqType a arg) then error [I. i |
356
+ Unexpected application. A function with type:
357
+
358
+ #{showPpr ty}
359
+
360
+ Got applied to an argument of type:
361
+
362
+ #{showPpr arg}
363
+ |]
364
+ else
365
+ Just res
352
366
| ForAllTy tv res <- ty
353
367
= let emptySubst = mkSubst (mkInScopeSet (tyFVsOfTypes [arg,res]))
354
368
in Just (substTy (extendTvSubst emptySubst tv arg) res)
@@ -379,16 +393,27 @@ piResultTyMaybe m ty arg
379
393
-- For efficiency reasons, when there are no foralls, we simply drop arrows from
380
394
-- a function type/kind.
381
395
piResultTys
382
- :: TyConMap
396
+ :: HasCallStack
397
+ => TyConMap
383
398
-> Type
384
399
-> [Type ]
385
400
-> Type
386
401
piResultTys _ ty [] = ty
387
402
piResultTys m ty origArgs@ (arg: args)
388
403
| Just ty' <- coreView1 m ty
389
404
= piResultTys m ty' origArgs
390
- | FunTy _ res <- tyView ty
391
- = piResultTys m res args
405
+ | FunTy a res <- tyView ty
406
+ = if debugIsOn && not (aeqType a arg) then error [I. i |
407
+ Unexpected application. A function with type:
408
+
409
+ #{showPpr ty}
410
+
411
+ Got applied to an argument of type:
412
+
413
+ #{showPpr arg}
414
+ |]
415
+ else
416
+ piResultTys m res args
392
417
| ForAllTy tv res <- ty
393
418
= go (extendVarEnv tv arg emptyVarEnv) res args
394
419
| otherwise
0 commit comments