{-# OPTIONS_GHC -fno-warn-missing-pattern-synonym-signatures -fno-warn-missing-signatures -fno-warn-type-defaults #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeSynonymInstances #-}
module Language.Rzk.Free.Syntax where
import Data.Bifunctor.TH
import Data.Char (chr, ord)
import Data.Coerce
import Data.Function (on)
import Data.Functor (void)
import Data.List (intercalate, nub, (\\))
import Data.Maybe (fromMaybe)
import Data.String
import Free.Scoped
import Free.Scoped.TH
import Debug.Trace
import qualified Language.Rzk.Syntax as Rzk
data RzkPosition = RzkPosition
{ RzkPosition -> Maybe [Char]
rzkFilePath :: Maybe FilePath
, RzkPosition -> BNFC'Position
rzkLineCol :: Rzk.BNFC'Position
}
ppRzkPosition :: RzkPosition -> String
ppRzkPosition :: RzkPosition -> [Char]
ppRzkPosition RzkPosition{Maybe [Char]
BNFC'Position
rzkFilePath :: RzkPosition -> Maybe [Char]
rzkLineCol :: RzkPosition -> BNFC'Position
rzkFilePath :: Maybe [Char]
rzkLineCol :: BNFC'Position
..} = [Char] -> [[Char]] -> [Char]
forall a. [a] -> [[a]] -> [a]
intercalate [Char]
":" ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ [[[Char]]] -> [[Char]]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [[Char] -> Maybe [Char] -> [Char]
forall a. a -> Maybe a -> a
fromMaybe [Char]
"<stdin>" Maybe [Char]
rzkFilePath]
, ((Int, Int) -> [[Char]]) -> BNFC'Position -> [[Char]]
forall m a. Monoid m => (a -> m) -> Maybe a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\(Int
row, Int
col) -> (Int -> [Char]) -> [Int] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map Int -> [Char]
forall a. Show a => a -> [Char]
show [Int
row, Int
col]) BNFC'Position
rzkLineCol]
newtype VarIdent = VarIdent { VarIdent -> VarIdent' RzkPosition
getVarIdent :: Rzk.VarIdent' RzkPosition }
instance Show VarIdent where
show :: VarIdent -> [Char]
show = VarIdent' RzkPosition -> [Char]
forall a. Print a => a -> [Char]
Rzk.printTree (VarIdent' RzkPosition -> [Char])
-> (VarIdent -> VarIdent' RzkPosition) -> VarIdent -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VarIdent -> VarIdent' RzkPosition
getVarIdent
instance Eq VarIdent where
== :: VarIdent -> VarIdent -> Bool
(==) = VarIdent' () -> VarIdent' () -> Bool
forall a. Eq a => a -> a -> Bool
(==) (VarIdent' () -> VarIdent' () -> Bool)
-> (VarIdent -> VarIdent' ()) -> VarIdent -> VarIdent -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (VarIdent' RzkPosition -> VarIdent' ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (VarIdent' RzkPosition -> VarIdent' ())
-> (VarIdent -> VarIdent' RzkPosition) -> VarIdent -> VarIdent' ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VarIdent -> VarIdent' RzkPosition
getVarIdent)
instance IsString VarIdent where
fromString :: [Char] -> VarIdent
fromString [Char]
s = VarIdent' RzkPosition -> VarIdent
VarIdent (RzkPosition -> VarIdentToken -> VarIdent' RzkPosition
forall a. a -> VarIdentToken -> VarIdent' a
Rzk.VarIdent (Maybe [Char] -> BNFC'Position -> RzkPosition
RzkPosition Maybe [Char]
forall a. Maybe a
Nothing BNFC'Position
forall a. Maybe a
Nothing) ([Char] -> VarIdentToken
forall a. IsString a => [Char] -> a
fromString [Char]
s))
ppVarIdentWithLocation :: VarIdent -> String
ppVarIdentWithLocation :: VarIdent -> [Char]
ppVarIdentWithLocation (VarIdent var :: VarIdent' RzkPosition
var@(Rzk.VarIdent RzkPosition
pos VarIdentToken
_ident)) =
VarIdent' RzkPosition -> [Char]
forall a. Print a => a -> [Char]
Rzk.printTree VarIdent' RzkPosition
var [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
" (" [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> RzkPosition -> [Char]
ppRzkPosition RzkPosition
pos [Char] -> [Char] -> [Char]
forall a. Semigroup a => a -> a -> a
<> [Char]
")"
varIdent :: Rzk.VarIdent -> VarIdent
varIdent :: VarIdent -> VarIdent
varIdent = Maybe [Char] -> VarIdent -> VarIdent
varIdentAt Maybe [Char]
forall a. Maybe a
Nothing
varIdentAt :: Maybe FilePath -> Rzk.VarIdent -> VarIdent
varIdentAt :: Maybe [Char] -> VarIdent -> VarIdent
varIdentAt Maybe [Char]
path (Rzk.VarIdent BNFC'Position
pos VarIdentToken
ident) = VarIdent' RzkPosition -> VarIdent
VarIdent (RzkPosition -> VarIdentToken -> VarIdent' RzkPosition
forall a. a -> VarIdentToken -> VarIdent' a
Rzk.VarIdent (Maybe [Char] -> BNFC'Position -> RzkPosition
RzkPosition Maybe [Char]
path BNFC'Position
pos) VarIdentToken
ident)
fromVarIdent :: VarIdent -> Rzk.VarIdent
fromVarIdent :: VarIdent -> VarIdent
fromVarIdent (VarIdent (Rzk.VarIdent (RzkPosition Maybe [Char]
_file BNFC'Position
pos) VarIdentToken
ident)) = BNFC'Position -> VarIdentToken -> VarIdent
forall a. a -> VarIdentToken -> VarIdent' a
Rzk.VarIdent BNFC'Position
pos VarIdentToken
ident
data TermF scope term
= UniverseF
| UniverseCubeF
| UniverseTopeF
| CubeUnitF
| CubeUnitStarF
| Cube2F
| Cube2_0F
| Cube2_1F
| CubeProductF term term
| TopeTopF
| TopeBottomF
| TopeEQF term term
| TopeLEQF term term
| TopeAndF term term
| TopeOrF term term
| RecBottomF
| RecOrF [(term, term)]
| TypeFunF (Maybe VarIdent) term (Maybe scope) scope
| TypeSigmaF (Maybe VarIdent) term scope
| TypeIdF term (Maybe term) term
| AppF term term
| LambdaF (Maybe VarIdent) (Maybe (term, Maybe scope)) scope
| PairF term term
| FirstF term
| SecondF term
| ReflF (Maybe (term, Maybe term))
| IdJF term term term term term term
| UnitF
| TypeUnitF
| TypeAscF term term
| TypeRestrictedF term [(term, term)]
deriving (TermF scope term -> TermF scope term -> Bool
(TermF scope term -> TermF scope term -> Bool)
-> (TermF scope term -> TermF scope term -> Bool)
-> Eq (TermF scope term)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall scope term.
(Eq scope, Eq term) =>
TermF scope term -> TermF scope term -> Bool
$c== :: forall scope term.
(Eq scope, Eq term) =>
TermF scope term -> TermF scope term -> Bool
== :: TermF scope term -> TermF scope term -> Bool
$c/= :: forall scope term.
(Eq scope, Eq term) =>
TermF scope term -> TermF scope term -> Bool
/= :: TermF scope term -> TermF scope term -> Bool
Eq, (forall a b. (a -> b) -> TermF scope a -> TermF scope b)
-> (forall a b. a -> TermF scope b -> TermF scope a)
-> Functor (TermF scope)
forall a b. a -> TermF scope b -> TermF scope a
forall a b. (a -> b) -> TermF scope a -> TermF scope b
forall scope a b. a -> TermF scope b -> TermF scope a
forall scope a b. (a -> b) -> TermF scope a -> TermF 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) -> TermF scope a -> TermF scope b
fmap :: forall a b. (a -> b) -> TermF scope a -> TermF scope b
$c<$ :: forall scope a b. a -> TermF scope b -> TermF scope a
<$ :: forall a b. a -> TermF scope b -> TermF scope a
Functor, (forall m. Monoid m => TermF scope m -> m)
-> (forall m a. Monoid m => (a -> m) -> TermF scope a -> m)
-> (forall m a. Monoid m => (a -> m) -> TermF scope a -> m)
-> (forall a b. (a -> b -> b) -> b -> TermF scope a -> b)
-> (forall a b. (a -> b -> b) -> b -> TermF scope a -> b)
-> (forall b a. (b -> a -> b) -> b -> TermF scope a -> b)
-> (forall b a. (b -> a -> b) -> b -> TermF scope a -> b)
-> (forall a. (a -> a -> a) -> TermF scope a -> a)
-> (forall a. (a -> a -> a) -> TermF scope a -> a)
-> (forall a. TermF scope a -> [a])
-> (forall a. TermF scope a -> Bool)
-> (forall a. TermF scope a -> Int)
-> (forall a. Eq a => a -> TermF scope a -> Bool)
-> (forall a. Ord a => TermF scope a -> a)
-> (forall a. Ord a => TermF scope a -> a)
-> (forall a. Num a => TermF scope a -> a)
-> (forall a. Num a => TermF scope a -> a)
-> Foldable (TermF scope)
forall a. Eq a => a -> TermF scope a -> Bool
forall a. Num a => TermF scope a -> a
forall a. Ord a => TermF scope a -> a
forall m. Monoid m => TermF scope m -> m
forall a. TermF scope a -> Bool
forall a. TermF scope a -> Int
forall a. TermF scope a -> [a]
forall a. (a -> a -> a) -> TermF scope a -> a
forall scope a. Eq a => a -> TermF scope a -> Bool
forall scope a. Num a => TermF scope a -> a
forall scope a. Ord a => TermF scope a -> a
forall scope m. Monoid m => TermF scope m -> m
forall m a. Monoid m => (a -> m) -> TermF scope a -> m
forall scope a. TermF scope a -> Bool
forall scope a. TermF scope a -> Int
forall scope a. TermF scope a -> [a]
forall b a. (b -> a -> b) -> b -> TermF scope a -> b
forall a b. (a -> b -> b) -> b -> TermF scope a -> b
forall scope a. (a -> a -> a) -> TermF scope a -> a
forall scope m a. Monoid m => (a -> m) -> TermF scope a -> m
forall scope b a. (b -> a -> b) -> b -> TermF scope a -> b
forall scope a b. (a -> b -> b) -> b -> TermF 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 -> 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 scope m. Monoid m => TermF scope m -> m
fold :: forall m. Monoid m => TermF scope m -> m
$cfoldMap :: forall scope m a. Monoid m => (a -> m) -> TermF scope a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> TermF scope a -> m
$cfoldMap' :: forall scope m a. Monoid m => (a -> m) -> TermF scope a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> TermF scope a -> m
$cfoldr :: forall scope a b. (a -> b -> b) -> b -> TermF scope a -> b
foldr :: forall a b. (a -> b -> b) -> b -> TermF scope a -> b
$cfoldr' :: forall scope a b. (a -> b -> b) -> b -> TermF scope a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> TermF scope a -> b
$cfoldl :: forall scope b a. (b -> a -> b) -> b -> TermF scope a -> b
foldl :: forall b a. (b -> a -> b) -> b -> TermF scope a -> b
$cfoldl' :: forall scope b a. (b -> a -> b) -> b -> TermF scope a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> TermF scope a -> b
$cfoldr1 :: forall scope a. (a -> a -> a) -> TermF scope a -> a
foldr1 :: forall a. (a -> a -> a) -> TermF scope a -> a
$cfoldl1 :: forall scope a. (a -> a -> a) -> TermF scope a -> a
foldl1 :: forall a. (a -> a -> a) -> TermF scope a -> a
$ctoList :: forall scope a. TermF scope a -> [a]
toList :: forall a. TermF scope a -> [a]
$cnull :: forall scope a. TermF scope a -> Bool
null :: forall a. TermF scope a -> Bool
$clength :: forall scope a. TermF scope a -> Int
length :: forall a. TermF scope a -> Int
$celem :: forall scope a. Eq a => a -> TermF scope a -> Bool
elem :: forall a. Eq a => a -> TermF scope a -> Bool
$cmaximum :: forall scope a. Ord a => TermF scope a -> a
maximum :: forall a. Ord a => TermF scope a -> a
$cminimum :: forall scope a. Ord a => TermF scope a -> a
minimum :: forall a. Ord a => TermF scope a -> a
$csum :: forall scope a. Num a => TermF scope a -> a
sum :: forall a. Num a => TermF scope a -> a
$cproduct :: forall scope a. Num a => TermF scope a -> a
product :: forall a. Num a => TermF scope a -> a
Foldable, Functor (TermF scope)
Foldable (TermF scope)
(Functor (TermF scope), Foldable (TermF scope)) =>
(forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> TermF scope a -> f (TermF scope b))
-> (forall (f :: * -> *) a.
Applicative f =>
TermF scope (f a) -> f (TermF scope a))
-> (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> TermF scope a -> m (TermF scope b))
-> (forall (m :: * -> *) a.
Monad m =>
TermF scope (m a) -> m (TermF scope a))
-> Traversable (TermF scope)
forall scope. Functor (TermF scope)
forall scope. Foldable (TermF scope)
forall scope (m :: * -> *) a.
Monad m =>
TermF scope (m a) -> m (TermF scope a)
forall scope (f :: * -> *) a.
Applicative f =>
TermF scope (f a) -> f (TermF scope a)
forall scope (m :: * -> *) a b.
Monad m =>
(a -> m b) -> TermF scope a -> m (TermF scope b)
forall scope (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> TermF scope a -> f (TermF 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 =>
TermF scope (m a) -> m (TermF scope a)
forall (f :: * -> *) a.
Applicative f =>
TermF scope (f a) -> f (TermF scope a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> TermF scope a -> m (TermF scope b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> TermF scope a -> f (TermF scope b)
$ctraverse :: forall scope (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> TermF scope a -> f (TermF scope b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> TermF scope a -> f (TermF scope b)
$csequenceA :: forall scope (f :: * -> *) a.
Applicative f =>
TermF scope (f a) -> f (TermF scope a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
TermF scope (f a) -> f (TermF scope a)
$cmapM :: forall scope (m :: * -> *) a b.
Monad m =>
(a -> m b) -> TermF scope a -> m (TermF scope b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> TermF scope a -> m (TermF scope b)
$csequence :: forall scope (m :: * -> *) a.
Monad m =>
TermF scope (m a) -> m (TermF scope a)
sequence :: forall (m :: * -> *) a.
Monad m =>
TermF scope (m a) -> m (TermF scope a)
Traversable)
deriveBifunctor ''TermF
deriveBifoldable ''TermF
deriveBitraversable ''TermF