{-# LANGUAGE DefaultSignatures #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE Rank2Types #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} -- | Expression for /lambda abstraction/s -- in /Higher-Order Abstract Syntax/ (HOAS). module Language.Symantic.Expr.Lambda where import qualified Control.Applicative as Applicative import qualified Data.Function as Fun import Data.Monoid import Data.Proxy (Proxy(..)) import Data.Type.Equality ((:~:)(Refl)) import Data.Text (Text) import qualified Data.Text as Text import Prelude hiding (const, id) import Language.Symantic.Type import Language.Symantic.Repr import Language.Symantic.Expr.Root import Language.Symantic.Expr.Error import Language.Symantic.Expr.From import Language.Symantic.Trans.Common -- * Class 'Sym_Lambda' -- | Symantic. class Sym_Lambda repr where -- | /Lambda application/. ($$) :: repr ((->) arg res) -> repr arg -> repr res default ($$) :: Trans t repr => t repr ((->) arg res) -> t repr arg -> t repr res ($$) f x = trans_lift (trans_apply f $$ trans_apply x) -- | /Lambda abstraction/. lam :: (repr arg -> repr res) -> repr ((->) arg res) default lam :: Trans t repr => (t repr arg -> t repr res) -> t repr ((->) arg res) lam f = trans_lift $ lam $ trans_apply . f . trans_lift -- | Convenient 'lam' and '$$' wrapper. let_ :: repr var -> (repr var -> repr res) -> repr res let_ x y = lam y $$ x id :: repr a -> repr a id a = (lam Fun.id) $$ a const :: repr a -> repr b -> repr a const a b = lam (lam . Fun.const) $$ a $$ b (#) :: repr (b -> c) -> repr (a -> b) -> repr a -> repr c (#) f g a = f $$ (g $$ a) flip :: repr (a -> b -> c) -> repr b -> repr a -> repr c flip f b a = f $$ a $$ b infixl 0 $$ infixr 9 # instance Sym_Lambda Repr_Host where ($$) = (Applicative.<*>) lam f = Repr_Host (unRepr_Host . f . Repr_Host) instance Sym_Lambda Repr_Text where ($$) (Repr_Text a1) (Repr_Text a2) = Repr_Text $ \p v -> let p' = precedence_App in paren p p' $ a1 p' v <> " " <> a2 p' v lam f = Repr_Text $ \p v -> let p' = precedence_Lambda in let x = "x" <> Text.pack (show v) in paren p p' $ "\\" <> x <> " -> " <> unRepr_Text (f (Repr_Text $ \_p _v -> x)) p' (succ v) let_ e in_ = Repr_Text $ \p v -> let p' = precedence_Let in let x = "x" <> Text.pack (show v) in paren p p' $ "let" <> " " <> x <> " = " <> unRepr_Text e p (succ v) <> " in " <> unRepr_Text (in_ (Repr_Text $ \_p _v -> x)) p (succ v) instance ( Sym_Lambda r1 , Sym_Lambda r2 ) => Sym_Lambda (Dup r1 r2) where ($$) (f1 `Dup` f2) (x1 `Dup` x2) = ($$) f1 x1 `Dup` ($$) f2 x2 lam f = dup1 (lam f) `Dup` dup2 (lam f) -- * Type 'Expr_Lambda' -- | Expression. data Expr_Lambda (root:: *) type instance Root_of_Expr (Expr_Lambda root) = root type instance Type_of_Expr (Expr_Lambda root) = Type_Fun type instance Sym_of_Expr (Expr_Lambda root) repr = Sym_Lambda repr type instance Error_of_Expr ast (Expr_Lambda root) = Error_Expr_Lambda ast -- | Parsing utility to check that the given type is a 'Type_Fun' -- or raise 'Error_Expr_Type_mismatch'. check_type_fun :: forall ast ex root ty h ret. ( root ~ Root_of_Expr ex , ty ~ Type_Root_of_Expr ex , Lift_Type Type_Fun (Type_of_Expr root) , Unlift_Type Type_Fun (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 -> (Type_Fun ty h -> Either (Error_of_Expr ast root) ret) -> Either (Error_of_Expr ast root) ret check_type_fun ex ast ty k = case unlift_type $ unType_Root ty of Just ty_f -> k ty_f Nothing -> Left $ error_expr ex $ Error_Expr_Type_mismatch ast (Exists_Type (type_var0 SZero `type_fun` type_var0 (SSucc SZero) :: ty ((->) Var0 Var0))) (Exists_Type ty) -- | Parse a /lambda variable/. var_from :: forall ast root hs ret. ( Type_from ast (Type_Root_of_Expr root) , Lift_Error_Expr (Error_Expr_Lambda ast) (Error_of_Expr ast root) , Root_of_Expr root ~ root ) => Text -> Expr_From ast (Expr_Lambda root) hs ret var_from name _ex ast = go where go :: forall ex hs'. (ex ~ (Expr_Lambda root)) => Context (Lambda_Var (Type_Root_of_Expr ex)) hs' -> ( 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 ) -> Either (Error_of_Expr ast (Root_of_Expr ex)) ret go c k' = case c of Context_Empty -> Left $ lift_error_expr $ Error_Expr_Lambda_Var_unbound name ast Lambda_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' -- | Parse 'app'. app_from :: forall ty ast root hs ret. ( ty ~ Type_Root_of_Expr root , Type_from ast ty , Eq_Type ty , Expr_from ast root , Lift_Type Type_Fun (Type_of_Expr root) , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast) (Error_of_Expr ast root) , Unlift_Type Type_Fun (Type_of_Expr root) , Root_of_Expr root ~ root ) => ast -> ast -> Expr_From ast (Expr_Lambda root) hs ret app_from ast_lam ast_arg_actual ex ast ctx k = expr_from (Proxy::Proxy root) ast_lam ctx $ \(ty_lam::ty h_lam) (Forall_Repr_with_Context l) -> expr_from (Proxy::Proxy root) ast_arg_actual ctx $ \(ty_arg_actual::ty h_arg_actual) (Forall_Repr_with_Context arg_actual) -> case unlift_type $ unType_Root ty_lam of Nothing -> Left $ error_expr ex $ Error_Expr_Type_mismatch ast (Exists_Type (type_var0 SZero `type_fun` type_var0 (SSucc SZero) :: ty ((->) Var0 Var0))) (Exists_Type ty_lam) Just (Type_Type2 Proxy ty_arg_expected ty_res :: Type_Fun ty h_lam) -> check_eq_type ex ast ty_arg_expected ty_arg_actual $ \Refl -> k ty_res $ Forall_Repr_with_Context $ \c -> l c $$ arg_actual c -- | Parse given /lambda abstraction/. lam_from :: forall ty ast root hs ret. ( ty ~ Type_Root_of_Expr root , root ~ Root_of_Expr root , Type_from ast ty , Expr_from ast root , Lift_Type Type_Fun (Type_of_Expr root) , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast) (Error_of_Expr ast root) ) => Text -> ast -> ast -> Expr_From ast (Expr_Lambda root) hs ret lam_from name ast_ty_arg ast_body ex ast ctx k = case type_from (Proxy::Proxy ty) ast_ty_arg (Right . Exists_Type) of Left err -> Left $ error_expr ex $ Error_Expr_Type err ast Right (Exists_Type (ty_arg::ty h_arg)) -> expr_from (Proxy::Proxy root) ast_body (Lambda_Var name ty_arg `Context_Next` ctx) $ \(ty_res::ty h_res) (Forall_Repr_with_Context res) -> k (ty_arg `type_fun` ty_res :: Root_of_Type ty ((->) h_arg h_res)) $ Forall_Repr_with_Context $ \c -> lam $ \arg -> res (arg `Context_Next` c) -- | Parse given /let/. let_from :: forall ty ast root hs ret. ( ty ~ Type_Root_of_Expr root , root ~ Root_of_Expr root , Type_from ast ty , Expr_from ast root , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast) (Error_of_Expr ast root) ) => Text -> ast -> ast -> Expr_From ast (Expr_Lambda root) hs ret let_from name ast_var ast_body _ex _ast ctx k = expr_from (Proxy::Proxy root) ast_var ctx $ \(ty_var::ty h_var) (Forall_Repr_with_Context var) -> expr_from (Proxy::Proxy root) ast_body (Lambda_Var name ty_var `Context_Next` ctx) $ \(ty_res::ty h_res) (Forall_Repr_with_Context res) -> k ty_res $ Forall_Repr_with_Context $ \c -> let_ (var c) $ \arg -> res (arg `Context_Next` c) -- * Type 'Error_Expr_Lambda' data Error_Expr_Lambda ast = Error_Expr_Lambda_Var_unbound Lambda_Var_Name ast deriving (Eq, Show)