1 {-# LANGUAGE DefaultSignatures #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE FlexibleInstances #-}
5 {-# LANGUAGE MultiParamTypeClasses #-}
6 {-# LANGUAGE OverloadedStrings #-}
7 {-# LANGUAGE Rank2Types #-}
8 {-# LANGUAGE ScopedTypeVariables #-}
9 {-# LANGUAGE TypeFamilies #-}
10 {-# LANGUAGE UndecidableInstances #-}
11 -- | Expression for 'Int'.
12 module Language.LOL.Symantic.Expr.Int where
14 import Data.Proxy (Proxy(..))
15 import Data.Type.Equality ((:~:)(Refl))
17 import Language.LOL.Symantic.AST
18 import Language.LOL.Symantic.Type
19 import Language.LOL.Symantic.Expr.Common
20 import Language.LOL.Symantic.Expr.Lambda
21 import Language.LOL.Symantic.Repr.Dup
22 import Language.LOL.Symantic.Trans.Common
27 class Sym_Int repr where
28 int :: Int -> repr Int
29 neg :: repr Int -> repr Int
30 add :: repr Int -> repr Int -> repr Int
32 default int :: Trans t repr => Int -> t repr Int
33 default neg :: Trans t repr => t repr Int -> t repr Int
34 default add :: Trans t repr => t repr Int -> t repr Int -> t repr Int
35 int = trans_lift . int
39 instance -- Sym_Int Dup
42 ) => Sym_Int (Dup r1 r2) where
43 int x = int x `Dup` int x
44 neg (x1 `Dup` x2) = neg x1 `Dup` neg x2
45 add (x1 `Dup` x2) (y1 `Dup` y2) = add x1 y1 `Dup` add x2 y2
49 data Expr_Int (root:: *)
50 type instance Root_of_Expr (Expr_Int root) = root
51 type instance Type_of_Expr (Expr_Int root) = Type_Int
52 type instance Sym_of_Expr (Expr_Int root) repr = Sym_Int repr
53 type instance Error_of_Expr ast (Expr_Int root) = ()
55 instance -- Expr_from AST Expr_Int
56 ( Type_from AST (Type_Root_of_Expr root)
59 , Type_Root_Lift Type_Int (Type_Root_of_Expr root)
60 , Error_Type_Lift (Error_Type_Fun AST)
61 (Error_of_Type AST (Type_Root_of_Expr root))
62 , Error_Expr_Lift (Error_Expr (Error_of_Type AST (Type_Root_of_Expr root))
63 ( Type_Root_of_Expr root)
65 (Error_of_Expr AST root)
67 , Type_Unlift Type_Int (Type_of_Expr root)
69 , Root_of_Expr root ~ root
71 , Implicit_HBool (Is_Last_Expr (Expr_Int root) root)
72 ) => Expr_from AST (Expr_Int root) where
73 expr_from px_ex ctx ast k =
78 case read_safe ast_int of
79 Left err -> Left $ error_expr px_ex $ Error_Expr_Read err ast
81 k type_int $ Forall_Repr_with_Context $
83 _ -> Left $ error_expr px_ex $
84 Error_Expr_Wrong_number_of_arguments 3 ast
85 AST "neg" asts -> unary_from asts neg
86 AST "add" asts -> binary_from asts add
88 case hbool :: HBool (Is_Last_Expr (Expr_Int root) root) of
89 HTrue -> error_expr px_ex $ Error_Expr_Unsupported ast
90 HFalse -> error_expr px_ex $ Error_Expr_Unsupported_here ast
93 (op::forall repr. Sym_Int repr
94 => repr Int -> repr Int) =
97 expr_from (Proxy::Proxy root) ctx ast_x $ \ty_x (Forall_Repr_with_Context x) ->
98 when_type_eq px_ex ast type_int ty_x $ \Refl ->
99 k ty_x $ Forall_Repr_with_Context (op . x)
100 _ -> Left $ error_expr px_ex $
101 Error_Expr_Wrong_number_of_arguments 1 ast
103 (op::forall repr. Sym_Int repr
104 => repr Int -> repr Int -> repr Int) =
107 expr_from (Proxy::Proxy root) ctx ast_x $ \ty_x (Forall_Repr_with_Context x) ->
108 expr_from (Proxy::Proxy root) ctx ast_y $ \ty_y (Forall_Repr_with_Context y) ->
109 when_type_eq px_ex ast type_int ty_x $ \Refl ->
110 when_type_eq px_ex ast type_int ty_y $ \Refl ->
111 k ty_x $ Forall_Repr_with_Context $
113 _ -> Left $ error_expr px_ex $
114 Error_Expr_Wrong_number_of_arguments 2 ast
116 -- ** Type 'Expr_Lambda_Int'
117 -- | Convenient alias.
118 type Expr_Lambda_Int lam = Expr_Root (Expr_Alt (Expr_Lambda lam) Expr_Int)
120 -- | Convenient alias around 'expr_from'.
123 Expr_from ast (Expr_Lambda_Int lam)
126 -> Either (Error_of_Expr ast (Expr_Lambda_Int lam))
127 (Exists_Type_and_Repr (Type_Root_of_Expr (Expr_Lambda_Int lam))
128 (Forall_Repr (Expr_Lambda_Int lam)))
129 expr_lambda_int_from _px_lam ast =
131 (Proxy::Proxy (Expr_Lambda_Int lam))
132 Context_Empty ast $ \ty (Forall_Repr_with_Context repr) ->
133 Right $ Exists_Type_and_Repr ty $
134 Forall_Repr $ repr Context_Empty