]> Git — Sourcephile - haskell/symantic.git/blob - symantic-lib/Language/Symantic/Lib/Function.hs
Support directories as targets in GNUmakefile.
[haskell/symantic.git] / symantic-lib / Language / Symantic / Lib / Function.hs
1 {-# LANGUAGE UndecidableInstances #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 -- | Symantic for '(->)'.
4 module Language.Symantic.Lib.Function where
5
6 import Prelude hiding (const, flip, id)
7 import qualified Data.Function as Fun
8 import qualified Data.MonoTraversable as MT
9
10 import Language.Symantic
11
12 -- * Class 'Sym_Function'
13 type instance Sym (Proxy (->)) = Sym_Function
14 class Sym_Function term where
15 comp :: term (b -> c) -> term (a -> b) -> term (a -> c); infixr 9 `comp`
16 const :: term a -> term b -> term a
17 flip :: term (a -> b -> c) -> term (b -> a -> c)
18 id :: term a -> term a
19 default comp :: Sym_Function (UnT term) => Trans term => term (b -> c) -> term (a -> b) -> term (a -> c)
20 default const :: Sym_Function (UnT term) => Trans term => term a -> term b -> term a
21 default flip :: Sym_Function (UnT term) => Trans term => term (a -> b -> c) -> term (b -> a -> c)
22 default id :: Sym_Function (UnT term) => Trans term => term a -> term a
23 comp = trans2 comp
24 const = trans2 const
25 flip = trans1 flip
26 id = trans1 id
27
28 -- Interpreting
29 instance Sym_Function Eval where
30 comp = eval2 (Fun..)
31 const = eval2 Fun.const
32 flip = eval1 Fun.flip
33 id = eval1 Fun.id
34 instance Sym_Function View where
35 comp = viewInfix "." (infixR 9)
36 const = view2 "const"
37 flip = view1 "flip"
38 id = view1 "id"
39 instance (Sym_Function r1, Sym_Function r2) => Sym_Function (Dup r1 r2) where
40 comp = dup2 @Sym_Function comp
41 const = dup2 @Sym_Function const
42 flip = dup1 @Sym_Function flip
43 id = dup1 @Sym_Function id
44
45 -- Transforming
46 instance (Sym_Function term, Sym_Lambda term) => Sym_Function (BetaT term)
47
48 -- Typing
49 instance ClassInstancesFor (->) where
50 proveConstraintFor _c (TyApp _ q (TyApp _ z _r))
51 | Just HRefl <- proj_ConstKiTy @_ @(->) z
52 = case () of
53 _ | Just HRefl <- proj_ConstKiTy @_ @Functor q -> Just Dict
54 | Just HRefl <- proj_ConstKiTy @_ @Applicative q -> Just Dict
55 | Just HRefl <- proj_ConstKiTy @_ @Monad q -> Just Dict
56 _ -> Nothing
57 proveConstraintFor _c (TyApp _ q (TyApp _ (TyApp _ z _a) b))
58 | Just HRefl <- proj_ConstKiTy @_ @(->) z
59 = case () of
60 _ | Just HRefl <- proj_ConstKiTy @_ @Monoid q
61 , Just Dict <- proveConstraint (q `tyApp` b) -> Just Dict
62 | Just HRefl <- proj_ConstKiTy @_ @MT.MonoFunctor q -> Just Dict
63 _ -> Nothing
64 proveConstraintFor _c _q = Nothing
65 instance TypeInstancesFor (->)
66
67 -- Compiling
68 instance Gram_Term_AtomsFor src ss g (->)
69 instance (Source src, Inj_Sym ss (->)) => Module src ss (->) where
70 module_ _s = [] `moduleWhere`
71 [ "const" := teFunction_const
72 , "flip" := teFunction_flip
73 , "id" := teFunction_id
74 , "." `withInfixR` 9 := teFunction_compose
75 -- , "$" `withInfixR` 0 := teFunction_app
76 ]
77
78 -- ** 'Type's
79 tyFun :: Source src => Inj_Len vs => Type src vs (->)
80 tyFun = tyConst @(K (->)) @(->)
81
82 a0 :: Source src => Inj_Len vs => Inj_Kind (K a) =>
83 Type src (Proxy a ': vs) a
84 a0 = tyVar "a" varZ
85
86 b1 :: Source src => Inj_Len vs => Inj_Kind (K b) =>
87 Type src (a ': Proxy b ': vs) b
88 b1 = tyVar "b" $ VarS varZ
89
90 c2 :: Source src => Inj_Len vs => Inj_Kind (K c) =>
91 Type src (a ': b ': Proxy c ': vs) c
92 c2 = tyVar "c" $ VarS $ VarS varZ
93
94 -- ** 'Term's
95 teFunction_compose :: TermDef (->) '[Proxy a, Proxy b, Proxy c] ((b -> c) -> (a -> b) -> (a -> c))
96 teFunction_compose = Term noConstraint ((b1 ~> c2) ~> (a0 ~> b1) ~> (a0 ~> c2)) $ teSym @(->) $ lam2 comp
97
98 teFunction_const :: TermDef (->) '[Proxy a, Proxy b] (a -> b -> a)
99 teFunction_const = Term noConstraint (a0 ~> b1 ~> a0) $ teSym @(->) $ lam2 const
100
101 teFunction_flip :: TermDef (->) '[Proxy a, Proxy b, Proxy c] ((a -> b -> c) -> (b -> a -> c))
102 teFunction_flip = Term noConstraint ((a0 ~> b1 ~> c2) ~> (b1 ~> a0 ~> c2)) $ teSym @(->) $ lam1 flip
103
104 teFunction_id :: TermDef (->) '[Proxy a] (a -> a)
105 teFunction_id = Term noConstraint (a0 ~> a0) $ teSym @(->) $ lam1 id