{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Monad.Free.Foil.Internal where
import Control.DeepSeq
import qualified Control.Monad.Foil.Internal as Foil
import Data.Bifoldable
import Data.Bitraversable
import Data.Bifunctor
import Data.ZipMatchK
import qualified Generics.Kind as Kind
import Generics.Kind (GenericK(..), Field, Exists, Var0, Var1, (:$:), Atom((:@:), Kon), (:+:), (:*:))
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IntMap
import Data.Map (Map)
import qualified Data.Map as Map
import GHC.Generics (Generic)
import Unsafe.Coerce (unsafeCoerce)
data ScopedAST binder sig n where
ScopedAST :: binder n l -> AST binder sig l -> ScopedAST binder sig n
instance (forall x y. NFData (binder x y), forall l. NFData (AST binder sig l)) => NFData (ScopedAST binder sig n) where
{-# INLINABLE rnf #-}
rnf :: ScopedAST binder sig n -> ()
rnf (ScopedAST binder n l
binder AST binder sig l
body) = binder n l -> ()
forall a. NFData a => a -> ()
rnf binder n l
binder () -> () -> ()
forall a b. a -> b -> b
`seq` AST binder sig l -> ()
forall a. NFData a => a -> ()
rnf AST binder sig l
body
data AST binder sig n where
Var :: {-# UNPACK #-} !(Foil.Name n) -> AST binder sig n
Node :: sig (ScopedAST binder sig n) (AST binder sig n) -> AST binder sig n
deriving instance Generic (AST binder sig n)
instance (forall x y. NFData (binder x y), forall scope term. (NFData scope, NFData term) => NFData (sig scope term))
=> NFData (AST binder sig n) where
{-# INLINABLE rnf #-}
rnf :: AST binder sig n -> ()
rnf (Var Name n
name) = Name n -> ()
forall a. NFData a => a -> ()
rnf Name n
name
rnf (Node sig (ScopedAST binder sig n) (AST binder sig n)
node) = sig (ScopedAST binder sig n) (AST binder sig n) -> ()
forall a. NFData a => a -> ()
rnf sig (ScopedAST binder sig n) (AST binder sig n)
node
instance GenericK (ScopedAST binder sig) where
type RepK (ScopedAST binder sig) =
Exists Foil.S
(Field (Kon binder :@: Var1 :@: Var0) :*: Field (Kon AST :@: Kon binder :@: Kon sig :@: Var0))
toK :: forall (x :: LoT (S -> *)).
RepK (ScopedAST binder sig) x -> ScopedAST binder sig :@@: x
toK (Kind.Exists (Kind.Field Interpret (('Kon binder ':@: Var1) ':@: Var0) (t ':&&: x)
binder Kind.:*: Kind.Field Interpret
((('Kon AST ':@: 'Kon binder) ':@: 'Kon sig) ':@: Var0) (t ':&&: x)
ast)) = binder (HeadLoT x) t
-> AST binder sig t -> ScopedAST binder sig (HeadLoT x)
forall (binder :: S -> S -> *) (n :: S) (l :: S)
(sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST binder (HeadLoT x) t
Interpret (('Kon binder ':@: Var1) ':@: Var0) (t ':&&: x)
binder Interpret
((('Kon AST ':@: 'Kon binder) ':@: 'Kon sig) ':@: Var0) (t ':&&: x)
AST binder sig t
ast
fromK :: forall (x :: LoT (S -> *)).
(ScopedAST binder sig :@@: x) -> RepK (ScopedAST binder sig) x
fromK (ScopedAST binder (HeadLoT x) l
binder AST binder sig l
ast) = (:*:)
(Field (('Kon binder ':@: Var1) ':@: Var0))
(Field ((('Kon AST ':@: 'Kon binder) ':@: 'Kon sig) ':@: Var0))
(l ':&&: x)
-> Exists
S
(Field (('Kon binder ':@: Var1) ':@: Var0)
:*: Field ((('Kon AST ':@: 'Kon binder) ':@: 'Kon sig) ':@: Var0))
x
forall k (t :: k) d (f :: LoT (k -> d) -> *) (x :: LoT d).
f (t ':&&: x) -> Exists k f x
Kind.Exists (Interpret (('Kon binder ':@: Var1) ':@: Var0) (l ':&&: x)
-> Field (('Kon binder ':@: Var1) ':@: Var0) (l ':&&: x)
forall {d} (t :: Atom d (*)) (x :: LoT d).
Interpret t x -> Field t x
Kind.Field binder (HeadLoT x) l
Interpret (('Kon binder ':@: Var1) ':@: Var0) (l ':&&: x)
binder Field (('Kon binder ':@: Var1) ':@: Var0) (l ':&&: x)
-> Field
((('Kon AST ':@: 'Kon binder) ':@: 'Kon sig) ':@: Var0) (l ':&&: x)
-> (:*:)
(Field (('Kon binder ':@: Var1) ':@: Var0))
(Field ((('Kon AST ':@: 'Kon binder) ':@: 'Kon sig) ':@: Var0))
(l ':&&: x)
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
Kind.:*: Interpret
((('Kon AST ':@: 'Kon binder) ':@: 'Kon sig) ':@: Var0) (l ':&&: x)
-> Field
((('Kon AST ':@: 'Kon binder) ':@: 'Kon sig) ':@: Var0) (l ':&&: x)
forall {d} (t :: Atom d (*)) (x :: LoT d).
Interpret t x -> Field t x
Kind.Field Interpret
((('Kon AST ':@: 'Kon binder) ':@: 'Kon sig) ':@: Var0) (l ':&&: x)
AST binder sig l
ast)
instance GenericK (AST binder sig) where
type RepK (AST binder sig) =
Field (Foil.Name :$: Var0)
:+: Field (sig
:$: (Kon ScopedAST :@: Kon binder :@: Kon sig :@: Var0)
:@: (Kon AST :@: Kon binder :@: Kon sig :@: Var0))
instance (Bifunctor sig, Foil.CoSinkable binder, Foil.SinkableK binder) => Foil.Sinkable (ScopedAST binder sig)
instance (Bifunctor sig, Foil.CoSinkable binder, Foil.SinkableK binder) => Foil.Sinkable (AST binder sig)
instance (Bifunctor sig, Foil.CoSinkable binder, Foil.SinkableK binder) => Foil.SinkableK (ScopedAST binder sig)
instance (Bifunctor sig, Foil.CoSinkable binder, Foil.SinkableK binder) => Foil.SinkableK (AST binder sig)
instance Foil.InjectName (AST binder sig) where
injectName :: forall (n :: S). Name n -> AST binder sig n
injectName = Name n -> AST binder sig n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var
{-# INLINE substitute #-}
substitute
:: forall sig binder o i. (Bifunctor sig, Foil.Distinct o, Foil.CoSinkable binder, Foil.SinkableK binder)
=> Foil.Scope o
-> Foil.Substitution (AST binder sig) i o
-> AST binder sig i
-> AST binder sig o
substitute :: forall (sig :: * -> * -> *) (binder :: S -> S -> *) (o :: S)
(i :: S).
(Bifunctor sig, Distinct o, CoSinkable binder, SinkableK binder) =>
Scope o
-> Substitution (AST binder sig) i o
-> AST binder sig i
-> AST binder sig o
substitute = Scope o
-> Substitution (AST binder sig) i o
-> AST binder sig i
-> AST binder sig o
forall (o' :: S) (i' :: S).
Distinct o' =>
Scope o'
-> Substitution (AST binder sig) i' o'
-> AST binder sig i'
-> AST binder sig o'
goSubst
where
goSubst
:: Foil.Distinct o'
=> Foil.Scope o'
-> Foil.Substitution (AST binder sig) i' o'
-> AST binder sig i'
-> AST binder sig o'
goSubst :: forall (o' :: S) (i' :: S).
Distinct o' =>
Scope o'
-> Substitution (AST binder sig) i' o'
-> AST binder sig i'
-> AST binder sig o'
goSubst Scope o'
scope Substitution (AST binder sig) i' o'
subst AST binder sig i'
term
| Substitution (AST binder sig) i' o' -> Bool
forall (e :: S -> *) (i :: S) (o :: S). Substitution e i o -> Bool
Foil.nullSubst Substitution (AST binder sig) i' o'
subst = AST binder sig i' -> AST binder sig o'
forall a b. a -> b
unsafeCoerce AST binder sig i'
term
| Bool
otherwise = AST binder sig i' -> AST binder sig o'
go AST binder sig i'
term
where
go :: AST binder sig i' -> AST binder sig o'
go = \case
Var Name i'
name -> Substitution (AST binder sig) i' o' -> Name i' -> AST binder sig o'
forall (e :: S -> *) (i :: S) (o :: S).
InjectName e =>
Substitution e i o -> Name i -> e o
Foil.lookupSubst Substitution (AST binder sig) i' o'
subst Name i'
name
Node sig (ScopedAST binder sig i') (AST binder sig i')
node -> sig (ScopedAST binder sig o') (AST binder sig o')
-> AST binder sig o'
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig (ScopedAST binder sig n) (AST binder sig n) -> AST binder sig n
Node ((ScopedAST binder sig i' -> ScopedAST binder sig o')
-> (AST binder sig i' -> AST binder sig o')
-> sig (ScopedAST binder sig i') (AST binder sig i')
-> sig (ScopedAST binder sig o') (AST binder sig o')
forall a b c d. (a -> b) -> (c -> d) -> sig a c -> sig b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap ScopedAST binder sig i' -> ScopedAST binder sig o'
f AST binder sig i' -> AST binder sig o'
go sig (ScopedAST binder sig i') (AST binder sig i')
node)
f :: ScopedAST binder sig i' -> ScopedAST binder sig o'
f (ScopedAST binder i' l
binder AST binder sig l
body) =
Scope o'
-> binder i' l
-> (forall (o' :: S).
DExt o' o' =>
(Substitution (AST binder sig) i' o'
-> Substitution (AST binder sig) l o')
-> binder o' o' -> Scope o' -> ScopedAST binder sig o')
-> ScopedAST binder sig o'
forall (o :: S) (pattern :: S -> S -> *) (e :: S -> *) (n :: S)
(l :: S) r.
(Distinct o, CoSinkable pattern, Sinkable e, InjectName e) =>
Scope o
-> pattern n l
-> (forall (o' :: S).
DExt o o' =>
(Substitution e n o -> Substitution e l o')
-> pattern o o' -> Scope o' -> r)
-> r
Foil.withRefreshedPattern Scope o'
scope binder i' l
binder ((forall (o' :: S).
DExt o' o' =>
(Substitution (AST binder sig) i' o'
-> Substitution (AST binder sig) l o')
-> binder o' o' -> Scope o' -> ScopedAST binder sig o')
-> ScopedAST binder sig o')
-> (forall (o' :: S).
DExt o' o' =>
(Substitution (AST binder sig) i' o'
-> Substitution (AST binder sig) l o')
-> binder o' o' -> Scope o' -> ScopedAST binder sig o')
-> ScopedAST binder sig o'
forall a b. (a -> b) -> a -> b
$ \Substitution (AST binder sig) i' o'
-> Substitution (AST binder sig) l o'
extendSubst binder o' o'
binder' Scope o'
scope' ->
let subst' :: Substitution (AST binder sig) l o'
subst' = Substitution (AST binder sig) i' o'
-> Substitution (AST binder sig) l o'
extendSubst (Substitution (AST binder sig) i' o'
-> Substitution (AST binder sig) i' o'
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Substitution (AST binder sig) i' o'
subst)
body' :: AST binder sig o'
body' = Scope o'
-> Substitution (AST binder sig) l o'
-> AST binder sig l
-> AST binder sig o'
forall (o' :: S) (i' :: S).
Distinct o' =>
Scope o'
-> Substitution (AST binder sig) i' o'
-> AST binder sig i'
-> AST binder sig o'
goSubst Scope o'
scope' Substitution (AST binder sig) l o'
subst' AST binder sig l
body
in binder o' o' -> AST binder sig o' -> ScopedAST binder sig o'
forall (binder :: S -> S -> *) (n :: S) (l :: S)
(sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST binder o' o'
binder' AST binder sig o'
body'
{-# INLINABLE substituteRefreshed #-}
substituteRefreshed
:: (Bifunctor sig, Foil.Distinct o, Foil.CoSinkable binder, Foil.SinkableK binder)
=> Foil.Scope o
-> Foil.Substitution (AST binder sig) i o
-> AST binder sig i
-> AST binder sig o
substituteRefreshed :: forall (sig :: * -> * -> *) (o :: S) (binder :: S -> S -> *)
(i :: S).
(Bifunctor sig, Distinct o, CoSinkable binder, SinkableK binder) =>
Scope o
-> Substitution (AST binder sig) i o
-> AST binder sig i
-> AST binder sig o
substituteRefreshed Scope o
scope Substitution (AST binder sig) i o
subst = \case
Var Name i
name -> Substitution (AST binder sig) i o -> Name i -> AST binder sig o
forall (e :: S -> *) (i :: S) (o :: S).
InjectName e =>
Substitution e i o -> Name i -> e o
Foil.lookupSubst Substitution (AST binder sig) i o
subst Name i
name
Node sig (ScopedAST binder sig i) (AST binder sig i)
node -> sig (ScopedAST binder sig o) (AST binder sig o) -> AST binder sig o
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig (ScopedAST binder sig n) (AST binder sig n) -> AST binder sig n
Node ((ScopedAST binder sig i -> ScopedAST binder sig o)
-> (AST binder sig i -> AST binder sig o)
-> sig (ScopedAST binder sig i) (AST binder sig i)
-> sig (ScopedAST binder sig o) (AST binder sig o)
forall a b c d. (a -> b) -> (c -> d) -> sig a c -> sig b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap ScopedAST binder sig i -> ScopedAST binder sig o
f (Scope o
-> Substitution (AST binder sig) i o
-> AST binder sig i
-> AST binder sig o
forall (sig :: * -> * -> *) (o :: S) (binder :: S -> S -> *)
(i :: S).
(Bifunctor sig, Distinct o, CoSinkable binder, SinkableK binder) =>
Scope o
-> Substitution (AST binder sig) i o
-> AST binder sig i
-> AST binder sig o
substituteRefreshed Scope o
scope Substitution (AST binder sig) i o
subst) sig (ScopedAST binder sig i) (AST binder sig i)
node)
where
f :: ScopedAST binder sig i -> ScopedAST binder sig o
f (ScopedAST binder i l
binder AST binder sig l
body) =
Scope o
-> binder i l
-> (forall (o' :: S).
DExt o o' =>
(Substitution (AST binder sig) i o
-> Substitution (AST binder sig) l o')
-> binder o o' -> Scope o' -> ScopedAST binder sig o)
-> ScopedAST binder sig o
forall (o :: S) (pattern :: S -> S -> *) (e :: S -> *) (n :: S)
(l :: S) r.
(Distinct o, CoSinkable pattern, Sinkable e, InjectName e) =>
Scope o
-> pattern n l
-> (forall (o' :: S).
DExt o o' =>
(Substitution e n o -> Substitution e l o')
-> pattern o o' -> Scope o' -> r)
-> r
Foil.withFreshPattern Scope o
scope binder i l
binder ((forall (o' :: S).
DExt o o' =>
(Substitution (AST binder sig) i o
-> Substitution (AST binder sig) l o')
-> binder o o' -> Scope o' -> ScopedAST binder sig o)
-> ScopedAST binder sig o)
-> (forall (o' :: S).
DExt o o' =>
(Substitution (AST binder sig) i o
-> Substitution (AST binder sig) l o')
-> binder o o' -> Scope o' -> ScopedAST binder sig o)
-> ScopedAST binder sig o
forall a b. (a -> b) -> a -> b
$ \Substitution (AST binder sig) i o
-> Substitution (AST binder sig) l o'
extendSubst binder o o'
binder' Scope o'
scope' ->
let subst' :: Substitution (AST binder sig) l o'
subst' = Substitution (AST binder sig) i o
-> Substitution (AST binder sig) l o'
extendSubst (Substitution (AST binder sig) i o
-> Substitution (AST binder sig) i o
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Substitution (AST binder sig) i o
subst)
body' :: AST binder sig o'
body' = Scope o'
-> Substitution (AST binder sig) l o'
-> AST binder sig l
-> AST binder sig o'
forall (sig :: * -> * -> *) (o :: S) (binder :: S -> S -> *)
(i :: S).
(Bifunctor sig, Distinct o, CoSinkable binder, SinkableK binder) =>
Scope o
-> Substitution (AST binder sig) i o
-> AST binder sig i
-> AST binder sig o
substituteRefreshed Scope o'
scope' Substitution (AST binder sig) l o'
subst' AST binder sig l
body
in binder o o' -> AST binder sig o' -> ScopedAST binder sig o
forall (binder :: S -> S -> *) (n :: S) (l :: S)
(sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST binder o o'
binder' AST binder sig o'
body'
instance (Bifunctor sig, Foil.CoSinkable binder, Foil.SinkableK binder)
=> Foil.RelMonad Foil.Name (AST binder sig) where
rreturn :: forall (a :: S). Name a -> AST binder sig a
rreturn = Name a -> AST binder sig a
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var
rbind :: forall (b :: S) (a :: S).
Distinct b =>
Scope b
-> AST binder sig a
-> (Name a -> AST binder sig b)
-> AST binder sig b
rbind Scope b
scope AST binder sig a
term Name a -> AST binder sig b
subst =
case AST binder sig a
term of
Var Name a
name -> Name a -> AST binder sig b
subst Name a
name
Node sig (ScopedAST binder sig a) (AST binder sig a)
node -> sig (ScopedAST binder sig b) (AST binder sig b) -> AST binder sig b
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig (ScopedAST binder sig n) (AST binder sig n) -> AST binder sig n
Node ((ScopedAST binder sig a -> ScopedAST binder sig b)
-> (AST binder sig a -> AST binder sig b)
-> sig (ScopedAST binder sig a) (AST binder sig a)
-> sig (ScopedAST binder sig b) (AST binder sig b)
forall a b c d. (a -> b) -> (c -> d) -> sig a c -> sig b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap ScopedAST binder sig a -> ScopedAST binder sig b
g' AST binder sig a -> AST binder sig b
g sig (ScopedAST binder sig a) (AST binder sig a)
node)
where
g :: AST binder sig a -> AST binder sig b
g AST binder sig a
x = Scope b
-> AST binder sig a
-> (Name a -> AST binder sig b)
-> AST binder sig b
forall (b :: S) (a :: S).
Distinct b =>
Scope b
-> AST binder sig a
-> (Name a -> AST binder sig b)
-> AST binder sig b
forall (f :: S -> *) (m :: S -> *) (b :: S) (a :: S).
(RelMonad f m, Distinct b) =>
Scope b -> m a -> (f a -> m b) -> m b
Foil.rbind Scope b
scope AST binder sig a
x Name a -> AST binder sig b
subst
g' :: ScopedAST binder sig a -> ScopedAST binder sig b
g' (ScopedAST binder a l
binder AST binder sig l
body) =
Scope b
-> binder a l
-> (forall (o' :: S).
DExt b o' =>
((Name a -> AST binder sig b) -> Name l -> AST binder sig o')
-> binder b o' -> Scope o' -> ScopedAST binder sig b)
-> ScopedAST binder sig b
forall (pattern :: S -> S -> *) (o :: S) (e :: S -> *) (n :: S)
(l :: S) r.
(CoSinkable pattern, Distinct o, InjectName e, Sinkable e) =>
Scope o
-> pattern n l
-> (forall (o' :: S).
DExt o o' =>
((Name n -> e o) -> Name l -> e o')
-> pattern o o' -> Scope o' -> r)
-> r
Foil.withRefreshedPattern' Scope b
scope binder a l
binder ((forall (o' :: S).
DExt b o' =>
((Name a -> AST binder sig b) -> Name l -> AST binder sig o')
-> binder b o' -> Scope o' -> ScopedAST binder sig b)
-> ScopedAST binder sig b)
-> (forall (o' :: S).
DExt b o' =>
((Name a -> AST binder sig b) -> Name l -> AST binder sig o')
-> binder b o' -> Scope o' -> ScopedAST binder sig b)
-> ScopedAST binder sig b
forall a b. (a -> b) -> a -> b
$ \(Name a -> AST binder sig b) -> Name l -> AST binder sig o'
extendSubst binder b o'
binder' Scope o'
scope' ->
let subst' :: Name l -> AST binder sig o'
subst' = (Name a -> AST binder sig b) -> Name l -> AST binder sig o'
extendSubst Name a -> AST binder sig b
subst
in binder b o' -> AST binder sig o' -> ScopedAST binder sig b
forall (binder :: S -> S -> *) (n :: S) (l :: S)
(sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST binder b o'
binder' (Scope o'
-> AST binder sig l
-> (Name l -> AST binder sig o')
-> AST binder sig o'
forall (b :: S) (a :: S).
Distinct b =>
Scope b
-> AST binder sig a
-> (Name a -> AST binder sig b)
-> AST binder sig b
forall (f :: S -> *) (m :: S -> *) (b :: S) (a :: S).
(RelMonad f m, Distinct b) =>
Scope b -> m a -> (f a -> m b) -> m b
Foil.rbind Scope o'
scope' AST binder sig l
body Name l -> AST binder sig o'
subst')
{-# INLINABLE substitutePattern #-}
substitutePattern
:: (Bifunctor sig, Foil.Distinct o, Foil.CoSinkable binder', Foil.CoSinkable binder, Foil.SinkableK binder)
=> Foil.Scope o
-> Foil.Substitution (AST binder sig) n o
-> binder' n i
-> [AST binder sig o]
-> AST binder sig i
-> AST binder sig o
substitutePattern :: forall (sig :: * -> * -> *) (o :: S) (binder' :: S -> S -> *)
(binder :: S -> S -> *) (n :: S) (i :: S).
(Bifunctor sig, Distinct o, CoSinkable binder', CoSinkable binder,
SinkableK binder) =>
Scope o
-> Substitution (AST binder sig) n o
-> binder' n i
-> [AST binder sig o]
-> AST binder sig i
-> AST binder sig o
substitutePattern Scope o
scope Substitution (AST binder sig) n o
env binder' n i
binders [AST binder sig o]
args AST binder sig i
body =
Scope o
-> Substitution (AST binder sig) i o
-> AST binder sig i
-> AST binder sig o
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (o :: S)
(i :: S).
(Bifunctor sig, Distinct o, CoSinkable binder, SinkableK binder) =>
Scope o
-> Substitution (AST binder sig) i o
-> AST binder sig i
-> AST binder sig o
substitute Scope o
scope Substitution (AST binder sig) i o
env' AST binder sig i
body
where
env' :: Substitution (AST binder sig) i o
env' = Substitution (AST binder sig) n o
-> binder' n i
-> [AST binder sig o]
-> Substitution (AST binder sig) i o
forall (binder :: S -> S -> *) (e :: S -> *) (i :: S) (o :: S)
(i' :: S).
CoSinkable binder =>
Substitution e i o -> binder i i' -> [e o] -> Substitution e i' o
Foil.addSubstPattern Substitution (AST binder sig) n o
env binder' n i
binders [AST binder sig o]
args
{-# INLINABLE foldAST #-}
foldAST
:: forall binder sig r s n. Bifunctor sig
=> (forall m. Foil.Name m -> r m)
-> (forall m l. binder m l -> r l -> s m)
-> (forall m. sig (s m) (r m) -> r m)
-> AST binder sig n
-> r n
foldAST :: forall (binder :: S -> S -> *) (sig :: * -> * -> *) (r :: S -> *)
(s :: S -> *) (n :: S).
Bifunctor sig =>
(forall (m :: S). Name m -> r m)
-> (forall (m :: S) (l :: S). binder m l -> r l -> s m)
-> (forall (m :: S). sig (s m) (r m) -> r m)
-> AST binder sig n
-> r n
foldAST forall (m :: S). Name m -> r m
var forall (m :: S) (l :: S). binder m l -> r l -> s m
scoped forall (m :: S). sig (s m) (r m) -> r m
node = AST binder sig n -> r n
forall (m :: S). AST binder sig m -> r m
go
where
go :: forall m. AST binder sig m -> r m
go :: forall (m :: S). AST binder sig m -> r m
go (Var Name m
x) = Name m -> r m
forall (m :: S). Name m -> r m
var Name m
x
go (Node sig (ScopedAST binder sig m) (AST binder sig m)
t) = sig (s m) (r m) -> r m
forall (m :: S). sig (s m) (r m) -> r m
node ((ScopedAST binder sig m -> s m)
-> (AST binder sig m -> r m)
-> sig (ScopedAST binder sig m) (AST binder sig m)
-> sig (s m) (r m)
forall a b c d. (a -> b) -> (c -> d) -> sig a c -> sig b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (\(ScopedAST binder m l
binder AST binder sig l
body) -> binder m l -> r l -> s m
forall (m :: S) (l :: S). binder m l -> r l -> s m
scoped binder m l
binder (AST binder sig l -> r l
forall (m :: S). AST binder sig m -> r m
go AST binder sig l
body)) AST binder sig m -> r m
forall (m :: S). AST binder sig m -> r m
go sig (ScopedAST binder sig m) (AST binder sig m)
t)
{-# INLINABLE hoistAST #-}
hoistAST
:: Bifunctor sig
=> (forall a b. sig a b -> sig' a b)
-> AST binder sig n
-> AST binder sig' n
hoistAST :: forall (sig :: * -> * -> *) (sig' :: * -> * -> *)
(binder :: S -> S -> *) (n :: S).
Bifunctor sig =>
(forall a b. sig a b -> sig' a b)
-> AST binder sig n -> AST binder sig' n
hoistAST forall a b. sig a b -> sig' a b
f = (forall (m :: S). Name m -> AST binder sig' m)
-> (forall (m :: S) (l :: S).
binder m l -> AST binder sig' l -> ScopedAST binder sig' m)
-> (forall (m :: S).
sig (ScopedAST binder sig' m) (AST binder sig' m)
-> AST binder sig' m)
-> AST binder sig n
-> AST binder sig' n
forall (binder :: S -> S -> *) (sig :: * -> * -> *) (r :: S -> *)
(s :: S -> *) (n :: S).
Bifunctor sig =>
(forall (m :: S). Name m -> r m)
-> (forall (m :: S) (l :: S). binder m l -> r l -> s m)
-> (forall (m :: S). sig (s m) (r m) -> r m)
-> AST binder sig n
-> r n
foldAST Name m -> AST binder sig' m
forall (m :: S). Name m -> AST binder sig' m
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var binder m l -> AST binder sig' l -> ScopedAST binder sig' m
forall (m :: S) (l :: S).
binder m l -> AST binder sig' l -> ScopedAST binder sig' m
forall (binder :: S -> S -> *) (n :: S) (l :: S)
(sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST (sig' (ScopedAST binder sig' m) (AST binder sig' m)
-> AST binder sig' m
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig (ScopedAST binder sig n) (AST binder sig n) -> AST binder sig n
Node (sig' (ScopedAST binder sig' m) (AST binder sig' m)
-> AST binder sig' m)
-> (sig (ScopedAST binder sig' m) (AST binder sig' m)
-> sig' (ScopedAST binder sig' m) (AST binder sig' m))
-> sig (ScopedAST binder sig' m) (AST binder sig' m)
-> AST binder sig' m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. sig (ScopedAST binder sig' m) (AST binder sig' m)
-> sig' (ScopedAST binder sig' m) (AST binder sig' m)
forall a b. sig a b -> sig' a b
f)
{-# INLINABLE refreshAST #-}
refreshAST
:: (Bifunctor sig, Foil.Distinct n, Foil.CoSinkable binder, Foil.SinkableK binder)
=> Foil.Scope n
-> AST binder sig n
-> AST binder sig n
refreshAST :: forall (sig :: * -> * -> *) (n :: S) (binder :: S -> S -> *).
(Bifunctor sig, Distinct n, CoSinkable binder, SinkableK binder) =>
Scope n -> AST binder sig n -> AST binder sig n
refreshAST Scope n
scope = \case
t :: AST binder sig n
t@Var{} -> AST binder sig n
t
Node sig (ScopedAST binder sig n) (AST binder sig n)
t -> sig (ScopedAST binder sig n) (AST binder sig n) -> AST binder sig n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig (ScopedAST binder sig n) (AST binder sig n) -> AST binder sig n
Node ((ScopedAST binder sig n -> ScopedAST binder sig n)
-> (AST binder sig n -> AST binder sig n)
-> sig (ScopedAST binder sig n) (AST binder sig n)
-> sig (ScopedAST binder sig n) (AST binder sig n)
forall a b c d. (a -> b) -> (c -> d) -> sig a c -> sig b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap (Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n
forall (sig :: * -> * -> *) (n :: S) (binder :: S -> S -> *).
(Bifunctor sig, Distinct n, CoSinkable binder, SinkableK binder) =>
Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n
refreshScopedAST Scope n
scope) (Scope n -> AST binder sig n -> AST binder sig n
forall (sig :: * -> * -> *) (n :: S) (binder :: S -> S -> *).
(Bifunctor sig, Distinct n, CoSinkable binder, SinkableK binder) =>
Scope n -> AST binder sig n -> AST binder sig n
refreshAST Scope n
scope) sig (ScopedAST binder sig n) (AST binder sig n)
t)
{-# INLINABLE refreshScopedAST #-}
refreshScopedAST :: (Bifunctor sig, Foil.Distinct n, Foil.CoSinkable binder, Foil.SinkableK binder)
=> Foil.Scope n
-> ScopedAST binder sig n
-> ScopedAST binder sig n
refreshScopedAST :: forall (sig :: * -> * -> *) (n :: S) (binder :: S -> S -> *).
(Bifunctor sig, Distinct n, CoSinkable binder, SinkableK binder) =>
Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n
refreshScopedAST Scope n
scope (ScopedAST binder n l
binder AST binder sig l
body) =
Scope n
-> binder n l
-> (forall (o' :: S).
DExt n o' =>
(Substitution (AST binder sig) n n
-> Substitution (AST binder sig) l o')
-> binder n o' -> Scope o' -> ScopedAST binder sig n)
-> ScopedAST binder sig n
forall (o :: S) (pattern :: S -> S -> *) (e :: S -> *) (n :: S)
(l :: S) r.
(Distinct o, CoSinkable pattern, Sinkable e, InjectName e) =>
Scope o
-> pattern n l
-> (forall (o' :: S).
DExt o o' =>
(Substitution e n o -> Substitution e l o')
-> pattern o o' -> Scope o' -> r)
-> r
Foil.withFreshPattern Scope n
scope binder n l
binder ((forall (o' :: S).
DExt n o' =>
(Substitution (AST binder sig) n n
-> Substitution (AST binder sig) l o')
-> binder n o' -> Scope o' -> ScopedAST binder sig n)
-> ScopedAST binder sig n)
-> (forall (o' :: S).
DExt n o' =>
(Substitution (AST binder sig) n n
-> Substitution (AST binder sig) l o')
-> binder n o' -> Scope o' -> ScopedAST binder sig n)
-> ScopedAST binder sig n
forall a b. (a -> b) -> a -> b
$ \Substitution (AST binder sig) n n
-> Substitution (AST binder sig) l o'
extendSubst binder n o'
binder' Scope o'
scope' ->
let subst :: Substitution (AST binder sig) l o'
subst = Substitution (AST binder sig) n n
-> Substitution (AST binder sig) l o'
extendSubst (Substitution (AST binder sig) n n
-> Substitution (AST binder sig) n n
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Substitution (AST binder sig) n n
forall (e :: S -> *) (i :: S). InjectName e => Substitution e i i
Foil.identitySubst)
in binder n o' -> AST binder sig o' -> ScopedAST binder sig n
forall (binder :: S -> S -> *) (n :: S) (l :: S)
(sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST binder n o'
binder' (Scope o'
-> Substitution (AST binder sig) l o'
-> AST binder sig l
-> AST binder sig o'
forall (sig :: * -> * -> *) (o :: S) (binder :: S -> S -> *)
(i :: S).
(Bifunctor sig, Distinct o, CoSinkable binder, SinkableK binder) =>
Scope o
-> Substitution (AST binder sig) i o
-> AST binder sig i
-> AST binder sig o
substituteRefreshed Scope o'
scope' Substitution (AST binder sig) l o'
subst AST binder sig l
body)
{-# INLINABLE alphaEquivRefreshed #-}
alphaEquivRefreshed
:: (Bifunctor sig, ZipMatchK sig, Foil.Distinct n, Foil.UnifiablePattern binder, Foil.SinkableK binder)
=> Foil.Scope n
-> AST binder sig n
-> AST binder sig n
-> Bool
alphaEquivRefreshed :: forall (sig :: * -> * -> *) (n :: S) (binder :: S -> S -> *).
(Bifunctor sig, ZipMatchK sig, Distinct n, UnifiablePattern binder,
SinkableK binder) =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
alphaEquivRefreshed Scope n
scope AST binder sig n
t1 AST binder sig n
t2 =
Scope n -> AST binder sig n -> AST binder sig n -> Bool
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
(ZipMatchK sig, UnifiablePattern binder, Distinct n) =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
unsafeEqASTIn Scope n
scope (Scope n -> AST binder sig n -> AST binder sig n
forall (sig :: * -> * -> *) (n :: S) (binder :: S -> S -> *).
(Bifunctor sig, Distinct n, CoSinkable binder, SinkableK binder) =>
Scope n -> AST binder sig n -> AST binder sig n
refreshAST Scope n
scope AST binder sig n
t1) (Scope n -> AST binder sig n -> AST binder sig n
forall (sig :: * -> * -> *) (n :: S) (binder :: S -> S -> *).
(Bifunctor sig, Distinct n, CoSinkable binder, SinkableK binder) =>
Scope n -> AST binder sig n -> AST binder sig n
refreshAST Scope n
scope AST binder sig n
t2)
instance (ZipMatchK sig, Foil.UnifiablePattern binder)
=> Foil.AlphaEquiv (AST binder sig) where
alphaEquivIn :: forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
alphaEquivIn = Scope n -> AST binder sig n -> AST binder sig n -> Bool
forall (sig :: * -> * -> *) (n :: S) (binder :: S -> S -> *).
(ZipMatchK sig, Distinct n, UnifiablePattern binder) =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
alphaEquiv
{-# INLINE alphaEquiv #-}
alphaEquiv
:: (ZipMatchK sig, Foil.Distinct n, Foil.UnifiablePattern binder)
=> Foil.Scope n
-> AST binder sig n
-> AST binder sig n
-> Bool
alphaEquiv :: forall (sig :: * -> * -> *) (n :: S) (binder :: S -> S -> *).
(ZipMatchK sig, Distinct n, UnifiablePattern binder) =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
alphaEquiv = AlphaEquivLoops binder sig
-> forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
forall (binder :: S -> S -> *) (sig :: * -> * -> *).
AlphaEquivLoops binder sig
-> forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
aeAlphaEquiv AlphaEquivLoops binder sig
forall (sig :: * -> * -> *) (binder :: S -> S -> *).
(ZipMatchK sig, UnifiablePattern binder) =>
AlphaEquivLoops binder sig
alphaEquivLoops
{-# INLINE alphaEquivScoped #-}
alphaEquivScoped
:: forall sig binder n. (ZipMatchK sig, Foil.Distinct n, Foil.UnifiablePattern binder)
=> Foil.Scope n
-> ScopedAST binder sig n
-> ScopedAST binder sig n
-> Bool
alphaEquivScoped :: forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
(ZipMatchK sig, Distinct n, UnifiablePattern binder) =>
Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
alphaEquivScoped = AlphaEquivLoops binder sig
-> forall (n :: S).
Distinct n =>
Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
forall (binder :: S -> S -> *) (sig :: * -> * -> *).
AlphaEquivLoops binder sig
-> forall (n :: S).
Distinct n =>
Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
aeAlphaEquivScoped AlphaEquivLoops binder sig
forall (sig :: * -> * -> *) (binder :: S -> S -> *).
(ZipMatchK sig, UnifiablePattern binder) =>
AlphaEquivLoops binder sig
alphaEquivLoops
renamedId :: (Foil.NameBinder n a -> Foil.NameBinder n b) -> Foil.Name a -> Int
renamedId :: forall (n :: S) (a :: S) (b :: S).
(NameBinder n a -> NameBinder n b) -> Name a -> Int
renamedId NameBinder n a -> NameBinder n b
rename = Name b -> Int
forall (l :: S). Name l -> Int
Foil.nameId (Name b -> Int) -> (Name a -> Name b) -> Name a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NameBinder n b -> Name b
forall (n :: S) (l :: S). NameBinder n l -> Name l
Foil.nameOf (NameBinder n b -> Name b)
-> (Name a -> NameBinder n b) -> Name a -> Name b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NameBinder n a -> NameBinder n b
rename (NameBinder n a -> NameBinder n b)
-> (Name a -> NameBinder n a) -> Name a -> NameBinder n b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name a -> NameBinder n a
forall (n :: S) (l :: S). Name l -> NameBinder n l
Foil.UnsafeNameBinder
{-# INLINE bindPairs #-}
bindPairs
:: forall sig binder m l1 l2. (ZipMatchK sig, Foil.Distinct m, Foil.UnifiablePattern binder)
=> Int -> IntMap Int -> IntMap Int -> [(Int, Int)]
-> Foil.Scope m
-> AST binder sig l1 -> AST binder sig l2 -> Bool
bindPairs :: forall (sig :: * -> * -> *) (binder :: S -> S -> *) (m :: S)
(l1 :: S) (l2 :: S).
(ZipMatchK sig, Distinct m, UnifiablePattern binder) =>
Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
bindPairs = AlphaEquivLoops binder sig
-> forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
forall (binder :: S -> S -> *) (sig :: * -> * -> *).
AlphaEquivLoops binder sig
-> forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
aeBindPairs AlphaEquivLoops binder sig
forall (sig :: * -> * -> *) (binder :: S -> S -> *).
(ZipMatchK sig, UnifiablePattern binder) =>
AlphaEquivLoops binder sig
alphaEquivLoops
{-# INLINE alphaEquivEnv #-}
alphaEquivEnv
:: forall sig binder n n1 n2. (ZipMatchK sig, Foil.Distinct n, Foil.UnifiablePattern binder)
=> Int
-> IntMap Int
-> IntMap Int
-> Foil.Scope n
-> AST binder sig n1
-> AST binder sig n2
-> Bool
alphaEquivEnv :: forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S)
(n1 :: S) (n2 :: S).
(ZipMatchK sig, Distinct n, UnifiablePattern binder) =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> AST binder sig n1
-> AST binder sig n2
-> Bool
alphaEquivEnv = AlphaEquivLoops binder sig
-> forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> AST binder sig n1
-> AST binder sig n2
-> Bool
forall (binder :: S -> S -> *) (sig :: * -> * -> *).
AlphaEquivLoops binder sig
-> forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> AST binder sig n1
-> AST binder sig n2
-> Bool
aeAlphaEquivEnv AlphaEquivLoops binder sig
forall (sig :: * -> * -> *) (binder :: S -> S -> *).
(ZipMatchK sig, UnifiablePattern binder) =>
AlphaEquivLoops binder sig
alphaEquivLoops
{-# INLINE alphaEquivScopedEnv #-}
alphaEquivScopedEnv
:: forall sig binder n n1 n2. (ZipMatchK sig, Foil.Distinct n, Foil.UnifiablePattern binder)
=> Int
-> IntMap Int
-> IntMap Int
-> Foil.Scope n
-> ScopedAST binder sig n1
-> ScopedAST binder sig n2
-> Bool
alphaEquivScopedEnv :: forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S)
(n1 :: S) (n2 :: S).
(ZipMatchK sig, Distinct n, UnifiablePattern binder) =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> ScopedAST binder sig n1
-> ScopedAST binder sig n2
-> Bool
alphaEquivScopedEnv = AlphaEquivLoops binder sig
-> forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> ScopedAST binder sig n1
-> ScopedAST binder sig n2
-> Bool
forall (binder :: S -> S -> *) (sig :: * -> * -> *).
AlphaEquivLoops binder sig
-> forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> ScopedAST binder sig n1
-> ScopedAST binder sig n2
-> Bool
aeAlphaEquivScopedEnv AlphaEquivLoops binder sig
forall (sig :: * -> * -> *) (binder :: S -> S -> *).
(ZipMatchK sig, UnifiablePattern binder) =>
AlphaEquivLoops binder sig
alphaEquivLoops
data AlphaEquivLoops binder sig = AlphaEquivLoops
{ forall (binder :: S -> S -> *) (sig :: * -> * -> *).
AlphaEquivLoops binder sig
-> forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
aeAlphaEquiv :: forall n. Foil.Distinct n => Foil.Scope n -> AST binder sig n -> AST binder sig n -> Bool
, forall (binder :: S -> S -> *) (sig :: * -> * -> *).
AlphaEquivLoops binder sig
-> forall (n :: S).
Distinct n =>
Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
aeAlphaEquivScoped :: forall n. Foil.Distinct n => Foil.Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
, forall (binder :: S -> S -> *) (sig :: * -> * -> *).
AlphaEquivLoops binder sig
-> forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
aeBindPairs :: forall m l1 l2. Foil.Distinct m => Int -> IntMap Int -> IntMap Int -> [(Int, Int)] -> Foil.Scope m -> AST binder sig l1 -> AST binder sig l2 -> Bool
, forall (binder :: S -> S -> *) (sig :: * -> * -> *).
AlphaEquivLoops binder sig
-> forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> AST binder sig n1
-> AST binder sig n2
-> Bool
aeAlphaEquivEnv :: forall n n1 n2. Foil.Distinct n => Int -> IntMap Int -> IntMap Int -> Foil.Scope n -> AST binder sig n1 -> AST binder sig n2 -> Bool
, forall (binder :: S -> S -> *) (sig :: * -> * -> *).
AlphaEquivLoops binder sig
-> forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> ScopedAST binder sig n1
-> ScopedAST binder sig n2
-> Bool
aeAlphaEquivScopedEnv :: forall n n1 n2. Foil.Distinct n => Int -> IntMap Int -> IntMap Int -> Foil.Scope n -> ScopedAST binder sig n1 -> ScopedAST binder sig n2 -> Bool
}
{-# INLINE alphaEquivLoops #-}
alphaEquivLoops
:: forall sig binder. (ZipMatchK sig, Foil.UnifiablePattern binder)
=> AlphaEquivLoops binder sig
alphaEquivLoops :: forall (sig :: * -> * -> *) (binder :: S -> S -> *).
(ZipMatchK sig, UnifiablePattern binder) =>
AlphaEquivLoops binder sig
alphaEquivLoops = (forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool)
-> (forall (n :: S).
Distinct n =>
Scope n
-> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool)
-> (forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool)
-> (forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> AST binder sig n1
-> AST binder sig n2
-> Bool)
-> (forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> ScopedAST binder sig n1
-> ScopedAST binder sig n2
-> Bool)
-> AlphaEquivLoops binder sig
forall (binder :: S -> S -> *) (sig :: * -> * -> *).
(forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool)
-> (forall (n :: S).
Distinct n =>
Scope n
-> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool)
-> (forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool)
-> (forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> AST binder sig n1
-> AST binder sig n2
-> Bool)
-> (forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> ScopedAST binder sig n1
-> ScopedAST binder sig n2
-> Bool)
-> AlphaEquivLoops binder sig
AlphaEquivLoops Scope n -> AST binder sig n -> AST binder sig n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
goAlpha Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
goScoped Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
goPairs Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> AST binder sig n1
-> AST binder sig n2
-> Bool
forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> AST binder sig n1
-> AST binder sig n2
-> Bool
goEnv Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> ScopedAST binder sig n1
-> ScopedAST binder sig n2
-> Bool
forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> ScopedAST binder sig n1
-> ScopedAST binder sig n2
-> Bool
goScopedEnv
where
goAlpha
:: Foil.Distinct n
=> Foil.Scope n
-> AST binder sig n
-> AST binder sig n
-> Bool
goAlpha :: forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
goAlpha Scope n
_scope (Var Name n
x) (Var Name n
y) = Name n
x Name n -> Name n -> Bool
forall a. Eq a => a -> a -> Bool
== Name n
y
goAlpha Scope n
scope (Node sig (ScopedAST binder sig n) (AST binder sig n)
l) (Node sig (ScopedAST binder sig n) (AST binder sig n)
r) =
case (ScopedAST binder sig n -> ScopedAST binder sig n -> Maybe ())
-> (AST binder sig n -> AST binder sig n -> Maybe ())
-> sig (ScopedAST binder sig n) (AST binder sig n)
-> sig (ScopedAST binder sig n) (AST binder sig n)
-> Maybe (sig () ())
forall (f :: * -> * -> *) a a' a'' b b' b''.
ZipMatchK f =>
(a -> a' -> Maybe a'')
-> (b -> b' -> Maybe b'') -> f a b -> f a' b' -> Maybe (f a'' b'')
zipMatchWith2 ((ScopedAST binder sig n -> Bool)
-> ScopedAST binder sig n -> Maybe ()
forall {t}. (t -> Bool) -> t -> Maybe ()
unit ((ScopedAST binder sig n -> Bool)
-> ScopedAST binder sig n -> Maybe ())
-> (ScopedAST binder sig n -> ScopedAST binder sig n -> Bool)
-> ScopedAST binder sig n
-> ScopedAST binder sig n
-> Maybe ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
goScoped Scope n
scope) ((AST binder sig n -> Bool) -> AST binder sig n -> Maybe ()
forall {t}. (t -> Bool) -> t -> Maybe ()
unit ((AST binder sig n -> Bool) -> AST binder sig n -> Maybe ())
-> (AST binder sig n -> AST binder sig n -> Bool)
-> AST binder sig n
-> AST binder sig n
-> Maybe ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scope n -> AST binder sig n -> AST binder sig n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
goAlpha Scope n
scope) sig (ScopedAST binder sig n) (AST binder sig n)
l sig (ScopedAST binder sig n) (AST binder sig n)
r of
Maybe (sig () ())
Nothing -> Bool
False
Just sig () ()
_ -> Bool
True
where
unit :: (t -> Bool) -> t -> Maybe ()
unit t -> Bool
f t
x = if t -> Bool
f t
x then () -> Maybe ()
forall a. a -> Maybe a
Just () else Maybe ()
forall a. Maybe a
Nothing
goAlpha Scope n
_ AST binder sig n
_ AST binder sig n
_ = Bool
False
goScoped
:: forall n. Foil.Distinct n
=> Foil.Scope n
-> ScopedAST binder sig n
-> ScopedAST binder sig n
-> Bool
goScoped :: forall (n :: S).
Distinct n =>
Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
goScoped Scope n
scope
(ScopedAST binder n l
binder1 AST binder sig l
body1)
(ScopedAST binder n l
binder2 AST binder sig l
body2) =
case Scope n
-> binder n l -> binder n l -> UnifyNameBinders binder n l l
forall (n :: S) (l :: S) (r :: S).
Distinct n =>
Scope n
-> binder n l -> binder n r -> UnifyNameBinders binder n l r
forall (pattern :: S -> S -> *) (n :: S) (l :: S) (r :: S).
(UnifiablePattern pattern, Distinct n) =>
Scope n
-> pattern n l -> pattern n r -> UnifyNameBinders pattern n l r
Foil.unifyPatternsIn Scope n
scope binder n l
binder1 binder n l
binder2 of
Foil.SameNameBinders{} ->
case binder n l -> DistinctEvidence l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct binder n l
binder1 of
DistinctEvidence l
Foil.Distinct ->
let scope1 :: Scope l
scope1 = binder n l -> Scope n -> Scope l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> Scope n -> Scope l
Foil.extendScopePattern binder n l
binder1 Scope n
scope
in Scope l -> AST binder sig l -> AST binder sig l -> Bool
forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
goAlpha Scope l
scope1 AST binder sig l
body1 AST binder sig l
AST binder sig l
body2
Foil.RenameLeftNameBinder NameBinders n l
_ NameBinder n l -> NameBinder n l
rename1to2 ->
case binder n l -> DistinctEvidence l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct binder n l
binder2 of
DistinctEvidence l
Foil.Distinct ->
let scope2 :: Scope l
scope2 = binder n l -> Scope n -> Scope l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> Scope n -> Scope l
Foil.extendScopePattern binder n l
binder2 Scope n
scope
pairs :: [(Int, Int)]
pairs = [ (Name l -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name l
x, (NameBinder n l -> NameBinder n l) -> Name l -> Int
forall (n :: S) (a :: S) (b :: S).
(NameBinder n a -> NameBinder n b) -> Name a -> Int
renamedId NameBinder n l -> NameBinder n l
rename1to2 Name l
x)
| Name l
x <- binder n l -> [Name l]
forall (pattern :: S -> S -> *) (n :: S) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> [Name l]
Foil.namesOfPattern binder n l
binder1 ]
in [(Int, Int)]
-> Scope l -> AST binder sig l -> AST binder sig l -> Bool
forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
[(Int, Int)]
-> Scope m -> AST binder sig l1 -> AST binder sig l2 -> Bool
enterEnv [(Int, Int)]
pairs Scope l
scope2 AST binder sig l
body1 AST binder sig l
body2
Foil.RenameRightNameBinder NameBinders n l
_ NameBinder n l -> NameBinder n l
rename2to1 ->
case binder n l -> DistinctEvidence l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct binder n l
binder1 of
DistinctEvidence l
Foil.Distinct ->
let scope1 :: Scope l
scope1 = binder n l -> Scope n -> Scope l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> Scope n -> Scope l
Foil.extendScopePattern binder n l
binder1 Scope n
scope
pairs :: [(Int, Int)]
pairs = [ ((NameBinder n l -> NameBinder n l) -> Name l -> Int
forall (n :: S) (a :: S) (b :: S).
(NameBinder n a -> NameBinder n b) -> Name a -> Int
renamedId NameBinder n l -> NameBinder n l
rename2to1 Name l
y, Name l -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name l
y)
| Name l
y <- binder n l -> [Name l]
forall (pattern :: S -> S -> *) (n :: S) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> [Name l]
Foil.namesOfPattern binder n l
binder2 ]
in [(Int, Int)]
-> Scope l -> AST binder sig l -> AST binder sig l -> Bool
forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
[(Int, Int)]
-> Scope m -> AST binder sig l1 -> AST binder sig l2 -> Bool
enterEnv [(Int, Int)]
pairs Scope l
scope1 AST binder sig l
body1 AST binder sig l
body2
Foil.RenameBothBinders NameBinders n lr
binder' NameBinder n l -> NameBinder n lr
rename1 NameBinder n l -> NameBinder n lr
rename2 ->
case NameBinders n lr -> DistinctEvidence lr
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct NameBinders n lr
binder' of
DistinctEvidence lr
Foil.Distinct ->
let scope' :: Scope lr
scope' = NameBinders n lr -> Scope n -> Scope lr
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> Scope n -> Scope l
Foil.extendScopePattern NameBinders n lr
binder' Scope n
scope
leftU :: IntMap Int
leftU = [(Int, Int)] -> IntMap Int
forall a. [(Int, a)] -> IntMap a
IntMap.fromList
[ ((NameBinder n l -> NameBinder n lr) -> Name l -> Int
forall (n :: S) (a :: S) (b :: S).
(NameBinder n a -> NameBinder n b) -> Name a -> Int
renamedId NameBinder n l -> NameBinder n lr
rename1 Name l
x, Name l -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name l
x)
| Name l
x <- binder n l -> [Name l]
forall (pattern :: S -> S -> *) (n :: S) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> [Name l]
Foil.namesOfPattern binder n l
binder1 ]
rightU :: IntMap Int
rightU = [(Int, Int)] -> IntMap Int
forall a. [(Int, a)] -> IntMap a
IntMap.fromList
[ ((NameBinder n l -> NameBinder n lr) -> Name l -> Int
forall (n :: S) (a :: S) (b :: S).
(NameBinder n a -> NameBinder n b) -> Name a -> Int
renamedId NameBinder n l -> NameBinder n lr
rename2 Name l
y, Name l -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name l
y)
| Name l
y <- binder n l -> [Name l]
forall (pattern :: S -> S -> *) (n :: S) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> [Name l]
Foil.namesOfPattern binder n l
binder2 ]
pairs :: [(Int, Int)]
pairs = IntMap (Int, Int) -> [(Int, Int)]
forall a. IntMap a -> [a]
IntMap.elems ((Int -> Int -> (Int, Int))
-> IntMap Int -> IntMap Int -> IntMap (Int, Int)
forall a b c. (a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
IntMap.intersectionWith (,) IntMap Int
leftU IntMap Int
rightU)
in [(Int, Int)]
-> Scope lr -> AST binder sig l -> AST binder sig l -> Bool
forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
[(Int, Int)]
-> Scope m -> AST binder sig l1 -> AST binder sig l2 -> Bool
enterEnv [(Int, Int)]
pairs Scope lr
scope' AST binder sig l
body1 AST binder sig l
body2
UnifyNameBinders binder n l l
Foil.NotUnifiable -> Bool
False
where
enterEnv
:: forall m l1 l2. Foil.Distinct m
=> [(Int, Int)] -> Foil.Scope m
-> AST binder sig l1 -> AST binder sig l2 -> Bool
enterEnv :: forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
[(Int, Int)]
-> Scope m -> AST binder sig l1 -> AST binder sig l2 -> Bool
enterEnv [(Int, Int)]
pairs Scope m
scope' = Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
goPairs Int
0 IntMap Int
forall a. IntMap a
IntMap.empty IntMap Int
forall a. IntMap a
IntMap.empty [(Int, Int)]
pairs Scope m
scope'
goPairs
:: forall m l1 l2. Foil.Distinct m
=> Int -> IntMap Int -> IntMap Int -> [(Int, Int)]
-> Foil.Scope m
-> AST binder sig l1 -> AST binder sig l2 -> Bool
goPairs :: forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
goPairs Int
lvl IntMap Int
envL IntMap Int
envR [(Int, Int)]
pairs Scope m
scope AST binder sig l1
body1 AST binder sig l2
body2 = case [(Int, Int)]
pairs of
[] -> Int
-> IntMap Int
-> IntMap Int
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> AST binder sig n1
-> AST binder sig n2
-> Bool
goEnv Int
lvl IntMap Int
envL IntMap Int
envR Scope m
scope AST binder sig l1
body1 AST binder sig l2
body2
((Int
x, Int
y) : [(Int, Int)]
rest)
| Int
x Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
y -> Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
goPairs Int
lvl (Int -> IntMap Int -> IntMap Int
forall a. Int -> IntMap a -> IntMap a
IntMap.delete Int
x IntMap Int
envL) (Int -> IntMap Int -> IntMap Int
forall a. Int -> IntMap a -> IntMap a
IntMap.delete Int
y IntMap Int
envR) [(Int, Int)]
rest Scope m
scope AST binder sig l1
body1 AST binder sig l2
body2
| Bool
otherwise -> Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
goPairs (Int
lvl Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int -> Int -> IntMap Int -> IntMap Int
forall a. Int -> a -> IntMap a -> IntMap a
IntMap.insert Int
x Int
lvl IntMap Int
envL) (Int -> Int -> IntMap Int -> IntMap Int
forall a. Int -> a -> IntMap a -> IntMap a
IntMap.insert Int
y Int
lvl IntMap Int
envR) [(Int, Int)]
rest Scope m
scope AST binder sig l1
body1 AST binder sig l2
body2
goEnv
:: forall n n1 n2. Foil.Distinct n
=> Int
-> IntMap Int
-> IntMap Int
-> Foil.Scope n
-> AST binder sig n1
-> AST binder sig n2
-> Bool
goEnv :: forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> AST binder sig n1
-> AST binder sig n2
-> Bool
goEnv Int
lvl IntMap Int
envL IntMap Int
envR Scope n
scope AST binder sig n1
t1 AST binder sig n2
t2
| IntMap Int -> Bool
forall a. IntMap a -> Bool
IntMap.null IntMap Int
envL Bool -> Bool -> Bool
&& IntMap Int -> Bool
forall a. IntMap a -> Bool
IntMap.null IntMap Int
envR =
Scope n -> AST binder sig n -> AST binder sig n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
goAlpha Scope n
scope (AST binder sig n1 -> AST binder sig n
forall a b. a -> b
unsafeCoerce AST binder sig n1
t1 :: AST binder sig n) (AST binder sig n2 -> AST binder sig n
forall a b. a -> b
unsafeCoerce AST binder sig n2
t2 :: AST binder sig n)
| Bool
otherwise = case (AST binder sig n1
t1, AST binder sig n2
t2) of
(Var Name n1
x, Var Name n2
y) ->
case (Int -> IntMap Int -> Maybe Int
forall a. Int -> IntMap a -> Maybe a
IntMap.lookup (Name n1 -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name n1
x) IntMap Int
envL, Int -> IntMap Int -> Maybe Int
forall a. Int -> IntMap a -> Maybe a
IntMap.lookup (Name n2 -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name n2
y) IntMap Int
envR) of
(Just Int
k1, Just Int
k2) -> Int
k1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
k2
(Maybe Int
Nothing, Maybe Int
Nothing) -> Name n1 -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name n1
x Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Name n2 -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name n2
y
(Maybe Int, Maybe Int)
_ -> Bool
False
(Node sig (ScopedAST binder sig n1) (AST binder sig n1)
l, Node sig (ScopedAST binder sig n2) (AST binder sig n2)
r) ->
case (ScopedAST binder sig n1 -> ScopedAST binder sig n2 -> Maybe ())
-> (AST binder sig n1 -> AST binder sig n2 -> Maybe ())
-> sig (ScopedAST binder sig n1) (AST binder sig n1)
-> sig (ScopedAST binder sig n2) (AST binder sig n2)
-> Maybe (sig () ())
forall (f :: * -> * -> *) a a' a'' b b' b''.
ZipMatchK f =>
(a -> a' -> Maybe a'')
-> (b -> b' -> Maybe b'') -> f a b -> f a' b' -> Maybe (f a'' b'')
zipMatchWith2
((ScopedAST binder sig n2 -> Bool)
-> ScopedAST binder sig n2 -> Maybe ()
forall {t}. (t -> Bool) -> t -> Maybe ()
unit ((ScopedAST binder sig n2 -> Bool)
-> ScopedAST binder sig n2 -> Maybe ())
-> (ScopedAST binder sig n1 -> ScopedAST binder sig n2 -> Bool)
-> ScopedAST binder sig n1
-> ScopedAST binder sig n2
-> Maybe ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> ScopedAST binder sig n1
-> ScopedAST binder sig n2
-> Bool
forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> ScopedAST binder sig n1
-> ScopedAST binder sig n2
-> Bool
goScopedEnv Int
lvl IntMap Int
envL IntMap Int
envR Scope n
scope)
((AST binder sig n2 -> Bool) -> AST binder sig n2 -> Maybe ()
forall {t}. (t -> Bool) -> t -> Maybe ()
unit ((AST binder sig n2 -> Bool) -> AST binder sig n2 -> Maybe ())
-> (AST binder sig n1 -> AST binder sig n2 -> Bool)
-> AST binder sig n1
-> AST binder sig n2
-> Maybe ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> AST binder sig n1
-> AST binder sig n2
-> Bool
forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> AST binder sig n1
-> AST binder sig n2
-> Bool
goEnv Int
lvl IntMap Int
envL IntMap Int
envR Scope n
scope) sig (ScopedAST binder sig n1) (AST binder sig n1)
l sig (ScopedAST binder sig n2) (AST binder sig n2)
r of
Maybe (sig () ())
Nothing -> Bool
False
Just sig () ()
_ -> Bool
True
(AST binder sig n1, AST binder sig n2)
_ -> Bool
False
where
unit :: (t -> Bool) -> t -> Maybe ()
unit t -> Bool
f t
x = if t -> Bool
f t
x then () -> Maybe ()
forall a. a -> Maybe a
Just () else Maybe ()
forall a. Maybe a
Nothing
goScopedEnv
:: forall n n1 n2. Foil.Distinct n
=> Int
-> IntMap Int
-> IntMap Int
-> Foil.Scope n
-> ScopedAST binder sig n1
-> ScopedAST binder sig n2
-> Bool
goScopedEnv :: forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> ScopedAST binder sig n1
-> ScopedAST binder sig n2
-> Bool
goScopedEnv Int
lvl IntMap Int
envL IntMap Int
envR Scope n
scope ScopedAST binder sig n1
scoped1 ScopedAST binder sig n2
scoped2 =
case (ScopedAST binder sig n1 -> ScopedAST binder sig n
forall a b. a -> b
unsafeCoerce ScopedAST binder sig n1
scoped1 :: ScopedAST binder sig n, ScopedAST binder sig n2 -> ScopedAST binder sig n
forall a b. a -> b
unsafeCoerce ScopedAST binder sig n2
scoped2 :: ScopedAST binder sig n) of
(ScopedAST binder n l
binder1 AST binder sig l
body1, ScopedAST binder n l
binder2 AST binder sig l
body2) ->
case Scope n
-> binder n l -> binder n l -> UnifyNameBinders binder n l l
forall (n :: S) (l :: S) (r :: S).
Distinct n =>
Scope n
-> binder n l -> binder n r -> UnifyNameBinders binder n l r
forall (pattern :: S -> S -> *) (n :: S) (l :: S) (r :: S).
(UnifiablePattern pattern, Distinct n) =>
Scope n
-> pattern n l -> pattern n r -> UnifyNameBinders pattern n l r
Foil.unifyPatternsIn Scope n
scope binder n l
binder1 binder n l
binder2 of
Foil.SameNameBinders{} ->
case binder n l -> DistinctEvidence l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct binder n l
binder1 of
DistinctEvidence l
Foil.Distinct ->
let scope' :: Scope l
scope' = binder n l -> Scope n -> Scope l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> Scope n -> Scope l
Foil.extendScopePattern binder n l
binder1 Scope n
scope
names :: [Int]
names = (Name l -> Int) -> [Name l] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Name l -> Int
forall (l :: S). Name l -> Int
Foil.nameId (binder n l -> [Name l]
forall (pattern :: S -> S -> *) (n :: S) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> [Name l]
Foil.namesOfPattern binder n l
binder1)
envL' :: IntMap Int
envL' = [Int] -> IntMap Int -> IntMap Int
forall {a}. [Int] -> IntMap a -> IntMap a
deleteAll [Int]
names IntMap Int
envL
envR' :: IntMap Int
envR' = [Int] -> IntMap Int -> IntMap Int
forall {a}. [Int] -> IntMap a -> IntMap a
deleteAll [Int]
names IntMap Int
envR
in Int
-> IntMap Int
-> IntMap Int
-> Scope l
-> AST binder sig l
-> AST binder sig l
-> Bool
forall (n :: S) (n1 :: S) (n2 :: S).
Distinct n =>
Int
-> IntMap Int
-> IntMap Int
-> Scope n
-> AST binder sig n1
-> AST binder sig n2
-> Bool
goEnv Int
lvl IntMap Int
envL' IntMap Int
envR' Scope l
scope' AST binder sig l
body1 AST binder sig l
body2
Foil.RenameLeftNameBinder NameBinders n l
_ NameBinder n l -> NameBinder n l
rename1to2 ->
case binder n l -> DistinctEvidence l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct binder n l
binder2 of
DistinctEvidence l
Foil.Distinct ->
let scope' :: Scope l
scope' = binder n l -> Scope n -> Scope l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> Scope n -> Scope l
Foil.extendScopePattern binder n l
binder2 Scope n
scope
pairs :: [(Int, Int)]
pairs = [ (Name l -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name l
x, (NameBinder n l -> NameBinder n l) -> Name l -> Int
forall (n :: S) (a :: S) (b :: S).
(NameBinder n a -> NameBinder n b) -> Name a -> Int
renamedId NameBinder n l -> NameBinder n l
rename1to2 Name l
x)
| Name l
x <- binder n l -> [Name l]
forall (pattern :: S -> S -> *) (n :: S) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> [Name l]
Foil.namesOfPattern binder n l
binder1 ]
in Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope l
-> AST binder sig l
-> AST binder sig l
-> Bool
forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
goPairs Int
lvl IntMap Int
envL IntMap Int
envR [(Int, Int)]
pairs Scope l
scope' AST binder sig l
body1 AST binder sig l
body2
Foil.RenameRightNameBinder NameBinders n l
_ NameBinder n l -> NameBinder n l
rename2to1 ->
case binder n l -> DistinctEvidence l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct binder n l
binder1 of
DistinctEvidence l
Foil.Distinct ->
let scope' :: Scope l
scope' = binder n l -> Scope n -> Scope l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> Scope n -> Scope l
Foil.extendScopePattern binder n l
binder1 Scope n
scope
pairs :: [(Int, Int)]
pairs = [ ((NameBinder n l -> NameBinder n l) -> Name l -> Int
forall (n :: S) (a :: S) (b :: S).
(NameBinder n a -> NameBinder n b) -> Name a -> Int
renamedId NameBinder n l -> NameBinder n l
rename2to1 Name l
y, Name l -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name l
y)
| Name l
y <- binder n l -> [Name l]
forall (pattern :: S -> S -> *) (n :: S) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> [Name l]
Foil.namesOfPattern binder n l
binder2 ]
in Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope l
-> AST binder sig l
-> AST binder sig l
-> Bool
forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
goPairs Int
lvl IntMap Int
envL IntMap Int
envR [(Int, Int)]
pairs Scope l
scope' AST binder sig l
body1 AST binder sig l
body2
Foil.RenameBothBinders NameBinders n lr
binder' NameBinder n l -> NameBinder n lr
rename1 NameBinder n l -> NameBinder n lr
rename2 ->
case NameBinders n lr -> DistinctEvidence lr
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct NameBinders n lr
binder' of
DistinctEvidence lr
Foil.Distinct ->
let scope' :: Scope lr
scope' = NameBinders n lr -> Scope n -> Scope lr
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> Scope n -> Scope l
Foil.extendScopePattern NameBinders n lr
binder' Scope n
scope
leftU :: IntMap Int
leftU = [(Int, Int)] -> IntMap Int
forall a. [(Int, a)] -> IntMap a
IntMap.fromList
[ ((NameBinder n l -> NameBinder n lr) -> Name l -> Int
forall (n :: S) (a :: S) (b :: S).
(NameBinder n a -> NameBinder n b) -> Name a -> Int
renamedId NameBinder n l -> NameBinder n lr
rename1 Name l
x, Name l -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name l
x)
| Name l
x <- binder n l -> [Name l]
forall (pattern :: S -> S -> *) (n :: S) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> [Name l]
Foil.namesOfPattern binder n l
binder1 ]
rightU :: IntMap Int
rightU = [(Int, Int)] -> IntMap Int
forall a. [(Int, a)] -> IntMap a
IntMap.fromList
[ ((NameBinder n l -> NameBinder n lr) -> Name l -> Int
forall (n :: S) (a :: S) (b :: S).
(NameBinder n a -> NameBinder n b) -> Name a -> Int
renamedId NameBinder n l -> NameBinder n lr
rename2 Name l
y, Name l -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name l
y)
| Name l
y <- binder n l -> [Name l]
forall (pattern :: S -> S -> *) (n :: S) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> [Name l]
Foil.namesOfPattern binder n l
binder2 ]
pairs :: [(Int, Int)]
pairs = IntMap (Int, Int) -> [(Int, Int)]
forall a. IntMap a -> [a]
IntMap.elems ((Int -> Int -> (Int, Int))
-> IntMap Int -> IntMap Int -> IntMap (Int, Int)
forall a b c. (a -> b -> c) -> IntMap a -> IntMap b -> IntMap c
IntMap.intersectionWith (,) IntMap Int
leftU IntMap Int
rightU)
in Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope lr
-> AST binder sig l
-> AST binder sig l
-> Bool
forall (m :: S) (l1 :: S) (l2 :: S).
Distinct m =>
Int
-> IntMap Int
-> IntMap Int
-> [(Int, Int)]
-> Scope m
-> AST binder sig l1
-> AST binder sig l2
-> Bool
goPairs Int
lvl IntMap Int
envL IntMap Int
envR [(Int, Int)]
pairs Scope lr
scope' AST binder sig l
body1 AST binder sig l
body2
UnifyNameBinders binder n l l
Foil.NotUnifiable -> Bool
False
where
deleteAll :: [Int] -> IntMap a -> IntMap a
deleteAll [Int]
names IntMap a
env = case [Int]
names of
[] -> IntMap a
env
(Int
i : [Int]
is) -> [Int] -> IntMap a -> IntMap a
deleteAll [Int]
is (Int -> IntMap a -> IntMap a
forall a. Int -> IntMap a -> IntMap a
IntMap.delete Int
i IntMap a
env)
{-# INLINE unsafeEqAST #-}
unsafeEqAST
:: (ZipMatchK sig, Foil.UnifiablePattern binder, Foil.Distinct n, Foil.Distinct l)
=> AST binder sig n
-> AST binder sig l
-> Bool
unsafeEqAST :: forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S)
(l :: S).
(ZipMatchK sig, UnifiablePattern binder, Distinct n, Distinct l) =>
AST binder sig n -> AST binder sig l -> Bool
unsafeEqAST = EqLoops binder sig
-> forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
AST binder sig n -> AST binder sig l -> Bool
forall (binder :: S -> S -> *) (sig :: * -> * -> *).
EqLoops binder sig
-> forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
AST binder sig n -> AST binder sig l -> Bool
eqAST EqLoops binder sig
forall (sig :: * -> * -> *) (binder :: S -> S -> *).
(ZipMatchK sig, UnifiablePattern binder) =>
EqLoops binder sig
unsafeEqLoops
{-# INLINE unsafeEqScopedAST #-}
unsafeEqScopedAST
:: (ZipMatchK sig, Foil.UnifiablePattern binder, Foil.Distinct n, Foil.Distinct l)
=> ScopedAST binder sig n
-> ScopedAST binder sig l
-> Bool
unsafeEqScopedAST :: forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S)
(l :: S).
(ZipMatchK sig, UnifiablePattern binder, Distinct n, Distinct l) =>
ScopedAST binder sig n -> ScopedAST binder sig l -> Bool
unsafeEqScopedAST = EqLoops binder sig
-> forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
ScopedAST binder sig n -> ScopedAST binder sig l -> Bool
forall (binder :: S -> S -> *) (sig :: * -> * -> *).
EqLoops binder sig
-> forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
ScopedAST binder sig n -> ScopedAST binder sig l -> Bool
eqScopedAST EqLoops binder sig
forall (sig :: * -> * -> *) (binder :: S -> S -> *).
(ZipMatchK sig, UnifiablePattern binder) =>
EqLoops binder sig
unsafeEqLoops
data EqLoops binder sig = EqLoops
{ forall (binder :: S -> S -> *) (sig :: * -> * -> *).
EqLoops binder sig
-> forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
AST binder sig n -> AST binder sig l -> Bool
eqAST :: forall n l. (Foil.Distinct n, Foil.Distinct l) => AST binder sig n -> AST binder sig l -> Bool
, forall (binder :: S -> S -> *) (sig :: * -> * -> *).
EqLoops binder sig
-> forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
ScopedAST binder sig n -> ScopedAST binder sig l -> Bool
eqScopedAST :: forall n l. (Foil.Distinct n, Foil.Distinct l) => ScopedAST binder sig n -> ScopedAST binder sig l -> Bool
}
{-# INLINE unsafeEqLoops #-}
unsafeEqLoops
:: forall sig binder. (ZipMatchK sig, Foil.UnifiablePattern binder)
=> EqLoops binder sig
unsafeEqLoops :: forall (sig :: * -> * -> *) (binder :: S -> S -> *).
(ZipMatchK sig, UnifiablePattern binder) =>
EqLoops binder sig
unsafeEqLoops = (forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
AST binder sig n -> AST binder sig l -> Bool)
-> (forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
ScopedAST binder sig n -> ScopedAST binder sig l -> Bool)
-> EqLoops binder sig
forall (binder :: S -> S -> *) (sig :: * -> * -> *).
(forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
AST binder sig n -> AST binder sig l -> Bool)
-> (forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
ScopedAST binder sig n -> ScopedAST binder sig l -> Bool)
-> EqLoops binder sig
EqLoops AST binder sig n -> AST binder sig l -> Bool
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
AST binder sig n -> AST binder sig l -> Bool
goAST ScopedAST binder sig n -> ScopedAST binder sig l -> Bool
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
ScopedAST binder sig n -> ScopedAST binder sig l -> Bool
goScoped
where
goAST :: forall n l. (Foil.Distinct n, Foil.Distinct l) => AST binder sig n -> AST binder sig l -> Bool
goAST :: forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
AST binder sig n -> AST binder sig l -> Bool
goAST (Var Name n
x) (Var Name l
y) = Name n -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name n
x Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Name l -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name l
y
goAST (Node sig (ScopedAST binder sig n) (AST binder sig n)
t1) (Node sig (ScopedAST binder sig l) (AST binder sig l)
t2) =
case (ScopedAST binder sig n -> ScopedAST binder sig l -> Maybe ())
-> (AST binder sig n -> AST binder sig l -> Maybe ())
-> sig (ScopedAST binder sig n) (AST binder sig n)
-> sig (ScopedAST binder sig l) (AST binder sig l)
-> Maybe (sig () ())
forall (f :: * -> * -> *) a a' a'' b b' b''.
ZipMatchK f =>
(a -> a' -> Maybe a'')
-> (b -> b' -> Maybe b'') -> f a b -> f a' b' -> Maybe (f a'' b'')
zipMatchWith2 ((ScopedAST binder sig l -> Bool)
-> ScopedAST binder sig l -> Maybe ()
forall {t}. (t -> Bool) -> t -> Maybe ()
unit ((ScopedAST binder sig l -> Bool)
-> ScopedAST binder sig l -> Maybe ())
-> (ScopedAST binder sig n -> ScopedAST binder sig l -> Bool)
-> ScopedAST binder sig n
-> ScopedAST binder sig l
-> Maybe ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScopedAST binder sig n -> ScopedAST binder sig l -> Bool
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
ScopedAST binder sig n -> ScopedAST binder sig l -> Bool
goScoped) ((AST binder sig l -> Bool) -> AST binder sig l -> Maybe ()
forall {t}. (t -> Bool) -> t -> Maybe ()
unit ((AST binder sig l -> Bool) -> AST binder sig l -> Maybe ())
-> (AST binder sig n -> AST binder sig l -> Bool)
-> AST binder sig n
-> AST binder sig l
-> Maybe ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AST binder sig n -> AST binder sig l -> Bool
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
AST binder sig n -> AST binder sig l -> Bool
goAST) sig (ScopedAST binder sig n) (AST binder sig n)
t1 sig (ScopedAST binder sig l) (AST binder sig l)
t2 of
Maybe (sig () ())
Nothing -> Bool
False
Just sig () ()
_ -> Bool
True
goAST AST binder sig n
_ AST binder sig l
_ = Bool
False
goScoped :: forall n l. (Foil.Distinct n, Foil.Distinct l) => ScopedAST binder sig n -> ScopedAST binder sig l -> Bool
goScoped :: forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
ScopedAST binder sig n -> ScopedAST binder sig l -> Bool
goScoped (ScopedAST binder n l
binder1 AST binder sig l
body1) (ScopedAST binder l l
binder2 AST binder sig l
body2) = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and
[ binder n l -> binder l l -> Bool
forall (pattern :: S -> S -> *) (n :: S) (l :: S) (n' :: S)
(l' :: S).
(UnifiablePattern pattern, Distinct n) =>
pattern n l -> pattern n' l' -> Bool
Foil.unsafeEqPattern binder n l
binder1 binder l l
binder2
, case (binder n l -> DistinctEvidence l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct binder n l
binder1, binder l l -> DistinctEvidence l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct binder l l
binder2) of
(DistinctEvidence l
Foil.Distinct, DistinctEvidence l
Foil.Distinct) -> AST binder sig l
body1 AST binder sig l -> AST binder sig l -> Bool
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
AST binder sig n -> AST binder sig l -> Bool
`goAST` AST binder sig l
body2
]
unit :: (t -> Bool) -> t -> Maybe ()
unit t -> Bool
f t
x = if t -> Bool
f t
x then () -> Maybe ()
forall a. a -> Maybe a
Just () else Maybe ()
forall a. Maybe a
Nothing
{-# INLINE unsafeEqASTIn #-}
unsafeEqASTIn
:: (ZipMatchK sig, Foil.UnifiablePattern binder, Foil.Distinct n)
=> Foil.Scope n
-> AST binder sig n
-> AST binder sig n
-> Bool
unsafeEqASTIn :: forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
(ZipMatchK sig, UnifiablePattern binder, Distinct n) =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
unsafeEqASTIn = EqInLoops binder sig
-> forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
forall (binder :: S -> S -> *) (sig :: * -> * -> *).
EqInLoops binder sig
-> forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
eqASTIn EqInLoops binder sig
forall (sig :: * -> * -> *) (binder :: S -> S -> *).
(ZipMatchK sig, UnifiablePattern binder) =>
EqInLoops binder sig
unsafeEqInLoops
{-# INLINE unsafeEqScopedASTIn #-}
unsafeEqScopedASTIn
:: (ZipMatchK sig, Foil.UnifiablePattern binder, Foil.Distinct n)
=> Foil.Scope n
-> ScopedAST binder sig n
-> ScopedAST binder sig n
-> Bool
unsafeEqScopedASTIn :: forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
(ZipMatchK sig, UnifiablePattern binder, Distinct n) =>
Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
unsafeEqScopedASTIn = EqInLoops binder sig
-> forall (n :: S).
Distinct n =>
Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
forall (binder :: S -> S -> *) (sig :: * -> * -> *).
EqInLoops binder sig
-> forall (n :: S).
Distinct n =>
Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
eqScopedASTIn EqInLoops binder sig
forall (sig :: * -> * -> *) (binder :: S -> S -> *).
(ZipMatchK sig, UnifiablePattern binder) =>
EqInLoops binder sig
unsafeEqInLoops
data EqInLoops binder sig = EqInLoops
{ forall (binder :: S -> S -> *) (sig :: * -> * -> *).
EqInLoops binder sig
-> forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
eqASTIn :: forall n. Foil.Distinct n => Foil.Scope n -> AST binder sig n -> AST binder sig n -> Bool
, forall (binder :: S -> S -> *) (sig :: * -> * -> *).
EqInLoops binder sig
-> forall (n :: S).
Distinct n =>
Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
eqScopedASTIn :: forall n. Foil.Distinct n => Foil.Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
}
{-# INLINE unsafeEqInLoops #-}
unsafeEqInLoops
:: forall sig binder. (ZipMatchK sig, Foil.UnifiablePattern binder)
=> EqInLoops binder sig
unsafeEqInLoops :: forall (sig :: * -> * -> *) (binder :: S -> S -> *).
(ZipMatchK sig, UnifiablePattern binder) =>
EqInLoops binder sig
unsafeEqInLoops = (forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool)
-> (forall (n :: S).
Distinct n =>
Scope n
-> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool)
-> EqInLoops binder sig
forall (binder :: S -> S -> *) (sig :: * -> * -> *).
(forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool)
-> (forall (n :: S).
Distinct n =>
Scope n
-> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool)
-> EqInLoops binder sig
EqInLoops Scope n -> AST binder sig n -> AST binder sig n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
goAST Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
goScoped
where
goAST :: forall n. Foil.Distinct n => Foil.Scope n -> AST binder sig n -> AST binder sig n -> Bool
goAST :: forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
goAST Scope n
_ (Var Name n
x) (Var Name n
y) = Name n
x Name n -> Name n -> Bool
forall a. Eq a => a -> a -> Bool
== Name n
y
goAST Scope n
scope (Node sig (ScopedAST binder sig n) (AST binder sig n)
t1) (Node sig (ScopedAST binder sig n) (AST binder sig n)
t2) =
case (ScopedAST binder sig n -> ScopedAST binder sig n -> Maybe ())
-> (AST binder sig n -> AST binder sig n -> Maybe ())
-> sig (ScopedAST binder sig n) (AST binder sig n)
-> sig (ScopedAST binder sig n) (AST binder sig n)
-> Maybe (sig () ())
forall (f :: * -> * -> *) a a' a'' b b' b''.
ZipMatchK f =>
(a -> a' -> Maybe a'')
-> (b -> b' -> Maybe b'') -> f a b -> f a' b' -> Maybe (f a'' b'')
zipMatchWith2 ((ScopedAST binder sig n -> Bool)
-> ScopedAST binder sig n -> Maybe ()
forall {t}. (t -> Bool) -> t -> Maybe ()
unit ((ScopedAST binder sig n -> Bool)
-> ScopedAST binder sig n -> Maybe ())
-> (ScopedAST binder sig n -> ScopedAST binder sig n -> Bool)
-> ScopedAST binder sig n
-> ScopedAST binder sig n
-> Maybe ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
goScoped Scope n
scope) ((AST binder sig n -> Bool) -> AST binder sig n -> Maybe ()
forall {t}. (t -> Bool) -> t -> Maybe ()
unit ((AST binder sig n -> Bool) -> AST binder sig n -> Maybe ())
-> (AST binder sig n -> AST binder sig n -> Bool)
-> AST binder sig n
-> AST binder sig n
-> Maybe ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Scope n -> AST binder sig n -> AST binder sig n -> Bool
forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
goAST Scope n
scope) sig (ScopedAST binder sig n) (AST binder sig n)
t1 sig (ScopedAST binder sig n) (AST binder sig n)
t2 of
Maybe (sig () ())
Nothing -> Bool
False
Just sig () ()
_ -> Bool
True
goAST Scope n
_ AST binder sig n
_ AST binder sig n
_ = Bool
False
goScoped :: forall n. Foil.Distinct n => Foil.Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
goScoped :: forall (n :: S).
Distinct n =>
Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
goScoped Scope n
scope (ScopedAST binder n l
binder1 AST binder sig l
body1) (ScopedAST binder n l
binder2 AST binder sig l
body2) =
case Scope n
-> binder n l -> binder n l -> UnifyNameBinders binder n l l
forall (n :: S) (l :: S) (r :: S).
Distinct n =>
Scope n
-> binder n l -> binder n r -> UnifyNameBinders binder n l r
forall (pattern :: S -> S -> *) (n :: S) (l :: S) (r :: S).
(UnifiablePattern pattern, Distinct n) =>
Scope n
-> pattern n l -> pattern n r -> UnifyNameBinders pattern n l r
Foil.unifyPatternsIn Scope n
scope binder n l
binder1 binder n l
binder2 of
Foil.SameNameBinders{} -> case binder n l -> DistinctEvidence l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct binder n l
binder1 of
DistinctEvidence l
Foil.Distinct -> Scope l -> AST binder sig l -> AST binder sig l -> Bool
forall (n :: S).
Distinct n =>
Scope n -> AST binder sig n -> AST binder sig n -> Bool
goAST (binder n l -> Scope n -> Scope l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> Scope n -> Scope l
Foil.extendScopePattern binder n l
binder1 Scope n
scope) AST binder sig l
body1 AST binder sig l
AST binder sig l
body2
UnifyNameBinders binder n l l
_ -> Bool
False
unit :: (t -> Bool) -> t -> Maybe ()
unit t -> Bool
f t
x = if t -> Bool
f t
x then () -> Maybe ()
forall a. a -> Maybe a
Just () else Maybe ()
forall a. Maybe a
Nothing
data UnresolvedName rawIdent = UnresolvedName
{ forall rawIdent. UnresolvedName rawIdent -> rawIdent
unresolvedIdent :: rawIdent
, forall rawIdent. UnresolvedName rawIdent -> [rawIdent]
unresolvedInScope :: [rawIdent]
} deriving (UnresolvedName rawIdent -> UnresolvedName rawIdent -> Bool
(UnresolvedName rawIdent -> UnresolvedName rawIdent -> Bool)
-> (UnresolvedName rawIdent -> UnresolvedName rawIdent -> Bool)
-> Eq (UnresolvedName rawIdent)
forall rawIdent.
Eq rawIdent =>
UnresolvedName rawIdent -> UnresolvedName rawIdent -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall rawIdent.
Eq rawIdent =>
UnresolvedName rawIdent -> UnresolvedName rawIdent -> Bool
== :: UnresolvedName rawIdent -> UnresolvedName rawIdent -> Bool
$c/= :: forall rawIdent.
Eq rawIdent =>
UnresolvedName rawIdent -> UnresolvedName rawIdent -> Bool
/= :: UnresolvedName rawIdent -> UnresolvedName rawIdent -> Bool
Eq, Int -> UnresolvedName rawIdent -> ShowS
[UnresolvedName rawIdent] -> ShowS
UnresolvedName rawIdent -> String
(Int -> UnresolvedName rawIdent -> ShowS)
-> (UnresolvedName rawIdent -> String)
-> ([UnresolvedName rawIdent] -> ShowS)
-> Show (UnresolvedName rawIdent)
forall rawIdent.
Show rawIdent =>
Int -> UnresolvedName rawIdent -> ShowS
forall rawIdent.
Show rawIdent =>
[UnresolvedName rawIdent] -> ShowS
forall rawIdent. Show rawIdent => UnresolvedName rawIdent -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall rawIdent.
Show rawIdent =>
Int -> UnresolvedName rawIdent -> ShowS
showsPrec :: Int -> UnresolvedName rawIdent -> ShowS
$cshow :: forall rawIdent. Show rawIdent => UnresolvedName rawIdent -> String
show :: UnresolvedName rawIdent -> String
$cshowList :: forall rawIdent.
Show rawIdent =>
[UnresolvedName rawIdent] -> ShowS
showList :: [UnresolvedName rawIdent] -> ShowS
Show, (forall a b. (a -> b) -> UnresolvedName a -> UnresolvedName b)
-> (forall a b. a -> UnresolvedName b -> UnresolvedName a)
-> Functor UnresolvedName
forall a b. a -> UnresolvedName b -> UnresolvedName a
forall a b. (a -> b) -> UnresolvedName a -> UnresolvedName b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> UnresolvedName a -> UnresolvedName b
fmap :: forall a b. (a -> b) -> UnresolvedName a -> UnresolvedName b
$c<$ :: forall a b. a -> UnresolvedName b -> UnresolvedName a
<$ :: forall a b. a -> UnresolvedName b -> UnresolvedName a
Functor, (forall m. Monoid m => UnresolvedName m -> m)
-> (forall m a. Monoid m => (a -> m) -> UnresolvedName a -> m)
-> (forall m a. Monoid m => (a -> m) -> UnresolvedName a -> m)
-> (forall a b. (a -> b -> b) -> b -> UnresolvedName a -> b)
-> (forall a b. (a -> b -> b) -> b -> UnresolvedName a -> b)
-> (forall b a. (b -> a -> b) -> b -> UnresolvedName a -> b)
-> (forall b a. (b -> a -> b) -> b -> UnresolvedName a -> b)
-> (forall a. (a -> a -> a) -> UnresolvedName a -> a)
-> (forall a. (a -> a -> a) -> UnresolvedName a -> a)
-> (forall rawIdent. UnresolvedName rawIdent -> [rawIdent])
-> (forall a. UnresolvedName a -> Bool)
-> (forall a. UnresolvedName a -> Int)
-> (forall a. Eq a => a -> UnresolvedName a -> Bool)
-> (forall a. Ord a => UnresolvedName a -> a)
-> (forall a. Ord a => UnresolvedName a -> a)
-> (forall a. Num a => UnresolvedName a -> a)
-> (forall a. Num a => UnresolvedName a -> a)
-> Foldable UnresolvedName
forall a. Eq a => a -> UnresolvedName a -> Bool
forall a. Num a => UnresolvedName a -> a
forall a. Ord a => UnresolvedName a -> a
forall m. Monoid m => UnresolvedName m -> m
forall a. UnresolvedName a -> Bool
forall a. UnresolvedName a -> Int
forall rawIdent. UnresolvedName rawIdent -> [rawIdent]
forall a. (a -> a -> a) -> UnresolvedName a -> a
forall m a. Monoid m => (a -> m) -> UnresolvedName a -> m
forall b a. (b -> a -> b) -> b -> UnresolvedName a -> b
forall a b. (a -> b -> b) -> b -> UnresolvedName a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => UnresolvedName m -> m
fold :: forall m. Monoid m => UnresolvedName m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> UnresolvedName a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> UnresolvedName a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> UnresolvedName a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> UnresolvedName a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> UnresolvedName a -> b
foldr :: forall a b. (a -> b -> b) -> b -> UnresolvedName a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> UnresolvedName a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> UnresolvedName a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> UnresolvedName a -> b
foldl :: forall b a. (b -> a -> b) -> b -> UnresolvedName a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> UnresolvedName a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> UnresolvedName a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> UnresolvedName a -> a
foldr1 :: forall a. (a -> a -> a) -> UnresolvedName a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> UnresolvedName a -> a
foldl1 :: forall a. (a -> a -> a) -> UnresolvedName a -> a
$ctoList :: forall rawIdent. UnresolvedName rawIdent -> [rawIdent]
toList :: forall rawIdent. UnresolvedName rawIdent -> [rawIdent]
$cnull :: forall a. UnresolvedName a -> Bool
null :: forall a. UnresolvedName a -> Bool
$clength :: forall a. UnresolvedName a -> Int
length :: forall a. UnresolvedName a -> Int
$celem :: forall a. Eq a => a -> UnresolvedName a -> Bool
elem :: forall a. Eq a => a -> UnresolvedName a -> Bool
$cmaximum :: forall a. Ord a => UnresolvedName a -> a
maximum :: forall a. Ord a => UnresolvedName a -> a
$cminimum :: forall a. Ord a => UnresolvedName a -> a
minimum :: forall a. Ord a => UnresolvedName a -> a
$csum :: forall a. Num a => UnresolvedName a -> a
sum :: forall a. Num a => UnresolvedName a -> a
$cproduct :: forall a. Num a => UnresolvedName a -> a
product :: forall a. Num a => UnresolvedName a -> a
Foldable, Functor UnresolvedName
Foldable UnresolvedName
(Functor UnresolvedName, Foldable UnresolvedName) =>
(forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> UnresolvedName a -> f (UnresolvedName b))
-> (forall (f :: * -> *) a.
Applicative f =>
UnresolvedName (f a) -> f (UnresolvedName a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> UnresolvedName a -> m (UnresolvedName b))
-> (forall (m :: * -> *) a.
Monad m =>
UnresolvedName (m a) -> m (UnresolvedName a))
-> Traversable UnresolvedName
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
UnresolvedName (m a) -> m (UnresolvedName a)
forall (f :: * -> *) a.
Applicative f =>
UnresolvedName (f a) -> f (UnresolvedName a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> UnresolvedName a -> m (UnresolvedName b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> UnresolvedName a -> f (UnresolvedName b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> UnresolvedName a -> f (UnresolvedName b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> UnresolvedName a -> f (UnresolvedName b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
UnresolvedName (f a) -> f (UnresolvedName a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
UnresolvedName (f a) -> f (UnresolvedName a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> UnresolvedName a -> m (UnresolvedName b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> UnresolvedName a -> m (UnresolvedName b)
$csequence :: forall (m :: * -> *) a.
Monad m =>
UnresolvedName (m a) -> m (UnresolvedName a)
sequence :: forall (m :: * -> *) a.
Monad m =>
UnresolvedName (m a) -> m (UnresolvedName a)
Traversable)
unresolvedNames
:: forall sig binder rawIdent rawTerm rawPattern rawScopedTerm n.
(Foil.Distinct n, Bifoldable sig, Ord rawIdent, Foil.CoSinkable binder)
=> (rawTerm -> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall x z. Foil.Distinct x
=> Foil.Scope x
-> Map rawIdent (Foil.Name x)
-> rawPattern
-> (forall y. Foil.DExt x y
=> binder x y
-> Map rawIdent (Foil.Name y)
-> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Foil.Scope n
-> Map rawIdent (Foil.Name n)
-> rawTerm
-> [UnresolvedName rawIdent]
unresolvedNames :: forall (sig :: * -> * -> *) (binder :: S -> S -> *) rawIdent
rawTerm rawPattern rawScopedTerm (n :: S).
(Distinct n, Bifoldable sig, Ord rawIdent, CoSinkable binder) =>
(rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Scope n
-> Map rawIdent (Name n)
-> rawTerm
-> [UnresolvedName rawIdent]
unresolvedNames rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)
toSig forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
fromRawPattern rawScopedTerm -> rawTerm
getScopedTerm Scope n
scope Map rawIdent (Name n)
names =
(rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Scope n
-> Map rawIdent (Name n)
-> Map rawIdent (AST binder sig n)
-> rawTerm
-> [UnresolvedName rawIdent]
forall (sig :: * -> * -> *) (binder :: S -> S -> *) rawIdent
rawTerm rawPattern rawScopedTerm (n :: S).
(Distinct n, Bifoldable sig, Ord rawIdent, CoSinkable binder) =>
(rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Scope n
-> Map rawIdent (Name n)
-> Map rawIdent (AST binder sig n)
-> rawTerm
-> [UnresolvedName rawIdent]
unresolvedNamesWith rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)
toSig Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
fromRawPattern rawScopedTerm -> rawTerm
getScopedTerm Scope n
scope Map rawIdent (Name n)
names Map rawIdent (AST binder sig n)
forall k a. Map k a
Map.empty
unresolvedNamesWith
:: forall sig binder rawIdent rawTerm rawPattern rawScopedTerm n.
(Foil.Distinct n, Bifoldable sig, Ord rawIdent, Foil.CoSinkable binder)
=> (rawTerm -> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall x z. Foil.Distinct x
=> Foil.Scope x
-> Map rawIdent (Foil.Name x)
-> rawPattern
-> (forall y. Foil.DExt x y
=> binder x y
-> Map rawIdent (Foil.Name y)
-> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Foil.Scope n
-> Map rawIdent (Foil.Name n)
-> Map rawIdent (AST binder sig n)
-> rawTerm
-> [UnresolvedName rawIdent]
unresolvedNamesWith :: forall (sig :: * -> * -> *) (binder :: S -> S -> *) rawIdent
rawTerm rawPattern rawScopedTerm (n :: S).
(Distinct n, Bifoldable sig, Ord rawIdent, CoSinkable binder) =>
(rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Scope n
-> Map rawIdent (Name n)
-> Map rawIdent (AST binder sig n)
-> rawTerm
-> [UnresolvedName rawIdent]
unresolvedNamesWith rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)
toSig forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
fromRawPattern rawScopedTerm -> rawTerm
getScopedTerm Scope n
scope0 Map rawIdent (Name n)
names0 Map rawIdent (AST binder sig n)
terms =
Scope n
-> Map rawIdent (Name n) -> rawTerm -> [UnresolvedName rawIdent]
forall (x :: S).
Distinct x =>
Scope x
-> Map rawIdent (Name x) -> rawTerm -> [UnresolvedName rawIdent]
go Scope n
scope0 Map rawIdent (Name n)
names0
where
constants :: [rawIdent]
constants = Map rawIdent (AST binder sig n) -> [rawIdent]
forall k a. Map k a -> [k]
Map.keys Map rawIdent (AST binder sig n)
terms
go :: forall x. Foil.Distinct x
=> Foil.Scope x -> Map rawIdent (Foil.Name x) -> rawTerm -> [UnresolvedName rawIdent]
go :: forall (x :: S).
Distinct x =>
Scope x
-> Map rawIdent (Name x) -> rawTerm -> [UnresolvedName rawIdent]
go Scope x
scope Map rawIdent (Name x)
names rawTerm
t = case rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)
toSig rawTerm
t of
Left rawIdent
x
| rawIdent -> Map rawIdent (Name x) -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member rawIdent
x Map rawIdent (Name x)
names Bool -> Bool -> Bool
|| rawIdent -> Map rawIdent (AST binder sig n) -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member rawIdent
x Map rawIdent (AST binder sig n)
terms -> []
| Bool
otherwise -> [rawIdent -> [rawIdent] -> UnresolvedName rawIdent
forall rawIdent. rawIdent -> [rawIdent] -> UnresolvedName rawIdent
UnresolvedName rawIdent
x (Map rawIdent (Name x) -> [rawIdent]
forall k a. Map k a -> [k]
Map.keys Map rawIdent (Name x)
names [rawIdent] -> [rawIdent] -> [rawIdent]
forall a. Semigroup a => a -> a -> a
<> [rawIdent]
constants)]
Right sig (rawPattern, rawScopedTerm) rawTerm
node -> ((rawPattern, rawScopedTerm) -> [UnresolvedName rawIdent])
-> (rawTerm -> [UnresolvedName rawIdent])
-> sig (rawPattern, rawScopedTerm) rawTerm
-> [UnresolvedName rawIdent]
forall m a b. Monoid m => (a -> m) -> (b -> m) -> sig a b -> m
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap (Scope x
-> Map rawIdent (Name x)
-> (rawPattern, rawScopedTerm)
-> [UnresolvedName rawIdent]
forall (x :: S).
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> (rawPattern, rawScopedTerm)
-> [UnresolvedName rawIdent]
goScoped Scope x
scope Map rawIdent (Name x)
names) (Scope x
-> Map rawIdent (Name x) -> rawTerm -> [UnresolvedName rawIdent]
forall (x :: S).
Distinct x =>
Scope x
-> Map rawIdent (Name x) -> rawTerm -> [UnresolvedName rawIdent]
go Scope x
scope Map rawIdent (Name x)
names) sig (rawPattern, rawScopedTerm) rawTerm
node
goScoped :: forall x. Foil.Distinct x
=> Foil.Scope x -> Map rawIdent (Foil.Name x)
-> (rawPattern, rawScopedTerm) -> [UnresolvedName rawIdent]
goScoped :: forall (x :: S).
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> (rawPattern, rawScopedTerm)
-> [UnresolvedName rawIdent]
goScoped Scope x
scope Map rawIdent (Name x)
names (rawPattern
pat, rawScopedTerm
scopedTerm) =
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> [UnresolvedName rawIdent])
-> [UnresolvedName rawIdent]
forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
fromRawPattern Scope x
scope Map rawIdent (Name x)
names rawPattern
pat ((forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> [UnresolvedName rawIdent])
-> [UnresolvedName rawIdent])
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> [UnresolvedName rawIdent])
-> [UnresolvedName rawIdent]
forall a b. (a -> b) -> a -> b
$ \binder x y
binder' Map rawIdent (Name y)
names' ->
Scope y
-> Map rawIdent (Name y) -> rawTerm -> [UnresolvedName rawIdent]
forall (x :: S).
Distinct x =>
Scope x
-> Map rawIdent (Name x) -> rawTerm -> [UnresolvedName rawIdent]
go (binder x y -> Scope x -> Scope y
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> Scope n -> Scope l
Foil.extendScopePattern binder x y
binder' Scope x
scope) Map rawIdent (Name y)
names' (rawScopedTerm -> rawTerm
getScopedTerm rawScopedTerm
scopedTerm)
tryConvertToAST
:: forall sig binder rawIdent rawTerm rawPattern rawScopedTerm n.
(Foil.Distinct n, Bitraversable sig, Ord rawIdent,
Foil.CoSinkable binder, Foil.SinkableK binder)
=> (rawTerm -> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall x z. Foil.Distinct x
=> Foil.Scope x
-> Map rawIdent (Foil.Name x)
-> rawPattern
-> (forall y. Foil.DExt x y
=> binder x y
-> Map rawIdent (Foil.Name y)
-> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Foil.Scope n
-> Map rawIdent (Foil.Name n)
-> rawTerm
-> Either (UnresolvedName rawIdent) (AST binder sig n)
tryConvertToAST :: forall (sig :: * -> * -> *) (binder :: S -> S -> *) rawIdent
rawTerm rawPattern rawScopedTerm (n :: S).
(Distinct n, Bitraversable sig, Ord rawIdent, CoSinkable binder,
SinkableK binder) =>
(rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Scope n
-> Map rawIdent (Name n)
-> rawTerm
-> Either (UnresolvedName rawIdent) (AST binder sig n)
tryConvertToAST rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)
toSig forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
fromRawPattern rawScopedTerm -> rawTerm
getScopedTerm Scope n
scope Map rawIdent (Name n)
names =
(rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Scope n
-> Map rawIdent (Name n)
-> Map rawIdent (AST binder sig n)
-> rawTerm
-> Either (UnresolvedName rawIdent) (AST binder sig n)
forall (sig :: * -> * -> *) (binder :: S -> S -> *) rawIdent
rawTerm rawPattern rawScopedTerm (n :: S).
(Distinct n, Bitraversable sig, Ord rawIdent, CoSinkable binder,
SinkableK binder) =>
(rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Scope n
-> Map rawIdent (Name n)
-> Map rawIdent (AST binder sig n)
-> rawTerm
-> Either (UnresolvedName rawIdent) (AST binder sig n)
tryConvertToASTWith rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)
toSig Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
fromRawPattern rawScopedTerm -> rawTerm
getScopedTerm Scope n
scope Map rawIdent (Name n)
names Map rawIdent (AST binder sig n)
forall k a. Map k a
Map.empty
tryConvertToASTWith
:: forall sig binder rawIdent rawTerm rawPattern rawScopedTerm n.
(Foil.Distinct n, Bitraversable sig, Ord rawIdent,
Foil.CoSinkable binder, Foil.SinkableK binder)
=> (rawTerm -> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall x z. Foil.Distinct x
=> Foil.Scope x
-> Map rawIdent (Foil.Name x)
-> rawPattern
-> (forall y. Foil.DExt x y
=> binder x y
-> Map rawIdent (Foil.Name y)
-> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Foil.Scope n
-> Map rawIdent (Foil.Name n)
-> Map rawIdent (AST binder sig n)
-> rawTerm
-> Either (UnresolvedName rawIdent) (AST binder sig n)
tryConvertToASTWith :: forall (sig :: * -> * -> *) (binder :: S -> S -> *) rawIdent
rawTerm rawPattern rawScopedTerm (n :: S).
(Distinct n, Bitraversable sig, Ord rawIdent, CoSinkable binder,
SinkableK binder) =>
(rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Scope n
-> Map rawIdent (Name n)
-> Map rawIdent (AST binder sig n)
-> rawTerm
-> Either (UnresolvedName rawIdent) (AST binder sig n)
tryConvertToASTWith rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)
toSig forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
fromRawPattern rawScopedTerm -> rawTerm
getScopedTerm = Scope n
-> Map rawIdent (Name n)
-> Map rawIdent (AST binder sig n)
-> rawTerm
-> Either (UnresolvedName rawIdent) (AST binder sig n)
forall (x :: S).
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> Map rawIdent (AST binder sig x)
-> rawTerm
-> Either (UnresolvedName rawIdent) (AST binder sig x)
go
where
go :: forall x. Foil.Distinct x
=> Foil.Scope x -> Map rawIdent (Foil.Name x)
-> Map rawIdent (AST binder sig x) -> rawTerm
-> Either (UnresolvedName rawIdent) (AST binder sig x)
go :: forall (x :: S).
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> Map rawIdent (AST binder sig x)
-> rawTerm
-> Either (UnresolvedName rawIdent) (AST binder sig x)
go Scope x
scope Map rawIdent (Name x)
names Map rawIdent (AST binder sig x)
terms rawTerm
t = case rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)
toSig rawTerm
t of
Left rawIdent
x -> case rawIdent -> Map rawIdent (Name x) -> Maybe (Name x)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup rawIdent
x Map rawIdent (Name x)
names of
Just Name x
name -> AST binder sig x
-> Either (UnresolvedName rawIdent) (AST binder sig x)
forall a b. b -> Either a b
Right (Name x -> AST binder sig x
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var Name x
name)
Maybe (Name x)
Nothing -> case rawIdent
-> Map rawIdent (AST binder sig x) -> Maybe (AST binder sig x)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup rawIdent
x Map rawIdent (AST binder sig x)
terms of
Just AST binder sig x
term -> AST binder sig x
-> Either (UnresolvedName rawIdent) (AST binder sig x)
forall a b. b -> Either a b
Right AST binder sig x
term
Maybe (AST binder sig x)
Nothing -> UnresolvedName rawIdent
-> Either (UnresolvedName rawIdent) (AST binder sig x)
forall a b. a -> Either a b
Left (rawIdent -> [rawIdent] -> UnresolvedName rawIdent
forall rawIdent. rawIdent -> [rawIdent] -> UnresolvedName rawIdent
UnresolvedName rawIdent
x (Map rawIdent (Name x) -> [rawIdent]
forall k a. Map k a -> [k]
Map.keys Map rawIdent (Name x)
names [rawIdent] -> [rawIdent] -> [rawIdent]
forall a. Semigroup a => a -> a -> a
<> Map rawIdent (AST binder sig x) -> [rawIdent]
forall k a. Map k a -> [k]
Map.keys Map rawIdent (AST binder sig x)
terms))
Right sig (rawPattern, rawScopedTerm) rawTerm
node ->
sig (ScopedAST binder sig x) (AST binder sig x) -> AST binder sig x
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig (ScopedAST binder sig n) (AST binder sig n) -> AST binder sig n
Node (sig (ScopedAST binder sig x) (AST binder sig x)
-> AST binder sig x)
-> Either
(UnresolvedName rawIdent)
(sig (ScopedAST binder sig x) (AST binder sig x))
-> Either (UnresolvedName rawIdent) (AST binder sig x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((rawPattern, rawScopedTerm)
-> Either (UnresolvedName rawIdent) (ScopedAST binder sig x))
-> (rawTerm -> Either (UnresolvedName rawIdent) (AST binder sig x))
-> sig (rawPattern, rawScopedTerm) rawTerm
-> Either
(UnresolvedName rawIdent)
(sig (ScopedAST binder sig x) (AST binder sig x))
forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> sig a b -> f (sig c d)
forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse (Scope x
-> Map rawIdent (Name x)
-> Map rawIdent (AST binder sig x)
-> (rawPattern, rawScopedTerm)
-> Either (UnresolvedName rawIdent) (ScopedAST binder sig x)
forall (x :: S).
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> Map rawIdent (AST binder sig x)
-> (rawPattern, rawScopedTerm)
-> Either (UnresolvedName rawIdent) (ScopedAST binder sig x)
goScoped Scope x
scope Map rawIdent (Name x)
names Map rawIdent (AST binder sig x)
terms) (Scope x
-> Map rawIdent (Name x)
-> Map rawIdent (AST binder sig x)
-> rawTerm
-> Either (UnresolvedName rawIdent) (AST binder sig x)
forall (x :: S).
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> Map rawIdent (AST binder sig x)
-> rawTerm
-> Either (UnresolvedName rawIdent) (AST binder sig x)
go Scope x
scope Map rawIdent (Name x)
names Map rawIdent (AST binder sig x)
terms) sig (rawPattern, rawScopedTerm) rawTerm
node
goScoped :: forall x. Foil.Distinct x
=> Foil.Scope x -> Map rawIdent (Foil.Name x)
-> Map rawIdent (AST binder sig x)
-> (rawPattern, rawScopedTerm)
-> Either (UnresolvedName rawIdent) (ScopedAST binder sig x)
goScoped :: forall (x :: S).
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> Map rawIdent (AST binder sig x)
-> (rawPattern, rawScopedTerm)
-> Either (UnresolvedName rawIdent) (ScopedAST binder sig x)
goScoped Scope x
scope Map rawIdent (Name x)
names Map rawIdent (AST binder sig x)
terms (rawPattern
pat, rawScopedTerm
scopedTerm) =
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y
-> Map rawIdent (Name y)
-> Either (UnresolvedName rawIdent) (ScopedAST binder sig x))
-> Either (UnresolvedName rawIdent) (ScopedAST binder sig x)
forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
fromRawPattern Scope x
scope Map rawIdent (Name x)
names rawPattern
pat ((forall (y :: S).
DExt x y =>
binder x y
-> Map rawIdent (Name y)
-> Either (UnresolvedName rawIdent) (ScopedAST binder sig x))
-> Either (UnresolvedName rawIdent) (ScopedAST binder sig x))
-> (forall (y :: S).
DExt x y =>
binder x y
-> Map rawIdent (Name y)
-> Either (UnresolvedName rawIdent) (ScopedAST binder sig x))
-> Either (UnresolvedName rawIdent) (ScopedAST binder sig x)
forall a b. (a -> b) -> a -> b
$ \binder x y
binder' Map rawIdent (Name y)
names' ->
binder x y -> AST binder sig y -> ScopedAST binder sig x
forall (binder :: S -> S -> *) (n :: S) (l :: S)
(sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST binder x y
binder'
(AST binder sig y -> ScopedAST binder sig x)
-> Either (UnresolvedName rawIdent) (AST binder sig y)
-> Either (UnresolvedName rawIdent) (ScopedAST binder sig x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Scope y
-> Map rawIdent (Name y)
-> Map rawIdent (AST binder sig y)
-> rawTerm
-> Either (UnresolvedName rawIdent) (AST binder sig y)
forall (x :: S).
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> Map rawIdent (AST binder sig x)
-> rawTerm
-> Either (UnresolvedName rawIdent) (AST binder sig x)
go (binder x y -> Scope x -> Scope y
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> Scope n -> Scope l
Foil.extendScopePattern binder x y
binder' Scope x
scope) Map rawIdent (Name y)
names'
(Map rawIdent (AST binder sig x) -> Map rawIdent (AST binder sig y)
forall (f :: * -> *) (e :: S -> *) (n :: S) (l :: S).
(Functor f, Sinkable e, DExt n l) =>
f (e n) -> f (e l)
Foil.sink1 Map rawIdent (AST binder sig x)
terms) (rawScopedTerm -> rawTerm
getScopedTerm rawScopedTerm
scopedTerm)
unsafeConvertToAST
:: (Foil.Distinct n, Bifunctor sig, Ord rawIdent, Foil.CoSinkable binder)
=> (rawTerm -> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall x z. Foil.Distinct x
=> Foil.Scope x
-> Map rawIdent (Foil.Name x)
-> rawPattern
-> (forall y. Foil.DExt x y
=> binder x y
-> Map rawIdent (Foil.Name y)
-> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Foil.Scope n
-> Map rawIdent (Foil.Name n)
-> rawTerm
-> AST binder sig n
unsafeConvertToAST :: forall (n :: S) (sig :: * -> * -> *) rawIdent
(binder :: S -> S -> *) rawTerm rawPattern rawScopedTerm.
(Distinct n, Bifunctor sig, Ord rawIdent, CoSinkable binder) =>
(rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Scope n
-> Map rawIdent (Name n)
-> rawTerm
-> AST binder sig n
unsafeConvertToAST rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)
toSig forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
fromRawPattern rawScopedTerm -> rawTerm
getScopedTerm Scope n
scope Map rawIdent (Name n)
names rawTerm
t =
case rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)
toSig rawTerm
t of
Left rawIdent
x ->
case rawIdent -> Map rawIdent (Name n) -> Maybe (Name n)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup rawIdent
x Map rawIdent (Name n)
names of
Maybe (Name n)
Nothing -> String -> AST binder sig n
forall a. HasCallStack => String -> a
error String
"undefined variable"
Just Name n
name -> Name n -> AST binder sig n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var Name n
name
Right sig (rawPattern, rawScopedTerm) rawTerm
node -> sig (ScopedAST binder sig n) (AST binder sig n) -> AST binder sig n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig (ScopedAST binder sig n) (AST binder sig n) -> AST binder sig n
Node (sig (ScopedAST binder sig n) (AST binder sig n)
-> AST binder sig n)
-> sig (ScopedAST binder sig n) (AST binder sig n)
-> AST binder sig n
forall a b. (a -> b) -> a -> b
$
((rawPattern, rawScopedTerm) -> ScopedAST binder sig n)
-> (rawTerm -> AST binder sig n)
-> sig (rawPattern, rawScopedTerm) rawTerm
-> sig (ScopedAST binder sig n) (AST binder sig n)
forall a b c d. (a -> b) -> (c -> d) -> sig a c -> sig b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap
((rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Scope n
-> Map rawIdent (Name n)
-> (rawPattern, rawScopedTerm)
-> ScopedAST binder sig n
forall (n :: S) (sig :: * -> * -> *) rawIdent
(binder :: S -> S -> *) rawTerm rawPattern rawScopedTerm.
(Distinct n, Bifunctor sig, Ord rawIdent, CoSinkable binder) =>
(rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Scope n
-> Map rawIdent (Name n)
-> (rawPattern, rawScopedTerm)
-> ScopedAST binder sig n
unsafeConvertToScopedAST rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)
toSig Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
fromRawPattern rawScopedTerm -> rawTerm
getScopedTerm Scope n
scope Map rawIdent (Name n)
names)
((rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Scope n
-> Map rawIdent (Name n)
-> rawTerm
-> AST binder sig n
forall (n :: S) (sig :: * -> * -> *) rawIdent
(binder :: S -> S -> *) rawTerm rawPattern rawScopedTerm.
(Distinct n, Bifunctor sig, Ord rawIdent, CoSinkable binder) =>
(rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Scope n
-> Map rawIdent (Name n)
-> rawTerm
-> AST binder sig n
unsafeConvertToAST rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)
toSig Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
fromRawPattern rawScopedTerm -> rawTerm
getScopedTerm Scope n
scope Map rawIdent (Name n)
names)
sig (rawPattern, rawScopedTerm) rawTerm
node
unsafeConvertToScopedAST
:: (Foil.Distinct n, Bifunctor sig, Ord rawIdent, Foil.CoSinkable binder)
=> (rawTerm -> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall x z. Foil.Distinct x
=> Foil.Scope x
-> Map rawIdent (Foil.Name x)
-> rawPattern
-> (forall y. Foil.DExt x y
=> binder x y
-> Map rawIdent (Foil.Name y)
-> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Foil.Scope n
-> Map rawIdent (Foil.Name n)
-> (rawPattern, rawScopedTerm)
-> ScopedAST binder sig n
unsafeConvertToScopedAST :: forall (n :: S) (sig :: * -> * -> *) rawIdent
(binder :: S -> S -> *) rawTerm rawPattern rawScopedTerm.
(Distinct n, Bifunctor sig, Ord rawIdent, CoSinkable binder) =>
(rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Scope n
-> Map rawIdent (Name n)
-> (rawPattern, rawScopedTerm)
-> ScopedAST binder sig n
unsafeConvertToScopedAST rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)
toSig forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
fromRawPattern rawScopedTerm -> rawTerm
getScopedTerm Scope n
scope Map rawIdent (Name n)
names (rawPattern
pat, rawScopedTerm
scopedTerm) =
Scope n
-> Map rawIdent (Name n)
-> rawPattern
-> (forall (y :: S).
DExt n y =>
binder n y -> Map rawIdent (Name y) -> ScopedAST binder sig n)
-> ScopedAST binder sig n
forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
fromRawPattern Scope n
scope Map rawIdent (Name n)
names rawPattern
pat ((forall (y :: S).
DExt n y =>
binder n y -> Map rawIdent (Name y) -> ScopedAST binder sig n)
-> ScopedAST binder sig n)
-> (forall (y :: S).
DExt n y =>
binder n y -> Map rawIdent (Name y) -> ScopedAST binder sig n)
-> ScopedAST binder sig n
forall a b. (a -> b) -> a -> b
$ \binder n y
binder' Map rawIdent (Name y)
names' ->
let scope' :: Scope y
scope' = binder n y -> Scope n -> Scope y
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> Scope n -> Scope l
Foil.extendScopePattern binder n y
binder' Scope n
scope
in binder n y -> AST binder sig y -> ScopedAST binder sig n
forall (binder :: S -> S -> *) (n :: S) (l :: S)
(sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST binder n y
binder' ((rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Scope y
-> Map rawIdent (Name y)
-> rawTerm
-> AST binder sig y
forall (n :: S) (sig :: * -> * -> *) rawIdent
(binder :: S -> S -> *) rawTerm rawPattern rawScopedTerm.
(Distinct n, Bifunctor sig, Ord rawIdent, CoSinkable binder) =>
(rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))
-> (forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z)
-> (rawScopedTerm -> rawTerm)
-> Scope n
-> Map rawIdent (Name n)
-> rawTerm
-> AST binder sig n
unsafeConvertToAST rawTerm
-> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)
toSig Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
forall (x :: S) z.
Distinct x =>
Scope x
-> Map rawIdent (Name x)
-> rawPattern
-> (forall (y :: S).
DExt x y =>
binder x y -> Map rawIdent (Name y) -> z)
-> z
fromRawPattern rawScopedTerm -> rawTerm
getScopedTerm Scope y
scope' Map rawIdent (Name y)
names' (rawScopedTerm -> rawTerm
getScopedTerm rawScopedTerm
scopedTerm))
convertFromAST
:: Bifunctor sig
=> (sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm)
-> (rawIdent -> rawTerm)
-> (forall x y. binder x y -> rawPattern)
-> (rawTerm -> rawScopedTerm)
-> (Int -> rawIdent)
-> AST binder sig n
-> rawTerm
convertFromAST :: forall (sig :: * -> * -> *) rawPattern rawScopedTerm rawTerm
rawIdent (binder :: S -> S -> *) (n :: S).
Bifunctor sig =>
(sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm)
-> (rawIdent -> rawTerm)
-> (forall (x :: S) (y :: S). binder x y -> rawPattern)
-> (rawTerm -> rawScopedTerm)
-> (Int -> rawIdent)
-> AST binder sig n
-> rawTerm
convertFromAST sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm
fromSig rawIdent -> rawTerm
fromVar forall (x :: S) (y :: S). binder x y -> rawPattern
makePattern rawTerm -> rawScopedTerm
makeScoped Int -> rawIdent
f = \case
Var Name n
x -> rawIdent -> rawTerm
fromVar (Int -> rawIdent
f (Name n -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name n
x))
Node sig (ScopedAST binder sig n) (AST binder sig n)
node -> sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm
fromSig (sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm)
-> sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm
forall a b. (a -> b) -> a -> b
$
(ScopedAST binder sig n -> (rawPattern, rawScopedTerm))
-> (AST binder sig n -> rawTerm)
-> sig (ScopedAST binder sig n) (AST binder sig n)
-> sig (rawPattern, rawScopedTerm) rawTerm
forall a b c d. (a -> b) -> (c -> d) -> sig a c -> sig b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap
((sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm)
-> (rawIdent -> rawTerm)
-> (forall (x :: S) (y :: S). binder x y -> rawPattern)
-> (rawTerm -> rawScopedTerm)
-> (Int -> rawIdent)
-> ScopedAST binder sig n
-> (rawPattern, rawScopedTerm)
forall (sig :: * -> * -> *) rawPattern rawScopedTerm rawTerm
rawIdent (binder :: S -> S -> *) (n :: S).
Bifunctor sig =>
(sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm)
-> (rawIdent -> rawTerm)
-> (forall (x :: S) (y :: S). binder x y -> rawPattern)
-> (rawTerm -> rawScopedTerm)
-> (Int -> rawIdent)
-> ScopedAST binder sig n
-> (rawPattern, rawScopedTerm)
convertFromScopedAST sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm
fromSig rawIdent -> rawTerm
fromVar binder x y -> rawPattern
forall (x :: S) (y :: S). binder x y -> rawPattern
makePattern rawTerm -> rawScopedTerm
makeScoped Int -> rawIdent
f)
((sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm)
-> (rawIdent -> rawTerm)
-> (forall (x :: S) (y :: S). binder x y -> rawPattern)
-> (rawTerm -> rawScopedTerm)
-> (Int -> rawIdent)
-> AST binder sig n
-> rawTerm
forall (sig :: * -> * -> *) rawPattern rawScopedTerm rawTerm
rawIdent (binder :: S -> S -> *) (n :: S).
Bifunctor sig =>
(sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm)
-> (rawIdent -> rawTerm)
-> (forall (x :: S) (y :: S). binder x y -> rawPattern)
-> (rawTerm -> rawScopedTerm)
-> (Int -> rawIdent)
-> AST binder sig n
-> rawTerm
convertFromAST sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm
fromSig rawIdent -> rawTerm
fromVar binder x y -> rawPattern
forall (x :: S) (y :: S). binder x y -> rawPattern
makePattern rawTerm -> rawScopedTerm
makeScoped Int -> rawIdent
f)
sig (ScopedAST binder sig n) (AST binder sig n)
node
convertFromScopedAST
:: Bifunctor sig
=> (sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm)
-> (rawIdent -> rawTerm)
-> (forall x y. binder x y -> rawPattern)
-> (rawTerm -> rawScopedTerm)
-> (Int -> rawIdent)
-> ScopedAST binder sig n
-> (rawPattern, rawScopedTerm)
convertFromScopedAST :: forall (sig :: * -> * -> *) rawPattern rawScopedTerm rawTerm
rawIdent (binder :: S -> S -> *) (n :: S).
Bifunctor sig =>
(sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm)
-> (rawIdent -> rawTerm)
-> (forall (x :: S) (y :: S). binder x y -> rawPattern)
-> (rawTerm -> rawScopedTerm)
-> (Int -> rawIdent)
-> ScopedAST binder sig n
-> (rawPattern, rawScopedTerm)
convertFromScopedAST sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm
fromSig rawIdent -> rawTerm
fromVar forall (x :: S) (y :: S). binder x y -> rawPattern
makePattern rawTerm -> rawScopedTerm
makeScoped Int -> rawIdent
f = \case
ScopedAST binder n l
binder AST binder sig l
body ->
( binder n l -> rawPattern
forall (x :: S) (y :: S). binder x y -> rawPattern
makePattern binder n l
binder
, rawTerm -> rawScopedTerm
makeScoped ((sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm)
-> (rawIdent -> rawTerm)
-> (forall (x :: S) (y :: S). binder x y -> rawPattern)
-> (rawTerm -> rawScopedTerm)
-> (Int -> rawIdent)
-> AST binder sig l
-> rawTerm
forall (sig :: * -> * -> *) rawPattern rawScopedTerm rawTerm
rawIdent (binder :: S -> S -> *) (n :: S).
Bifunctor sig =>
(sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm)
-> (rawIdent -> rawTerm)
-> (forall (x :: S) (y :: S). binder x y -> rawPattern)
-> (rawTerm -> rawScopedTerm)
-> (Int -> rawIdent)
-> AST binder sig n
-> rawTerm
convertFromAST sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm
fromSig rawIdent -> rawTerm
fromVar binder x y -> rawPattern
forall (x :: S) (y :: S). binder x y -> rawPattern
makePattern rawTerm -> rawScopedTerm
makeScoped Int -> rawIdent
f AST binder sig l
body))
convertFromASTWith
:: forall sig binder rawIdent rawTerm rawPattern rawScopedTerm n.
(Bifunctor sig, Foil.Distinct n, Foil.CoSinkable binder)
=> (sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm)
-> (rawIdent -> rawTerm)
-> (forall x y. binder x y -> rawPattern)
-> (rawTerm -> rawScopedTerm)
-> (Foil.Name n -> rawIdent)
-> (Int -> rawIdent)
-> AST binder sig n
-> rawTerm
convertFromASTWith :: forall (sig :: * -> * -> *) (binder :: S -> S -> *) rawIdent
rawTerm rawPattern rawScopedTerm (n :: S).
(Bifunctor sig, Distinct n, CoSinkable binder) =>
(sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm)
-> (rawIdent -> rawTerm)
-> (forall (x :: S) (y :: S). binder x y -> rawPattern)
-> (rawTerm -> rawScopedTerm)
-> (Name n -> rawIdent)
-> (Int -> rawIdent)
-> AST binder sig n
-> rawTerm
convertFromASTWith sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm
fromSig rawIdent -> rawTerm
fromVar forall (x :: S) (y :: S). binder x y -> rawPattern
makePattern rawTerm -> rawScopedTerm
makeScoped Name n -> rawIdent
freeName Int -> rawIdent
boundName =
(Name n -> Maybe (Name n)) -> AST binder sig n -> rawTerm
forall (x :: S).
Distinct x =>
(Name x -> Maybe (Name n)) -> AST binder sig x -> rawTerm
go Name n -> Maybe (Name n)
forall a. a -> Maybe a
Just
where
go :: forall x. Foil.Distinct x
=> (Foil.Name x -> Maybe (Foil.Name n)) -> AST binder sig x -> rawTerm
go :: forall (x :: S).
Distinct x =>
(Name x -> Maybe (Name n)) -> AST binder sig x -> rawTerm
go Name x -> Maybe (Name n)
unsink = \case
Var Name x
x -> rawIdent -> rawTerm
fromVar (rawIdent -> rawTerm) -> rawIdent -> rawTerm
forall a b. (a -> b) -> a -> b
$ case Name x -> Maybe (Name n)
unsink Name x
x of
Just Name n
name -> Name n -> rawIdent
freeName Name n
name
Maybe (Name n)
Nothing -> Int -> rawIdent
boundName (Name x -> Int
forall (l :: S). Name l -> Int
Foil.nameId Name x
x)
Node sig (ScopedAST binder sig x) (AST binder sig x)
node -> sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm
fromSig ((ScopedAST binder sig x -> (rawPattern, rawScopedTerm))
-> (AST binder sig x -> rawTerm)
-> sig (ScopedAST binder sig x) (AST binder sig x)
-> sig (rawPattern, rawScopedTerm) rawTerm
forall a b c d. (a -> b) -> (c -> d) -> sig a c -> sig b d
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap ((Name x -> Maybe (Name n))
-> ScopedAST binder sig x -> (rawPattern, rawScopedTerm)
forall (x :: S).
Distinct x =>
(Name x -> Maybe (Name n))
-> ScopedAST binder sig x -> (rawPattern, rawScopedTerm)
goScoped Name x -> Maybe (Name n)
unsink) ((Name x -> Maybe (Name n)) -> AST binder sig x -> rawTerm
forall (x :: S).
Distinct x =>
(Name x -> Maybe (Name n)) -> AST binder sig x -> rawTerm
go Name x -> Maybe (Name n)
unsink) sig (ScopedAST binder sig x) (AST binder sig x)
node)
goScoped :: forall x. Foil.Distinct x
=> (Foil.Name x -> Maybe (Foil.Name n))
-> ScopedAST binder sig x -> (rawPattern, rawScopedTerm)
goScoped :: forall (x :: S).
Distinct x =>
(Name x -> Maybe (Name n))
-> ScopedAST binder sig x -> (rawPattern, rawScopedTerm)
goScoped Name x -> Maybe (Name n)
unsink (ScopedAST binder x l
binder AST binder sig l
body) =
case binder x l -> DistinctEvidence l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct binder x l
binder of
DistinctEvidence l
Foil.Distinct ->
( binder x l -> rawPattern
forall (x :: S) (y :: S). binder x y -> rawPattern
makePattern binder x l
binder
, rawTerm -> rawScopedTerm
makeScoped
((Name l -> Maybe (Name n)) -> AST binder sig l -> rawTerm
forall (x :: S).
Distinct x =>
(Name x -> Maybe (Name n)) -> AST binder sig x -> rawTerm
go (\Name l
name -> binder x l -> Name l -> Maybe (Name x)
forall (pattern :: S -> S -> *) (n :: S) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> Name l -> Maybe (Name n)
Foil.unsinkNamePattern binder x l
binder Name l
name Maybe (Name x) -> (Name x -> Maybe (Name n)) -> Maybe (Name n)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Name x -> Maybe (Name n)
unsink) AST binder sig l
body) )
supportOf
:: (Foil.Distinct n, Foil.CoSinkable binder, Bifoldable sig)
=> AST binder sig n -> Foil.NameSet n
supportOf :: forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
(Distinct n, CoSinkable binder, Bifoldable sig) =>
AST binder sig n -> NameSet n
supportOf = \case
Var Name n
name -> Name n -> NameSet n
forall (n :: S). Name n -> NameSet n
Foil.nameSetSingleton Name n
name
Node sig (ScopedAST binder sig n) (AST binder sig n)
node -> (ScopedAST binder sig n -> NameSet n)
-> (AST binder sig n -> NameSet n)
-> sig (ScopedAST binder sig n) (AST binder sig n)
-> NameSet n
forall m a b. Monoid m => (a -> m) -> (b -> m) -> sig a b -> m
forall (p :: * -> * -> *) m a b.
(Bifoldable p, Monoid m) =>
(a -> m) -> (b -> m) -> p a b -> m
bifoldMap ScopedAST binder sig n -> NameSet n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
(Distinct n, CoSinkable binder, Bifoldable sig) =>
ScopedAST binder sig n -> NameSet n
supportOfScopedAST AST binder sig n -> NameSet n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
(Distinct n, CoSinkable binder, Bifoldable sig) =>
AST binder sig n -> NameSet n
supportOf sig (ScopedAST binder sig n) (AST binder sig n)
node
supportOfScopedAST
:: (Foil.Distinct n, Foil.CoSinkable binder, Bifoldable sig)
=> ScopedAST binder sig n -> Foil.NameSet n
supportOfScopedAST :: forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
(Distinct n, CoSinkable binder, Bifoldable sig) =>
ScopedAST binder sig n -> NameSet n
supportOfScopedAST (ScopedAST binder n l
binder AST binder sig l
body) =
case binder n l -> DistinctEvidence l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct binder n l
binder of
DistinctEvidence l
Foil.Distinct -> binder n l -> NameSet l -> NameSet n
forall (binder :: S -> S -> *) (n :: S) (l :: S).
CoSinkable binder =>
binder n l -> NameSet l -> NameSet n
Foil.unsinkNameSet binder n l
binder (AST binder sig l -> NameSet l
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
(Distinct n, CoSinkable binder, Bifoldable sig) =>
AST binder sig n -> NameSet n
supportOf AST binder sig l
body)
withRelevantScope
:: (Foil.Distinct n, Foil.CoSinkable binder, Bifoldable sig)
=> AST binder sig n
-> (forall m. (Foil.Ext m n, Foil.Distinct m)
=> Foil.Scope m -> AST binder sig m -> r)
-> r
withRelevantScope :: forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *) r.
(Distinct n, CoSinkable binder, Bifoldable sig) =>
AST binder sig n
-> (forall (m :: S).
(Ext m n, Distinct m) =>
Scope m -> AST binder sig m -> r)
-> r
withRelevantScope AST binder sig n
term forall (m :: S).
(Ext m n, Distinct m) =>
Scope m -> AST binder sig m -> r
cont =
NameSet n
-> (forall (m :: S). (Ext m n, Distinct m) => Scope m -> r) -> r
forall (n :: S) r.
Distinct n =>
NameSet n
-> (forall (m :: S). (Ext m n, Distinct m) => Scope m -> r) -> r
Foil.withRestrictedScope (AST binder sig n -> NameSet n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
(Distinct n, CoSinkable binder, Bifoldable sig) =>
AST binder sig n -> NameSet n
supportOf AST binder sig n
term) ((forall (m :: S). (Ext m n, Distinct m) => Scope m -> r) -> r)
-> (forall (m :: S). (Ext m n, Distinct m) => Scope m -> r) -> r
forall a b. (a -> b) -> a -> b
$ \Scope m
scope ->
Scope m -> AST binder sig m -> r
forall (m :: S).
(Ext m n, Distinct m) =>
Scope m -> AST binder sig m -> r
cont Scope m
scope (AST binder sig n -> AST binder sig m
forall a b. a -> b
unsafeCoerce AST binder sig n
term)
unsinkAST
:: (Foil.DExt n l, Foil.CoSinkable binder, Bifoldable sig)
=> Foil.Scope n -> AST binder sig l -> Maybe (AST binder sig n)
unsinkAST :: forall (n :: S) (l :: S) (binder :: S -> S -> *)
(sig :: * -> * -> *).
(DExt n l, CoSinkable binder, Bifoldable sig) =>
Scope n -> AST binder sig l -> Maybe (AST binder sig n)
unsinkAST Scope n
scope AST binder sig l
term
| NameSet l -> Scope n -> Bool
forall (n :: S) (l :: S). Ext n l => NameSet l -> Scope n -> Bool
Foil.nameSetSubsetOfScope (AST binder sig l -> NameSet l
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
(Distinct n, CoSinkable binder, Bifoldable sig) =>
AST binder sig n -> NameSet n
supportOf AST binder sig l
term) Scope n
scope = AST binder sig n -> Maybe (AST binder sig n)
forall a. a -> Maybe a
Just (AST binder sig l -> AST binder sig n
forall a b. a -> b
unsafeCoerce AST binder sig l
term)
| Bool
otherwise = Maybe (AST binder sig n)
forall a. Maybe a
Nothing
freeVarsOf
:: (Foil.Distinct n, Foil.CoSinkable binder, Bifoldable sig)
=> AST binder sig n -> [Foil.Name n]
freeVarsOf :: forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
(Distinct n, CoSinkable binder, Bifoldable sig) =>
AST binder sig n -> [Name n]
freeVarsOf = NameSet n -> [Name n]
forall (n :: S). NameSet n -> [Name n]
Foil.nameSetToList (NameSet n -> [Name n])
-> (AST binder sig n -> NameSet n) -> AST binder sig n -> [Name n]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AST binder sig n -> NameSet n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
(Distinct n, CoSinkable binder, Bifoldable sig) =>
AST binder sig n -> NameSet n
supportOf
freeVarsOfScopedAST
:: (Foil.Distinct n, Foil.CoSinkable binder, Bifoldable sig)
=> ScopedAST binder sig n -> [Foil.Name n]
freeVarsOfScopedAST :: forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
(Distinct n, CoSinkable binder, Bifoldable sig) =>
ScopedAST binder sig n -> [Name n]
freeVarsOfScopedAST = NameSet n -> [Name n]
forall (n :: S). NameSet n -> [Name n]
Foil.nameSetToList (NameSet n -> [Name n])
-> (ScopedAST binder sig n -> NameSet n)
-> ScopedAST binder sig n
-> [Name n]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScopedAST binder sig n -> NameSet n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
(Distinct n, CoSinkable binder, Bifoldable sig) =>
ScopedAST binder sig n -> NameSet n
supportOfScopedAST