]> 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.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
18
19 -- * Class 'Sym_Ord'
20 -- | Symantic.
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
26
27 instance (Sym_Ord r1, Sym_Ord r2) => Sym_Ord (Dup r1 r2) where
28 compare (x1 `Dup` x2) (y1 `Dup` y2) =
29 compare x1 y1 `Dup` compare x2 y2
30
31 -- * Type 'Expr_Ord'
32 -- | Expression.
33 data Expr_Ord (root:: *)
34 type instance Root_of_Expr (Expr_Ord root) = root
35 type instance Type_of_Expr (Expr_Ord root) = Type_Ordering
36 type instance Sym_of_Expr (Expr_Ord root) repr = (Sym_Ord repr)
37 type instance Error_of_Expr ast (Expr_Ord root) = No_Error_Expr
38
39 compare_from
40 :: forall root ty ast hs ret.
41 ( ty ~ Type_Root_of_Expr (Expr_Ord root)
42 , Type_Root_Lift Type_Ordering (Type_Root_of_Expr root)
43 , Eq_Type (Type_Root_of_Expr root)
44 , Expr_from ast root
45 , Error_Expr_Lift (Error_Expr (Error_of_Type ast ty) ty ast)
46 (Error_of_Expr ast root)
47 , Root_of_Expr root ~ root
48 , Constraint_Type Ord ty
49 ) => ast -> ast
50 -> Expr_From ast (Expr_Ord root) hs ret
51 compare_from ast_x ast_y ex ast ctx k =
52 expr_from (Proxy::Proxy root) ast_x ctx $
53 \(ty_x::Type_Root_of_Expr root h_x) (Forall_Repr_with_Context x) ->
54 expr_from (Proxy::Proxy root) ast_y ctx $
55 \(ty_y::Type_Root_of_Expr root h_y) (Forall_Repr_with_Context y) ->
56 check_eq_type ex ast ty_x ty_y $ \Refl ->
57 check_constraint_type ex (Proxy::Proxy Ord) ast ty_x $ \Dict ->
58 k type_ordering $ Forall_Repr_with_Context $
59 \c -> x c `compare` y c