{-# LANGUAGE DefaultSignatures #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} {-# OPTIONS_GHC -fno-warn-orphans #-} -- | Expression for 'Ord'. module Language.Symantic.Expr.Ord where import Control.Monad import Data.Monoid import qualified Data.Ord as Ord import Data.Proxy (Proxy(..)) import Data.Type.Equality ((:~:)(Refl)) import Prelude hiding (compare) 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.Expr.Eq import Language.Symantic.Trans.Common -- * Class 'Sym_Ord' -- | Symantic. class Sym_Eq repr => Sym_Ord repr where compare :: Ord a => repr a -> repr a -> repr Ordering default compare :: (Trans t repr, Ord a) => t repr a -> t repr a -> t repr Ordering compare = trans_map2 compare instance Sym_Ord Repr_Host where compare = liftM2 Ord.compare instance Sym_Ord Repr_Text where compare (Repr_Text x) (Repr_Text y) = Repr_Text $ \p v -> let p' = precedence_Eq in paren p p' $ "compare " <> x p' v <> " " <> y p' v instance ( Sym_Ord r1 , Sym_Ord r2 ) => Sym_Ord (Dup r1 r2) where compare (x1 `Dup` x2) (y1 `Dup` y2) = compare x1 y1 `Dup` compare x2 y2 -- * Type 'Expr_Ord' -- | Expression. data Expr_Ord (root:: *) type instance Root_of_Expr (Expr_Ord root) = root type instance Type_of_Expr (Expr_Ord root) = Type_Ordering type instance Sym_of_Expr (Expr_Ord root) repr = (Sym_Ord repr) type instance Error_of_Expr ast (Expr_Ord root) = No_Error_Expr compare_from :: forall root ty ast hs ret. ( ty ~ Type_Root_of_Expr (Expr_Ord root) , Eq_Type ty , Expr_from ast root , Lift_Type Type_Ordering (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 , Constraint_Type Ord ty ) => ast -> ast -> Expr_From ast (Expr_Ord root) hs ret compare_from ast_x ast_y ex ast ctx k = expr_from (Proxy::Proxy root) ast_x ctx $ \(ty_x::ty h_x) (Forall_Repr_with_Context x) -> expr_from (Proxy::Proxy root) ast_y ctx $ \(ty_y::ty h_y) (Forall_Repr_with_Context y) -> check_eq_type ex ast ty_x ty_y $ \Refl -> check_constraint_type ex (Proxy::Proxy Ord) ast ty_x $ \Dict -> k type_ordering $ Forall_Repr_with_Context $ \c -> x c `compare` y c