{-# LANGUAGE DefaultSignatures #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} {-# OPTIONS_GHC -fno-warn-orphans #-} -- | Expression for 'Maybe'. module Language.Symantic.Expr.Maybe where import Data.Proxy (Proxy(..)) import Data.Type.Equality ((:~:)(Refl)) import Prelude hiding (maybe) import Language.Symantic.Type import Language.Symantic.Trans.Common import Language.Symantic.Expr.Root import Language.Symantic.Expr.Error import Language.Symantic.Expr.From import Language.Symantic.Expr.Lambda -- * Class 'Sym_Maybe_Lam' -- | Symantic. class Sym_Maybe repr where nothing :: repr (Maybe a) just :: repr a -> repr (Maybe a) maybe :: repr b -> repr ((->) a b) -> repr (Maybe a) -> repr b default nothing :: Trans t repr => t repr (Maybe a) default just :: Trans t repr => t repr a -> t repr (Maybe a) default maybe :: Trans t repr => t repr b -> t repr ((->) a b) -> t repr (Maybe a) -> t repr b nothing = trans_lift nothing just = trans_map1 just maybe = trans_map3 maybe -- * Type 'Expr_Maybe' -- | Expression. data Expr_Maybe (root:: *) type instance Root_of_Expr (Expr_Maybe root) = root type instance Type_of_Expr (Expr_Maybe root) = Type_Maybe type instance Sym_of_Expr (Expr_Maybe root) repr = (Sym_Maybe repr) type instance Error_of_Expr ast (Expr_Maybe root) = No_Error_Expr -- | Parsing utility to check that the given type is a 'Type_Maybe' -- or raise 'Error_Expr_Type_mismatch'. check_type_maybe :: forall ast ex root ty h ret. ( root ~ Root_of_Expr ex , ty ~ Type_Root_of_Expr ex , Lift_Type Type_Maybe (Type_of_Expr root) , Unlift_Type Type_Maybe (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_Maybe ty h -> Either (Error_of_Expr ast root) ret) -> Either (Error_of_Expr ast root) ret check_type_maybe ex ast ty k = case unlift_type $ unType_Root ty of Just ty_l -> k ty_l Nothing -> Left $ error_expr ex $ Error_Expr_Type_mismatch ast (Exists_Type (type_maybe $ type_var0 SZero :: ty (Maybe Var0))) (Exists_Type ty) -- | Parse 'maybe'. maybe_from :: forall root ty ast hs ret. ( ty ~ Type_Root_of_Expr (Expr_Maybe root) , Eq_Type ty , Expr_from ast root , Lift_Type Type_Fun (Type_of_Expr root) , Unlift_Type Type_Fun (Type_of_Expr root) , Lift_Type Type_Maybe (Type_of_Expr root) , Unlift_Type Type_Maybe (Type_of_Expr root) , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast) (Error_of_Expr ast root) , Root_of_Expr root ~ root ) => ast -> ast -> ast -> Expr_From ast (Expr_Maybe root) hs ret maybe_from ast_n ast_j ast_m ex ast ctx k = expr_from (Proxy::Proxy root) ast_n ctx $ \(ty_n::ty h_n) (Forall_Repr_with_Context n) -> expr_from (Proxy::Proxy root) ast_j ctx $ \(ty_j::ty h_j) (Forall_Repr_with_Context j) -> expr_from (Proxy::Proxy root) ast_m ctx $ \(ty_m::ty h_m) (Forall_Repr_with_Context m) -> check_type_fun ex ast ty_j $ \(Type_Type2 Proxy ty_j_a ty_j_b :: Type_Fun ty h_j) -> check_type_maybe ex ast ty_m $ \(Type_Type1 _ ty_m_a) -> check_eq_type ex ast ty_n ty_j_b $ \Refl -> check_eq_type ex ast ty_m_a ty_j_a $ \Refl -> k ty_n $ Forall_Repr_with_Context $ \c -> maybe (n c) (j c) (m c) -- | Parse 'nothing'. nothing_from :: forall root ty ast hs ret. ( ty ~ Type_Root_of_Expr (Expr_Maybe root) , Type_from ast ty , Lift_Type Type_Maybe (Type_of_Expr root) , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast) (Error_of_Expr ast root) , Root_of_Expr root ~ root ) => ast -> Expr_From ast (Expr_Maybe root) hs ret nothing_from ast_ty_a ex ast _ctx k = case type_from (Proxy::Proxy ty) ast_ty_a (Right . Exists_Type) of Left err -> Left $ error_expr ex $ Error_Expr_Type err ast Right (Exists_Type ty_a) -> k (type_maybe ty_a) $ Forall_Repr_with_Context $ const nothing -- | Parse 'just'. just_from :: forall root ty ast hs ret. ( ty ~ Type_Root_of_Expr (Expr_Maybe root) , Expr_from ast root , Lift_Type Type_Maybe (Type_of_Expr root) , Root_of_Expr root ~ root ) => ast -> Expr_From ast (Expr_Maybe root) hs ret just_from ast_a _ex _ast ctx k = expr_from (Proxy::Proxy root) ast_a ctx $ \(ty_a::ty h_a) (Forall_Repr_with_Context a) -> k (type_maybe ty_a) $ Forall_Repr_with_Context $ \c -> just (a c)