]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/Ord.hs
init
[haskell/symantic.git] / Language / Symantic / Expr / Ord.hs
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
8
9 import Data.Proxy (Proxy(..))
10 import Data.Type.Equality ((:~:)(Refl))
11 import Prelude hiding (compare)
12
13 import Language.Symantic.Trans.Common
14 import Language.Symantic.Type
15 import Language.Symantic.Expr.Common
16 import Language.Symantic.Expr.Eq
17
18 -- * Class 'Sym_Ord'
19 -- | Symantic.
20 class Sym_Eq repr => Sym_Ord repr where
21 compare :: Ord a => repr a -> repr a -> repr Ordering
22 default compare :: (Trans t repr, Ord a)
23 => t repr a -> t repr a -> t repr Ordering
24 compare = trans_map2 compare
25
26 -- * Type 'Expr_Ord'
27 -- | Expression.
28 data Expr_Ord (root:: *)
29 type instance Root_of_Expr (Expr_Ord root) = root
30 type instance Type_of_Expr (Expr_Ord root) = Type_Ordering
31 type instance Sym_of_Expr (Expr_Ord root) repr = (Sym_Ord repr)
32 type instance Error_of_Expr ast (Expr_Ord root) = No_Error_Expr
33
34 compare_from
35 :: forall root ty ast hs ret.
36 ( ty ~ Type_Root_of_Expr (Expr_Ord root)
37 , Lift_Type_Root Type_Ordering (Type_Root_of_Expr root)
38 , Eq_Type (Type_Root_of_Expr root)
39 , Expr_from ast root
40 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
41 (Error_of_Expr ast root)
42 , Root_of_Expr root ~ root
43 , Constraint_Type Ord ty
44 ) => ast -> ast
45 -> Expr_From ast (Expr_Ord root) hs ret
46 compare_from ast_x ast_y ex ast ctx k =
47 expr_from (Proxy::Proxy root) ast_x ctx $
48 \(ty_x::Type_Root_of_Expr root h_x) (Forall_Repr_with_Context x) ->
49 expr_from (Proxy::Proxy root) ast_y ctx $
50 \(ty_y::Type_Root_of_Expr root h_y) (Forall_Repr_with_Context y) ->
51 check_eq_type ex ast ty_x ty_y $ \Refl ->
52 check_constraint_type ex (Proxy::Proxy Ord) ast ty_x $ \Dict ->
53 k type_ordering $ Forall_Repr_with_Context $
54 \c -> x c `compare` y c