99{-# LANGUAGE CPP #-}
1010{-# LANGUAGE NamedFieldPuns #-}
1111{-# LANGUAGE OverloadedStrings #-}
12+ {-# LANGUAGE QuasiQuotes #-}
1213{-# LANGUAGE TemplateHaskell #-}
1314
1415module Clash.Core.Util where
@@ -23,6 +24,7 @@ import Data.List
2324import Data.List.Extra (nubOrd )
2425import Data.Maybe
2526 (fromJust , isJust , mapMaybe , catMaybes )
27+ import qualified Data.String.Interpolate as I
2628import qualified Data.Text as T
2729import Data.Text.Prettyprint.Doc (line )
2830#if !MIN_VERSION_base(4,11,0)
@@ -39,7 +41,7 @@ import Clash.Core.Name
3941import Clash.Core.Pretty (ppr , showPpr )
4042import Clash.Core.Subst
4143 (extendTvSubst , mkSubst , mkTvSubst , substTy , substTyWith ,
42- substTyInVar , extendTvSubstList )
44+ substTyInVar , extendTvSubstList , aeqType )
4345import Clash.Core.Term
4446 (LetBinding , Pat (.. ), PrimInfo (.. ), Term (.. ), Alt , WorkInfo (.. ),
4547 TickInfo (.. ), collectArgs )
@@ -327,7 +329,8 @@ applyTypeToArgs e m opTy args = go opTy args
327329-- Do not iterate 'piResultTy', because it's inefficient to substitute one
328330-- variable at a time; instead use 'piResultTys'
329331piResultTy
330- :: TyConMap
332+ :: HasCallStack
333+ => TyConMap
331334 -> Type
332335 -> Type
333336 -> Type
@@ -340,15 +343,26 @@ piResultTy m ty arg = case piResultTyMaybe m ty arg of
340343-- Do not iterate 'piResultTyMaybe', because it's inefficient to substitute one
341344-- variable at a time; instead use 'piResultTys'
342345piResultTyMaybe
343- :: TyConMap
346+ :: HasCallStack
347+ => TyConMap
344348 -> Type
345349 -> Type
346350 -> Maybe Type
347351piResultTyMaybe m ty arg
348352 | Just ty' <- coreView1 m ty
349353 = 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
352366 | ForAllTy tv res <- ty
353367 = let emptySubst = mkSubst (mkInScopeSet (tyFVsOfTypes [arg,res]))
354368 in Just (substTy (extendTvSubst emptySubst tv arg) res)
@@ -379,16 +393,27 @@ piResultTyMaybe m ty arg
379393-- For efficiency reasons, when there are no foralls, we simply drop arrows from
380394-- a function type/kind.
381395piResultTys
382- :: TyConMap
396+ :: HasCallStack
397+ => TyConMap
383398 -> Type
384399 -> [Type ]
385400 -> Type
386401piResultTys _ ty [] = ty
387402piResultTys m ty origArgs@ (arg: args)
388403 | Just ty' <- coreView1 m ty
389404 = 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
392417 | ForAllTy tv res <- ty
393418 = go (extendVarEnv tv arg emptyVarEnv) res args
394419 | otherwise
0 commit comments