1 {-# LANGUAGE ConstraintKinds #-}
2 {-# LANGUAGE FlexibleContexts #-}
3 {-# LANGUAGE DefaultSignatures #-}
4 {-# LANGUAGE MultiParamTypeClasses #-}
5 {-# LANGUAGE NoMonomorphismRestriction #-}
6 {-# LANGUAGE ScopedTypeVariables #-}
7 {-# LANGUAGE TypeApplications #-}
8 {-# LANGUAGE TypeFamilies #-}
9 {-# LANGUAGE NoImplicitPrelude #-}
10 module Symantic.Typed.Lang where
12 import Data.Char (Char)
13 import Data.Bool (Bool(..))
14 import Data.Either (Either(..))
16 import Data.Maybe (Maybe(..))
17 import qualified Data.Function as Fun
19 import Symantic.Typed.Trans
21 class Abstractable repr where
22 -- | Application, aka. unabstract.
23 (.@) :: repr (a->b) -> repr a -> repr b; infixl 9 .@
24 -- | Lambda term abstraction, in HOAS (Higher-Order Abstract Syntax) style.
25 lam :: (repr a -> repr b) -> repr (a->b)
26 -- | Like 'lam' but whose argument is used only once,
27 -- hence safe to beta-reduce (inline) without duplicating work.
28 lam1 :: (repr a -> repr b) -> repr (a->b)
29 const :: repr (a -> b -> a)
30 flip :: repr ((a -> b -> c) -> b -> a -> c)
32 (.) :: repr ((b->c) -> (a->b) -> a -> c); infixr 9 .
33 ($) :: repr ((a->b) -> a -> b); infixr 0 $
34 var :: repr a -> repr a
36 Liftable2 repr => Abstractable (Unlifted repr) =>
37 repr (a->b) -> repr a -> repr b
39 Liftable repr => Unliftable repr => Abstractable (Unlifted repr) =>
40 (repr a -> repr b) -> repr (a->b)
42 Liftable repr => Unliftable repr => Abstractable (Unlifted repr) =>
43 (repr a -> repr b) -> repr (a->b)
45 Liftable repr => Abstractable (Unlifted repr) =>
48 Liftable repr => Abstractable (Unlifted repr) =>
49 repr ((a -> b -> c) -> b -> a -> c)
51 Liftable repr => Abstractable (Unlifted repr) =>
54 Liftable repr => Abstractable (Unlifted repr) =>
55 repr ((b->c) -> (a->b) -> a -> c)
57 Liftable repr => Abstractable (Unlifted repr) =>
58 repr ((a->b) -> a -> b)
60 Liftable1 repr => Abstractable (Unlifted repr) =>
63 lam f = lift (lam (trans Fun.. f Fun.. trans))
64 lam1 f = lift (lam1 (trans Fun.. f Fun.. trans))
71 class Anythingable repr where
72 anything :: repr a -> repr a
74 class Constantable c repr where
75 constant :: c -> repr c
77 Liftable repr => Constantable c (Unlifted repr) =>
79 constant = lift Fun.. constant
80 bool :: Constantable Bool repr => Bool -> repr Bool
82 char :: Constantable Char repr => Char -> repr Char
84 unit :: Constantable () repr => repr ()
85 unit = constant @() ()
86 class Eitherable repr where
87 left :: repr (l -> Either l r)
88 right :: repr (r -> Either l r)
90 Liftable repr => Eitherable (Unlifted repr) =>
91 repr (l -> Either l r)
93 Liftable repr => Eitherable (Unlifted repr) =>
94 repr (r -> Either l r)
97 class Equalable repr where
98 equal :: Eq a => repr (a -> a -> Bool)
100 Liftable repr => Equalable (Unlifted repr) =>
101 Eq a => repr (a -> a -> Bool)
104 (==) :: (Abstractable repr, Equalable repr, Eq a) => repr (a -> a -> Bool)
105 (==) = lam (\x -> lam (\y -> equal .@ x .@ y))
106 class IfThenElseable repr where
107 ifThenElse :: repr Bool -> repr a -> repr a -> repr a
108 default ifThenElse ::
109 Liftable3 repr => IfThenElseable (Unlifted repr) =>
110 repr Bool -> repr a -> repr a -> repr a
111 ifThenElse = lift3 ifThenElse
112 class Listable repr where
113 cons :: repr (a -> [a] -> [a])
116 Liftable repr => Listable (Unlifted repr) =>
117 repr (a -> [a] -> [a])
119 Liftable repr => Listable (Unlifted repr) =>
123 class Maybeable repr where
124 nothing :: repr (Maybe a)
125 just :: repr (a -> Maybe a)
127 Liftable repr => Maybeable (Unlifted repr) =>
130 Liftable repr => Maybeable (Unlifted repr) =>
132 nothing = lift nothing