git.iko.soy/rowtype-yoga/purescript-native
on 0888ada help sign in
84 lines 2.8 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.TypeClassDictionaries
4-- Copyright : (c) 2014 Phil Freeman
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.TypeClassDictionaries where
19import Data.Data
21import Language.PureScript.Names
22import Language.PureScript.Types
24-- |
25-- Data representing a type class dictionary which is in scope
26--
27data TypeClassDictionaryInScope
28 = TypeClassDictionaryInScope {
29 -- | The identifier with which the dictionary can be accessed at runtime
30 tcdName :: Qualified Ident
31 -- | How to obtain this instance via superclass relationships
32 , tcdPath :: [(Qualified ProperName, Integer)]
33 -- | The name of the type class to which this type class instance applies
34 , tcdClassName :: Qualified ProperName
35 -- | The types to which this type class instance applies
36 , tcdInstanceTypes :: [Type]
37 -- | Type class dependencies which must be satisfied to construct this dictionary
38 , tcdDependencies :: Maybe [Constraint]
39 -- | The type of this dictionary
40 , tcdType :: TypeClassDictionaryType
41 } deriving (Show, Read, Data, Typeable)
43-- |
44-- The type of a type class dictionary
45--
46data TypeClassDictionaryType
47 -- |
48 -- A regular type class dictionary
49 --
50 = TCDRegular
51 -- |
52 -- A type class dictionary which is an alias for an imported dictionary from another module
53 --
54 | TCDAlias (Qualified Ident) deriving (Show, Read, Eq, Data, Typeable)
56-- |
57-- A simplified representation of expressions which are used to represent type
58-- class dictionaries at runtime, which can be compared for equality
59--
60data DictionaryValue
61 -- |
62 -- A dictionary which is brought into scope by a local constraint
63 --
64 = LocalDictionaryValue (Qualified Ident)
65 -- |
66 -- A dictionary which is brought into scope by an instance declaration
67 --
68 | GlobalDictionaryValue (Qualified Ident)
69 -- |
70 -- A dictionary which depends on other dictionaries
71 --
72 | DependentDictionaryValue (Qualified Ident) [DictionaryValue]
73 -- |
74 -- A subclass dictionary
75 --
76 | SubclassDictionaryValue DictionaryValue (Qualified ProperName) Integer
77 deriving (Show, Read, Ord, Eq)
79-- |
80-- Find the original dictionary which a type class dictionary in scope refers to
81--
82canonicalizeDictionary :: TypeClassDictionaryInScope -> Qualified Ident
83canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDRegular, tcdName = nm }) = nm
84canonicalizeDictionary (TypeClassDictionaryInScope { tcdType = TCDAlias nm }) = nm