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