{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell #-}
module Clash.Core.Term
( Term (..)
, TmName
, TmOccName
, LetBinding
, Pat (..)
, Alt
)
where
import Control.DeepSeq
import Data.Hashable (Hashable)
import Data.Text (Text)
import GHC.Generics
import Unbound.Generics.LocallyNameless hiding (Name)
import Unbound.Generics.LocallyNameless.Extra ()
import Clash.Core.DataCon (DataCon)
import Clash.Core.Literal (Literal)
import Clash.Core.Name (Name (..), OccName)
import {-# SOURCE #-} Clash.Core.Type (Type)
import Clash.Core.Var (Id, TyVar)
data Term
= Var !Type !TmName
| Data !DataCon
| Literal !Literal
| Prim !Text !Type
| Lam !(Bind Id Term)
| TyLam !(Bind TyVar Term)
| App !Term !Term
| TyApp !Term !Type
| Letrec !(Bind (Rec [LetBinding]) Term)
| Case !Term !Type [Alt]
| Cast !Term !Type !Type
deriving (Show,Generic,NFData,Hashable)
type TmName = Name Term
type TmOccName = OccName Term
type LetBinding = (Id, Embed Term)
data Pat
= DataPat !(Embed DataCon) !(Rebind [TyVar] [Id])
| LitPat !(Embed Literal)
| DefaultPat
deriving (Eq,Show,Generic,NFData,Alpha,Hashable)
type Alt = Bind Pat Term
instance Eq Term where
(==) = aeq
instance Ord Term where
compare = acompare
instance Alpha Term where
aeq' c (Var _ n) (Var _ m) = aeq' c n m
aeq' _ (Prim t1 _) (Prim t2 _) = t1 == t2
aeq' c t1 t2 = gaeq c (from t1) (from t2)
acompare' c (Var _ n) (Var _ m) = acompare' c n m
acompare' _ (Prim t1 _) (Prim t2 _) = compare t1 t2
acompare' c t1 t2 = gacompare c (from t1) (from t2)
instance Subst Type Pat
instance Subst Term Pat
instance Subst Term Term where
isvar (Var _ x) = Just (SubstName (nameOcc x))
isvar _ = Nothing
instance Subst Type Term