0 | module Language.Reflection.Expr
2 | import public Control.Applicative.Const
4 | import public Data.Bits
5 | import public Data.Cozippable
6 | import public Data.Fin.Set
7 | import public Data.Fin.ToFin
8 | import public Data.List.Ex
9 | import public Data.List.Quantifiers
10 | import public Data.SortedSet
11 | import public Data.These
12 | import public Data.Vect.Dependent
14 | import public Language.Reflection
15 | import public Language.Reflection.Syntax
16 | import Language.Reflection.Syntax.Ops
18 | import public Syntax.IHateParens.List
27 | data IsNamedArg : Arg -> Type where
28 | ItIsNamed : IsNamedArg $
MkArg cnt pii (Just n) ty
31 | isNamedArg : (arg : Arg) -> Dec $
IsNamedArg arg
32 | isNamedArg (MkArg count piInfo (Just x) type) = Yes ItIsNamed
33 | isNamedArg (MkArg count piInfo Nothing type) = No $
\case ItIsNamed
impossible
40 | stname : Maybe Name -> Name
41 | stname = fromMaybe $
UN Underscore
44 | argName : (a : Arg) -> (0 _ : IsNamedArg a) => Name
45 | argName (MkArg _ _ Nothing _) impossible
46 | argName (MkArg _ _ (Just x) _) = x
49 | argName' : Arg -> Name
50 | argName' = stname . (.name)
53 | cleanupNamedHoles : TTImp -> TTImp
54 | cleanupNamedHoles = mapTTImp substHoles . mapTTImp cleanApps where
56 | cleanApps : TTImp -> TTImp
57 | cleanApps $
IAutoApp _ e $
IHole {} = e
58 | cleanApps $
INamedApp _ e _ $
IHole {} = e
61 | substHoles : TTImp -> TTImp
62 | substHoles $
IHole {} = implicitFalse
67 | cleanupArg : Arg -> Arg
68 | cleanupArg = { type $= cleanupNamedHoles, piInfo $= map cleanupNamedHoles }
71 | argNames : (l : List Arg) -> (0 _ : All IsNamedArg l) => List Name
73 | argNames (x :: xs) @{_ :: _} = Expr.argName x :: argNames xs
80 | normaliseAs' : Elaboration m =>
81 | (0 expected : Type) ->
82 | (preProcess : TTImp -> TTImp) ->
83 | {0 resulting : _} -> (0 postProcess : (x : expected) -> resulting x) ->
85 | normaliseAs' expected pre post expr = do
86 | let expr = cleanupNamedHoles expr
87 | expr' <- quote $
post !(check {expected} $
pre expr)
88 | let (args, _) = unPi expr
89 | let (args', ty) = unPi expr'
90 | let args'' = comergeWith (\pre => {name := pre.name}) args args'
91 | pure $
piAll ty args''
93 | public export %inline
94 | normaliseAs : Elaboration m => (0 expected : Type) -> TTImp -> m TTImp
95 | normaliseAs ty = normaliseAs' ty id id
98 | public export %inline
99 | normalise : Elaboration m => TTImp -> m TTImp
100 | normalise = normaliseAs' (
ty ** ty)
(\expr => `((
_ **
~expr)
)) snd
103 | public export %inline
104 | normaliseAsType : Elaboration m => TTImp -> m TTImp
105 | normaliseAsType = normaliseAs Type
114 | | NamedApp Name TTImp
119 | appArg : Arg -> TTImp -> AnyApp
120 | appArg (MkArg {piInfo=ExplicitArg, _}) expr = PosApp expr
121 | appArg (MkArg {piInfo=ImplicitArg, name, _}) expr = NamedApp (stname name) expr
122 | appArg (MkArg {piInfo=DefImplicit _, name, _}) expr = NamedApp (stname name) expr
123 | appArg (MkArg {piInfo=AutoImplicit, _}) expr = AutoApp expr
126 | getExpr : AnyApp -> TTImp
127 | getExpr $
PosApp e = e
128 | getExpr $
NamedApp _ e = e
129 | getExpr $
AutoApp e = e
130 | getExpr $
WithApp e = e
134 | mapExpr : (TTImp -> TTImp) -> AnyApp -> AnyApp
135 | mapExpr f $
PosApp e = PosApp $
f e
136 | mapExpr f $
NamedApp n e = NamedApp n $
f e
137 | mapExpr f $
AutoApp e = AutoApp $
f e
138 | mapExpr f $
WithApp e = WithApp $
f e
141 | unAppAny : TTImp -> (TTImp, List AnyApp)
142 | unAppAny = runTR [] where
143 | runTR : List AnyApp -> TTImp -> (TTImp, List AnyApp)
144 | runTR curr $
IApp _ lhs rhs = runTR (PosApp rhs :: curr) lhs
145 | runTR curr $
INamedApp _ lhs n rhs = runTR (NamedApp n rhs :: curr) lhs
146 | runTR curr $
IAutoApp _ lhs rhs = runTR (AutoApp rhs :: curr) lhs
147 | runTR curr $
IWithApp _ lhs rhs = runTR (WithApp rhs :: curr) lhs
148 | runTR curr lhs = (lhs, curr)
151 | reAppAny1 : TTImp -> AnyApp -> TTImp
152 | reAppAny1 l $
PosApp e = app l e
153 | reAppAny1 l $
NamedApp n e = namedApp l n e
154 | reAppAny1 l $
AutoApp e = autoApp l e
155 | reAppAny1 l $
WithApp e = IWithApp EmptyFC l e
157 | public export %inline
158 | reAppAny : Foldable f => TTImp -> f AnyApp -> TTImp
159 | reAppAny = foldl reAppAny1
169 | record AllApps where
170 | constructor MkAllApps
171 | explicitArgs : List TTImp
172 | autoArgs : List TTImp
173 | namedArgs : SortedMap Name TTImp
177 | addApp : AnyApp -> AllApps -> AllApps
178 | addApp (PosApp s) = {explicitArgs $= (s ::)}
179 | addApp (NamedApp nm s) = {namedArgs $= insert nm s}
180 | addApp (AutoApp s) = {autoArgs $= (s ::)}
181 | addApp (WithApp s) = id
187 | mkAllApps : List AnyApp -> AllApps
188 | mkAllApps laa = foldl (flip addApp) (MkAllApps [] [] empty) $
reverse laa
194 | popNamed : Maybe Name -> AllApps -> Maybe (TTImp, AllApps)
195 | popNamed Nothing ap = Nothing
196 | popNamed (Just x) ap =
197 | case lookup x ap.namedArgs of
199 | Just t => Just (t, {namedArgs $= delete x} ap)
205 | popArgVal : Arg -> AllApps -> Maybe (TTImp, AllApps)
206 | popArgVal (MkArg _ ImplicitArg name _) ap = popNamed name ap
207 | popArgVal (MkArg _ ExplicitArg name _) (MkAllApps (x :: xs) autoArgs namedArgs) = Just (x, MkAllApps xs autoArgs namedArgs)
208 | popArgVal (MkArg _ ExplicitArg name _) ap = popNamed name ap
209 | popArgVal (MkArg _ AutoImplicit name _) (MkAllApps explicitArgs (x :: xs) namedArgs) = Just (x, MkAllApps explicitArgs xs namedArgs)
210 | popArgVal (MkArg _ AutoImplicit name _) ap = popNamed name ap
211 | popArgVal (MkArg _ (DefImplicit x) Nothing _) ap = Just (x, ap)
212 | popArgVal (MkArg _ (DefImplicit x) (Just n) _) ap =
213 | case lookup n ap.namedArgs of
214 | Nothing => Just (x, ap)
215 | Just t => Just (t , {namedArgs $= delete n} ap)
219 | popArgVals : List Arg -> AllApps -> List (Maybe TTImp)
220 | popArgVals [] aa = []
221 | popArgVals (x :: xs) aa = do
222 | let pav = popArgVal x aa
223 | let mr = fst <$> pav
224 | let aa = fromMaybe aa $
snd <$> pav
225 | mr :: popArgVals xs aa
233 | liftList : Foldable f => f TTImp -> TTImp
234 | liftList = foldr (\l, r => `(~l ::
~r)) `([]
)
238 | liftList' : Foldable f => f TTImp -> TTImp
239 | liftList' = foldr (\l, r => `(Prelude.(::)
~l ~r)) `(Prelude.Nil
)
245 | applySyn : TTImp -> TTImp -> TTImp
246 | applySyn (ILam _ _ _ Nothing _ lamExpr) _ = lamExpr
247 | applySyn (ILam _ _ _ (Just n) _ lamExpr) rhs = mapTTImp (\case orig@(IVar _ n') => if n == n' then rhs else orig;
e => e) lamExpr
248 | applySyn lhs rhs = lhs `app` rhs
253 | unDPair : TTImp -> (List Arg, TTImp)
254 | unDPair (IApp _ (IApp _ (IVar _ `{Builtin.DPair.DPair
}) typ) (ILam _ cnt piInfo mbname _ lamTy)) =
255 | mapFst (MkArg cnt piInfo mbname typ ::) $
unDPair lamTy
256 | unDPair expr = ([], expr)
259 | unDPairUnAlt : TTImp -> Maybe (List Arg, TTImp)
260 | unDPairUnAlt (IAlternative _ _ alts) = case filter (not . null . Builtin.fst) $
unDPair <$> alts of
263 | unDPairUnAlt x = Just $
unDPair x
266 | buildDPair : (rhs : TTImp) -> List (Name, TTImp) -> TTImp
267 | buildDPair = foldr $
\(name, type), res =>
268 | var `{Builtin.DPair.DPair
} .$ type .$ lam (MkArg MW ExplicitArg (Just name) type) res
278 | unNS : Name -> (List String, Name)
279 | unNS (NS (MkNS revNSs) nm) = mapFst (reverse revNSs ++) $
unNS nm
280 | unNS (DN _ nm) = unNS nm
288 | allNameSuffixes : Name -> List Name
289 | allNameSuffixes nm = do
290 | let (nss, n) = unNS nm
291 | tails nss <&> \case
293 | ns => NS (MkNS $
reverse ns) n
296 | isNamespaced : Name -> Bool
297 | isNamespaced = not . null . fst . unNS
300 | isImplicit : PiInfo c -> Bool
301 | isImplicit ImplicitArg = True
302 | isImplicit (DefImplicit x) = True
303 | isImplicit AutoImplicit = True
304 | isImplicit ExplicitArg = False
311 | isSameTypeAs : Name -> Name -> Elab Bool
312 | isSameTypeAs n m = let eq = (==) `on` fst in [| lookupName n `eq` lookupName m |]
315 | nameConformsTo : (cand, origin : Name) -> Bool
316 | nameConformsTo cand origin = do
317 | let (cns, cn) = simplify cand
318 | let (ons, on) = simplify origin
319 | cn == on && (cns `isPrefixOf` ons)
321 | simplify : Name -> (List String, Name)
322 | simplify (NS (MkNS ns) nm) = mapFst (++ ns) $
simplify nm
323 | simplify (DN _ nm) = simplify nm
324 | simplify x = ([], x)
326 | 0 nct_corr_eq : nameConformsTo `{A.B.c
} `{A.B.c
} = True;
nct_corr_eq = Refl
327 | 0 nct_corr_le : nameConformsTo `{B.c
} `{A.B.c
} = True;
nct_corr_le = Refl
328 | 0 nct_corr_ge : nameConformsTo `{A.B.c
} `{B.c
} = False;
nct_corr_ge = Refl
332 | allVarNames' : TTImp -> SortedSet Name
333 | allVarNames' = runConst . mapATTImp' f where
334 | f : TTImp -> Const (SortedSet Name) TTImp -> Const (SortedSet Name) TTImp
335 | f (IVar _ n) = const $
MkConst $
singleton n
340 | allVarNames : TTImp -> List Name
341 | allVarNames = Prelude.toList . allVarNames'
344 | isVar : TTImp -> Bool
345 | isVar $
IVar {} = True
349 | 0 ArgDeps : Nat -> Type
350 | ArgDeps n = DVect n $
FinSet . Fin.finToNat
353 | argDeps : (args : List Arg) -> ArgDeps args.length
355 | let nameToIndices = SortedMap.fromList $
mapI args $
\i, arg => (argName' arg, Fin.Set.singleton i)
356 | let args = Vect.fromList args <&> \arg => allVarNames arg.type <&> fromMaybe empty . lookup' nameToIndices
357 | flip upmapI args $
\i, deps => flip concatMap deps $
\candidates =>
358 | maybe empty singleton $
last' $
mapMaybe tryToFit $
Fin.Set.toList candidates
361 | dependees : (args : List Arg) -> FinSet args.length
362 | dependees args = do
363 | let nameToIndex = SortedMap.fromList $
mapI args $
\i, arg => (argName' arg, i)
364 | let varsInTypes = concatMap (\arg => allVarNames' arg.type) args
365 | fromList $
mapMaybe (lookup' nameToIndex) $
Prelude.toList varsInTypes
367 | namespace UpToRenaming
371 | compWithSubst : (subst : List $
These Name Name) => (from, to : Maybe Name) -> TTImp -> TTImp -> Bool
372 | compWithSubst (Just n) (Just n') e e' = n == n' && (e == e') @{UpToSubst} || (e == e') @{UpToSubst @{Both n n' :: subst}}
373 | compWithSubst (Just n) Nothing e e' = (e == e') @{UpToSubst @{This n :: subst}}
374 | compWithSubst Nothing (Just n') e e' = (e == e') @{UpToSubst @{That n' :: subst}}
375 | compWithSubst Nothing Nothing e e' = (e == e') @{UpToSubst}
377 | [UpToSubst] (subst : List $
These Name Name) => Eq TTImp where
378 | IVar _ v == IVar _ v' = maybe (v == v') (== Both v v') $
flip find subst $
\ior => fromThis ior == Just v || fromThat ior == Just v'
379 | IPi _ c i n a r == IPi _ c' i' n' a' r' =
380 | c == c' && (assert_total $
i == i') && a == a' && (assert_total $
compWithSubst n n' r r')
381 | ILam _ c i n a r == ILam _ c' i' n' a' r' =
382 | c == c' && (assert_total $
i == i') && a == a' && (assert_total $
compWithSubst n n' r r')
383 | ILet _ _ c n ty val s == ILet _ _ c' n' ty' val' s' =
384 | c == c' && ty == ty' && val == val' && (assert_total $
compWithSubst (Just n) (Just n') s s')
386 | ICase _ os t ty cs == ICase _ os' t' ty' cs' =
387 | t == t' && (assert_total $
os == os') && ty == ty' && (assert_total $
cs == cs')
388 | ILocal _ ds e == ILocal _ ds' e' =
389 | (assert_total $
ds == ds') && e == e'
390 | IUpdate _ fs t == IUpdate _ fs' t' =
391 | (assert_total $
fs == fs') && t == t'
393 | IApp _ f x == IApp _ f' x' = f == f' && x == x'
394 | INamedApp _ f n x == INamedApp _ f' n' x' =
395 | f == f' && n == n' && x == x'
396 | IAutoApp _ f x == IAutoApp _ f' x' = f == f' && x == x'
397 | IWithApp _ f x == IWithApp _ f' x' = f == f' && x == x'
399 | ISearch _ n == ISearch _ n' = n == n'
400 | IAlternative _ t as == IAlternative _ t' as' =
401 | (assert_total $
t == t') && (assert_total $
as == as')
402 | IRewrite _ p q == IRewrite _ p' q' =
405 | IBindHere _ m t == IBindHere _ m' t' =
407 | IBindVar _ s == IBindVar _ s' = s == s'
408 | IAs _ _ u n t == IAs _ _ u' n' t' =
409 | u == u' && n == n' && t == t'
410 | IMustUnify _ r t == IMustUnify _ r' t' =
413 | IDelayed _ r t == IDelayed _ r' t' = r == r' && t == t'
414 | IDelay _ t == IDelay _ t' = t == t'
415 | IForce _ t == IForce _ t' = t == t'
417 | IQuote _ tm == IQuote _ tm' = tm == tm'
418 | IQuoteName _ n == IQuoteName _ n' = n == n'
419 | IQuoteDecl _ ds == IQuoteDecl _ ds' = assert_total $
ds == ds'
420 | IUnquote _ tm == IUnquote _ tm' = tm == tm'
422 | IPrimVal _ c == IPrimVal _ c' = c == c'
423 | IType _ == IType _ = True
424 | IHole _ s == IHole _ s' = True
426 | Implicit _ b == Implicit _ b' = b == b'
427 | IWithUnambigNames _ ns t == IWithUnambigNames _ ns' t' =
428 | map snd ns == map snd ns' && t == t'
433 | [UpToRenaming] Eq TTImp where
434 | x == y = (x == y) @{UpToSubst @{empty}}