{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE UndecidableInstances #-}
+{-# OPTIONS_GHC -Wno-missing-signatures #-}
+{-# OPTIONS_GHC -Wno-monomorphism-restriction #-}
module Symantic.Syntaxes where
-import Control.Applicative (Applicative (..))
-import Control.Monad (Monad (..))
-import Control.Monad.Trans.Except qualified as MT
-import Control.Monad.Trans.Reader qualified as MT
-import Control.Monad.Trans.State qualified as MT
-import Data.Bool (otherwise)
import Data.Either (Either (..))
-import Data.Eq (Eq (..))
-import Data.Function (id, ($), (.))
-import Data.Functor (Functor (..), (<$>))
-import Data.Functor.Constant (Constant (..))
+import Data.Function (($))
import Data.Int (Int)
-import Data.Kind (Constraint, Type)
-import Data.Maybe (Maybe (..), isJust)
-import Data.Proxy (Proxy (..))
+import Data.Kind (Type)
+import Data.Maybe (Maybe (..))
import Data.Semigroup (Semigroup (..))
-import Data.String (String)
-import GHC.Types
-import Text.Read (Read (..), reads)
import Text.Show (Show (..))
-import Type.Reflection (TypeRep, Typeable, eqTypeRep, typeRep, (:~~:) (..))
+import Type.Reflection (typeRep, (:~~:) (..))
import Unsafe.Coerce (unsafeCoerce)
import Prelude (error)
-import Prelude qualified
import Symantic
-import Symantic.Typer
+import Symantic.Typer ()
class Addable sem where
lit :: Int -> sem Int
mul :: sem Int -> sem Int -> sem Int
class Abstractable sem where
-- | Lambda term abstraction, in HOAS (Higher-Order Abstract Syntax) style.
- lam :: Typeable a => (sem a -> sem b) -> sem (a -> b)
+ lam :: Tyable a => (sem a -> sem b) -> sem (a -> b)
-- | Application, aka. unabstract.
(.@) :: sem (a -> b) -> sem a -> sem b
ForallSem a2b .@ ForallSem a = ForallSem (a2b .@ a)
lam f = ForallSem (lam (\a -> let ForallSem b = f (forallSem a) in b))
where
- -- Safe here because a :: sem a and b :: sem b share the same 'sem'.
+ -- Safe here because (a :: sem a) and (b :: sem b) share the same 'sem'.
forallSem :: sem a -> ForallSem syn a
forallSem a = ForallSem (unsafeCoerce a)
BinTree0
( TokenTermAbst
"x"
- (TyVT TyConst{tyConst = Const{constType = typeRep @a, constMeta = ()}, tyConstLen = LenZ})
+ (TyVT (typeOf @a))
(unPrinter (f (Printer (BinTree0 (TokenTermAtom "x")))))
)
tree0 = add (lit 8) (neg (add (lit 1) (lit 2)))
tree1 = mul (add (lit 8) (neg (add (lit 1) (lit 2)))) (lit 2)
-tree2 = lam (\(x :: sem Int) -> mul (lit 0) (add (lit 1) (lit 2)))
+tree2 = lam (\(x :: sem Int) -> mul (lit 0) (add (lit 1) x))
tree0Print = print tree0
tree1Print = print tree1
tree2Print = print tree2
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE StandaloneDeriving #-}
+{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE UndecidableSuperClasses #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wno-partial-fields #-}
TyApp :: {tyAppFun :: Ty meta vs f, tyAppArg :: Ty meta vs a} -> Ty meta vs (f a)
infixl 9 `TyApp`
+-- ** Class 'Tyable'
+class Tyable a where
+ typeOf :: Monoid meta => Ty meta '[] a
+instance (Tyable a, Tyable b) => Tyable (a b) where
+ typeOf = TyApp (typeOf @a) (typeOf @b)
+instance {-# OVERLAPPABLE #-} Typeable a => Tyable a where
+ typeOf = TyConst{tyConst = Const{constType = typeRep, constMeta = mempty}, tyConstLen = LenZ}
+
pattern FUN ::
-- CReq
forall k (fun :: k) meta vs.