{-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE ExistentialQuantification #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE Rank2Types #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE UndecidableInstances #-} module Language.LOL.Symantic.Expr.Common where import Data.Proxy (Proxy(..)) import GHC.Prim (Constraint) import Data.Text (Text) import Data.Peano import Language.LOL.Symantic.AST import Language.LOL.Symantic.Type -- * Class 'Expr_from' class Expr_from ast (ex:: *) where expr_from :: forall hs ret . Proxy ex -- ^ Select the 'Expr_from' instance for the expression @ex@. -> Context (Var (Type_Root_of_Expr ex)) hs -- ^ The bound variables in scope and their types: -- held in the heterogeneous list @hs@, -- from the closest including lambda abstraction to the farest. -> ast -- ^ The input data to parse. -> ( forall h . Type_Root_of_Expr ex h -> Forall_Repr_with_Context ex hs h -> Either (Error_of_Expr ast (Root_of_Expr ex)) ret ) -- ^ The accumulating continuation. -> Either (Error_of_Expr ast (Root_of_Expr ex)) ret -- ** Type 'Context' -- | GADT for a typing context, -- accumulating an @item@ at each lambda; -- used to accumulate object-types of lambda variables in 'Expr_from' -- or host-terms of lambda variables in 'Repr_Host'. data Context :: (* -> *) -> [*] -> * where Context_Empty :: Context item '[] Context_Next :: item h -> Context item hs -> Context item (h ': hs) infixr 5 `Context_Next` -- ** Type 'Var' -- | Join a name and a type. -- -- This data type is used to handle lambda variables by name -- (instead of DeBruijn indices for instance). data Var ty h = Var Var_Name (ty h) type Var_Name = Text -- ** Type 'Forall_Repr_with_Context' -- | A data type embedding a universal quantification -- over an interpreter @repr@ -- and qualified by the symantics of an expression. -- -- Moreover the expression is abstracted by a 'Context' -- built at parsing time to build a /Higher-Order Abstract Syntax/ (HOAS) -- for lambda abstractions. -- -- This data type is used to keep a parsed expression polymorphic enough -- to stay interpretable by different interpreters. -- -- NOTE: 'Sym_of_Expr'@ ex repr@ -- is needed to be able to use symantic methods of the parsed expression -- into a 'Forall_Repr_with_Context' @ex@. -- -- NOTE: 'Sym_of_Expr'@ (@'Root_of_Expr'@ ex) repr@ -- is needed to be able to use an expression -- out of a 'Forall_Repr_with_Context'@ (@'Root_of_Expr'@ ex)@ -- into a 'Forall_Repr_with_Context'@ ex@, -- which happens when a symantic method includes a polymorphic type. data Forall_Repr_with_Context ex hs h = Forall_Repr_with_Context ( forall repr. ( Sym_of_Expr ex repr , Sym_of_Expr (Root_of_Expr ex) repr ) => Context repr hs -> repr h ) -- ** Type 'Forall_Repr' data Forall_Repr ex h = Forall_Repr { unForall_Repr :: forall repr . Sym_of_Expr ex repr => repr h } -- ** Type family 'Root_of_Expr' -- | The root expression, closing an expression with itself. type family Root_of_Expr (ex:: *) :: * -- ** Type family 'Sym_of_Expr' -- | The symantic of an expression. type family Sym_of_Expr (ex:: *) (repr:: * -> *) :: Constraint -- ** Type family 'Error_of_Expr' -- | The error(s) of an expression. type family Error_of_Expr (ast:: *) (ex:: *) :: * -- ** Type family 'Type_of_Expr' -- | The type of an expression, parameterized by a root type. type family Type_of_Expr (ex:: *) :: {-root-}(* -> *) -> {-h-}* -> * -- ** Type 'Type_Root_of_Expr' -- | Convenient alias. type Type_Root_of_Expr ex = Type_Root (Type_of_Expr (Root_of_Expr ex)) -- * Type 'Expr_Root' -- | The root expression, passing itself as parameter to the given expression. newtype Expr_Root (ex:: * -> *) = Expr_Root (ex (Expr_Root ex)) type instance Root_of_Expr (Expr_Root ex) = Expr_Root ex type instance Type_of_Expr (Expr_Root ex) = Type_of_Expr (ex (Expr_Root ex)) -- NOTE: require UndecidableInstances. type instance Error_of_Expr ast (Expr_Root ex) = Error_Expr_Cons (Error_Expr ast) (Error_of_Expr ast (ex (Expr_Root ex))) -- NOTE: require UndecidableInstances. type instance Sym_of_Expr (Expr_Root ex) repr = Sym_of_Expr (ex (Expr_Root ex)) repr -- NOTE: require UndecidableInstances. instance -- Expr_from ( Expr_from ast (ex (Expr_Root ex)) , Root_of_Expr (ex (Expr_Root ex)) ~ Expr_Root ex ) => Expr_from ast (Expr_Root ex) where expr_from _px_ex ctx ast k = expr_from (Proxy::Proxy (ex (Expr_Root ex))) ctx ast $ \ty (Forall_Repr_with_Context repr) -> k ty (Forall_Repr_with_Context repr) -- ** Class 'Expr_Root_Lift' -- | Lift a given expression to a given root expression. class Expr_Root_Lift ex root where expr_root_lift :: ex root -> root instance Expr_Lift ex root => Expr_Root_Lift ex (Expr_Root root) where expr_root_lift = Expr_Root . expr_lift -- * Type 'Expr_Cons' -- | Combine two expressions into one. data Expr_Cons curr next (root:: *) = Expr_Curr (curr root) | Expr_Next (next root) type instance Root_of_Expr (Expr_Cons curr next root) = root type instance Type_of_Expr (Expr_Cons curr next root) = Type_Cons (Type_of_Expr (curr root)) (Type_of_Expr (next root)) type instance Error_of_Expr ast (Expr_Cons curr next root) = Error_Expr_Cons (Error_of_Expr ast (curr root)) (Error_of_Expr ast (next root)) type instance Sym_of_Expr (Expr_Cons curr next root) repr = ( Sym_of_Expr (curr root) repr , Sym_of_Expr (next root) repr ) instance -- Expr_from ( Expr_from ast (curr root) , Expr_from ast (next root) , Root_of_Expr (curr root) ~ root , Root_of_Expr (next root) ~ root , Error_Expr_Unlift (Error_Expr ast) (Error_of_Expr ast root) ) => Expr_from ast (Expr_Cons curr next root) where expr_from _px_ex ctx ast k = case expr_from (Proxy::Proxy (curr root)) ctx ast $ \ty (Forall_Repr_with_Context repr) -> Right $ k ty (Forall_Repr_with_Context repr) of Right ret -> ret Left err -> case error_expr_unlift err of Just (Error_Expr_Unsupported (_::ast)) -> expr_from (Proxy::Proxy (next root)) ctx ast $ \ty (Forall_Repr_with_Context repr) -> k ty (Forall_Repr_with_Context repr) _ -> Left err -- ** Type 'Expr_Lift' -- | Apply 'Peano_of_Expr' on 'Expr_LiftN'. type Expr_Lift ex exs = Expr_LiftN (Peano_of_Expr ex exs) ex exs -- | Convenient wrapper around 'expr_liftN', -- passing it the 'Peano' number from 'Peano_of_Expr', -- used to avoid @OverlappingInstances@. expr_lift :: forall ex exs (root:: *). Expr_Lift ex exs => ex root -> exs root expr_lift = expr_liftN (Proxy::Proxy (Peano_of_Expr ex exs)) -- *** Type family 'Peano_of_Expr' -- | Return a 'Peano' number derived from the location -- of a given expression within a given expression stack. type family Peano_of_Expr (ex:: * -> *) (exs:: * -> *) :: Peano where Peano_of_Expr ex ex = 'Zero Peano_of_Expr ex (Expr_Cons ex next) = 'Zero Peano_of_Expr other (Expr_Cons curr next) = 'Succ (Peano_of_Expr other next) -- *** Class 'Expr_LiftN' -- | Lift a given expression to the top of a given expression stack including it, -- by constructing the appropriate sequence of 'Expr_Curr' and 'Expr_Next'. class Expr_LiftN (n::Peano) ex exs where expr_liftN :: forall (root:: *). Proxy n -> ex root -> exs root instance Expr_LiftN 'Zero curr curr where expr_liftN _ = id instance Expr_LiftN 'Zero curr (Expr_Cons curr next) where expr_liftN _ = Expr_Curr instance Expr_LiftN n other next => Expr_LiftN ('Succ n) other (Expr_Cons curr next) where expr_liftN _ = Expr_Next . expr_liftN (Proxy::Proxy n) {- NOTE: unused code so far -- ** Type 'Expr_Unlift' -- | Apply 'Peano_of_Expr' on 'Expr_UnliftN'. type Expr_Unlift ex exs = Expr_UnliftN (Peano_of_Expr ex exs) ex exs -- | Convenient wrapper around 'expr_unliftN', -- passing it the 'Peano' number from 'Peano_of_Expr', -- used to avoid @OverlappingInstances@. expr_unlift :: forall ex exs (root:: *). Expr_Unlift ex exs => exs root -> Maybe (ex root) expr_unlift = expr_unliftN (Proxy::Proxy (Peano_of_Expr ex exs)) -- *** Class 'Expr_UnliftN' -- | Try to unlift a given expression out of a given expression stack including it, -- by deconstructing the appropriate sequence of 'Expr_Curr' and 'Expr_Next'. class Expr_UnliftN (n::Peano) ex exs where expr_unliftN :: forall (root:: *). Proxy n -> exs root -> Maybe (ex root) instance Expr_UnliftN 'Zero curr curr where expr_unliftN _ = Just instance Expr_UnliftN 'Zero curr (Expr_Cons curr next) where expr_unliftN _ (Expr_Curr x) = Just x expr_unliftN _ (Expr_Next _) = Nothing instance Expr_UnliftN n other next => Expr_UnliftN ('Succ n) other (Expr_Cons curr next) where expr_unliftN _ (Expr_Next x) = expr_unliftN (Proxy::Proxy n) x expr_unliftN _ (Expr_Curr _) = Nothing -} -- * Type 'Error_Expr_Cons' -- | Combine two expression errors into one. data Error_Expr_Cons curr next = Error_Expr_Curr curr | Error_Expr_Next next deriving (Eq, Show) -- ** Type 'Error_Expr_Lift' -- | Apply 'Peano_of_Error_Expr' on 'Error_Expr_LiftN'. type Error_Expr_Lift err errs = Error_Expr_LiftN (Peano_of_Error_Expr err errs) err errs -- | Convenient wrapper around 'error_expr_liftN', -- passing it the 'Peano' number from 'Peano_of_Error_Expr', -- used to avoid @OverlappingInstances@. error_expr_lift :: forall err errs. Error_Expr_Lift err errs => err -> errs error_expr_lift = error_expr_liftN (Proxy::Proxy (Peano_of_Error_Expr err errs)) -- *** Type family 'Peano_of_Error_Expr' -- | Return a 'Peano' number derived from the location -- of a given error type within a given error type stack. type family Peano_of_Error_Expr (err:: *) (errs:: *) :: Peano where Peano_of_Error_Expr err err = 'Zero Peano_of_Error_Expr err (Error_Expr_Cons err next) = 'Zero Peano_of_Error_Expr other (Error_Expr_Cons curr next) = 'Succ (Peano_of_Error_Expr other next) -- *** Class 'Error_Expr_LiftN' -- | Lift a given expression to the top of a given expression stack including it, -- by constructing the appropriate sequence of 'Error_Expr_Curr' and 'Error_Expr_Next'. class Error_Expr_LiftN (n::Peano) err errs where error_expr_liftN :: Proxy n -> err -> errs instance Error_Expr_LiftN 'Zero curr curr where error_expr_liftN _ = id instance Error_Expr_LiftN 'Zero curr (Error_Expr_Cons curr next) where error_expr_liftN _ = Error_Expr_Curr instance Error_Expr_LiftN n other next => Error_Expr_LiftN ('Succ n) other (Error_Expr_Cons curr next) where error_expr_liftN _ = Error_Expr_Next . error_expr_liftN (Proxy::Proxy n) -- ** Type 'Error_Expr_Unlift' -- | Apply 'Peano_of_Error_Expr' on 'Error_Expr_UnliftN'. type Error_Expr_Unlift ex exs = Error_Expr_UnliftN (Peano_of_Error_Expr ex exs) ex exs -- | Convenient wrapper around 'error_expr_unliftN', -- passing it the 'Peano' number from 'Peano_of_Error_Expr', -- used to avoid @OverlappingInstances@. error_expr_unlift :: forall ex exs. Error_Expr_Unlift ex exs => exs -> Maybe ex error_expr_unlift = error_expr_unliftN (Proxy::Proxy (Peano_of_Error_Expr ex exs)) -- *** Class 'Error_Expr_UnliftN' -- | Try to unlift a given expression error out of a given expression error stack including it, -- by deconstructing the appropriate sequence of 'Error_Expr_Curr' and 'Error_Expr_Next'. class Error_Expr_UnliftN (n::Peano) ex exs where error_expr_unliftN :: Proxy n -> exs -> Maybe ex instance Error_Expr_UnliftN 'Zero curr curr where error_expr_unliftN _ = Just instance Error_Expr_UnliftN 'Zero curr (Error_Expr_Cons curr next) where error_expr_unliftN _ (Error_Expr_Curr x) = Just x error_expr_unliftN _ (Error_Expr_Next _) = Nothing instance Error_Expr_UnliftN n other next => Error_Expr_UnliftN ('Succ n) other (Error_Expr_Cons curr next) where error_expr_unliftN _ (Error_Expr_Next x) = error_expr_unliftN (Proxy::Proxy n) x error_expr_unliftN _ (Error_Expr_Curr _) = Nothing -- ** Type 'Error_Expr_Read' -- | Common expression errors. data Error_Expr ast = Error_Expr_Read Error_Read ast | Error_Expr_Unsupported ast deriving (Eq, Show)