11{-# LANGUAGE TemplateHaskell #-}
2- {-# LANGUAGE UndecidableInstances #-}
32
43module Language.PureScript.Backend.IR.Types where
54
@@ -8,7 +7,7 @@ import Data.Deriving (deriveEq1, deriveOrd1)
87import Data.Map qualified as Map
98import Data.MonoidMap (MonoidMap )
109import Data.MonoidMap qualified as MMap
11- import Language.PureScript.Names (ModuleName )
10+ import Language.PureScript.Names (ModuleName , runModuleName )
1211import Quiet (Quiet (.. ))
1312import Prelude hiding (show )
1413
@@ -123,6 +122,10 @@ newtype Name = Name {nameToText ∷ Text}
123122data QName = QName { qnameModuleName ∷ ModuleName , qnameName ∷ Name }
124123 deriving stock (Eq , Ord , Show )
125124
125+ printQName ∷ QName → Text
126+ printQName QName {.. } =
127+ runModuleName qnameModuleName <> " ∷" <> nameToText qnameName
128+
126129newtype TyName = TyName { renderTyName ∷ Text }
127130 deriving newtype (Eq , Ord )
128131 deriving stock (Generic )
@@ -528,7 +531,7 @@ countFreeRefs = fmap getSum . MMap.toMap . countFreeRefs' mempty
528531 go a <> foldMap (go . unAnn . snd ) patches
529532 IfThenElse (unAnn → p) (unAnn → th) (unAnn → el) →
530533 go p <> go th <> go el
531- -- Non-recursives :
534+ -- Terminals :
532535 LiteralInt {} → mempty
533536 LiteralBool {} → mempty
534537 LiteralFloat {} → mempty
@@ -553,18 +556,31 @@ substitute
553556 → Exp
554557 -- ^ The expression to substitute into
555558 → Exp
556- substitute name index replacement expression =
557- case expression of
559+ substitute name idx replacement = substitute' idx
560+ where
561+ substitute' index subExpression = case subExpression of
558562 Ref name' index'
559- | name == name' && index == index' → replacement
563+ | name == name' && index == index' →
564+ {-
565+ trace
566+ ( "Substituting "
567+ <> show name
568+ <> "\n\tfor "
569+ <> show replacement
570+ <> "\n\tin "
571+ <> show expression
572+ )
573+ -}
574+ replacement
560575 | otherwise → ref name' index'
561- Abs argument@ (unAnn → ParamNamed argName) body →
562- Abs argument (body <&> substitute name index' replacement')
563- where
564- index' = if name == Local argName then index + 1 else index
565- replacement' = shift 1 argName 0 replacement
566- Let binds body →
567- Let binds' body'
576+ Abs param body →
577+ Abs param case unAnn param of
578+ ParamUnused → go <$> body
579+ ParamNamed pName → substitute name index' replacement' <$> body
580+ where
581+ index' = if name == Local pName then index + 1 else index
582+ replacement' = shift 1 pName 0 replacement
583+ Let binds body → Let binds' body'
568584 where
569585 binds' =
570586 binds <&> \ grouping →
@@ -598,17 +614,23 @@ substitute name index replacement expression =
598614 LiteralArray as → LiteralArray (go <<$>> as)
599615 LiteralObject props → LiteralObject (fmap go <<$>> props)
600616 ReflectCtor a → ReflectCtor (go <$> a)
601- DataArgumentByIndex idx a → DataArgumentByIndex idx (go <$> a)
617+ DataArgumentByIndex i a → DataArgumentByIndex i (go <$> a)
602618 Eq a b → Eq (go <$> a) (go <$> b)
603619 ArrayLength a → ArrayLength (go <$> a)
604620 ArrayIndex a indx → ArrayIndex (go <$> a) indx
605621 ObjectProp a prop → ObjectProp (go <$> a) prop
606622 ObjectUpdate a patches → ObjectUpdate (go <$> a) (fmap go <<$>> patches)
607623 IfThenElse p th el → IfThenElse (go <$> p) (go <$> th) (go <$> el)
608- _ → expression
609- where
610- go ∷ Exp → Exp
611- go = substitute name index replacement
624+ -- Terminals:
625+ LiteralInt {} → subExpression
626+ LiteralBool {} → subExpression
627+ LiteralFloat {} → subExpression
628+ LiteralString {} → subExpression
629+ LiteralChar {} → subExpression
630+ Ctor {} → subExpression
631+ Exception {} → subExpression
632+ where
633+ go ∷ Exp → Exp = substitute' index
612634
613635-- | Increase the index of all bound variables matching the given variable name
614636shift
0 commit comments