Plan 9 from Bell Labs’s /usr/web/sources/contrib/fernan/nhc98/src/greencard/Type.lhs

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


%
% Copyright (C) 1997 Thomas Nordin and Alastair Reid
%

\begin{code}

module Type
    ( Type(..), typeApply
    , ppType
    , typeArgs, typeResult
    , isPureType
    ) where

import Name

import Pretty
import PrettyUtils( ppTuple, ppParen )

\end{code}

\begin{code}

data Type       
  = Arrow Type Type
  | TypeList  Type
  | TypeTuple [Type]
  | TypeApply Type [Type]  -- non-empty arglist
  | TypeVar Name 
  deriving ( Show )

typeApply :: Type -> [Type] -> Type
typeApply f [] = f
typeApply f args = TypeApply f args

\end{code}

%************************************************************************
%*                                                                      *
\subsection{Pretty-printing types}
%*                                                                      *
%************************************************************************

\begin{code}

ppType :: Type -> Doc

ppType = ppType' False

ppType' :: Bool -> Type -> Doc

ppType' d (Arrow a b)	   = ppParen d (ppType' True a <+> text "->" <+> ppType' False b)
ppType' d (TypeList t)     = brackets (ppType' False t)
ppType' d (TypeTuple ts)   = ppTuple (map (ppType' False) ts)
ppType' d (TypeApply f ts) = ppParen d (hsep $ map (ppType' True) (f:ts))
ppType' d (TypeVar s)	   = text s

\end{code}

%************************************************************************
%*                                                                      *
\subsection{Separating the arguments from the result in a type}
%*                                                                      *
%************************************************************************

\begin{code}

typeResult :: Type -> Type
typeResult (Arrow t1 t2) = typeResult t2
typeResult x             = x

typeArgs :: Type -> [Type]
typeArgs (Arrow t1 t2) = t1 : typeArgs t2
typeArgs x             = []

isPureType :: Type -> Bool
isPureType t = case typeResult t of 
               TypeApply (TypeVar "IO") _ -> False
               _ -> True

\end{code}


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].