git.iko.soy/rowtype-yoga/purescript-native
on 4f77064 help sign in
160 lines 3.8 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-- Data types for types
13--
14-----------------------------------------------------------------------------
16{-# LANGUAGE DeriveDataTypeable #-}
18module Language.PureScript.Types where
20import Data.Data
21import Data.Generics (mkT, mkQ, everywhereBut)
23import Control.Monad.Unify
25import Language.PureScript.Names
27-- |
28-- An identifier for the scope of a skolem variable
29--
30newtype SkolemScope = SkolemScope { runSkolemScope :: Int } deriving (Show, Eq, Ord, Data, Typeable)
32-- |
33-- The type of types
34--
35data Type
36 -- |
37 -- A unification variable of type Type
38 --
39 = TUnknown Unknown
40 -- |
41 -- Javascript numbers
42 --
43 | Object Type
44 -- |
45 -- A named type variable
46 --
47 | TypeVar String
48 -- |
49 -- A type constructor
50 --
51 | TypeConstructor (Qualified ProperName)
52 -- |
53 -- A type application
54 --
55 | TypeApp Type Type
56 -- |
57 -- A type synonym which is \"saturated\", i.e. fully applied
58 --
59 | SaturatedTypeSynonym (Qualified ProperName) [Type]
60 -- |
61 -- Forall quantifier
62 --
63 | ForAll String Type (Maybe SkolemScope)
64 -- |
65 -- A type with a set of type class constraints
66 --
67 | ConstrainedType [(Qualified ProperName, Type)] Type
68 -- |
69 -- A skolem constant
70 --
71 | Skolem Int SkolemScope
72 -- |
73 -- An empty row
74 --
75 | REmpty
76 -- |
77 -- A non-empty row
78 --
79 | RCons String Type Type deriving (Show, Eq, Data, Typeable)
81-- |
82-- Type constructor for functions
83--
84tyFunction :: Type
85tyFunction = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prelude") (ProperName "Function")
87-- |
88-- Type constructor for strings
89--
90tyString :: Type
91tyString = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prelude") (ProperName "String")
93-- |
94-- Type constructor for numbers
95--
96tyNumber :: Type
97tyNumber = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prelude") (ProperName "Number")
99-- |
100-- Type constructor for booleans
101--
102tyBoolean :: Type
103tyBoolean = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prelude") (ProperName "Boolean")
105-- |
106-- Type constructor for arrays
107--
108tyArray :: Type
109tyArray = TypeConstructor $ (Qualified $ Just $ ModuleName $ ProperName "Prelude") (ProperName "Array")
111-- |
112-- Smart constructor for function types
113--
114function :: Type -> Type -> Type
115function t1 t2 = TypeApp (TypeApp tyFunction t1) t2
117-- |
118-- Convert a row to a list of pairs of labels and types
119--
120rowToList :: Type -> ([(String, Type)], Type)
121rowToList (RCons name ty row) = let (tys, rest) = rowToList row
122 in ((name, ty):tys, rest)
123rowToList r = ([], r)
125-- |
126-- Convert a list of labels and types to a row
127--
128rowFromList :: ([(String, Type)], Type) -> Type
129rowFromList ([], r) = r
130rowFromList ((name, t):ts, r) = RCons name t (rowFromList (ts, r))
132-- |
133-- Check whether a type is a monotype
134--
135isMonoType :: Type -> Bool
136isMonoType (ForAll _ _ _) = False
137isMonoType ty = True
139-- |
140-- Universally quantify a type
141--
142mkForAll :: [String] -> Type -> Type
143mkForAll args ty = foldl (\t arg -> ForAll arg t Nothing) ty args
145-- |
146-- The empty record type
147--
148unit :: Type
149unit = Object REmpty
151-- |
152-- Replace a type variable, taking into account variable shadowing
153--
154replaceTypeVars :: (Data d) => String -> Type -> d -> d
155replaceTypeVars name t = everywhereBut (mkQ False isShadowed) (mkT replaceTypeVar)
156 where
157 replaceTypeVar (TypeVar v) | v == name = t
158 replaceTypeVar other = other
159 isShadowed (ForAll v _ _) | v == name = True
160 isShadowed _ = False