Repository navigation
Expand file tree
/
Copy pathTypes.hs
More file actions
139 lines (112 loc) · 4.32 KB
/
Copy pathTypes.hs
File metadata and controls
139 lines (112 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
module Agda.TypeChecking.DiscrimTree.Types where
import Control.DeepSeq
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import Data.Map.Strict (Map)
import Data.Set (Set)
import GHC.Generics (Generic)
import Agda.Syntax.Internal
import Agda.Syntax.Position
import Agda.Utils.Impossible
import Agda.Utils.Null
data Key
= RigidK {-# UNPACK #-} !QName {-# UNPACK #-} !Int
-- ^ Rigid symbols (constructors, data types, record types,
-- postulates) identified by a QName.
| LocalK {-# UNPACK #-} !Int {-# UNPACK #-} !Int
-- ^ Local variables.
| PiK
-- ^ Dependent function types. The domain will be represented
-- accurately, for the case of a genuine dependent function type,
-- the codomain will be a dummy.
| ConstK
-- ^ Constant lambdas.
| SortK
-- ^ Universes.
| FlexK
-- ^ Anything else.
deriving (Show, Eq, Ord, Generic)
instance NFData Key
-- | A 'Term'-indexed associative data structure supporting
-- /approximate/ (conservative) lookup. Rather than using a @Trie@ keyed
-- by 'Key' directly, a 'DiscrimTree' is instead represented more like a
-- /case/ tree.
--
-- This allows us to exploit the fact that instance selection often
-- focuses on a small part of the term: Only that critical chain is
-- represented in the tree. As an example, level parameters are unlikely
-- to contribute to narrowing a search problem, so it would be wasteful
-- to have an indirection in the tree for every 'FlexK' standing for a
-- level parameter.
data DiscrimTree a
= EmptyDT
-- ^ The empty discrimination tree.
| DoneDT (Set a)
-- ^ Succeed with a given set of values.
| CaseDT
-- ^ Do case analysis on a term. 'CaseDT' is scoped in the same way
-- as fast case trees for the abstract machine: When matching
-- actually succeeds, the variable that was matched gets replaced by
-- its arguments directly in the context.
{-# UNPACK #-} !Int -- ^ The variable to case on.
(Map Key (DiscrimTree a)) -- ^ The proper branches.
(DiscrimTree a) -- ^ A further tree, which should always be explored.
deriving (Generic, Eq, Show)
{-
The extra continuation to CaseDT is used to represent instance tables
which have non-trivial overlap, e.g.
instance
a : Foo X ?
b : Foo ? X
If we commited to the {a} branch of the discrimination tree, then we
would miss {b} entirely. Note that an "obvious" overlap like
instance
a : Bar X
b : Bar X
would be represented as
case 0 of
Bar → case 0 of
X → done {a, b}
and the extra continuation would be empty.
-}
instance NFData a => NFData (DiscrimTree a)
instance (KillRange a, Ord a) => KillRange (DiscrimTree a) where
killRange = \case
EmptyDT -> EmptyDT
DoneDT s -> killRangeN DoneDT s
CaseDT i k o -> killRangeN CaseDT i k o
instance Null (DiscrimTree a) where
empty = EmptyDT
null = \case
EmptyDT -> True
_ -> False
-- | Merge a pair of discrimination trees. This function tries to build
-- the minimal discrimination tree that yields the union of the inputs'
-- results, though it does so slightly naïvely, without considerable
-- optimisations (e.g. it does not turn single-alternative 'CaseDT's
-- into 'DoneDT's).
mergeDT :: Ord a => DiscrimTree a -> DiscrimTree a -> DiscrimTree a
mergeDT EmptyDT x = x
mergeDT (DoneDT s) x = case x of
EmptyDT -> DoneDT s
DoneDT s' -> DoneDT (s <> s')
CaseDT i bs x -> CaseDT i bs (mergeDT (DoneDT s) x)
mergeDT (CaseDT i bs els) x = case x of
EmptyDT -> CaseDT i bs els
DoneDT s -> CaseDT i bs (mergeDT (DoneDT s) els)
CaseDT j bs' els' -> case compare i j of
EQ -> CaseDT j (Map.unionWith mergeDT bs bs') (mergeDT els els')
LT -> CaseDT i bs (mergeDT els (CaseDT j bs' els'))
GT -> CaseDT j bs' (mergeDT els' (CaseDT i bs els))
instance Ord a => Semigroup (DiscrimTree a) where
(<>) = mergeDT
instance Ord a => Monoid (DiscrimTree a) where
mempty = EmptyDT
-- | Construct the case tree corresponding to only performing proper
-- matches on the given key. In this context, a "proper match" is any
-- 'Key' that is not 'FlexK'.
singletonDT :: [Key] -> a -> DiscrimTree a
singletonDT key val = go 0 key where
go focus [] = DoneDT $ Set.singleton val
go focus (FlexK:ts) = go (focus + 1) ts
go focus (t:ts) = CaseDT focus (Map.singleton t (go focus ts)) EmptyDT