]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/Ord.hs
IO, Monoid, Foldable, Text
[haskell/symantic.git] / Language / Symantic / Expr / Ord.hs
1 {-# LANGUAGE DefaultSignatures #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE ScopedTypeVariables #-}
6 {-# LANGUAGE TypeFamilies #-}
7 {-# LANGUAGE TypeOperators #-}
8 -- | Expression for 'Ord'.
9 module Language.Symantic.Expr.Ord where
10
11 import Data.Proxy (Proxy(..))
12 import Data.Type.Equality ((:~:)(Refl))
13 import Prelude hiding (compare)
14
15 import Language.Symantic.Trans.Common
16 import Language.Symantic.Type
17 import Language.Symantic.Expr.Root
18 import Language.Symantic.Expr.Error
19 import Language.Symantic.Expr.From
20 import Language.Symantic.Expr.Eq
21
22 -- * Class 'Sym_Ord'
23 -- | Symantic.
24 class Sym_Eq repr => Sym_Ord repr where
25 compare :: Ord a => repr a -> repr a -> repr Ordering
26 default compare :: (Trans t repr, Ord a)
27 => t repr a -> t repr a -> t repr Ordering
28 compare = trans_map2 compare
29
30 -- * Type 'Expr_Ord'
31 -- | Expression.
32 data Expr_Ord (root:: *)
33 type instance Root_of_Expr (Expr_Ord root) = root
34 type instance Type_of_Expr (Expr_Ord root) = Type_Ordering
35 type instance Sym_of_Expr (Expr_Ord root) repr = (Sym_Ord repr)
36 type instance Error_of_Expr ast (Expr_Ord root) = No_Error_Expr
37
38 instance Constraint_Type Ord (Type_Var0 root)
39 instance Constraint_Type Ord (Type_Var1 root)
40
41 compare_from
42 :: forall root ty ast hs ret.
43 ( ty ~ Type_Root_of_Expr (Expr_Ord root)
44 , Eq_Type ty
45 , Expr_from ast root
46 , Lift_Type Type_Ordering (Type_of_Expr root)
47 , Lift_Error_Expr (Error_Expr (Error_of_Type ast ty) ty ast)
48 (Error_of_Expr ast root)
49 , Root_of_Expr root ~ root
50 , Constraint_Type Ord ty
51 ) => ast -> ast
52 -> Expr_From ast (Expr_Ord root) hs ret
53 compare_from ast_x ast_y ex ast ctx k =
54 expr_from (Proxy::Proxy root) ast_x ctx $
55 \(ty_x::ty h_x) (Forall_Repr_with_Context x) ->
56 expr_from (Proxy::Proxy root) ast_y ctx $
57 \(ty_y::ty h_y) (Forall_Repr_with_Context y) ->
58 check_eq_type ex ast ty_x ty_y $ \Refl ->
59 check_constraint_type ex (Proxy::Proxy Ord) ast ty_x $ \Dict ->
60 k type_ordering $ Forall_Repr_with_Context $
61 \c -> x c `compare` y c