{-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE Rank2Types #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE UndecidableInstances #-} module Language.Symantic.Expr.From where import Data.Maybe (fromMaybe) import Data.Proxy (Proxy(..)) import Data.Text (Text) import Data.Type.Equality ((:~:)(Refl)) import GHC.Prim (Constraint) import Language.Symantic.Type import Language.Symantic.Expr.Root import Language.Symantic.Expr.Alt import Language.Symantic.Expr.Error -- * Class 'Expr_from' -- | Parse given @ast@ into -- a 'Type_Root_of_Expr' and -- a 'Forall_Repr_with_Context', -- or return an 'Error_of_Expr'. class Expr_from ast (ex:: *) where expr_from :: Expr_From ast ex hs ret 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 _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) 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 , Unlift_Error_Expr (Error_Expr (Error_of_Type ast (Type_Root_of_Expr root)) (Type_Root_of_Expr root) ast) (Error_of_Expr ast root) ) => Expr_from ast (Expr_Alt curr next root) where expr_from _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 unlift_error_expr err of Just (Error_Expr_Unsupported_here _ :: Error_Expr (Error_of_Type ast (Type_Root_of_Expr root)) (Type_Root_of_Expr root) 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_From' type Expr_From ast ex hs ret = Proxy ex -- ^ Select the 'Expr_from' instance. -> ast -- ^ The input data to parse. -> Context (Lambda_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. -> ( 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 (in 'Expr_from') -- or host-terms (in 'Repr_Host') -- associated with the 'Lambda_Var's in scope. data Context :: (* -> *) -> [*] -> * where Context_Empty :: Context item '[] Context_Next :: item h -> Context item hs -> Context item (h ': hs) infixr 5 `Context_Next` -- ** Type 'Lambda_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 Lambda_Var ty h = Lambda_Var Lambda_Var_Name (ty h) type Lambda_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 -- and thus calls: 'expr_from'@ (Proxy::Proxy (@'Root_of_Expr'@ ex))@. 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 , Sym_of_Expr (Root_of_Expr ex) repr ) => repr h } -- ** Type family 'Sym_of_Expr' -- | The symantic of an expression. type family Sym_of_Expr (ex:: *) (repr:: * -> *) :: Constraint type instance Sym_of_Expr (Expr_Root ex) repr = Sym_of_Expr (ex (Expr_Root ex)) repr type instance Sym_of_Expr (Expr_Alt curr next root) repr = ( Sym_of_Expr (curr root) repr , Sym_of_Expr (next root) repr ) -- * Checks -- | Parsing utility to check that two types are equal, -- or raise 'Error_Expr_Type_mismatch'. check_eq_type :: forall ast ex root ty x y ret. ( root ~ Root_of_Expr ex , ty ~ Type_Root_of_Expr ex , Eq_Type ty , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast) (Error_of_Expr ast root) ) => Proxy ex -> ast -> ty x -> ty y -> (x :~: y -> Either (Error_of_Expr ast root) ret) -> Either (Error_of_Expr ast root) ret check_eq_type ex ast x y k = case x `eq_type` y of Just Refl -> k Refl Nothing -> Left $ error_expr ex $ Error_Expr_Type_mismatch ast (Exists_Type x) (Exists_Type y) -- | Parsing utility to check that two 'Type_Type1' are equal, -- or raise 'Error_Expr_Type_mismatch'. check_eq_type1 :: forall ast ex root ty h1 h2 a1 a2 ret. ( root ~ Root_of_Expr ex , ty ~ Type_Root_of_Expr ex , Eq_Type1 ty , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast) (Error_of_Expr ast root) ) => Proxy ex -> ast -> ty (h1 a1) -> ty (h2 a2) -> (h1 :~: h2 -> Either (Error_of_Expr ast root) ret) -> Either (Error_of_Expr ast root) ret check_eq_type1 ex ast h1 h2 k = case h1 `eq_type1` h2 of Just Refl -> k Refl Nothing -> Left $ error_expr ex $ Error_Expr_Type_mismatch ast (Exists_Type h1) (Exists_Type h2) -- | Parsing utility to check that a 'Type_Type0' or higher -- is an instance of a given 'Constraint', -- or raise 'Error_Expr_Constraint_missing'. check_constraint_type :: forall ast ex c root ty h ret. ( root ~ Root_of_Expr ex , ty ~ Type_Root_of_Expr ex , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast) (Error_of_Expr ast root) , Constraint_Type c ty ) => Proxy ex -> Proxy c -> ast -> ty h -> (Dict (c h) -> Either (Error_of_Expr ast root) ret) -> Either (Error_of_Expr ast root) ret check_constraint_type ex c ast ty k = case constraint_type c ty of Just Dict -> k Dict Nothing -> Left $ error_expr ex $ Error_Expr_Constraint_missing ast {-(Exists_Dict c)-} -- FIXME: not easy to report the constraint -- and still support 'Eq' and 'Show' deriving. (Exists_Type ty) -- | Parsing utility to check that a 'Type_Type1' or higher -- is an instance of a given 'Constraint', -- or raise 'Error_Expr_Constraint_missing'. check_constraint_type1 :: forall ast ex c root ty h a ret. ( root ~ Root_of_Expr ex , ty ~ Type_Root_of_Expr ex , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast) (Error_of_Expr ast root) , Constraint_Type1 c ty ) => Proxy ex -> Proxy c -> ast -> ty (h a) -> (Dict (c h) -> Either (Error_of_Expr ast root) ret) -> Either (Error_of_Expr ast root) ret check_constraint_type1 ex c ast ty k = case constraint_type1 c ty of Just Dict -> k Dict Nothing -> Left $ error_expr ex $ Error_Expr_Constraint_missing ast (Exists_Type ty) -- | Parsing utility to check that the given type is at least a 'Type_Type1' -- or raise 'Error_Expr_Type_mismatch'. check_type1 :: forall ast ex root ty h ret. ( root ~ Root_of_Expr ex , ty ~ Type_Root_of_Expr ex , Unlift_Type1 (Type_of_Expr root) , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast) (Error_of_Expr ast root) ) => Proxy ex -> ast -> ty h -> (forall (t1:: *). ( Type_Type1 t1 ty h , Lift_Type1 t1 ty (Type_Var0 :|: Type_Var1 :|: Type_of_Expr root) ) -> Either (Error_of_Expr ast root) ret) -> Either (Error_of_Expr ast root) ret check_type1 ex ast ty k = (`fromMaybe` unlift_type1 (unType_Root ty) (Just . k)) $ Left $ error_expr ex $ Error_Expr_Type_mismatch ast (Exists_Type (type_var1 SZero (type_var0 SZero) :: Type_Root_of_Expr ex (Var1 Var0))) (Exists_Type ty) -- * Parsers -- | Like 'expr_from' but for a root expression. root_expr_from :: forall lam ast root. ( Expr_from ast root , Root_of_Expr root ~ root ) => Proxy root -> Proxy lam -> ast -> Either (Error_of_Expr ast root) (Exists_Type_and_Repr (Type_Root_of_Expr root) (Forall_Repr root)) root_expr_from _ex _lam ast = expr_from (Proxy::Proxy root) ast Context_Empty $ \ty (Forall_Repr_with_Context repr) -> Right $ Exists_Type_and_Repr ty $ Forall_Repr $ repr Context_Empty -- | Parse a literal. lit_from :: forall ty lit ex ast hs ret. ( ty ~ Type_Root_of_Expr ex , Read lit , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast) (Error_of_Expr ast (Root_of_Expr ex)) ) => (forall repr. Sym_of_Expr ex repr => lit -> repr lit) -> ty lit -> Lambda_Var_Name -> Expr_From ast ex hs ret lit_from lit ty_lit toread ex ast _ctx k = case read_safe toread of Left err -> Left $ error_expr ex $ Error_Expr_Read err ast Right (i::lit) -> k ty_lit $ Forall_Repr_with_Context $ const $ lit i -- | Parse a unary operator. op1_from :: forall root ty lit ex ast hs ret. ( ty ~ Type_Root_of_Expr ex , root ~ Root_of_Expr ex , Eq_Type (Type_Root_of_Expr root) , Expr_from ast root , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast) (Error_of_Expr ast root) , Root_of_Expr root ~ root ) => (forall repr. Sym_of_Expr ex repr => repr lit -> repr lit) -> ty lit -> ast -> Expr_From ast ex hs ret op1_from op ty_lit ast_x ex ast ctx k = expr_from (Proxy::Proxy root) ast_x ctx $ \ty_x (Forall_Repr_with_Context x) -> check_eq_type ex ast ty_lit ty_x $ \Refl -> k ty_x $ Forall_Repr_with_Context (op . x) -- | Parse a binary operator. op2_from :: forall root ty lit ex ast hs ret. ( ty ~ Type_Root_of_Expr ex , root ~ Root_of_Expr ex , Eq_Type (Type_Root_of_Expr root) , Expr_from ast root , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast) (Error_of_Expr ast root) , Root_of_Expr root ~ root ) => (forall repr. Sym_of_Expr ex repr => repr lit -> repr lit -> repr lit) -> ty lit -> ast -> ast -> Expr_From ast ex hs ret op2_from op ty_lit ast_x ast_y ex ast ctx k = expr_from (Proxy::Proxy root) ast_x ctx $ \ty_x (Forall_Repr_with_Context x) -> expr_from (Proxy::Proxy root) ast_y ctx $ \ty_y (Forall_Repr_with_Context y) -> check_eq_type ex ast ty_lit ty_x $ \Refl -> check_eq_type ex ast ty_lit ty_y $ \Refl -> k ty_x $ Forall_Repr_with_Context $ \c -> x c `op` y c