@@ -66,50 +66,62 @@ findTyconInPrefix nm = fip []
66
66
apply :: Spine -> Spine -> Spine
67
67
apply ! a ! l = rebuildSpine a [l]
68
68
69
- rebuildSpine :: Spine -> [Spine ] -> Spine
70
- rebuildSpine s [] = s
71
- rebuildSpine (Spine " #imp_abs#" [_, Abs nm ty rst]) apps = case findTyconInPrefix nm apps of
72
- Just (v, apps) -> rebuildSpine (Abs nm ty rst) (v: apps)
73
- Nothing -> seq sp $ if ty == atom && S. notMember nm (freeVariables rs) then rs else irs
74
- -- proof irrelevance hack
75
- -- we know we can prove that type "prop" is inhabited
76
- -- irs - the proof doesn't matter
77
- -- rs - the proof matters
78
- -- irs - here, the proof might matter, but we don't know if we can prove the thing,
79
- -- so we need to try
80
- where nm' = newNameFor nm $ freeVariables apps
81
- sp = subst (nm |-> var nm') rst
82
- rs = rebuildSpine sp apps
83
- irs = infer nm ty rs
84
- rebuildSpine (Spine c apps) apps' = Spine c $ apps ++ apps'
85
- rebuildSpine (Abs nm _ rst) (a: apps') = let sp = subst (nm |-> a) $ rst
86
- in seq sp $ rebuildSpine sp apps'
87
69
88
70
newNameFor :: Name -> S. Set Name -> Name
89
71
newNameFor nm fv = nm'
90
72
where nm' = fromJust $ find free $ nm: map (\ s -> show s ++ " /?" ) [0 .. ]
91
73
free k = not $ S. member k fv
92
74
93
75
newName :: Name -> Map Name Spine -> S. Set Name -> (Name , Map Name Spine , S. Set Name )
94
- newName " " so fo = (" " ,so,fo)
95
- newName nm so fo = (nm',s',f')
96
- where s = M. delete nm so
76
+ newName " " so fo = (" " ,so, S. delete " " fo)
77
+ newName nm so fo' = (nm',s',f')
78
+ where fo = S. delete nm fo'
79
+ s = M. delete nm so
97
80
-- could reduce the size of the free variable set here, but for efficiency it is not really necessary
98
81
-- for beautification of output it is
99
- (s',f') = if nm == nm' then (s,fo) else (M. insert nm (var nm') s , S. insert nm' fo)
82
+ (s',f') = if nm == nm' then (s,fo) else (M. insert nm (var nm') s , fo)
100
83
nm' = fromJust $ find free $ nm: map (\ s -> show s ++ " /" ) [0 .. ]
101
84
fv = mappend (M. keysSet s) (freeVariables s)
102
85
free k = not $ S. member k fv
103
86
104
- class Subst a where
105
- substFree :: Substitution -> S. Set Name -> a -> a
87
+
88
+ freeWithout sp [] = freeVariables sp
89
+ freeWithout (Abs nm tp rst) (a: lst) = S. delete nm $ freeWithout rst lst
90
+ freeWithout (Spine " #imp_abs#" [_, Abs nm tp rst]) apps = case findTyconInPrefix nm apps of
91
+ Just (v,apps) -> S. delete nm $ freeWithout rst apps
92
+ Nothing -> S. delete nm $ freeWithout rst apps
93
+ freeWithout l apps = freeVariables l
94
+
106
95
107
- subst :: Subst a => Substitution -> a -> a
108
- subst s = substFree s $ freeVariables s
96
+ subst :: ( Show a , Subst a ) => Substitution -> a -> a
97
+ subst s a = substFree s mempty a
109
98
99
+ class Subst a where
100
+ substFree :: Substitution -> S. Set Name -> a -> a
101
+
110
102
class Alpha a where
111
103
alphaConvert :: S. Set Name -> Map Name Name -> a -> a
112
104
rebuildFromMem :: Map Name Name -> a -> a
105
+
106
+
107
+ rebuildSpine :: Spine -> [Spine ] -> Spine
108
+ rebuildSpine s [] = s
109
+ rebuildSpine (Spine " #imp_abs#" [_, Abs nm ty rst]) apps = case findTyconInPrefix nm apps of
110
+ Just (v, apps) -> rebuildSpine (Abs nm ty rst) (v: apps)
111
+ Nothing -> seq sp $ if ty == atom && S. notMember nm (freeVariables rs) then rs else irs
112
+ -- proof irrelevance hack
113
+ -- we know we can prove that type "prop" is inhabited
114
+ -- irs - the proof doesn't matter
115
+ -- rs - the proof matters
116
+ -- irs - here, the proof might matter, but we don't know if we can prove the thing,
117
+ -- so we need to try
118
+ where nm' = newNameFor nm $ freeVariables apps
119
+ sp = substFree (nm |-> var nm') mempty rst
120
+ rs = rebuildSpine sp apps
121
+ irs = infer nm ty rs
122
+ rebuildSpine (Spine c apps) apps' = Spine c $ apps ++ apps'
123
+ rebuildSpine (Abs nm _ rst) (a: apps') = let sp = substFree (nm |-> a) mempty rst
124
+ in seq sp $ rebuildSpine sp apps'
113
125
114
126
instance Subst a => Subst [a ] where
115
127
substFree s f t = substFree s f <$> t
@@ -120,24 +132,24 @@ instance Alpha a => Alpha [a] where
120
132
121
133
instance (Subst a , Subst b ) => Subst (a ,b ) where
122
134
substFree s f ~ (a,b) = (substFree s f a , substFree s f b)
123
-
135
+
124
136
instance Subst Spine where
125
- substFree s f sp@ (Spine " #imp_forall#" [_, Abs nm tp rst]) = case " " /= nm && S. member nm f && not (S. null $ S. intersection (M. keysSet s) $ freeVariables sp) of
126
- False -> imp_forall nm (substFree s f tp) $ substFree (M. delete nm s) f rst
127
- True -> error $
128
- " can not capture free variables because implicits quantifiers can not alpha convert: " ++ show sp
129
- ++ " \n\t for: " ++ show s
130
- substFree s f sp@ (Spine " #imp_abs#" [_, Abs nm tp rst]) = case " " /= nm && S. member nm f && not (S. null $ S. intersection (M. keysSet s) $ freeVariables sp) of
131
- False -> imp_abs nm (substFree s f tp) $ substFree (M. delete nm s) f rst
132
- True -> error $
133
- " can not capture free variables because implicit binds can not alpha convert: " ++ show sp
134
- ++ " \n\t for: " ++ show s
137
+ substFree s f sp@ (Spine " #imp_forall#" [_, Abs nm tp rst]) =
138
+ imp_forall nm (substFree s f tp) $ substFree (M. delete nm s) (S. insert nm f) rst
139
+
140
+ substFree s f sp@ (Spine " #imp_abs#" [_, Abs nm tp rst]) =
141
+ imp_abs nm (substFree s f tp) $ substFree (M. delete nm s) (S. insert nm f) rst
135
142
substFree s f (Abs nm tp rst) = Abs nm' (substFree s f tp) $ substFree s' f' rst
136
143
where (nm',s',f') = newName nm s f
137
144
substFree s f (Spine " #tycon#" [Spine c [v]]) = Spine " #tycon#" [Spine c [substFree s f v]]
138
- substFree s f (Spine nm apps) = let apps' = substFree s f <$> apps in
145
+ substFree s f sp @ (Spine nm apps) = let apps' = substFree s f <$> apps in
139
146
case s ! nm of
140
- Just nm -> rebuildSpine nm apps'
147
+ Just new -> case S. null $ S. intersection f (freeWithout new apps') of
148
+ True -> rebuildSpine new apps'
149
+ False -> error $
150
+ " can not capture free variables because implicits quantifiers can not alpha convert: " ++ show sp
151
+ ++ " \n\t for: " ++ show s
152
+ ++ " \n\t bound by: " ++ show f
141
153
_ -> Spine nm apps'
142
154
143
155
instance Alpha Spine where
0 commit comments