Skip to content

Commit b26aa82

Browse files
Sarra SoussiaSarra Soussia
authored andcommitted
modified grade and dimension logic
1 parent 38750d8 commit b26aa82

File tree

1 file changed

+78
-140
lines changed
  • code/drasil-lang/lib/Language/Drasil

1 file changed

+78
-140
lines changed
Lines changed: 78 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,175 +1,123 @@
1-
{-# LANGUAGE DataKinds #-}
2-
{-# LANGUAGE FlexibleContexts #-}
3-
{-# LANGUAGE FlexibleInstances #-}
4-
{-# LANGUAGE GADTs #-}
5-
{-# LANGUAGE KindSignatures #-}
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE GADTs #-}
3+
{-# LANGUAGE FlexibleContexts #-}
4+
{-# LANGUAGE FlexibleInstances #-}
5+
{-# LANGUAGE KindSignatures #-}
66
{-# LANGUAGE MultiParamTypeClasses #-}
77

8-
-- | Number space types and functions.
8+
-- | Number space types and functions, including Clifford algebra (symbolic).
99
module Language.Drasil.Space (
10-
-- * Types
11-
Space(..), Primitive,
12-
RealInterval(..), Inclusive(..),
13-
DomainDesc(..), RTopology(..), DiscreteDomainDesc, ContinuousDomainDesc,
14-
-- * Class
15-
HasSpace(..),
16-
-- * Functions
17-
getActorName, getInnerType, mkFunction, isBasicNumSpace,
18-
Dimension(..), scalarS, vect2DS, vect3DS, vectS, vectNDS,
19-
bivector2DS, bivector3DS, bivectorS, bivectorNDS,
20-
multivector2DS, multivector3DS, multivectorS, multivectorNDS,
21-
ClifKind(..)
22-
10+
-- * Types
11+
Space(..), Primitive,
12+
Dimension(..),
13+
RealInterval(..), Inclusive(..),
14+
DomainDesc(..), RTopology(..), DiscreteDomainDesc, ContinuousDomainDesc,
15+
-- * Classes
16+
HasSpace(..),
17+
-- * Functions
18+
getActorName, getInnerType, mkFunction, isBasicNumSpace,
19+
-- * Clifford helpers
20+
scalarS, vect2DS, vect3DS, vectS,
21+
bivector2DS, bivector3DS, bivectorS,
22+
multivector2DS, multivector3DS, multivectorS
2323
) where
2424

25-
import qualified Data.List.NonEmpty as NE
26-
27-
import Control.Lens (Getter)
28-
import Language.Drasil.Symbol (Symbol)
29-
import Numeric.Natural (Natural)
30-
31-
32-
-- FIXME: These need to be spaces and not just types.
33-
34-
-- | The difference kinds of spaces that may exist. This type holds
35-
-- numerical spaces (such as the set of integers, rationals, etc.),
36-
-- a space for booleans, a space for characters, dimensional spaces (vectors, arrays, etc.),
37-
-- a space for Actors, discrete sets (both for numbers and strings), and a void space.
38-
data Space where
39-
Integer :: Space
40-
Rational :: Space
41-
Real :: Space
42-
Natural :: Space
43-
Boolean :: Space
44-
Char :: Space
45-
String :: Space
46-
Set :: Space -> Space
47-
Matrix :: Int -> Int -> Space -> Space
48-
Array :: Space -> Space
49-
Actor :: String -> Space
50-
Function :: (NE.NonEmpty Primitive) -> Primitive -> Space
51-
Void :: Space
52-
-- | Clifford algebra objects (Clifs) with a dimension and kind (e.g., Scalar, Vector, Bivector, Multivector)
53-
ClifS :: Dimension -> ClifKind -> Space -> Space
54-
-- ClifS (Fixed 3) [0,1,2] Real -- Contains grades 0, 1, 2
55-
-- ClifS (Fixed 3) [1] Real --pure vector
25+
import qualified Data.List.NonEmpty as NE
26+
import Control.Lens (Getter)
27+
import Language.Drasil.Symbol (Symbol)
28+
import Numeric.Natural (Natural)
5629

30+
-------------------------------------------------------------------------------
31+
-- | Dimension of the underlying vector space.
32+
-- Currently we only support fixed, concrete dimensions (Fixed n)
33+
-- to simplify grade checking and avoid symbolic/type-level complications.
34+
newtype Dimension = Fixed Natural
5735
deriving (Eq, Show)
5836

59-
-- grade selection can be undefined if we want to create pure grade objects (vectors..) bc clifkinf implies the grade
60-
-- gradeselect fct handles grade extraction at runtime
61-
62-
-- | Kinds of Clifford algebra objects.
63-
data ClifKind = Scalar | Vector | Bivector | Multivector --here the grade is implicit but
64-
-- with the gradeSelect function i have a type lvl and runtime lvl inconsistency
65-
deriving (Eq, Show)
66-
67-
-- To explicitly define the grade its either I change the ClifKind to Natural
68-
-- i add both grade and kind
69-
70-
-- Suggestion: In order to support things like Fixed (a + b) in the future,
71-
-- we want to use an Expr or symbolic type instead of Natural / String.
72-
-- Not needed now, but worth keeping in mind.
73-
74-
-- | The dimension of a clif
75-
data Dimension where
76-
-- | Fixed dimension
77-
Fixed :: Natural -> Dimension
78-
-- | Variable dimension
79-
VDim :: String -> Dimension
37+
-------------------------------------------------------------------------------
38+
-- | Clifford algebra object represented by a list of grades.
39+
-- Grades are integers between 0 and the space dimension.
40+
data Space where
41+
Integer :: Space
42+
Rational :: Space
43+
Real :: Space
44+
Natural :: Space
45+
Boolean :: Space
46+
Char :: Space
47+
String :: Space
48+
Set :: Space -> Space
49+
Matrix :: Int -> Int -> Space -> Space
50+
Array :: Space -> Space
51+
Actor :: String -> Space
52+
Function :: NE.NonEmpty Primitive -> Primitive -> Space
53+
Void :: Space
54+
ClifS :: Dimension -> [Int] -> Space -> Space -- Clifford algebra object
8055
deriving (Eq, Show)
8156

82-
-- Example of a 3D vector of real numbers using ClifS
83-
-- exampleVector :: Space
84-
-- exampleVector = ClifS (Fixed 3) Vector Real
85-
86-
-- A scalar (real number) using ClifS
87-
-- exampleScalar :: Space
88-
-- exampleScalar = ClifS (Fixed 1) Scalar Real
89-
90-
-- An n-dimensional multivector of real numbers:
91-
-- ClifS (VDim "n") Multivector Real
92-
93-
-- TODO: check if non-real numbers in Clifs make any sense; allowing for now to avoid errors in offending examples
94-
-- as we figure out matrices
57+
-------------------------------------------------------------------------------
9558
-- | Only allow Real as the inner space for now.
9659
checkClifSpace :: Space -> Bool
9760
checkClifSpace Real = True
98-
checkClifSpace _ = True --error $ "Non-real clif spaces unsupported"
61+
checkClifSpace _ = True -- placeholder for future type checking
62+
63+
-- | Helper functions to construct Clifford objects (scalar, vector, bivector, multivector)
9964

100-
-- | Helper that constructs a scalar Clifford object
10165
scalarS :: Space -> Space
102-
scalarS s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed 1) Scalar s
103-
scalarS s = s -- fallback: leave the space unchanged
66+
scalarS s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed 1) [0] s
67+
scalarS s = s
10468

10569
vect2DS :: Space -> Space
106-
vect2DS s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed 2) Vector s
70+
vect2DS s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed 2) [1] s
10771
vect2DS s = s
10872

10973
vect3DS :: Space -> Space
110-
vect3DS s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed 3) Vector s
74+
vect3DS s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed 3) [1] s
11175
vect3DS s = s
11276

11377
vectS :: Natural -> Space -> Space
114-
vectS n s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed n) Vector s
78+
vectS n s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed n) [1] s
11579
vectS _ s = s
11680

117-
vectNDS :: String -> Space -> Space
118-
vectNDS x s | isBasicNumSpace s && checkClifSpace s = ClifS (VDim x) Vector s
119-
vectNDS _ s = s
120-
121-
bivector2DS :: Space -> Space
122-
bivector2DS s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed 2) Bivector s
81+
bivector2DS, bivector3DS :: Space -> Space
82+
bivector2DS s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed 2) [2] s
12383
bivector2DS s = s
124-
125-
bivector3DS :: Space -> Space
126-
bivector3DS s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed 3) Bivector s
84+
bivector3DS s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed 3) [2] s
12785
bivector3DS s = s
12886

12987
bivectorS :: Natural -> Space -> Space
130-
bivectorS n s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed n) Bivector s
88+
bivectorS n s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed n) [2] s
13189
bivectorS _ s = s
13290

133-
bivectorNDS :: String -> Space -> Space
134-
bivectorNDS x s | isBasicNumSpace s && checkClifSpace s = ClifS (VDim x) Bivector s
135-
bivectorNDS _ s = s
136-
137-
multivector2DS :: Space -> Space
138-
multivector2DS s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed 2) Multivector s
91+
multivector2DS, multivector3DS :: Space -> Space
92+
multivector2DS s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed 2) [0,1,2] s
13993
multivector2DS s = s
140-
141-
multivector3DS :: Space -> Space
142-
multivector3DS s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed 3) Multivector s
94+
multivector3DS s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed 3) [0,1,2,3] s
14395
multivector3DS s = s
14496

14597
multivectorS :: Natural -> Space -> Space
146-
multivectorS n s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed n) Multivector s
98+
multivectorS n s | isBasicNumSpace s && checkClifSpace s = ClifS (Fixed n) [0..fromIntegral n] s
14799
multivectorS _ s = s
148100

149-
multivectorNDS :: String -> Space -> Space
150-
multivectorNDS x s | isBasicNumSpace s && checkClifSpace s = ClifS (VDim x) Multivector s
151-
multivectorNDS _ s = s
152-
101+
-------------------------------------------------------------------------------
153102
-- | HasSpace is anything which has a 'Space'.
154103
class HasSpace c where
155-
-- | Provides a 'Getter' to the 'Space'.
156-
typ :: Getter c Space
104+
typ :: Getter c Space
157105

158106
type Primitive = Space
159107

160108
mkFunction :: [Primitive] -> Primitive -> Space
161109
mkFunction [] = error "Function space creation requires at least 1 input Space"
162110
mkFunction ins = Function (NE.fromList ins)
163111

164-
-- The 'spaces' below are all good.
165-
112+
-------------------------------------------------------------------------------
166113
-- | Topology of a subset of reals.
167114
data RTopology = Continuous | Discrete
168115

169-
-- | Describes the domain of a 'Symbol' given a topology. Can be bounded or encase all of the domain.
116+
-- | Describes the domain of a 'Symbol' given a topology.
117+
-- Can be bounded or encase all of the domain.
170118
data DomainDesc (tplgy :: RTopology) a b where
171-
BoundedDD :: Symbol -> RTopology -> a -> b -> DomainDesc 'Discrete a b
172-
AllDD :: Symbol -> RTopology -> DomainDesc 'Continuous a b
119+
BoundedDD :: Symbol -> RTopology -> a -> b -> DomainDesc 'Discrete a b
120+
AllDD :: Symbol -> RTopology -> DomainDesc 'Continuous a b
173121

174122
type DiscreteDomainDesc a b = DomainDesc 'Discrete a b
175123
type ContinuousDomainDesc a b = DomainDesc 'Continuous a b
@@ -178,39 +126,29 @@ type ContinuousDomainDesc a b = DomainDesc 'Continuous a b
178126
data Inclusive = Inc | Exc
179127

180128
-- | A RealInterval is a subset of 'Real' (as a 'Space').
181-
-- These come in different flavours.
182-
-- For now, we embed 'Expr' for the bounds, but that will change as well.
183129
data RealInterval a b where
184-
Bounded :: (Inclusive, a) -> (Inclusive, b) -> RealInterval a b -- ^ Interval from (x .. y).
185-
UpTo :: (Inclusive, a) -> RealInterval a b -- ^ Interval from (-infinity .. x).
186-
UpFrom :: (Inclusive, b) -> RealInterval a b -- ^ Interval from (x .. infinity).
130+
Bounded :: (Inclusive, a) -> (Inclusive, b) -> RealInterval a b
131+
UpTo :: (Inclusive, a) -> RealInterval a b
132+
UpFrom :: (Inclusive, b) -> RealInterval a b
187133

134+
-------------------------------------------------------------------------------
188135
-- | Gets the name of an 'Actor'.
189136
getActorName :: Space -> String
190137
getActorName (Actor n) = n
191138
getActorName _ = error "getActorName called on non-actor space"
192139

193-
-- | Gets the inner 'Type'
140+
-- | Gets the inner 'Space'
194141
getInnerType :: Space -> Space
195142
getInnerType (Set s) = s
196-
getInnerType (Array s) = s
143+
getInnerType (Array s) = s
197144
getInnerType (Matrix _ _ s) = s
198145
getInnerType (ClifS _ _ s) = s
199-
getInnerType _ = error "getInnerType called on non-vector space"
146+
getInnerType _ = error "getInnerType called on non-container space"
200147

201148
-- | Is this Space a basic numeric space?
202149
isBasicNumSpace :: Space -> Bool
203150
isBasicNumSpace Integer = True
204151
isBasicNumSpace Rational = True
205152
isBasicNumSpace Real = True
206153
isBasicNumSpace Natural = True
207-
isBasicNumSpace Boolean = False
208-
isBasicNumSpace Char = False
209-
isBasicNumSpace String = False
210-
isBasicNumSpace Set {} = False
211-
isBasicNumSpace Matrix {} = False
212-
isBasicNumSpace Array {} = False
213-
isBasicNumSpace Actor {} = False
214-
isBasicNumSpace Function {} = False
215-
isBasicNumSpace Void = False
216-
isBasicNumSpace (ClifS _ _ s) = isBasicNumSpace s
154+
isBasicNumSpace _ = False

0 commit comments

Comments
 (0)