git.iko.soy/rowtype-yoga/purescript-native
on f761337 help sign in
67 lines 1.9 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Types
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12--
13-----------------------------------------------------------------------------
15{-# LANGUAGE DeriveDataTypeable #-}
17module Language.PureScript.Types where
19import Data.Data
20import Language.PureScript.Names
21import Language.PureScript.Unknown (Unknown(..))
23data Type
24 = TUnknown (Unknown Type)
25 | Number
26 | String
27 | Boolean
28 | Array Type
29 | Object Type
30 | Function [Type] Type
31 | TypeVar String
32 | TypeConstructor (Qualified ProperName)
33 | TypeApp Type Type
34 | SaturatedTypeSynonym (Qualified ProperName) [Type]
35 | ForAll String Type
36 | ConstrainedType [(Qualified ProperName, Type)] Type
37 | Skolem Int
38 | REmpty
39 | RCons String Type Type deriving (Show, Eq, Data, Typeable)
41rowToList :: Type -> ([(String, Type)], Type)
42rowToList (RCons name ty row) = let (tys, rest) = rowToList row
43 in ((name, ty):tys, rest)
44rowToList r = ([], r)
46rowFromList :: ([(String, Type)], Type) -> Type
47rowFromList ([], r) = r
48rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
50isMonoType :: Type -> Bool
51isMonoType (ForAll _ _) = False
52isMonoType ty = isPolyType ty
54isPolyType :: Type -> Bool
55isPolyType (Array ty) = isMonoType ty
56isPolyType (Object ps) = all isPolyType (map snd . fst $ rowToList ps)
57isPolyType (Function args ret) = all isPolyType args && isPolyType ret
58isPolyType (TypeApp t1 t2) = isMonoType t1 && isMonoType t2
59isPolyType (SaturatedTypeSynonym _ args) = all isPolyType args
60isPolyType (ForAll _ ty) = isPolyType ty
61isPolyType _ = True
63mkForAll :: [String] -> Type -> Type
64mkForAll = flip . foldl . flip $ ForAll
66unit :: Type
67unit = Object REmpty