1 {-# LANGUAGE DefaultSignatures #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE ScopedTypeVariables #-}
4 {-# LANGUAGE TypeFamilies #-}
5 {-# LANGUAGE TypeOperators #-}
6 -- | Expression for 'Ord'.
7 module Language.Symantic.Expr.Ord where
9 import Data.Proxy (Proxy(..))
10 import Data.Type.Equality ((:~:)(Refl))
11 import Prelude hiding (compare)
13 import Language.Symantic.Repr.Dup
14 import Language.Symantic.Trans.Common
15 import Language.Symantic.Type
16 import Language.Symantic.Expr.Common
17 import Language.Symantic.Expr.Eq
21 class Sym_Eq repr => Sym_Ord repr where
22 compare :: Ord a => repr a -> repr a -> repr Ordering
23 default compare :: (Trans t repr, Ord a)
24 => t repr a -> t repr a -> t repr Ordering
25 compare = trans_map2 compare
27 instance (Sym_Ord r1, Sym_Ord r2) => Sym_Ord (Dup r1 r2) where
28 compare (x1 `Dup` x2) (y1 `Dup` y2) =
34 data Expr_Ord (root:: *)
35 type instance Root_of_Expr (Expr_Ord root) = root
36 type instance Type_of_Expr (Expr_Ord root) = No_Type
37 type instance Sym_of_Expr (Expr_Ord root) repr = (Sym_Ord repr)
38 type instance Error_of_Expr ast (Expr_Ord root) = No_Error_Expr
41 :: forall root ty ast hs ret.
42 ( ty ~ Type_Root_of_Expr (Expr_Ord root)
43 , Type_Root_Lift Type_Ordering (Type_Root_of_Expr root)
44 , Type_Eq (Type_Root_of_Expr root)
46 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
47 (Error_of_Expr ast root)
48 , Root_of_Expr root ~ root
49 , Type_Constraint Ord ty
51 -> Expr_From ast (Expr_Ord root) hs ret
52 compare_from ast_x ast_y ex ast ctx k =
53 expr_from (Proxy::Proxy root) ast_x ctx $
54 \(ty_x::Type_Root_of_Expr root h_x) (Forall_Repr_with_Context x) ->
55 expr_from (Proxy::Proxy root) ast_y ctx $
56 \(ty_y::Type_Root_of_Expr root h_y) (Forall_Repr_with_Context y) ->
57 check_type_eq ex ast ty_x ty_y $ \Refl ->
58 check_type_constraint ex (Proxy::Proxy Ord) ast ty_x $ \Dict ->
59 k type_ordering $ Forall_Repr_with_Context $
60 \c -> x c `compare` y c