|
| 1 | +{-# LANGUAGE AllowAmbiguousTypes #-} |
| 2 | +{-# LANGUAGE QuantifiedConstraints #-} |
| 3 | + |
1 | 4 | module DicePrelude where
|
2 | 5 |
|
3 | 6 | import Data.Map qualified as M
|
4 |
| -import Data.Singletons (SingI) |
5 |
| -import TypeCheckerCore (DType (..), ExprT (Lit), HsOf, Res (Res)) |
6 |
| - |
7 |
| -class (HsOf (DOf l) ~ l, SingI (DOf l)) => Lit l where |
8 |
| - type DOf l :: DType |
9 |
| - lit :: l -> Res |
10 |
| - lit = Res @(DOf l) . lit' |
11 |
| - lit' :: l -> ExprT (DOf l) |
12 |
| - lit' = Lit |
13 |
| - |
14 |
| -instance Lit Int where |
15 |
| - type DOf Int = DInt |
16 |
| - |
17 |
| -instance Lit Bool where |
18 |
| - type DOf Bool = DBool |
| 7 | +import TypeCheckerCore (DType (..), ExprT (Hask), Res (Res), HRefable (..), Ref, sRef, HsOf) |
| 8 | +import Prelude.Singletons (withSingI) |
| 9 | +import Data.Singletons (SingI(..), Sing) |
19 | 10 |
|
20 |
| -instance (Lit a, Lit b) => Lit (a -> b) where |
21 |
| - type DOf (a -> b) = DFun (DOf a) (DOf b) |
22 | 11 |
|
23 | 12 | prelude :: Map Text Res
|
24 | 13 | prelude =
|
25 | 14 | M.fromList
|
26 |
| - [ ("+", lit @(Int -> _) (+)) |
27 |
| - , ("-", lit @(Int -> _) (-)) |
28 |
| - , ("*", lit @(Int -> _) (*)) |
29 |
| - , ("/", lit @(Int -> _) div) |
30 |
| - , ("^", lit @(Int -> Int -> Int) (^)) |
31 |
| - , ("**", lit @(Int -> Int -> Int) (^)) |
32 |
| - , ("&&", lit @(Bool -> _) (&&)) |
33 |
| - , ("||", lit @(Bool -> _) (&&)) |
| 15 | + [ ("+", easy $ (+) @Int) |
| 16 | + , ("-", easy $ (-) @Int) |
| 17 | + , ("*", easy $ (*) @Int) |
| 18 | + , ("/", easy $ div @Int) |
| 19 | + , ("^", easy $ (^) @Int @Int) |
| 20 | + , ("**", easy $ (^) @Int @Int) |
| 21 | + , ("&&", easy (&&)) |
| 22 | + , ("||", easy (||)) |
| 23 | + -- TODO classes so you can compare lists and such? |
| 24 | + , (">", easy $ (>) @Int) |
| 25 | + , ("<", easy $ (<) @Int) |
| 26 | + , (">=", easy $ (>=) @Int) |
| 27 | + , ("<=", easy $ (<=) @Int) |
| 28 | + , ("==", easy $ (==) @Int) |
| 29 | + , ("/=", easy $ (/=) @Int) |
| 30 | + , ("++", easyP @([H0] -> [H0] -> [H0]) (++)) |
| 31 | + , ("ifte", easyP @(Bool -> H0 -> H0 -> H0) |
| 32 | + (\c t f -> if c then t else f)) |
| 33 | + , ("True" , easy True) |
| 34 | + , ("False" , easy False) |
| 35 | + , ("replicate" , easyP @(Int -> H0 -> [H0]) replicate) |
| 36 | + -- TODO probably better if this works more like replicateM |
| 37 | + -- may require seperate builtin? |
| 38 | + , ("even" , easy @(Int -> Bool) even) |
| 39 | + , ("odd" , easy @(Int -> Bool) odd) |
| 40 | + , ("id", easyP @(H0 -> H0) id) |
| 41 | + , ("length" , easyP @([H0] -> Int) length) |
| 42 | + , ("filter" , easyP @((H0 -> Bool) -> [H0] -> [H0]) filter) |
| 43 | + -- TODO more prelude functions |
| 44 | + -- especially (.) |
34 | 45 | ]
|
| 46 | + |
| 47 | + |
| 48 | +easyP :: forall h. |
| 49 | + (SingI (DOf h) ,Just0 (DOf h)) => (forall d. HsOf (Ref 0 d (DOf h))) -> Res |
| 50 | +easyP h = Res @(DOf h) $ buildPoly @h (\(Proxy :: Proxy d) -> h @d) |
| 51 | + |
| 52 | +buildPoly :: forall h . (SingI (DOf h) ,Just0 (DOf h)) => |
| 53 | + (forall d. Proxy d -> HsOf (Ref 0 d (DOf h))) -> ExprT (DOf h) |
| 54 | +buildPoly h = Hask $ HRef p (h $ Proxy @(DVar 0)) |
| 55 | + where |
| 56 | + p :: forall n t. (SingI n,SingI t) => |
| 57 | + Proxy '(n,t) -> HRefable (Ref n t (DOf h)) |
| 58 | + p Proxy = case sRef (sing @n) (sing @t) (sing @(DVar 0)) of |
| 59 | + (t' :: Sing t') -> withSingI t' $ case j01 @(DOf h) (Proxy @'(n,t)) of |
| 60 | + J01 -> go (Proxy @t') |
| 61 | + |
| 62 | + go :: forall t. SingI t => Proxy t -> HRefable (Ref 0 t (DOf h)) |
| 63 | + go Proxy = HRef |
| 64 | + (\(Proxy :: Proxy '(n1,t1)) -> case sRef (sing @n1) (sing @t1) (sing @t) of |
| 65 | + (t' :: Sing t') -> case j02 @(DOf h) (Proxy @'(n1,t,t1)) of |
| 66 | + J02 -> withSingI t' $ go (Proxy @t') |
| 67 | + ) |
| 68 | + (h $ Proxy @t) |
| 69 | + |
| 70 | +data HVar (n :: Natural) where |
| 71 | +type H0 = HVar 0 |
| 72 | +type H1 = HVar 1 |
| 73 | + |
| 74 | +type DOf :: Type -> DType |
| 75 | +type family DOf (t :: Type) where |
| 76 | + DOf Int = DInt |
| 77 | + DOf Bool = DBool |
| 78 | + DOf [a] = DList (DOf a) |
| 79 | + DOf (a -> b) = DFun (DOf a) (DOf b) |
| 80 | + DOf (HVar n) = DVar n |
| 81 | + |
| 82 | +-- Class for DTypes only polymorphic over DVar 0 |
| 83 | +class Ref 0 (DVar 0) d ~ d => Just0 (d :: DType) where |
| 84 | + j01 :: Proxy '(n,t) -> Just0D1 n t d |
| 85 | + j02 :: Proxy '(n,t,t1) -> Just0D2 n t t1 d |
| 86 | + |
| 87 | +data Just0D1 n t (d :: DType) where |
| 88 | + J01 :: Ref 0 (Ref n t (DVar 0)) d ~ Ref n t d => Just0D1 n t d |
| 89 | + |
| 90 | +data Just0D2 n t1 t2 (d :: DType) where |
| 91 | + J02 :: Ref 0 (Ref n t2 t1) d ~ Ref n t2 (Ref 0 t1 d) => Just0D2 n t1 t2 d |
| 92 | + |
| 93 | +instance Just0 (DVar 0) where |
| 94 | + j01 Proxy = J01 |
| 95 | + j02 Proxy = J02 |
| 96 | + |
| 97 | +instance Just0 DInt where |
| 98 | + j01 Proxy = J01 |
| 99 | + j02 Proxy = J02 |
| 100 | + |
| 101 | +instance Just0 DBool where |
| 102 | + j01 Proxy = J01 |
| 103 | + j02 Proxy = J02 |
| 104 | + |
| 105 | +instance Just0 l => Just0 (DList l) where |
| 106 | + j01 p = case j01 @l p of |
| 107 | + J01 -> J01 |
| 108 | + j02 p = case j02 @l p of |
| 109 | + J02 -> J02 |
| 110 | + |
| 111 | +instance (Just0 a,Just0 b) => Just0 (DFun a b) where |
| 112 | + j01 p = case j01 @a p of |
| 113 | + J01 -> case j01 @b p of |
| 114 | + J01 -> J01 |
| 115 | + j02 p = case j02 @a p of |
| 116 | + J02 -> case j02 @b p of |
| 117 | + J02 -> J02 |
| 118 | + |
| 119 | +-- Constant under refs |
| 120 | +data CRef (d :: DType) (n :: Natural) (t :: DType) where |
| 121 | + CRef :: d ~ Ref n t d => CRef d n t |
| 122 | + |
| 123 | +withCref :: forall d n t a. DEasy d => (Ref n t d ~ d => a) -> a |
| 124 | +withCref = case cref @d (Proxy @'(n,t)) of |
| 125 | + CRef -> id |
| 126 | + |
| 127 | +class SingI d => DEasy (d :: DType) where |
| 128 | + cref :: Proxy '(n,t) -> CRef d n t |
| 129 | + easy' :: HsOf d -> ExprT d |
| 130 | + |
| 131 | +easy :: forall h. (HsOf (DOf h) ~ h, DEasy (DOf h)) => h -> Res |
| 132 | +easy a = Res $ easy' @(DOf h) a |
| 133 | + |
| 134 | +instance DEasy DInt where |
| 135 | + cref Proxy = CRef |
| 136 | + easy' n = let go = HRef (\Proxy -> go) n in Hask go |
| 137 | + |
| 138 | +instance DEasy DBool where |
| 139 | + cref Proxy = CRef |
| 140 | + easy' n = let go = HRef (\Proxy -> go) n in Hask go |
| 141 | + |
| 142 | +instance DEasy l => DEasy (DList l) where |
| 143 | + cref (Proxy :: Proxy '(n,t)) = withCref @l @n @t CRef |
| 144 | + easy' n = let go = HRef (\(Proxy :: Proxy '(n,t)) -> withCref @l @n @t go) n in Hask go |
| 145 | + |
| 146 | +instance (DEasy a,DEasy b) => DEasy (DFun a b) where |
| 147 | + cref (Proxy :: Proxy '(n,t)) = withCref @a @n @t $ withCref @b @n @t $ CRef |
| 148 | + easy' n = let go = HRef (\(Proxy :: Proxy '(n,t)) -> withCref @a @n @t $ withCref @b @n @t go) n in Hask go |
0 commit comments