summaryrefslogtreecommitdiff
path: root/src/Propellor/Types/Target.hs
blob: 228aae70b25bc95a1ed2ad252544e5274c7d730c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
{-# LANGUAGE TypeOperators, PolyKinds, DataKinds, TypeFamilies, UndecidableInstances #-}

module Propellor.Types.Target (
	Target(..),
	Targeting(..),
	target,
	UnixLike,
	unixLike,
	DebianOnly,
	debian,
	BuntishOnly,
	buntish,
	FreeBSDOnly,
	freeBSD,
	includeTarget,
	intersectTarget,
) where

import Network.BSD (HostName)
import Data.Typeable
import Data.String

data Property target = Property target (IO ())

mkProperty :: IO () -> Property UnixLike
mkProperty a = Property unixLike a

-- | Changes the target of a property.
--
-- This can only tighten the target list to contain fewer targets.
target 
	:: (newtarget' ~ IntersectTarget oldtarget newtarget,  CannotCombineTargets oldtarget newtarget newtarget' ~ CanCombineTargets)
	=> Targeting newtarget -> Property (Targeting oldtarget) -> Property (Targeting newtarget')
target newtarget (Property oldtarget a) = Property (intersectTarget oldtarget newtarget) a

-- | Makes a property that uses either of the two input properties,
-- depending on the targeted OS.
--
-- If both input properties support the targeted OS, then the first will be
-- used.
orProperty
	:: Property (Targeting a)
	-> Property (Targeting b)
	-> Property (Targeting (UnionTarget a b))
orProperty a@(Property ta ioa) b@(Property tb iob) =
	Property (unionTarget ta tb) io
  where
	-- TODO pick with of ioa or iob to use based on final OS of
	-- system being run on.
	io = undefined

----- DEMO ----------
-- Intentionally a type error! :)
--foo :: Property (Targeting '[OSDebian, OSFreeBSD])
--foo = Property supportedos $ do
--	ensureProperty supportedos jail
--  where supportedos = includeTarget debian freeBSD

--bar :: Property (Targeting '[OSDebian, OSFreeBSD])
bar = aptinstall `orProperty` jail

aptinstall :: Property DebianOnly
aptinstall = target debian $ mkProperty $ do
	return ()

jail :: Property FreeBSDOnly
jail = target freeBSD $ mkProperty $ do
	return ()
----- END DEMO ----------

-- | A Target system, where a Property is indended to be used.
data Target = OSDebian | OSBuntish | OSFreeBSD
	deriving (Show, Eq)

-- | A type-level and value-level list of targets.
data Targeting (os :: [Target]) = Targeting [Target]
	deriving (Show, Eq)

type UnixLike = Targeting '[OSDebian, OSBuntish, OSFreeBSD]

unixLike :: UnixLike
unixLike = Targeting [OSDebian, OSBuntish, OSFreeBSD]

type DebianOnly = Targeting '[OSDebian]

debian :: DebianOnly
debian = targeting OSDebian

type BuntishOnly = Targeting '[OSBuntish]

buntish :: BuntishOnly
buntish = targeting OSBuntish

type FreeBSDOnly = Targeting '[OSFreeBSD]

freeBSD :: FreeBSDOnly
freeBSD = targeting OSFreeBSD

targeting :: Target -> Targeting os
targeting o = Targeting [o]

ensureProperty
	:: ((innertarget `NotSupersetTargets` outertarget) ~ CanCombineTargets)
	=> Targeting outertarget
	-> Property (Targeting innertarget)
	-> IO ()
ensureProperty outeros (Property inneros a) = a

-- | Adds to a list of targets.
includeTarget
	:: (r ~ ConcatTargeting l1 l2)
	=> Targeting l1
	-> Targeting l2
	-> Targeting r
includeTarget (Targeting l1) (Targeting l2) = Targeting (l1 ++ l2)

-- | Type level concat for Targeting.
type family ConcatTargeting (list1 :: [a]) (list2 :: [a]) :: [a]
type instance ConcatTargeting '[] list2 = list2
type instance ConcatTargeting (a ': rest) list2 = a ': ConcatTargeting rest list2

-- | The intersection between two lists of Targets.
intersectTarget
	:: (r ~ IntersectTarget l1 l2, CannotCombineTargets l1 l2 r ~ CanCombineTargets)
	=> Targeting l1
	-> Targeting l2
	-> Targeting r
intersectTarget (Targeting l1) (Targeting l2) = Targeting (filter (`elem` l2) l1)

data CheckCombineTargets = CannotCombineTargets | CanCombineTargets

-- | Detect intersection of two lists that don't have any common OS.
-- 
-- The name of this was chosen to make type errors a more understandable.
type family CannotCombineTargets (list1 :: [a]) (list2 :: [a]) (listr :: [a]) :: CheckCombineTargets
type instance CannotCombineTargets l1 l2 '[] = 'CannotCombineTargets
type instance CannotCombineTargets l1 l2 (a ': rest) = 'CanCombineTargets

-- | Everything in the subset must be in the superset.
--
-- The name of this was chosen to make type errors a more understandable.
type family NotSupersetTargets (superset :: [a]) (subset :: [a]) :: CheckCombineTargets
type instance NotSupersetTargets superset '[] = 'CanCombineTargets
type instance NotSupersetTargets superset (s ': rest) = 
	If (ElemTarget s superset)
		(NotSupersetTargets superset rest)
		'CannotCombineTargets

-- | Type level intersection for Targeting
type family IntersectTarget (list1 :: [a]) (list2 :: [a]) :: [a]
type instance IntersectTarget '[] list2 = '[]
type instance IntersectTarget (a ': rest) list2 = 
	If (ElemTarget a list2)
		(a ': IntersectTarget rest list2)
		(IntersectTarget rest list2)

-- | Type level elem for Target
type family ElemTarget (a :: Target) (list :: [Target]) :: Bool
type instance ElemTarget a '[] = 'False
type instance ElemTarget a (b ': bs) = EqTarget a b || ElemTarget a bs

-- | Type level equality for Target
--
-- This is a very clumsy implmentation, but it works back to ghc 7.6.
type family EqTarget (a :: Target) (b :: Target) :: Bool
type instance EqTarget OSDebian  OSDebian  = 'True
type instance EqTarget OSBuntish OSBuntish = 'True
type instance EqTarget OSFreeBSD OSFreeBSD = 'True
type instance EqTarget OSDebian  OSBuntish = 'False
type instance EqTarget OSDebian  OSFreeBSD = 'False
type instance EqTarget OSBuntish OSDebian  = 'False
type instance EqTarget OSBuntish OSFreeBSD = 'False
type instance EqTarget OSFreeBSD OSDebian  = 'False
type instance EqTarget OSFreeBSD OSBuntish = 'False
-- More modern version if the combinatiorial explosion gets too bad later:
--
-- type family EqTarget (a :: Target) (b :: Target) where
-- 	EqTarget a a = True
-- 	EqTarget a b = False

-- | This is in Data.Type.Bool with modern versions of ghc, but is included
-- here to support ghc 7.6.
type family If (cond :: Bool) (tru :: a) (fls :: a) :: a
type instance If 'True  tru fls = tru
type instance If 'False tru fls = fls
type family (a :: Bool) || (b :: Bool) :: Bool
type instance 'False || 'False = 'False
type instance 'True  || 'True  = 'True
type instance 'True  || 'False = 'True
type instance 'False || 'True  = 'True