{-# LANGUAGE DefaultSignatures #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE Rank2Types #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE UndecidableInstances #-} -- | Expression for /lambda abstraction/s -- in /Higher-Order Abstract Syntax/ (HOAS). module Language.LOL.Symantic.Expr.Lambda where import Data.Proxy (Proxy(..)) import Data.Type.Equality ((:~:)(Refl)) import Language.LOL.Symantic.AST import Language.LOL.Symantic.Type import Language.LOL.Symantic.Expr.Common import Language.LOL.Symantic.Repr.Dup import Language.LOL.Symantic.Trans.Common -- * Class 'Sym_Lambda' -- | Symantic. -- -- NOTE: argument @arg@ and result @res@ of 'Lambda' -- wrapped inside 'lam': to control the calling -- in the 'Repr_Host' instance. -- -- NOTE: the default definitions supplied for: -- 'app', 'inline', 'val' and 'lazy' -- are there to avoid boilerplate code -- when writting 'Trans' instances which -- do not need to alterate those methods. class (lam ~ Lambda_from_Repr repr) => Sym_Lambda lam repr where -- | This type constructor is used like -- the functional dependency: @repr -> lam@ -- (ie. knowing @repr@ one can determine @lam@) -- in order to avoid to introduce a 'Proxy' @lam@ -- in 'let_inline', 'let_val' and 'let_lazy'. -- -- Distinguishing between @lam@ and @repr@ is used to maintain -- the universal polymorphism of @repr@ in 'Expr_from', -- the downside with this however is that -- to be an instance of 'Sym_Lambda' for all @lam@, -- the @repr@ type of an interpreter -- has to be parameterized by @lam@, -- even though it does not actually need @lam@ to do its work. -- -- Basically this means having sometimes to add a type annotation -- to the interpreter call to specify @lam@. type Lambda_from_Repr repr :: {-lam-}(* -> *) -- | Lambda application. app :: repr (Lambda lam arg res) -> repr arg -> repr res -- | /Call-by-name/ lambda. inline :: (repr arg -> repr res) -> repr (Lambda lam arg res) -- | /Call-by-value/ lambda. val :: (repr arg -> repr res) -> repr (Lambda lam arg res) -- | /Call-by-need/ lambda (aka. /lazyness/): lazy shares its argument, no matter what. lazy :: (repr arg -> repr res) -> repr (Lambda lam arg res) default app :: Trans t repr => t repr (Lambda lam arg res) -> t repr arg -> t repr res default inline :: Trans t repr => (t repr arg -> t repr res) -> t repr (Lambda lam arg res) default val :: Trans t repr => (t repr arg -> t repr res) -> t repr (Lambda lam arg res) default lazy :: Trans t repr => (t repr arg -> t repr res) -> t repr (Lambda lam arg res) app f x = trans_lift $ trans_apply f `app` trans_apply x inline f = trans_lift $ inline $ trans_apply . f . trans_lift val f = trans_lift $ val $ trans_apply . f . trans_lift lazy f = trans_lift $ lazy $ trans_apply . f . trans_lift -- | Convenient 'inline' wrapper. let_inline :: Sym_Lambda lam repr => repr var -> (repr var -> repr res) -> repr res let_inline x y = inline y `app` x -- | Convenient 'val' wrapper. let_val :: Sym_Lambda lam repr => repr var -> (repr var -> repr res) -> repr res let_val x y = val y `app` x -- | Convenient 'lazy' wrapper. let_lazy :: Sym_Lambda lam repr => repr var -> (repr var -> repr res) -> repr res let_lazy x y = lazy y `app` x infixl 5 `app` instance -- Sym_Lambda Dup ( Sym_Lambda lam r1 , Sym_Lambda lam r2 ) => Sym_Lambda lam (Dup r1 r2) where type Lambda_from_Repr (Dup r1 r2) = Lambda_from_Repr r1 app (f1 `Dup` f2) (x1 `Dup` x2) = app f1 x1 `Dup` app f2 x2 inline f = dup1 (inline f) `Dup` dup2 (inline f) val f = dup1 (val f) `Dup` dup2 (val f) lazy f = dup1 (lazy f) `Dup` dup2 (lazy f) -- * Type 'Expr_Lambda' -- | Expression. data Expr_Lambda (lam:: * -> *) (root:: *) type instance Root_of_Expr (Expr_Lambda lam root) = root type instance Type_of_Expr (Expr_Lambda lam root) = Type_Fun lam type instance Sym_of_Expr (Expr_Lambda lam root) repr = Sym_Lambda lam repr type instance Error_of_Expr ast (Expr_Lambda lam root) = Error_Expr_Lambda ast instance -- Expr_from AST Expr_Lambda ( Type_from AST (Type_Root_of_Expr root) , Expr_from AST root , Type_Root_Lift (Type_Fun lam) (Type_Root_of_Expr root) , Error_Type_Lift (Error_Type_Fun AST) (Error_of_Type AST (Type_Root_of_Expr root)) , Error_Expr_Lift (Error_Expr_Lambda AST) (Error_of_Expr AST root) , Error_Expr_Lift (Error_Expr (Error_of_Type AST (Type_Root_of_Expr root)) ( Type_Root_of_Expr root) AST) (Error_of_Expr AST root) , Error_Expr_Unlift (Error_Expr (Error_of_Type AST (Type_Root_of_Expr root)) ( Type_Root_of_Expr root) AST) (Error_of_Expr AST root) , Type_Unlift (Type_Fun lam) (Type_of_Expr root) , Root_of_Expr root ~ root , Implicit_HBool (Is_Last_Expr (Expr_Lambda lam root) root) ) => Expr_from AST (Expr_Lambda lam root) where expr_from px_ex ctx ast k = case ast of AST "var" asts -> case asts of [AST name []] -> go ctx k where go :: forall hs ret . Context (Var (Type_Root_of_Expr root)) hs -> ( forall h . Type_Root_of_Expr root h -> Forall_Repr_with_Context (Expr_Lambda lam root) hs h -> Either (Error_of_Expr AST root) ret ) -> Either (Error_of_Expr AST root) ret go c k' = case c of Context_Empty -> Left $ error_expr_lift $ Error_Expr_Lambda_Var_unbound name ast Var n ty `Context_Next` _ | n == name -> k' ty $ Forall_Repr_with_Context $ \(repr `Context_Next` _) -> repr _ `Context_Next` ctx' -> go ctx' $ \ty (Forall_Repr_with_Context repr) -> k' ty $ Forall_Repr_with_Context $ \(_ `Context_Next` c') -> repr c' _ -> Left $ error_expr px_ex $ Error_Expr_Wrong_number_of_arguments 1 ast AST "app" asts -> case asts of [ast_lam, ast_arg_actual] -> expr_from (Proxy::Proxy root) ctx ast_lam $ \(ty_lam::Type_Root_of_Expr root h_lam) (Forall_Repr_with_Context lam) -> expr_from (Proxy::Proxy root) ctx ast_arg_actual $ \(ty_arg_actual::Type_Root_of_Expr root h_arg_actual) (Forall_Repr_with_Context arg_actual) -> case type_unlift $ unType_Root ty_lam of Just (ty_arg_expected `Type_Fun` ty_res :: Type_Fun lam (Type_Root_of_Expr root) h_lam) -> when_type_eq px_ex ast ty_arg_expected ty_arg_actual $ \Refl -> k ty_res $ Forall_Repr_with_Context $ \c -> lam c `app` arg_actual c Nothing -> Left $ error_expr_lift $ Error_Expr_Lambda_Not_a_lambda ast _ -> Left $ error_expr px_ex $ Error_Expr_Type (error_type_lift $ Error_Type_Fun_Wrong_number_of_arguments 2 ast) ast AST "inline" asts -> lambda_from asts inline AST "val" asts -> lambda_from asts val AST "lazy" asts -> lambda_from asts lazy AST "let_inline" asts -> let_from asts let_inline AST "let_val" asts -> let_from asts let_val AST "let_lazy" asts -> let_from asts let_lazy _ -> Left $ case hbool :: HBool (Is_Last_Expr (Expr_Lambda lam root) root) of HTrue -> error_expr px_ex $ Error_Expr_Unsupported ast HFalse -> error_expr px_ex $ Error_Expr_Unsupported_here ast where lambda_from asts (lam::forall repr arg res. Sym_Lambda lam repr => (repr arg -> repr res) -> repr (Lambda lam arg res)) = case asts of [AST name [], ast_ty_arg, ast_body] -> case type_from (Proxy::Proxy (Type_Root_of_Expr root)) ast_ty_arg (Right . Exists_Type) of Left err -> Left $ error_expr px_ex $ Error_Expr_Type err ast Right (Exists_Type (ty_arg::Type_Root_of_Expr root h_arg)) -> expr_from (Proxy::Proxy root) (Var name ty_arg `Context_Next` ctx) ast_body $ \(ty_res::Type_Root_of_Expr root h_res) (Forall_Repr_with_Context res) -> k (ty_arg `type_fun` ty_res :: Root_of_Type (Type_Root_of_Expr root) (Lambda lam h_arg h_res)) $ Forall_Repr_with_Context $ \c -> lam $ \arg -> res (arg `Context_Next` c) _ -> Left $ error_expr px_ex $ Error_Expr_Wrong_number_of_arguments 3 ast let_from asts (let_::forall repr var res. Sym_Lambda lam repr => repr var -> (repr var -> repr res) -> repr res) = case asts of [AST name [], ast_var, ast_body] -> expr_from (Proxy::Proxy root) ctx ast_var $ \(ty_var::Type_Root_of_Expr root h_var) (Forall_Repr_with_Context var) -> expr_from (Proxy::Proxy root) (Var name ty_var `Context_Next` ctx) ast_body $ \(ty_res::Type_Root_of_Expr root h_res) (Forall_Repr_with_Context res) -> k ty_res $ Forall_Repr_with_Context $ \c -> let_ (var c) $ \arg -> res (arg `Context_Next` c) _ -> Left $ error_expr px_ex $ Error_Expr_Wrong_number_of_arguments 3 ast -- * Type 'Error_Expr_Lambda' data Error_Expr_Lambda ast = Error_Expr_Lambda_Not_a_lambda ast | Error_Expr_Lambda_Var_unbound Var_Name ast deriving (Eq, Show)