{-# LANGUAGE DataKinds             #-}
{-# LANGUAGE DeriveFoldable        #-}
{-# LANGUAGE DeriveFunctor         #-}
{-# LANGUAGE DeriveTraversable     #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE GADTs                 #-}
{-# LANGUAGE KindSignatures        #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PatternSynonyms       #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TemplateHaskell       #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE TypeOperators         #-}
{-# LANGUAGE UndecidableInstances  #-}
{-# LANGUAGE ViewPatterns          #-}
-- The 'ZipMatchK' instances of the surface types are orphans: the class is
-- free-foil's and the types are the surface syntax's, and they belong with
-- the signature that holds them.
{-# OPTIONS_GHC -Wno-orphans #-}
-- | The syntax of the checker: one signature over a pattern binder, with a
-- raw term (every node carrying where it was written) and a core term (no
-- annotation), which closures hold and readback produces.
--
-- A binder is a 'Pattern': a variable, the unit pattern, or a pair of
-- patterns, each leaf binding one name. A pair pattern binds its names
-- directly, so no projection of a single variable stands for a component.
--
-- A shape is a term: the shape of a Π or of a λ over a cube is a tope
-- family @cube → TOPE@, applied to the bound point where it is assumed. So
-- @(t : I | φ)@ holds the family @\\ t → φ@, and @(t : Δ¹)@ holds @Δ¹@.
module Rzk.Check.Syntax
  ( -- * Binders
    Pattern (..)
  , patternNames
    -- * The signature
  , Param (..)
  , Sig (..)
    -- * Terms
  , Raw
  , ScopedRaw
  , Core
  , ScopedCore
  , SrcPos (..)
  , rawNode
  , panicImpossible
  , noPosition
  , positionOfRaw
  , stripPositions
    -- * Core patterns
  , pattern Universe, pattern UniverseCube, pattern UniverseTope
  , pattern CubeUnit, pattern CubeUnitStar, pattern Cube2, pattern Cube2_0, pattern Cube2_1
  , pattern CubeI, pattern CubeI_0, pattern CubeI_1
  , pattern CubeProduct, pattern CubeFlip, pattern CubeUnflip, pattern CubeSup, pattern CubeInf
  , pattern TopeTop, pattern TopeBottom, pattern TopeEQ, pattern TopeLEQ, pattern TopeAnd, pattern TopeOr
  , pattern TopeInv, pattern TopeUninv
  , pattern RecBottom, pattern RecOr
  , pattern TypeFun, pattern TypeShape, pattern TypeSigma, pattern TypeId, pattern App, pattern Let, pattern Lambda
  , pattern Pair, pattern First, pattern Second, pattern Refl, pattern IdJ
  , pattern Match, pattern MatchArm, pattern Unit, pattern TypeUnit, pattern TypeAsc, pattern Ann
  , pattern TypeRestricted, pattern TypeModal, pattern ModApp, pattern ModExtract, pattern LetMod
  , pattern Hole
    -- * Raw patterns
  , pattern RawNode
  , pattern RUniverse
  , pattern RUniverseCube
  , pattern RUniverseTope
  , pattern RCubeUnit
  , pattern RCubeUnitStar
  , pattern RCube2
  , pattern RCube2_0
  , pattern RCube2_1
  , pattern RCubeI
  , pattern RCubeI_0
  , pattern RCubeI_1
  , pattern RCubeProduct
  , pattern RCubeFlip
  , pattern RCubeUnflip
  , pattern RCubeSup
  , pattern RCubeInf
  , pattern RTopeTop
  , pattern RTopeBottom
  , pattern RTopeEQ
  , pattern RTopeLEQ
  , pattern RTopeAnd
  , pattern RTopeOr
  , pattern RTopeInv
  , pattern RTopeUninv
  , pattern RRecBottom
  , pattern RRecOr
  , pattern RTypeFun
  , pattern RTypeShape
  , pattern RTypeSigma
  , pattern RTypeId
  , pattern RApp
  , pattern RLet
  , pattern RLambda
  , pattern RPair
  , pattern RFirst
  , pattern RSecond
  , pattern RRefl
  , pattern RIdJ
  , pattern RMatch
  , pattern RMatchArm
  , pattern RUnit
  , pattern RTypeUnit
  , pattern RTypeAsc
  , pattern RAnn
  , pattern RTypeRestricted
  , pattern RTypeModal
  , pattern RModApp
  , pattern RModExtract
  , pattern RLetMod
  , pattern RHole
    -- * Queries
  , freeNamesOf
  , stripAnnotations
  , substituteName
  , abstractName
  , containsHole
  , holeNamesOf
  , isHoleHeaded
  , eqCore
  ) where

import           Control.Monad.Foil                (Distinct, NameBinder, S)
import qualified Control.Monad.Foil                as Foil
import           Control.Monad.Free.Foil           (AST (..), ScopedAST (..),
                                                    substitute,
                                                    unsafeEqAST)
import           Control.Monad.Free.Foil.Annotated (AnnSig (..))
import           Data.Bifoldable                   (bifoldMap, bifoldr)
import           Data.Bifunctor                    (bimap)
import           Data.Maybe                        (mapMaybe)
import           Data.Bifunctor.TH                 (deriveBifoldable,
                                                    deriveBifunctor,
                                                    deriveBitraversable)
import           Data.ZipMatchK                    (ZipMatchFoldK, ZipMatchK (..),
                                                    zipMatchViaEq)
import           Data.ZipMatchK.TH                 (deriveZipMatchK)
import           Generics.Kind.TH                  (deriveGenericK)

import           Language.Rzk.Foil.Names           (RzkPosition (..),
                                                    TModality (..), VarIdent)
import           Rzk.Check.Foil.Extras             (singletonSubstitution)

-- * Binders

-- | A binder. A variable carries the name the user wrote, if any. A wildcard
-- binds a hidden name, which conversion may need when binder shapes differ.
-- The unit pattern binds no name. A pair pattern binds the names of both
-- components, the right one in the scope of the left.
data Pattern (n :: S) (l :: S) where
  PatternVar      :: Maybe VarIdent -> NameBinder n l -> Pattern n l
  PatternWildcard :: NameBinder n l -> Pattern n l
  PatternUnit     :: Pattern n n
  PatternPair     :: Pattern n i -> Pattern i l -> Pattern n l

deriveGenericK ''Pattern
instance Foil.SinkableK Pattern
instance Foil.CoSinkable Pattern
instance Foil.HasNameBinders Pattern
instance Foil.UnifiablePattern Pattern

-- | The names a pattern binds, in binding order, with their display names.
-- The names of the left component are names of the right one's scope too:
-- a name sinks by coercion, as free-foil's own 'Foil.sink' does.
patternNames :: (Distinct n, Distinct l) => Pattern n l -> [(Maybe VarIdent, Foil.Name l)]
patternNames :: forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Pattern n l -> [(Maybe VarIdent, Name l)]
patternNames = \case
  PatternVar Maybe VarIdent
x NameBinder n l
b    -> [(Maybe VarIdent
x, NameBinder n l -> Name l
forall (n :: S) (l :: S). NameBinder n l -> Name l
Foil.nameOf NameBinder n l
b)]
  PatternWildcard NameBinder n l
b -> [(Maybe VarIdent
forall k1. Maybe k1
Nothing, NameBinder n l -> Name l
forall (n :: S) (l :: S). NameBinder n l -> Name l
Foil.nameOf NameBinder n l
b)]
  Pattern n l
PatternUnit       -> []
  PatternPair Pattern n i
l Pattern i l
r -> case (Pattern n i -> DistinctEvidence i
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct Pattern n i
l, Pattern i l -> ExtEvidence i l
forall (pattern :: S -> S -> *) (n :: S) (l :: S).
CoSinkable pattern =>
pattern n l -> ExtEvidence n l
Foil.assertExt Pattern i l
r) of
    (DistinctEvidence i
Foil.Distinct, ExtEvidence i l
Foil.Ext) ->
      ((Maybe VarIdent, Name i) -> (Maybe VarIdent, Name l))
-> [(Maybe VarIdent, Name i)] -> [(Maybe VarIdent, Name l)]
forall a b. (a -> b) -> [a] -> [b]
map ((Name i -> Name l)
-> (Maybe VarIdent, Name i) -> (Maybe VarIdent, Name l)
forall a b. (a -> b) -> (Maybe VarIdent, a) -> (Maybe VarIdent, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Name i -> Name l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink) (Pattern n i -> [(Maybe VarIdent, Name i)]
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Pattern n l -> [(Maybe VarIdent, Name l)]
patternNames Pattern n i
l) [(Maybe VarIdent, Name l)]
-> [(Maybe VarIdent, Name l)] -> [(Maybe VarIdent, Name l)]
forall a. Semigroup a => a -> a -> a
<> Pattern i l -> [(Maybe VarIdent, Name l)]
forall (n :: S) (l :: S).
(Distinct n, Distinct l) =>
Pattern n l -> [(Maybe VarIdent, Name l)]
patternNames Pattern i l
r

-- * The signature

-- | The parameter of a λ: its modality, its domain (a type, or the cube of
-- a shape) and, for a λ over a shape, the shape as a tope family over the
-- cube.
data Param term = Param
  { forall term. Param term -> TModality
paramModality :: TModality
    -- ^ the modality of the binder
  , forall term. Param term -> term
paramDomain   :: term
    -- ^ the type of an ordinary binder, or the cube of a shaped binder
  , forall term. Param term -> Maybe term
paramShape    :: Maybe term
    -- ^ the shape as a tope family over the cube, if the binder has one
  } deriving ((forall a b. (a -> b) -> Param a -> Param b)
-> (forall a b. a -> Param b -> Param a) -> Functor Param
forall a b. a -> Param b -> Param a
forall a b. (a -> b) -> Param a -> Param 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) -> Param a -> Param b
fmap :: forall a b. (a -> b) -> Param a -> Param b
$c<$ :: forall a b. a -> Param b -> Param a
<$ :: forall a b. a -> Param b -> Param a
Functor, (forall m. Monoid m => Param m -> m)
-> (forall m a. Monoid m => (a -> m) -> Param a -> m)
-> (forall m a. Monoid m => (a -> m) -> Param a -> m)
-> (forall a b. (a -> b -> b) -> b -> Param a -> b)
-> (forall a b. (a -> b -> b) -> b -> Param a -> b)
-> (forall b a. (b -> a -> b) -> b -> Param a -> b)
-> (forall b a. (b -> a -> b) -> b -> Param a -> b)
-> (forall a. (a -> a -> a) -> Param a -> a)
-> (forall a. (a -> a -> a) -> Param a -> a)
-> (forall a. Param a -> [a])
-> (forall a. Param a -> Bool)
-> (forall a. Param a -> RawName)
-> (forall a. Eq a => a -> Param a -> Bool)
-> (forall a. Ord a => Param a -> a)
-> (forall a. Ord a => Param a -> a)
-> (forall a. Num a => Param a -> a)
-> (forall a. Num a => Param a -> a)
-> Foldable Param
forall a. Eq a => a -> Param a -> Bool
forall a. Num a => Param a -> a
forall a. Ord a => Param a -> a
forall m. Monoid m => Param m -> m
forall a. Param a -> Bool
forall a. Param a -> RawName
forall a. Param a -> [a]
forall a. (a -> a -> a) -> Param a -> a
forall m a. Monoid m => (a -> m) -> Param a -> m
forall b a. (b -> a -> b) -> b -> Param a -> b
forall a b. (a -> b -> b) -> b -> Param 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 -> RawName)
-> (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 => Param m -> m
fold :: forall m. Monoid m => Param m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Param a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Param a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Param a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> Param a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> Param a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Param a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Param a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Param a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Param a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Param a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Param a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> Param a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> Param a -> a
foldr1 :: forall a. (a -> a -> a) -> Param a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Param a -> a
foldl1 :: forall a. (a -> a -> a) -> Param a -> a
$ctoList :: forall a. Param a -> [a]
toList :: forall a. Param a -> [a]
$cnull :: forall a. Param a -> Bool
null :: forall a. Param a -> Bool
$clength :: forall a. Param a -> RawName
length :: forall a. Param a -> RawName
$celem :: forall a. Eq a => a -> Param a -> Bool
elem :: forall a. Eq a => a -> Param a -> Bool
$cmaximum :: forall a. Ord a => Param a -> a
maximum :: forall a. Ord a => Param a -> a
$cminimum :: forall a. Ord a => Param a -> a
minimum :: forall a. Ord a => Param a -> a
$csum :: forall a. Num a => Param a -> a
sum :: forall a. Num a => Param a -> a
$cproduct :: forall a. Num a => Param a -> a
product :: forall a. Num a => Param a -> a
Foldable, Functor Param
Foldable Param
(Functor Param, Foldable Param) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> Param a -> f (Param b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Param (f a) -> f (Param a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Param a -> m (Param b))
-> (forall (m :: * -> *) a. Monad m => Param (m a) -> m (Param a))
-> Traversable Param
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 => Param (m a) -> m (Param a)
forall (f :: * -> *) a. Applicative f => Param (f a) -> f (Param a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Param a -> m (Param b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Param a -> f (Param b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Param a -> f (Param b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Param a -> f (Param b)
$csequenceA :: forall (f :: * -> *) a. Applicative f => Param (f a) -> f (Param a)
sequenceA :: forall (f :: * -> *) a. Applicative f => Param (f a) -> f (Param a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Param a -> m (Param b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Param a -> m (Param b)
$csequence :: forall (m :: * -> *) a. Monad m => Param (m a) -> m (Param a)
sequence :: forall (m :: * -> *) a. Monad m => Param (m a) -> m (Param a)
Traversable, RawName -> Param term -> ShowS
[Param term] -> ShowS
Param term -> String
(RawName -> Param term -> ShowS)
-> (Param term -> String)
-> ([Param term] -> ShowS)
-> Show (Param term)
forall term. Show term => RawName -> Param term -> ShowS
forall term. Show term => [Param term] -> ShowS
forall term. Show term => Param term -> String
forall a.
(RawName -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall term. Show term => RawName -> Param term -> ShowS
showsPrec :: RawName -> Param term -> ShowS
$cshow :: forall term. Show term => Param term -> String
show :: Param term -> String
$cshowList :: forall term. Show term => [Param term] -> ShowS
showList :: [Param term] -> ShowS
Show)

instance ZipMatchK TModality where
  zipMatchWithK :: forall (as :: LoT (*)) (bs :: LoT (*)) (cs :: LoT (*)).
Mappings as bs cs
-> (TModality :@@: as)
-> (TModality :@@: bs)
-> Maybe (TModality :@@: cs)
zipMatchWithK = Mappings as bs cs
-> (TModality :@@: as)
-> (TModality :@@: bs)
-> Maybe (TModality :@@: cs)
Mappings as bs cs -> TModality -> TModality -> Maybe TModality
forall {k} a (as :: LoT k) (bs :: LoT k) (cs :: LoT k).
Eq a =>
Mappings as bs cs -> a -> a -> Maybe a
zipMatchViaEq

instance ZipMatchK VarIdent where
  zipMatchWithK :: forall (as :: LoT (*)) (bs :: LoT (*)) (cs :: LoT (*)).
Mappings as bs cs
-> (VarIdent :@@: as)
-> (VarIdent :@@: bs)
-> Maybe (VarIdent :@@: cs)
zipMatchWithK = Mappings as bs cs
-> (VarIdent :@@: as)
-> (VarIdent :@@: bs)
-> Maybe (VarIdent :@@: cs)
Mappings as bs cs -> VarIdent -> VarIdent -> Maybe VarIdent
forall {k} a (as :: LoT k) (bs :: LoT k) (cs :: LoT k).
Eq a =>
Mappings as bs cs -> a -> a -> Maybe a
zipMatchViaEq

-- | A hole's name is a whole field, matched as a constant.
instance ZipMatchK (Maybe VarIdent) where
  zipMatchWithK :: forall (as :: LoT (*)) (bs :: LoT (*)) (cs :: LoT (*)).
Mappings as bs cs
-> (Maybe VarIdent :@@: as)
-> (Maybe VarIdent :@@: bs)
-> Maybe (Maybe VarIdent :@@: cs)
zipMatchWithK = Mappings as bs cs
-> Maybe VarIdent -> Maybe VarIdent -> Maybe (Maybe VarIdent)
Mappings as bs cs
-> (Maybe VarIdent :@@: as)
-> (Maybe VarIdent :@@: bs)
-> Maybe (Maybe VarIdent :@@: cs)
forall {k} a (as :: LoT k) (bs :: LoT k) (cs :: LoT k).
Eq a =>
Mappings as bs cs -> a -> a -> Maybe a
zipMatchViaEq

deriveGenericK ''Param
instance ZipMatchK Param
instance ZipMatchFoldK Param

-- | The signature of the core: every construct of the language. Scoped
-- positions bind a 'Pattern'.
data Sig scope term
  = UniverseF
    -- ^ the universe @U@ of ordinary types
  | UniverseCubeF
    -- ^ the universe @CUBE@ of cubes
  | UniverseTopeF
    -- ^ the universe @TOPE@ of topes
  | CubeUnitF
    -- ^ the unit cube
  | CubeUnitStarF
    -- ^ the unique point of the unit cube
  | Cube2F
    -- ^ the directed interval cube @2@
  | Cube2_0F
    -- ^ the left endpoint of @2@
  | Cube2_1F
    -- ^ the right endpoint of @2@
  | CubeIF
    -- ^ the auxiliary directed interval cube @𝕀@
  | CubeI_0F
    -- ^ the left endpoint of @𝕀@
  | CubeI_1F
    -- ^ the right endpoint of @𝕀@
  | CubeProductF term term
    -- ^ a product of two cubes
  | CubeFlipF term
    -- ^ reversal of an interval point
  | CubeUnflipF term
    -- ^ the inverse operation to 'CubeFlipF'
  | CubeSupF term term
    -- ^ the join of two interval points
  | CubeInfF term term
    -- ^ the meet of two interval points
  | TopeTopF
    -- ^ the trivial tope @⊤@
  | TopeBottomF
    -- ^ the empty tope @⊥@
  | TopeEQF term term
    -- ^ equality of two cube points
  | TopeLEQF term term
    -- ^ the order relation on two directed-interval points
  | TopeAndF term term
    -- ^ conjunction of topes
  | TopeOrF term term
    -- ^ disjunction of topes
  | TopeInvF term
    -- ^ inversion of a tope under @ᵒᵖ@
  | TopeUninvF term
    -- ^ the inverse operation to 'TopeInvF'
  | RecBottomF
    -- ^ ex falso, @recBOT@
  | RecOrF (Maybe term) [(term, term)]
    -- ^ a raw @recOR@ has no type; elaboration attaches its type, which a
    -- stuck semantic @recOR@ needs when it heads an elimination
  | TypeFunF TModality term (Maybe term) scope
    -- ^ a Π: the modality of the binder, its domain (a type, or the cube of
    -- a shape), the shape as a tope family over the cube, and the codomain
  | TypeShapeF TModality term (Maybe term)
    -- ^ the type of a tope family: @{t : cube | shape} → TOPE@, whose
    -- codomain is constant, so that the subtyping of families, which is
    -- covariant in the shape, can tell it from a Π
  | TypeSigmaF TModality term scope
    -- ^ a dependent pair type, with the modality and type of its first component
  | TypeIdF term (Maybe term) term
    -- ^ an identity type: the endpoints and, once elaborated, their carrier
  | AppF term term
    -- ^ application of a function or a tope family
  | LetF (Maybe term) term scope
    -- ^ a local definition: its optional type, value, and body
  | LambdaF (Maybe (Param term)) scope
    -- ^ a λ abstraction, with an optional parameter annotation and its body
  | PairF term term
    -- ^ a pair of terms or cube points
  | FirstF term
    -- ^ the first projection of a pair
  | SecondF term
    -- ^ the second projection of a pair
  | ReflF (Maybe (term, Maybe term))
    -- ^ reflexivity, optionally annotated by its element and carrier
  | IdJF term term term term term term
    -- ^ path induction: carrier, base point, motive, base case, endpoint, path
  | MatchF TModality term (Maybe term) [(VarIdent, term)]
    -- ^ a match on a @#data@ value: the modality, the scrutinee, the
    -- @into@ motive, and the branches, each an arm chain
  | MatchArmF scope
    -- ^ one binder of a match branch
  | UnitF
    -- ^ the unique inhabitant of the unit type
  | TypeUnitF
    -- ^ the unit type
  | TypeAscF term term
    -- ^ a user ascription; evaluation erases it
  | AnnF term term
    -- ^ a neutral value with its type, which evaluation builds so that the
    -- type of a neutral is read off it rather than computed from its head.
    -- Evaluating it drops it, the readbacks of "Rzk.Check.Value" strip it
    -- ('stripAnnotations'), and the type takes no part in conversion or in
    -- the free names of a term.
  | TypeRestrictedF term [(term, term)]
    -- ^ an extension type: its underlying type and tope/boundary faces
  | TypeModalF TModality term
    -- ^ a type under a modality
  | ModAppF TModality term
    -- ^ introduction of a term under a modality
  | ModExtractF TModality TModality term
    -- ^ an internal modal elimination, with its outer and inner modalities
  | LetModF TModality TModality (Maybe term) (Maybe term) term scope
    -- ^ modal elimination by @let mod@, with modalities, optional type and
    -- motive, payload, and body
  | HoleF (Maybe VarIdent)
    -- ^ a user hole, with its optional name; not an internal unknown type
  deriving ((forall a b. (a -> b) -> Sig scope a -> Sig scope b)
-> (forall a b. a -> Sig scope b -> Sig scope a)
-> Functor (Sig scope)
forall a b. a -> Sig scope b -> Sig scope a
forall a b. (a -> b) -> Sig scope a -> Sig scope b
forall scope a b. a -> Sig scope b -> Sig scope a
forall scope a b. (a -> b) -> Sig scope a -> Sig scope b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall scope a b. (a -> b) -> Sig scope a -> Sig scope b
fmap :: forall a b. (a -> b) -> Sig scope a -> Sig scope b
$c<$ :: forall scope a b. a -> Sig scope b -> Sig scope a
<$ :: forall a b. a -> Sig scope b -> Sig scope a
Functor, (forall m. Monoid m => Sig scope m -> m)
-> (forall m a. Monoid m => (a -> m) -> Sig scope a -> m)
-> (forall m a. Monoid m => (a -> m) -> Sig scope a -> m)
-> (forall a b. (a -> b -> b) -> b -> Sig scope a -> b)
-> (forall a b. (a -> b -> b) -> b -> Sig scope a -> b)
-> (forall b a. (b -> a -> b) -> b -> Sig scope a -> b)
-> (forall b a. (b -> a -> b) -> b -> Sig scope a -> b)
-> (forall a. (a -> a -> a) -> Sig scope a -> a)
-> (forall a. (a -> a -> a) -> Sig scope a -> a)
-> (forall a. Sig scope a -> [a])
-> (forall a. Sig scope a -> Bool)
-> (forall a. Sig scope a -> RawName)
-> (forall a. Eq a => a -> Sig scope a -> Bool)
-> (forall a. Ord a => Sig scope a -> a)
-> (forall a. Ord a => Sig scope a -> a)
-> (forall a. Num a => Sig scope a -> a)
-> (forall a. Num a => Sig scope a -> a)
-> Foldable (Sig scope)
forall a. Eq a => a -> Sig scope a -> Bool
forall a. Num a => Sig scope a -> a
forall a. Ord a => Sig scope a -> a
forall m. Monoid m => Sig scope m -> m
forall a. Sig scope a -> Bool
forall a. Sig scope a -> RawName
forall a. Sig scope a -> [a]
forall a. (a -> a -> a) -> Sig scope a -> a
forall scope a. Eq a => a -> Sig scope a -> Bool
forall scope a. Num a => Sig scope a -> a
forall scope a. Ord a => Sig scope a -> a
forall scope m. Monoid m => Sig scope m -> m
forall m a. Monoid m => (a -> m) -> Sig scope a -> m
forall scope a. Sig scope a -> Bool
forall scope a. Sig scope a -> RawName
forall scope a. Sig scope a -> [a]
forall b a. (b -> a -> b) -> b -> Sig scope a -> b
forall a b. (a -> b -> b) -> b -> Sig scope a -> b
forall scope a. (a -> a -> a) -> Sig scope a -> a
forall scope m a. Monoid m => (a -> m) -> Sig scope a -> m
forall scope b a. (b -> a -> b) -> b -> Sig scope a -> b
forall scope a b. (a -> b -> b) -> b -> Sig scope 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 -> RawName)
-> (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 scope m. Monoid m => Sig scope m -> m
fold :: forall m. Monoid m => Sig scope m -> m
$cfoldMap :: forall scope m a. Monoid m => (a -> m) -> Sig scope a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Sig scope a -> m
$cfoldMap' :: forall scope m a. Monoid m => (a -> m) -> Sig scope a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> Sig scope a -> m
$cfoldr :: forall scope a b. (a -> b -> b) -> b -> Sig scope a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Sig scope a -> b
$cfoldr' :: forall scope a b. (a -> b -> b) -> b -> Sig scope a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Sig scope a -> b
$cfoldl :: forall scope b a. (b -> a -> b) -> b -> Sig scope a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Sig scope a -> b
$cfoldl' :: forall scope b a. (b -> a -> b) -> b -> Sig scope a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> Sig scope a -> b
$cfoldr1 :: forall scope a. (a -> a -> a) -> Sig scope a -> a
foldr1 :: forall a. (a -> a -> a) -> Sig scope a -> a
$cfoldl1 :: forall scope a. (a -> a -> a) -> Sig scope a -> a
foldl1 :: forall a. (a -> a -> a) -> Sig scope a -> a
$ctoList :: forall scope a. Sig scope a -> [a]
toList :: forall a. Sig scope a -> [a]
$cnull :: forall scope a. Sig scope a -> Bool
null :: forall a. Sig scope a -> Bool
$clength :: forall scope a. Sig scope a -> RawName
length :: forall a. Sig scope a -> RawName
$celem :: forall scope a. Eq a => a -> Sig scope a -> Bool
elem :: forall a. Eq a => a -> Sig scope a -> Bool
$cmaximum :: forall scope a. Ord a => Sig scope a -> a
maximum :: forall a. Ord a => Sig scope a -> a
$cminimum :: forall scope a. Ord a => Sig scope a -> a
minimum :: forall a. Ord a => Sig scope a -> a
$csum :: forall scope a. Num a => Sig scope a -> a
sum :: forall a. Num a => Sig scope a -> a
$cproduct :: forall scope a. Num a => Sig scope a -> a
product :: forall a. Num a => Sig scope a -> a
Foldable, Functor (Sig scope)
Foldable (Sig scope)
(Functor (Sig scope), Foldable (Sig scope)) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> Sig scope a -> f (Sig scope b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Sig scope (f a) -> f (Sig scope a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Sig scope a -> m (Sig scope b))
-> (forall (m :: * -> *) a.
    Monad m =>
    Sig scope (m a) -> m (Sig scope a))
-> Traversable (Sig scope)
forall scope. Functor (Sig scope)
forall scope. Foldable (Sig scope)
forall scope (m :: * -> *) a.
Monad m =>
Sig scope (m a) -> m (Sig scope a)
forall scope (f :: * -> *) a.
Applicative f =>
Sig scope (f a) -> f (Sig scope a)
forall scope (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Sig scope a -> m (Sig scope b)
forall scope (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Sig scope a -> f (Sig scope b)
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 =>
Sig scope (m a) -> m (Sig scope a)
forall (f :: * -> *) a.
Applicative f =>
Sig scope (f a) -> f (Sig scope a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Sig scope a -> m (Sig scope b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Sig scope a -> f (Sig scope b)
$ctraverse :: forall scope (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Sig scope a -> f (Sig scope b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Sig scope a -> f (Sig scope b)
$csequenceA :: forall scope (f :: * -> *) a.
Applicative f =>
Sig scope (f a) -> f (Sig scope a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Sig scope (f a) -> f (Sig scope a)
$cmapM :: forall scope (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Sig scope a -> m (Sig scope b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Sig scope a -> m (Sig scope b)
$csequence :: forall scope (m :: * -> *) a.
Monad m =>
Sig scope (m a) -> m (Sig scope a)
sequence :: forall (m :: * -> *) a.
Monad m =>
Sig scope (m a) -> m (Sig scope a)
Traversable)

deriveBifunctor ''Sig
deriveBifoldable ''Sig
deriveBitraversable ''Sig
deriveGenericK ''Sig

deriveZipMatchK ''Sig

-- * Terms

-- | Where a term was written: the annotation of a raw term, a phantom in the
-- node's term parameter.
newtype SrcPos term = SrcPos RzkPosition
  deriving ((forall a b. (a -> b) -> SrcPos a -> SrcPos b)
-> (forall a b. a -> SrcPos b -> SrcPos a) -> Functor SrcPos
forall a b. a -> SrcPos b -> SrcPos a
forall a b. (a -> b) -> SrcPos a -> SrcPos 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) -> SrcPos a -> SrcPos b
fmap :: forall a b. (a -> b) -> SrcPos a -> SrcPos b
$c<$ :: forall a b. a -> SrcPos b -> SrcPos a
<$ :: forall a b. a -> SrcPos b -> SrcPos a
Functor, (forall m. Monoid m => SrcPos m -> m)
-> (forall m a. Monoid m => (a -> m) -> SrcPos a -> m)
-> (forall m a. Monoid m => (a -> m) -> SrcPos a -> m)
-> (forall a b. (a -> b -> b) -> b -> SrcPos a -> b)
-> (forall a b. (a -> b -> b) -> b -> SrcPos a -> b)
-> (forall b a. (b -> a -> b) -> b -> SrcPos a -> b)
-> (forall b a. (b -> a -> b) -> b -> SrcPos a -> b)
-> (forall a. (a -> a -> a) -> SrcPos a -> a)
-> (forall a. (a -> a -> a) -> SrcPos a -> a)
-> (forall a. SrcPos a -> [a])
-> (forall a. SrcPos a -> Bool)
-> (forall a. SrcPos a -> RawName)
-> (forall a. Eq a => a -> SrcPos a -> Bool)
-> (forall a. Ord a => SrcPos a -> a)
-> (forall a. Ord a => SrcPos a -> a)
-> (forall a. Num a => SrcPos a -> a)
-> (forall a. Num a => SrcPos a -> a)
-> Foldable SrcPos
forall a. Eq a => a -> SrcPos a -> Bool
forall a. Num a => SrcPos a -> a
forall a. Ord a => SrcPos a -> a
forall m. Monoid m => SrcPos m -> m
forall a. SrcPos a -> Bool
forall a. SrcPos a -> RawName
forall a. SrcPos a -> [a]
forall a. (a -> a -> a) -> SrcPos a -> a
forall m a. Monoid m => (a -> m) -> SrcPos a -> m
forall b a. (b -> a -> b) -> b -> SrcPos a -> b
forall a b. (a -> b -> b) -> b -> SrcPos 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 -> RawName)
-> (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 => SrcPos m -> m
fold :: forall m. Monoid m => SrcPos m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> SrcPos a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> SrcPos a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> SrcPos a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> SrcPos a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> SrcPos a -> b
foldr :: forall a b. (a -> b -> b) -> b -> SrcPos a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> SrcPos a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> SrcPos a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> SrcPos a -> b
foldl :: forall b a. (b -> a -> b) -> b -> SrcPos a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> SrcPos a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> SrcPos a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> SrcPos a -> a
foldr1 :: forall a. (a -> a -> a) -> SrcPos a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> SrcPos a -> a
foldl1 :: forall a. (a -> a -> a) -> SrcPos a -> a
$ctoList :: forall a. SrcPos a -> [a]
toList :: forall a. SrcPos a -> [a]
$cnull :: forall a. SrcPos a -> Bool
null :: forall a. SrcPos a -> Bool
$clength :: forall a. SrcPos a -> RawName
length :: forall a. SrcPos a -> RawName
$celem :: forall a. Eq a => a -> SrcPos a -> Bool
elem :: forall a. Eq a => a -> SrcPos a -> Bool
$cmaximum :: forall a. Ord a => SrcPos a -> a
maximum :: forall a. Ord a => SrcPos a -> a
$cminimum :: forall a. Ord a => SrcPos a -> a
minimum :: forall a. Ord a => SrcPos a -> a
$csum :: forall a. Num a => SrcPos a -> a
sum :: forall a. Num a => SrcPos a -> a
$cproduct :: forall a. Num a => SrcPos a -> a
product :: forall a. Num a => SrcPos a -> a
Foldable, Functor SrcPos
Foldable SrcPos
(Functor SrcPos, Foldable SrcPos) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> SrcPos a -> f (SrcPos b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    SrcPos (f a) -> f (SrcPos a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> SrcPos a -> m (SrcPos b))
-> (forall (m :: * -> *) a.
    Monad m =>
    SrcPos (m a) -> m (SrcPos a))
-> Traversable SrcPos
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 => SrcPos (m a) -> m (SrcPos a)
forall (f :: * -> *) a.
Applicative f =>
SrcPos (f a) -> f (SrcPos a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SrcPos a -> m (SrcPos b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SrcPos a -> f (SrcPos b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SrcPos a -> f (SrcPos b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SrcPos a -> f (SrcPos b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
SrcPos (f a) -> f (SrcPos a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
SrcPos (f a) -> f (SrcPos a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SrcPos a -> m (SrcPos b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> SrcPos a -> m (SrcPos b)
$csequence :: forall (m :: * -> *) a. Monad m => SrcPos (m a) -> m (SrcPos a)
sequence :: forall (m :: * -> *) a. Monad m => SrcPos (m a) -> m (SrcPos a)
Traversable)

-- | Two terms written in different places are the same term.
instance ZipMatchK SrcPos where
  zipMatchWithK :: forall (as :: LoT (* -> *)) (bs :: LoT (* -> *))
       (cs :: LoT (* -> *)).
Mappings as bs cs
-> (SrcPos :@@: as) -> (SrcPos :@@: bs) -> Maybe (SrcPos :@@: cs)
zipMatchWithK Mappings as bs cs
_ (SrcPos RzkPosition
pos) SrcPos :@@: bs
_ = SrcPos (HeadLoT cs) -> Maybe (SrcPos (HeadLoT cs))
forall k1. k1 -> Maybe k1
Just (RzkPosition -> SrcPos (HeadLoT cs)
forall term. RzkPosition -> SrcPos term
SrcPos RzkPosition
pos)

-- | A raw term: the surface syntax converted, every node carrying where it
-- was written. What the checker takes.
type Raw = AST Pattern (AnnSig SrcPos Sig)

type ScopedRaw = ScopedAST Pattern (AnnSig SrcPos Sig)

-- | A core term: what the checker produces, what closures hold, and what
-- readback gives.
type Core = AST Pattern Sig

type ScopedCore = ScopedAST Pattern Sig

-- | A raw node at a position.
rawNode :: RzkPosition -> Sig (ScopedRaw n) (Raw n) -> Raw n
rawNode :: forall (n :: S). RzkPosition -> Sig (ScopedRaw n) (Raw n) -> Raw n
rawNode RzkPosition
pos Sig (ScopedRaw n) (Raw n)
sig = AnnSig SrcPos Sig (ScopedRaw n) (Raw n) -> Raw n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig (ScopedAST binder sig n) (AST binder sig n) -> AST binder sig n
Node (SrcPos (Raw n)
-> Sig (ScopedRaw n) (Raw n)
-> AnnSig SrcPos Sig (ScopedRaw n) (Raw n)
forall (ann :: * -> *) (sig :: * -> * -> *) scope term.
ann term -> sig scope term -> AnnSig ann sig scope term
AnnSig (RzkPosition -> SrcPos (Raw n)
forall term. RzkPosition -> SrcPos term
SrcPos RzkPosition
pos) Sig (ScopedRaw n) (Raw n)
sig)

-- | Fail on a case the checker's invariants rule out, asking for a bug report.
panicImpossible :: String -> a
panicImpossible :: forall a. String -> a
panicImpossible String
msg = String -> a
forall a. HasCallStack => String -> a
error (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ [String] -> String
unlines
  [ String
"PANIC! Impossible happened (" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
msg String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")!"
  , String
"Please, report a bug at https://github.com/rzk-lang/rzk/issues"
  ]

-- | The position of a node the checker builds, which was written nowhere.
noPosition :: RzkPosition
noPosition :: RzkPosition
noPosition = Maybe String -> BNFC'Position -> RzkPosition
RzkPosition Maybe String
forall k1. Maybe k1
Nothing BNFC'Position
forall k1. Maybe k1
Nothing

-- | Where a raw term was written, when it came from a file. A variable is a
-- leaf and carries no position.
positionOfRaw :: Raw n -> Maybe RzkPosition
positionOfRaw :: forall (n :: S). Raw n -> Maybe RzkPosition
positionOfRaw (Var Name n
_) = Maybe RzkPosition
forall k1. Maybe k1
Nothing
positionOfRaw (Node (AnnSig (SrcPos RzkPosition
pos) Sig
  (ScopedAST Pattern (AnnSig SrcPos Sig) n)
  (AST Pattern (AnnSig SrcPos Sig) n)
_)) = case RzkPosition -> BNFC'Position
rzkLineCol RzkPosition
pos of
  BNFC'Position
Nothing -> Maybe RzkPosition
forall k1. Maybe k1
Nothing
  Just (RawName, RawName)
_  -> RzkPosition -> Maybe RzkPosition
forall k1. k1 -> Maybe k1
Just RzkPosition
pos


-- | A raw term as a core term: the positions dropped.
stripPositions :: Raw n -> Core n
stripPositions :: forall (n :: S). Raw n -> Core n
stripPositions (Var Name n
x) = Name n -> AST Pattern Sig n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var Name n
x
stripPositions (Node (AnnSig SrcPos (AST Pattern (AnnSig SrcPos Sig) n)
_ Sig
  (ScopedAST Pattern (AnnSig SrcPos Sig) n)
  (AST Pattern (AnnSig SrcPos Sig) n)
sig)) = Sig (ScopedAST Pattern Sig n) (AST Pattern Sig n)
-> AST Pattern Sig n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig (ScopedAST binder sig n) (AST binder sig n) -> AST binder sig n
Node ((ScopedAST Pattern (AnnSig SrcPos Sig) n
 -> ScopedAST Pattern Sig n)
-> (AST Pattern (AnnSig SrcPos Sig) n -> AST Pattern Sig n)
-> Sig
     (ScopedAST Pattern (AnnSig SrcPos Sig) n)
     (AST Pattern (AnnSig SrcPos Sig) n)
-> Sig (ScopedAST Pattern Sig n) (AST Pattern 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 ScopedAST Pattern (AnnSig SrcPos Sig) n -> ScopedAST Pattern Sig n
forall {n :: S}.
ScopedAST Pattern (AnnSig SrcPos Sig) n -> ScopedAST Pattern Sig n
scoped AST Pattern (AnnSig SrcPos Sig) n -> AST Pattern Sig n
forall (n :: S). Raw n -> Core n
stripPositions Sig
  (ScopedAST Pattern (AnnSig SrcPos Sig) n)
  (AST Pattern (AnnSig SrcPos Sig) n)
sig)
  where
    scoped :: ScopedAST Pattern (AnnSig SrcPos Sig) n -> ScopedAST Pattern Sig n
scoped (ScopedAST Pattern n l
pat AST Pattern (AnnSig SrcPos Sig) l
body) = Pattern n l -> AST Pattern Sig l -> ScopedAST Pattern Sig n
forall (binder :: S -> S -> *) (n :: S) (l :: S)
       (sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST Pattern n l
pat (AST Pattern (AnnSig SrcPos Sig) l -> AST Pattern Sig l
forall (n :: S). Raw n -> Core n
stripPositions AST Pattern (AnnSig SrcPos Sig) l
body)

-- * Core patterns

pattern Universe :: Core n
pattern $mUniverse :: forall {r} {n :: S}. Core n -> ((# #) -> r) -> ((# #) -> r) -> r
$bUniverse :: forall (n :: S). Core n
Universe = Node UniverseF
pattern UniverseCube :: Core n
pattern $mUniverseCube :: forall {r} {n :: S}. Core n -> ((# #) -> r) -> ((# #) -> r) -> r
$bUniverseCube :: forall (n :: S). Core n
UniverseCube = Node UniverseCubeF
pattern UniverseTope :: Core n
pattern $mUniverseTope :: forall {r} {n :: S}. Core n -> ((# #) -> r) -> ((# #) -> r) -> r
$bUniverseTope :: forall (n :: S). Core n
UniverseTope = Node UniverseTopeF
pattern CubeUnit :: Core n
pattern $mCubeUnit :: forall {r} {n :: S}. Core n -> ((# #) -> r) -> ((# #) -> r) -> r
$bCubeUnit :: forall (n :: S). Core n
CubeUnit = Node CubeUnitF
pattern CubeUnitStar :: Core n
pattern $mCubeUnitStar :: forall {r} {n :: S}. Core n -> ((# #) -> r) -> ((# #) -> r) -> r
$bCubeUnitStar :: forall (n :: S). Core n
CubeUnitStar = Node CubeUnitStarF
pattern Cube2 :: Core n
pattern $mCube2 :: forall {r} {n :: S}. Core n -> ((# #) -> r) -> ((# #) -> r) -> r
$bCube2 :: forall (n :: S). Core n
Cube2 = Node Cube2F
pattern Cube2_0 :: Core n
pattern $mCube2_0 :: forall {r} {n :: S}. Core n -> ((# #) -> r) -> ((# #) -> r) -> r
$bCube2_0 :: forall (n :: S). Core n
Cube2_0 = Node Cube2_0F
pattern Cube2_1 :: Core n
pattern $mCube2_1 :: forall {r} {n :: S}. Core n -> ((# #) -> r) -> ((# #) -> r) -> r
$bCube2_1 :: forall (n :: S). Core n
Cube2_1 = Node Cube2_1F
pattern CubeI :: Core n
pattern $mCubeI :: forall {r} {n :: S}. Core n -> ((# #) -> r) -> ((# #) -> r) -> r
$bCubeI :: forall (n :: S). Core n
CubeI = Node CubeIF
pattern CubeI_0 :: Core n
pattern $mCubeI_0 :: forall {r} {n :: S}. Core n -> ((# #) -> r) -> ((# #) -> r) -> r
$bCubeI_0 :: forall (n :: S). Core n
CubeI_0 = Node CubeI_0F
pattern CubeI_1 :: Core n
pattern $mCubeI_1 :: forall {r} {n :: S}. Core n -> ((# #) -> r) -> ((# #) -> r) -> r
$bCubeI_1 :: forall (n :: S). Core n
CubeI_1 = Node CubeI_1F
pattern CubeProduct :: Core n -> Core n -> Core n
pattern $mCubeProduct :: forall {r} {n :: S}.
Core n -> (Core n -> Core n -> r) -> ((# #) -> r) -> r
$bCubeProduct :: forall (n :: S). Core n -> Core n -> Core n
CubeProduct l r = Node (CubeProductF l r)
pattern CubeFlip :: Core n -> Core n
pattern $mCubeFlip :: forall {r} {n :: S}. Core n -> (Core n -> r) -> ((# #) -> r) -> r
$bCubeFlip :: forall (n :: S). Core n -> Core n
CubeFlip t = Node (CubeFlipF t)
pattern CubeUnflip :: Core n -> Core n
pattern $mCubeUnflip :: forall {r} {n :: S}. Core n -> (Core n -> r) -> ((# #) -> r) -> r
$bCubeUnflip :: forall (n :: S). Core n -> Core n
CubeUnflip t = Node (CubeUnflipF t)
pattern CubeSup :: Core n -> Core n -> Core n
pattern $mCubeSup :: forall {r} {n :: S}.
Core n -> (Core n -> Core n -> r) -> ((# #) -> r) -> r
$bCubeSup :: forall (n :: S). Core n -> Core n -> Core n
CubeSup l r = Node (CubeSupF l r)
pattern CubeInf :: Core n -> Core n -> Core n
pattern $mCubeInf :: forall {r} {n :: S}.
Core n -> (Core n -> Core n -> r) -> ((# #) -> r) -> r
$bCubeInf :: forall (n :: S). Core n -> Core n -> Core n
CubeInf l r = Node (CubeInfF l r)
pattern TopeTop :: Core n
pattern $mTopeTop :: forall {r} {n :: S}. Core n -> ((# #) -> r) -> ((# #) -> r) -> r
$bTopeTop :: forall (n :: S). Core n
TopeTop = Node TopeTopF
pattern TopeBottom :: Core n
pattern $mTopeBottom :: forall {r} {n :: S}. Core n -> ((# #) -> r) -> ((# #) -> r) -> r
$bTopeBottom :: forall (n :: S). Core n
TopeBottom = Node TopeBottomF
pattern TopeEQ :: Core n -> Core n -> Core n
pattern $mTopeEQ :: forall {r} {n :: S}.
Core n -> (Core n -> Core n -> r) -> ((# #) -> r) -> r
$bTopeEQ :: forall (n :: S). Core n -> Core n -> Core n
TopeEQ l r = Node (TopeEQF l r)
pattern TopeLEQ :: Core n -> Core n -> Core n
pattern $mTopeLEQ :: forall {r} {n :: S}.
Core n -> (Core n -> Core n -> r) -> ((# #) -> r) -> r
$bTopeLEQ :: forall (n :: S). Core n -> Core n -> Core n
TopeLEQ l r = Node (TopeLEQF l r)
pattern TopeAnd :: Core n -> Core n -> Core n
pattern $mTopeAnd :: forall {r} {n :: S}.
Core n -> (Core n -> Core n -> r) -> ((# #) -> r) -> r
$bTopeAnd :: forall (n :: S). Core n -> Core n -> Core n
TopeAnd l r = Node (TopeAndF l r)
pattern TopeOr :: Core n -> Core n -> Core n
pattern $mTopeOr :: forall {r} {n :: S}.
Core n -> (Core n -> Core n -> r) -> ((# #) -> r) -> r
$bTopeOr :: forall (n :: S). Core n -> Core n -> Core n
TopeOr l r = Node (TopeOrF l r)
pattern TopeInv :: Core n -> Core n
pattern $mTopeInv :: forall {r} {n :: S}. Core n -> (Core n -> r) -> ((# #) -> r) -> r
$bTopeInv :: forall (n :: S). Core n -> Core n
TopeInv t = Node (TopeInvF t)
pattern TopeUninv :: Core n -> Core n
pattern $mTopeUninv :: forall {r} {n :: S}. Core n -> (Core n -> r) -> ((# #) -> r) -> r
$bTopeUninv :: forall (n :: S). Core n -> Core n
TopeUninv t = Node (TopeUninvF t)
pattern RecBottom :: Core n
pattern $mRecBottom :: forall {r} {n :: S}. Core n -> ((# #) -> r) -> ((# #) -> r) -> r
$bRecBottom :: forall (n :: S). Core n
RecBottom = Node RecBottomF
pattern RecOr :: Core n -> [(Core n, Core n)] -> Core n
pattern $mRecOr :: forall {r} {n :: S}.
Core n -> (Core n -> [(Core n, Core n)] -> r) -> ((# #) -> r) -> r
$bRecOr :: forall (n :: S). Core n -> [(Core n, Core n)] -> Core n
RecOr ty rs = Node (RecOrF (Just ty) rs)
pattern TypeFun :: TModality -> Core n -> Maybe (Core n) -> ScopedCore n -> Core n
pattern $mTypeFun :: forall {r} {n :: S}.
Core n
-> (TModality -> Core n -> Maybe (Core n) -> ScopedCore n -> r)
-> ((# #) -> r)
-> r
$bTypeFun :: forall (n :: S).
TModality -> Core n -> Maybe (Core n) -> ScopedCore n -> Core n
TypeFun md dom mshape ret = Node (TypeFunF md dom mshape ret)
pattern TypeShape :: TModality -> Core n -> Maybe (Core n) -> Core n
pattern $mTypeShape :: forall {r} {n :: S}.
Core n
-> (TModality -> Core n -> Maybe (Core n) -> r)
-> ((# #) -> r)
-> r
$bTypeShape :: forall (n :: S). TModality -> Core n -> Maybe (Core n) -> Core n
TypeShape md cube mshape = Node (TypeShapeF md cube mshape)
pattern TypeSigma :: TModality -> Core n -> ScopedCore n -> Core n
pattern $mTypeSigma :: forall {r} {n :: S}.
Core n
-> (TModality -> Core n -> ScopedCore n -> r) -> ((# #) -> r) -> r
$bTypeSigma :: forall (n :: S). TModality -> Core n -> ScopedCore n -> Core n
TypeSigma md a b = Node (TypeSigmaF md a b)
pattern TypeId :: Core n -> Maybe (Core n) -> Core n -> Core n
pattern $mTypeId :: forall {r} {n :: S}.
Core n
-> (Core n -> Maybe (Core n) -> Core n -> r) -> ((# #) -> r) -> r
$bTypeId :: forall (n :: S). Core n -> Maybe (Core n) -> Core n -> Core n
TypeId x mtA y = Node (TypeIdF x mtA y)
pattern App :: Core n -> Core n -> Core n
pattern $mApp :: forall {r} {n :: S}.
Core n -> (Core n -> Core n -> r) -> ((# #) -> r) -> r
$bApp :: forall (n :: S). Core n -> Core n -> Core n
App f x = Node (AppF f x)
pattern Let :: Maybe (Core n) -> Core n -> ScopedCore n -> Core n
pattern $mLet :: forall {r} {n :: S}.
Core n
-> (Maybe (Core n) -> Core n -> ScopedCore n -> r)
-> ((# #) -> r)
-> r
$bLet :: forall (n :: S). Maybe (Core n) -> Core n -> ScopedCore n -> Core n
Let mty val body = Node (LetF mty val body)
pattern Lambda :: Maybe (Param (Core n)) -> ScopedCore n -> Core n
pattern $mLambda :: forall {r} {n :: S}.
Core n
-> (Maybe (Param (Core n)) -> ScopedCore n -> r)
-> ((# #) -> r)
-> r
$bLambda :: forall (n :: S). Maybe (Param (Core n)) -> ScopedCore n -> Core n
Lambda mparam body = Node (LambdaF mparam body)
pattern Pair :: Core n -> Core n -> Core n
pattern $mPair :: forall {r} {n :: S}.
Core n -> (Core n -> Core n -> r) -> ((# #) -> r) -> r
$bPair :: forall (n :: S). Core n -> Core n -> Core n
Pair l r = Node (PairF l r)
pattern First :: Core n -> Core n
pattern $mFirst :: forall {r} {n :: S}. Core n -> (Core n -> r) -> ((# #) -> r) -> r
$bFirst :: forall (n :: S). Core n -> Core n
First t = Node (FirstF t)
pattern Second :: Core n -> Core n
pattern $mSecond :: forall {r} {n :: S}. Core n -> (Core n -> r) -> ((# #) -> r) -> r
$bSecond :: forall (n :: S). Core n -> Core n
Second t = Node (SecondF t)
pattern Refl :: Maybe (Core n, Maybe (Core n)) -> Core n
pattern $mRefl :: forall {r} {n :: S}.
Core n
-> (Maybe (Core n, Maybe (Core n)) -> r) -> ((# #) -> r) -> r
$bRefl :: forall (n :: S). Maybe (Core n, Maybe (Core n)) -> Core n
Refl mx = Node (ReflF mx)
pattern IdJ :: Core n -> Core n -> Core n -> Core n -> Core n -> Core n -> Core n
pattern $mIdJ :: forall {r} {n :: S}.
Core n
-> (Core n -> Core n -> Core n -> Core n -> Core n -> Core n -> r)
-> ((# #) -> r)
-> r
$bIdJ :: forall (n :: S).
Core n -> Core n -> Core n -> Core n -> Core n -> Core n -> Core n
IdJ a b c d e f = Node (IdJF a b c d e f)
pattern Match :: TModality -> Core n -> Maybe (Core n) -> [(VarIdent, Core n)] -> Core n
pattern $mMatch :: forall {r} {n :: S}.
Core n
-> (TModality
    -> Core n -> Maybe (Core n) -> [(VarIdent, Core n)] -> r)
-> ((# #) -> r)
-> r
$bMatch :: forall (n :: S).
TModality
-> Core n -> Maybe (Core n) -> [(VarIdent, Core n)] -> Core n
Match md scrut mmotive branches = Node (MatchF md scrut mmotive branches)
pattern MatchArm :: ScopedCore n -> Core n
pattern $mMatchArm :: forall {r} {n :: S}.
Core n -> (ScopedCore n -> r) -> ((# #) -> r) -> r
$bMatchArm :: forall (n :: S). ScopedCore n -> Core n
MatchArm arm = Node (MatchArmF arm)
pattern Unit :: Core n
pattern $mUnit :: forall {r} {n :: S}. Core n -> ((# #) -> r) -> ((# #) -> r) -> r
$bUnit :: forall (n :: S). Core n
Unit = Node UnitF
pattern TypeUnit :: Core n
pattern $mTypeUnit :: forall {r} {n :: S}. Core n -> ((# #) -> r) -> ((# #) -> r) -> r
$bTypeUnit :: forall (n :: S). Core n
TypeUnit = Node TypeUnitF
pattern TypeAsc :: Core n -> Core n -> Core n
pattern $mTypeAsc :: forall {r} {n :: S}.
Core n -> (Core n -> Core n -> r) -> ((# #) -> r) -> r
$bTypeAsc :: forall (n :: S). Core n -> Core n -> Core n
TypeAsc term ty = Node (TypeAscF term ty)

pattern Ann :: Core n -> Core n -> Core n
pattern $mAnn :: forall {r} {n :: S}.
Core n -> (Core n -> Core n -> r) -> ((# #) -> r) -> r
$bAnn :: forall (n :: S). Core n -> Core n -> Core n
Ann term ty = Node (AnnF term ty)
pattern TypeRestricted :: Core n -> [(Core n, Core n)] -> Core n
pattern $mTypeRestricted :: forall {r} {n :: S}.
Core n -> (Core n -> [(Core n, Core n)] -> r) -> ((# #) -> r) -> r
$bTypeRestricted :: forall (n :: S). Core n -> [(Core n, Core n)] -> Core n
TypeRestricted ty rs = Node (TypeRestrictedF ty rs)
pattern TypeModal :: TModality -> Core n -> Core n
pattern $mTypeModal :: forall {r} {n :: S}.
Core n -> (TModality -> Core n -> r) -> ((# #) -> r) -> r
$bTypeModal :: forall (n :: S). TModality -> Core n -> Core n
TypeModal md ty = Node (TypeModalF md ty)
pattern ModApp :: TModality -> Core n -> Core n
pattern $mModApp :: forall {r} {n :: S}.
Core n -> (TModality -> Core n -> r) -> ((# #) -> r) -> r
$bModApp :: forall (n :: S). TModality -> Core n -> Core n
ModApp md t = Node (ModAppF md t)
pattern ModExtract :: TModality -> TModality -> Core n -> Core n
pattern $mModExtract :: forall {r} {n :: S}.
Core n
-> (TModality -> TModality -> Core n -> r) -> ((# #) -> r) -> r
$bModExtract :: forall (n :: S). TModality -> TModality -> Core n -> Core n
ModExtract app inn t = Node (ModExtractF app inn t)
pattern LetMod :: TModality -> TModality -> Maybe (Core n) -> Maybe (Core n) -> Core n -> ScopedCore n -> Core n
pattern $mLetMod :: forall {r} {n :: S}.
Core n
-> (TModality
    -> TModality
    -> Maybe (Core n)
    -> Maybe (Core n)
    -> Core n
    -> ScopedCore n
    -> r)
-> ((# #) -> r)
-> r
$bLetMod :: forall (n :: S).
TModality
-> TModality
-> Maybe (Core n)
-> Maybe (Core n)
-> Core n
-> ScopedCore n
-> Core n
LetMod app inn mty mmotive val body = Node (LetModF app inn mty mmotive val body)
pattern Hole :: Maybe VarIdent -> Core n
pattern $mHole :: forall {r} {n :: S}.
Core n -> (Maybe VarIdent -> r) -> ((# #) -> r) -> r
$bHole :: forall (n :: S). Maybe VarIdent -> Core n
Hole mname = Node (HoleF mname)

{-# COMPLETE Var, Universe, UniverseCube, UniverseTope, CubeUnit, CubeUnitStar,
  Cube2, Cube2_0, Cube2_1, CubeI, CubeI_0, CubeI_1, CubeProduct, CubeFlip,
  CubeUnflip, CubeSup, CubeInf, TopeTop, TopeBottom, TopeEQ, TopeLEQ, TopeAnd,
  TopeOr, TopeInv, TopeUninv, RecBottom, RecOr, TypeFun, TypeShape, TypeSigma, TypeId, App,
  Let, Lambda, Pair, First, Second, Refl, IdJ, Match, MatchArm, Unit, TypeUnit,
  TypeAsc, Ann, TypeRestricted, TypeModal, ModApp, ModExtract, LetMod, Hole #-}

-- * Raw patterns

-- | A raw node, matched without regard for where it was written.
pattern RawNode :: Sig (ScopedRaw n) (Raw n) -> Raw n
pattern $mRawNode :: forall {r} {n :: S}.
Raw n -> (Sig (ScopedRaw n) (Raw n) -> r) -> ((# #) -> r) -> r
RawNode sig <- Node (AnnSig _ sig)

{-# COMPLETE Var, RawNode #-}

-- | A raw node of each constructor, matched without regard for where it was
-- written: @RTypeFun@ for 'TypeFunF', and so on.
pattern RUniverse :: Raw n
pattern $mRUniverse :: forall {r} {n :: S}. Raw n -> ((# #) -> r) -> ((# #) -> r) -> r
RUniverse <- RawNode UniverseF
pattern RUniverseCube :: Raw n
pattern $mRUniverseCube :: forall {r} {n :: S}. Raw n -> ((# #) -> r) -> ((# #) -> r) -> r
RUniverseCube <- RawNode UniverseCubeF
pattern RUniverseTope :: Raw n
pattern $mRUniverseTope :: forall {r} {n :: S}. Raw n -> ((# #) -> r) -> ((# #) -> r) -> r
RUniverseTope <- RawNode UniverseTopeF
pattern RCubeUnit :: Raw n
pattern $mRCubeUnit :: forall {r} {n :: S}. Raw n -> ((# #) -> r) -> ((# #) -> r) -> r
RCubeUnit <- RawNode CubeUnitF
pattern RCubeUnitStar :: Raw n
pattern $mRCubeUnitStar :: forall {r} {n :: S}. Raw n -> ((# #) -> r) -> ((# #) -> r) -> r
RCubeUnitStar <- RawNode CubeUnitStarF
pattern RCube2 :: Raw n
pattern $mRCube2 :: forall {r} {n :: S}. Raw n -> ((# #) -> r) -> ((# #) -> r) -> r
RCube2 <- RawNode Cube2F
pattern RCube2_0 :: Raw n
pattern $mRCube2_0 :: forall {r} {n :: S}. Raw n -> ((# #) -> r) -> ((# #) -> r) -> r
RCube2_0 <- RawNode Cube2_0F
pattern RCube2_1 :: Raw n
pattern $mRCube2_1 :: forall {r} {n :: S}. Raw n -> ((# #) -> r) -> ((# #) -> r) -> r
RCube2_1 <- RawNode Cube2_1F
pattern RCubeI :: Raw n
pattern $mRCubeI :: forall {r} {n :: S}. Raw n -> ((# #) -> r) -> ((# #) -> r) -> r
RCubeI <- RawNode CubeIF
pattern RCubeI_0 :: Raw n
pattern $mRCubeI_0 :: forall {r} {n :: S}. Raw n -> ((# #) -> r) -> ((# #) -> r) -> r
RCubeI_0 <- RawNode CubeI_0F
pattern RCubeI_1 :: Raw n
pattern $mRCubeI_1 :: forall {r} {n :: S}. Raw n -> ((# #) -> r) -> ((# #) -> r) -> r
RCubeI_1 <- RawNode CubeI_1F
pattern RCubeProduct :: Raw n -> Raw n -> Raw n
pattern $mRCubeProduct :: forall {r} {n :: S}.
Raw n -> (Raw n -> Raw n -> r) -> ((# #) -> r) -> r
RCubeProduct x1 x2 <- RawNode (CubeProductF x1 x2)
pattern RCubeFlip :: Raw n -> Raw n
pattern $mRCubeFlip :: forall {r} {n :: S}. Raw n -> (Raw n -> r) -> ((# #) -> r) -> r
RCubeFlip x1 <- RawNode (CubeFlipF x1)
pattern RCubeUnflip :: Raw n -> Raw n
pattern $mRCubeUnflip :: forall {r} {n :: S}. Raw n -> (Raw n -> r) -> ((# #) -> r) -> r
RCubeUnflip x1 <- RawNode (CubeUnflipF x1)
pattern RCubeSup :: Raw n -> Raw n -> Raw n
pattern $mRCubeSup :: forall {r} {n :: S}.
Raw n -> (Raw n -> Raw n -> r) -> ((# #) -> r) -> r
RCubeSup x1 x2 <- RawNode (CubeSupF x1 x2)
pattern RCubeInf :: Raw n -> Raw n -> Raw n
pattern $mRCubeInf :: forall {r} {n :: S}.
Raw n -> (Raw n -> Raw n -> r) -> ((# #) -> r) -> r
RCubeInf x1 x2 <- RawNode (CubeInfF x1 x2)
pattern RTopeTop :: Raw n
pattern $mRTopeTop :: forall {r} {n :: S}. Raw n -> ((# #) -> r) -> ((# #) -> r) -> r
RTopeTop <- RawNode TopeTopF
pattern RTopeBottom :: Raw n
pattern $mRTopeBottom :: forall {r} {n :: S}. Raw n -> ((# #) -> r) -> ((# #) -> r) -> r
RTopeBottom <- RawNode TopeBottomF
pattern RTopeEQ :: Raw n -> Raw n -> Raw n
pattern $mRTopeEQ :: forall {r} {n :: S}.
Raw n -> (Raw n -> Raw n -> r) -> ((# #) -> r) -> r
RTopeEQ x1 x2 <- RawNode (TopeEQF x1 x2)
pattern RTopeLEQ :: Raw n -> Raw n -> Raw n
pattern $mRTopeLEQ :: forall {r} {n :: S}.
Raw n -> (Raw n -> Raw n -> r) -> ((# #) -> r) -> r
RTopeLEQ x1 x2 <- RawNode (TopeLEQF x1 x2)
pattern RTopeAnd :: Raw n -> Raw n -> Raw n
pattern $mRTopeAnd :: forall {r} {n :: S}.
Raw n -> (Raw n -> Raw n -> r) -> ((# #) -> r) -> r
RTopeAnd x1 x2 <- RawNode (TopeAndF x1 x2)
pattern RTopeOr :: Raw n -> Raw n -> Raw n
pattern $mRTopeOr :: forall {r} {n :: S}.
Raw n -> (Raw n -> Raw n -> r) -> ((# #) -> r) -> r
RTopeOr x1 x2 <- RawNode (TopeOrF x1 x2)
pattern RTopeInv :: Raw n -> Raw n
pattern $mRTopeInv :: forall {r} {n :: S}. Raw n -> (Raw n -> r) -> ((# #) -> r) -> r
RTopeInv x1 <- RawNode (TopeInvF x1)
pattern RTopeUninv :: Raw n -> Raw n
pattern $mRTopeUninv :: forall {r} {n :: S}. Raw n -> (Raw n -> r) -> ((# #) -> r) -> r
RTopeUninv x1 <- RawNode (TopeUninvF x1)
pattern RRecBottom :: Raw n
pattern $mRRecBottom :: forall {r} {n :: S}. Raw n -> ((# #) -> r) -> ((# #) -> r) -> r
RRecBottom <- RawNode RecBottomF
pattern RRecOr :: [(Raw n, Raw n)] -> Raw n
pattern $mRRecOr :: forall {r} {n :: S}.
Raw n -> ([(Raw n, Raw n)] -> r) -> ((# #) -> r) -> r
RRecOr x1 <- RawNode (RecOrF Nothing x1)
pattern RTypeFun :: TModality -> Raw n -> Maybe (Raw n) -> ScopedRaw n -> Raw n
pattern $mRTypeFun :: forall {r} {n :: S}.
Raw n
-> (TModality -> Raw n -> Maybe (Raw n) -> ScopedRaw n -> r)
-> ((# #) -> r)
-> r
RTypeFun x1 x2 x3 x4 <- RawNode (TypeFunF x1 x2 x3 x4)
pattern RTypeShape :: TModality -> Raw n -> Maybe (Raw n) -> Raw n
pattern $mRTypeShape :: forall {r} {n :: S}.
Raw n
-> (TModality -> Raw n -> Maybe (Raw n) -> r) -> ((# #) -> r) -> r
RTypeShape x1 x2 x3 <- RawNode (TypeShapeF x1 x2 x3)
pattern RTypeSigma :: TModality -> Raw n -> ScopedRaw n -> Raw n
pattern $mRTypeSigma :: forall {r} {n :: S}.
Raw n
-> (TModality -> Raw n -> ScopedRaw n -> r) -> ((# #) -> r) -> r
RTypeSigma x1 x2 x3 <- RawNode (TypeSigmaF x1 x2 x3)
pattern RTypeId :: Raw n -> Maybe (Raw n) -> Raw n -> Raw n
pattern $mRTypeId :: forall {r} {n :: S}.
Raw n
-> (Raw n -> Maybe (Raw n) -> Raw n -> r) -> ((# #) -> r) -> r
RTypeId x1 x2 x3 <- RawNode (TypeIdF x1 x2 x3)
pattern RApp :: Raw n -> Raw n -> Raw n
pattern $mRApp :: forall {r} {n :: S}.
Raw n -> (Raw n -> Raw n -> r) -> ((# #) -> r) -> r
RApp x1 x2 <- RawNode (AppF x1 x2)
pattern RLet :: Maybe (Raw n) -> Raw n -> ScopedRaw n -> Raw n
pattern $mRLet :: forall {r} {n :: S}.
Raw n
-> (Maybe (Raw n) -> Raw n -> ScopedRaw n -> r)
-> ((# #) -> r)
-> r
RLet x1 x2 x3 <- RawNode (LetF x1 x2 x3)
pattern RLambda :: Maybe (Param (Raw n)) -> ScopedRaw n -> Raw n
pattern $mRLambda :: forall {r} {n :: S}.
Raw n
-> (Maybe (Param (Raw n)) -> ScopedRaw n -> r) -> ((# #) -> r) -> r
RLambda x1 x2 <- RawNode (LambdaF x1 x2)
pattern RPair :: Raw n -> Raw n -> Raw n
pattern $mRPair :: forall {r} {n :: S}.
Raw n -> (Raw n -> Raw n -> r) -> ((# #) -> r) -> r
RPair x1 x2 <- RawNode (PairF x1 x2)
pattern RFirst :: Raw n -> Raw n
pattern $mRFirst :: forall {r} {n :: S}. Raw n -> (Raw n -> r) -> ((# #) -> r) -> r
RFirst x1 <- RawNode (FirstF x1)
pattern RSecond :: Raw n -> Raw n
pattern $mRSecond :: forall {r} {n :: S}. Raw n -> (Raw n -> r) -> ((# #) -> r) -> r
RSecond x1 <- RawNode (SecondF x1)
pattern RRefl :: Maybe (Raw n, Maybe (Raw n)) -> Raw n
pattern $mRRefl :: forall {r} {n :: S}.
Raw n -> (Maybe (Raw n, Maybe (Raw n)) -> r) -> ((# #) -> r) -> r
RRefl x1 <- RawNode (ReflF x1)
pattern RIdJ :: Raw n -> Raw n -> Raw n -> Raw n -> Raw n -> Raw n -> Raw n
pattern $mRIdJ :: forall {r} {n :: S}.
Raw n
-> (Raw n -> Raw n -> Raw n -> Raw n -> Raw n -> Raw n -> r)
-> ((# #) -> r)
-> r
RIdJ x1 x2 x3 x4 x5 x6 <- RawNode (IdJF x1 x2 x3 x4 x5 x6)
pattern RMatch :: TModality -> Raw n -> Maybe (Raw n) -> [(VarIdent, Raw n)] -> Raw n
pattern $mRMatch :: forall {r} {n :: S}.
Raw n
-> (TModality
    -> Raw n -> Maybe (Raw n) -> [(VarIdent, Raw n)] -> r)
-> ((# #) -> r)
-> r
RMatch x1 x2 x3 x4 <- RawNode (MatchF x1 x2 x3 x4)
pattern RMatchArm :: ScopedRaw n -> Raw n
pattern $mRMatchArm :: forall {r} {n :: S}.
Raw n -> (ScopedRaw n -> r) -> ((# #) -> r) -> r
RMatchArm x1 <- RawNode (MatchArmF x1)
pattern RUnit :: Raw n
pattern $mRUnit :: forall {r} {n :: S}. Raw n -> ((# #) -> r) -> ((# #) -> r) -> r
RUnit <- RawNode UnitF
pattern RTypeUnit :: Raw n
pattern $mRTypeUnit :: forall {r} {n :: S}. Raw n -> ((# #) -> r) -> ((# #) -> r) -> r
RTypeUnit <- RawNode TypeUnitF
pattern RTypeAsc :: Raw n -> Raw n -> Raw n
pattern $mRTypeAsc :: forall {r} {n :: S}.
Raw n -> (Raw n -> Raw n -> r) -> ((# #) -> r) -> r
RTypeAsc x1 x2 <- RawNode (TypeAscF x1 x2)
pattern RAnn :: Raw n -> Raw n -> Raw n
pattern $mRAnn :: forall {r} {n :: S}.
Raw n -> (Raw n -> Raw n -> r) -> ((# #) -> r) -> r
RAnn x1 x2 <- RawNode (AnnF x1 x2)
pattern RTypeRestricted :: Raw n -> [(Raw n, Raw n)] -> Raw n
pattern $mRTypeRestricted :: forall {r} {n :: S}.
Raw n -> (Raw n -> [(Raw n, Raw n)] -> r) -> ((# #) -> r) -> r
RTypeRestricted x1 x2 <- RawNode (TypeRestrictedF x1 x2)
pattern RTypeModal :: TModality -> Raw n -> Raw n
pattern $mRTypeModal :: forall {r} {n :: S}.
Raw n -> (TModality -> Raw n -> r) -> ((# #) -> r) -> r
RTypeModal x1 x2 <- RawNode (TypeModalF x1 x2)
pattern RModApp :: TModality -> Raw n -> Raw n
pattern $mRModApp :: forall {r} {n :: S}.
Raw n -> (TModality -> Raw n -> r) -> ((# #) -> r) -> r
RModApp x1 x2 <- RawNode (ModAppF x1 x2)
pattern RModExtract :: TModality -> TModality -> Raw n -> Raw n
pattern $mRModExtract :: forall {r} {n :: S}.
Raw n
-> (TModality -> TModality -> Raw n -> r) -> ((# #) -> r) -> r
RModExtract x1 x2 x3 <- RawNode (ModExtractF x1 x2 x3)
pattern RLetMod :: TModality -> TModality -> Maybe (Raw n) -> Maybe (Raw n) -> Raw n -> ScopedRaw n -> Raw n
pattern $mRLetMod :: forall {r} {n :: S}.
Raw n
-> (TModality
    -> TModality
    -> Maybe (Raw n)
    -> Maybe (Raw n)
    -> Raw n
    -> ScopedRaw n
    -> r)
-> ((# #) -> r)
-> r
RLetMod x1 x2 x3 x4 x5 x6 <- RawNode (LetModF x1 x2 x3 x4 x5 x6)
pattern RHole :: Maybe VarIdent -> Raw n
pattern $mRHole :: forall {r} {n :: S}.
Raw n -> (Maybe VarIdent -> r) -> ((# #) -> r) -> r
RHole x1 <- RawNode (HoleF x1)

{-# COMPLETE Var, RUniverse, RUniverseCube, RUniverseTope, RCubeUnit, RCubeUnitStar, RCube2, RCube2_0, RCube2_1, RCubeI, RCubeI_0, RCubeI_1, RCubeProduct, RCubeFlip, RCubeUnflip, RCubeSup, RCubeInf, RTopeTop, RTopeBottom, RTopeEQ, RTopeLEQ, RTopeAnd, RTopeOr, RTopeInv, RTopeUninv, RRecBottom, RRecOr, RTypeFun, RTypeShape, RTypeSigma, RTypeId, RApp, RLet, RLambda, RPair, RFirst, RSecond, RRefl, RIdJ, RMatch, RMatchArm, RUnit, RTypeUnit, RTypeAsc, RAnn, RTypeRestricted, RTypeModal, RModApp, RModExtract, RLetMod, RHole #-}

-- * Queries

-- | The free names of a term, in order of mention, with repeats. Names bound
-- on the way down are removed with 'Foil.unsinkNamePattern'.
freeNamesOf :: Distinct n => Core n -> [Foil.Name n]
freeNamesOf :: forall (n :: S). Distinct n => Core n -> [Name n]
freeNamesOf (Var Name n
x) = [Name n
x]
freeNamesOf (Ann AST Pattern Sig n
t AST Pattern Sig n
_) = AST Pattern Sig n -> [Name n]
forall (n :: S). Distinct n => Core n -> [Name n]
freeNamesOf AST Pattern Sig n
t
freeNamesOf (Node Sig (ScopedAST Pattern Sig n) (AST Pattern Sig n)
sig) = (ScopedAST Pattern Sig n -> [Name n])
-> (AST Pattern Sig n -> [Name n])
-> Sig (ScopedAST Pattern Sig n) (AST Pattern Sig n)
-> [Name 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 Pattern Sig n -> [Name n]
forall (n' :: S). Distinct n' => ScopedCore n' -> [Name n']
scoped AST Pattern Sig n -> [Name n]
forall (n :: S). Distinct n => Core n -> [Name n]
freeNamesOf Sig (ScopedAST Pattern Sig n) (AST Pattern Sig n)
sig
  where
    scoped :: Distinct n' => ScopedCore n' -> [Foil.Name n']
    scoped :: forall (n' :: S). Distinct n' => ScopedCore n' -> [Name n']
scoped (ScopedAST Pattern n' l
pat AST Pattern Sig l
body) = case Pattern n' l -> DistinctEvidence l
forall (n :: S) (pattern :: S -> S -> *) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> DistinctEvidence l
Foil.assertDistinct Pattern n' l
pat of
      DistinctEvidence l
Foil.Distinct -> (Name l -> Maybe (Name n')) -> [Name l] -> [Name n']
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Pattern n' l -> Name l -> Maybe (Name n')
forall (pattern :: S -> S -> *) (n :: S) (l :: S).
(Distinct n, CoSinkable pattern) =>
pattern n l -> Name l -> Maybe (Name n)
Foil.unsinkNamePattern Pattern n' l
pat) (AST Pattern Sig l -> [Name l]
forall (n :: S). Distinct n => Core n -> [Name n]
freeNamesOf AST Pattern Sig l
body)

-- | A term without the annotations of its neutrals, which the library's
-- readback keeps. The types are not looked at.
stripAnnotations :: Core n -> Core n
stripAnnotations :: forall (n :: S). Core n -> Core n
stripAnnotations = \case
  Var Name n
x    -> Name n -> Core n
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var Name n
x
  Ann Core n
t Core n
_  -> Core n -> Core n
forall (n :: S). Core n -> Core n
stripAnnotations Core n
t
  Node Sig (ScopedAST Pattern Sig n) (Core n)
sig -> Sig (ScopedAST Pattern Sig n) (Core n) -> Core n
forall (sig :: * -> * -> *) (binder :: S -> S -> *) (n :: S).
sig (ScopedAST binder sig n) (AST binder sig n) -> AST binder sig n
Node ((ScopedAST Pattern Sig n -> ScopedAST Pattern Sig n)
-> (Core n -> Core n)
-> Sig (ScopedAST Pattern Sig n) (Core n)
-> Sig (ScopedAST Pattern Sig n) (Core 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 (\(ScopedAST Pattern n l
pat AST Pattern Sig l
body) -> Pattern n l -> AST Pattern Sig l -> ScopedAST Pattern Sig n
forall (binder :: S -> S -> *) (n :: S) (l :: S)
       (sig :: * -> * -> *).
binder n l -> AST binder sig l -> ScopedAST binder sig n
ScopedAST Pattern n l
pat (AST Pattern Sig l -> AST Pattern Sig l
forall (n :: S). Core n -> Core n
stripAnnotations AST Pattern Sig l
body)) Core n -> Core n
forall (n :: S). Core n -> Core n
stripAnnotations Sig (ScopedAST Pattern Sig n) (Core n)
sig)

-- | Replace a free name of a term by a term of the same scope.
substituteName :: Foil.Distinct n => Foil.Scope n -> Foil.Name n -> Core n -> Core n -> Core n
substituteName :: forall (n :: S).
Distinct n =>
Scope n -> Name n -> Core n -> Core n -> Core n
substituteName Scope n
scope Name n
name Core n
value =
  Scope n -> Substitution (AST Pattern Sig) n n -> Core n -> Core n
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 n
scope (Name n -> Core n -> Substitution (AST Pattern Sig) n n
forall (i :: S) (e :: S -> *) (o :: S).
Name i -> e o -> Substitution e i o
singletonSubstitution Name n
name Core n
value)

-- | Abstract a free name out of a term: the pattern the continuation
-- receives binds what the name stood for, under the name's display name.
-- The name stays in the scope index, as a scope only grows, but the term
-- no longer mentions it.
abstractName
  :: Foil.Distinct n
  => Foil.Scope n -> Maybe VarIdent -> Foil.Name n -> Core n
  -> (forall l. (Foil.DExt n l, Foil.Distinct l) => Pattern n l -> Core l -> r)
  -> r
abstractName :: forall (n :: S) r.
Distinct n =>
Scope n
-> Maybe VarIdent
-> Name n
-> Core n
-> (forall (l :: S).
    (DExt n l, Distinct l) =>
    Pattern n l -> Core l -> r)
-> r
abstractName Scope n
scope Maybe VarIdent
mname Name n
name Core n
term forall (l :: S).
(DExt n l, Distinct l) =>
Pattern n l -> Core l -> r
k = Scope n -> (forall (l :: S). DExt n l => NameBinder n l -> r) -> r
forall (n :: S) r.
Distinct n =>
Scope n -> (forall (l :: S). DExt n l => NameBinder n l -> r) -> r
Foil.withFresh Scope n
scope ((forall (l :: S). DExt n l => NameBinder n l -> r) -> r)
-> (forall (l :: S). DExt n l => NameBinder n l -> r) -> r
forall a b. (a -> b) -> a -> b
$ \NameBinder n l
binder ->
  let scope' :: Scope l
scope' = NameBinder n l -> Scope n -> Scope l
forall (n :: S) (l :: S). NameBinder n l -> Scope n -> Scope l
Foil.extendScope NameBinder n l
binder Scope n
scope
      term' :: Core l
term' = Scope l -> Name l -> Core l -> Core l -> Core l
forall (n :: S).
Distinct n =>
Scope n -> Name n -> Core n -> Core n -> Core n
substituteName Scope l
scope' (Name n -> Name l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Name n
name) (Name l -> Core l
forall (n :: S) (binder :: S -> S -> *) (sig :: * -> * -> *).
Name n -> AST binder sig n
Var (NameBinder n l -> Name l
forall (n :: S) (l :: S). NameBinder n l -> Name l
Foil.nameOf NameBinder n l
binder)) (Core n -> Core l
forall (e :: S -> *) (n :: S) (l :: S).
(Sinkable e, DExt n l) =>
e n -> e l
Foil.sink Core n
term)
  in Pattern n l -> Core l -> r
forall (l :: S).
(DExt n l, Distinct l) =>
Pattern n l -> Core l -> r
k (Maybe VarIdent -> NameBinder n l -> Pattern n l
forall (n :: S) (l :: S).
Maybe VarIdent -> NameBinder n l -> Pattern n l
PatternVar Maybe VarIdent
mname NameBinder n l
binder) Core l
term'

-- | Does the term contain a hole anywhere?
containsHole :: Core n -> Bool
containsHole :: forall (n :: S). Core n -> Bool
containsHole (Var Name n
_) = Bool
False
containsHole (Node (HoleF Maybe VarIdent
_)) = Bool
True
containsHole (Node Sig (ScopedAST Pattern Sig n) (AST Pattern Sig n)
sig) =
  (ScopedAST Pattern Sig n -> Bool -> Bool)
-> (AST Pattern Sig n -> Bool -> Bool)
-> Bool
-> Sig (ScopedAST Pattern Sig n) (AST Pattern Sig n)
-> Bool
forall a c b. (a -> c -> c) -> (b -> c -> c) -> c -> Sig a b -> c
forall (p :: * -> * -> *) a c b.
Bifoldable p =>
(a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c
bifoldr (\(ScopedAST Pattern n l
_ AST Pattern Sig l
body) Bool
acc -> AST Pattern Sig l -> Bool
forall (n :: S). Core n -> Bool
containsHole AST Pattern Sig l
body Bool -> Bool -> Bool
|| Bool
acc)
          (\AST Pattern Sig n
t Bool
acc -> AST Pattern Sig n -> Bool
forall (n :: S). Core n -> Bool
containsHole AST Pattern Sig n
t Bool -> Bool -> Bool
|| Bool
acc) Bool
False Sig (ScopedAST Pattern Sig n) (AST Pattern Sig n)
sig

-- | The name of every hole in a term.
holeNamesOf :: Core n -> [Maybe VarIdent]
holeNamesOf :: forall (n :: S). Core n -> [Maybe VarIdent]
holeNamesOf (Var Name n
_) = []
holeNamesOf (Node (HoleF Maybe VarIdent
mname)) = [Maybe VarIdent
mname]
holeNamesOf (Node Sig (ScopedAST Pattern Sig n) (AST Pattern Sig n)
sig) =
  (ScopedAST Pattern Sig n -> [Maybe VarIdent] -> [Maybe VarIdent])
-> (AST Pattern Sig n -> [Maybe VarIdent] -> [Maybe VarIdent])
-> [Maybe VarIdent]
-> Sig (ScopedAST Pattern Sig n) (AST Pattern Sig n)
-> [Maybe VarIdent]
forall a c b. (a -> c -> c) -> (b -> c -> c) -> c -> Sig a b -> c
forall (p :: * -> * -> *) a c b.
Bifoldable p =>
(a -> c -> c) -> (b -> c -> c) -> c -> p a b -> c
bifoldr (\(ScopedAST Pattern n l
_ AST Pattern Sig l
body) [Maybe VarIdent]
acc -> AST Pattern Sig l -> [Maybe VarIdent]
forall (n :: S). Core n -> [Maybe VarIdent]
holeNamesOf AST Pattern Sig l
body [Maybe VarIdent] -> [Maybe VarIdent] -> [Maybe VarIdent]
forall a. Semigroup a => a -> a -> a
<> [Maybe VarIdent]
acc)
          (\AST Pattern Sig n
t [Maybe VarIdent]
acc -> AST Pattern Sig n -> [Maybe VarIdent]
forall (n :: S). Core n -> [Maybe VarIdent]
holeNamesOf AST Pattern Sig n
t [Maybe VarIdent] -> [Maybe VarIdent] -> [Maybe VarIdent]
forall a. Semigroup a => a -> a -> a
<> [Maybe VarIdent]
acc) [] Sig (ScopedAST Pattern Sig n) (AST Pattern Sig n)
sig

-- | Is the term a hole, or an elimination headed by one (@? a@, @first ?@)?
-- Such a term has no shape of its own yet.
isHoleHeaded :: Core n -> Bool
isHoleHeaded :: forall (n :: S). Core n -> Bool
isHoleHeaded = \case
  Hole Maybe VarIdent
_     -> Bool
True
  App Core n
f Core n
_    -> Core n -> Bool
forall (n :: S). Core n -> Bool
isHoleHeaded Core n
f
  First Core n
t    -> Core n -> Bool
forall (n :: S). Core n -> Bool
isHoleHeaded Core n
t
  Second Core n
t   -> Core n -> Bool
forall (n :: S). Core n -> Bool
isHoleHeaded Core n
t
  Core n
_          -> Bool
False

-- | Syntactic equality of two core terms: conservative, as two α-equivalent
-- terms whose binders differ are not equal, and cheaper than α-equivalence.
eqCore :: Foil.Distinct n => Core n -> Core n -> Bool
eqCore :: forall (n :: S). Distinct n => Core n -> Core n -> Bool
eqCore = AST Pattern Sig n -> AST Pattern Sig n -> Bool
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