Skip to content

Commit

Permalink
Merge pull request #3183 from JacquesCarette/Issue3162
Browse files Browse the repository at this point in the history
Implement vector + and -
  • Loading branch information
balacij authored Dec 20, 2022
2 parents 4bdf163 + 2cd99a6 commit f7eaea7
Show file tree
Hide file tree
Showing 18 changed files with 81 additions and 12 deletions.
5 changes: 3 additions & 2 deletions code/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ SUMMARIZE_TEX=no

# Some commands now prefer to have TERM defined when things go awry
# and since this is going to be run in batch, 'dumb' is best default.
TERM=dumb
# But don't set TERM itself, as it will make things ugly for interactive use.
BATCHTERM=dumb

########################
#--- Actual targets ---#
Expand Down Expand Up @@ -314,7 +315,7 @@ $(filter %$(STABILIZE_E_SUFFIX), $(STABILIZE_EXAMPLES)): %$(STABILIZE_E_SUFFIX):
%$(TEX_E_SUFFIX): EXAMPLE=$(shell echo $* | tr a-z A-Z)
%$(TEX_E_SUFFIX): EDIR=$($(EXAMPLE)_DIR)
$(filter %$(TEX_E_SUFFIX), $(TEX_EXAMPLES)): %$(TEX_E_SUFFIX): %$(TRACE_GRAPH_SUFFIX)
EDIR="$(EDIR)" BUILD_FOLDER="$(BUILD_FOLDER)" SUMMARIZE_TEX=$(SUMMARIZE_TEX) MAKE="$(MAKE)" TERM="$(TERM)" "$(SHELL)" "$(SCRIPT_FOLDER)"tex_build.sh
EDIR="$(EDIR)" BUILD_FOLDER="$(BUILD_FOLDER)" SUMMARIZE_TEX=$(SUMMARIZE_TEX) MAKE="$(MAKE)" TERM="$(BATCHTERM)" "$(SHELL)" "$(SCRIPT_FOLDER)"tex_build.sh

#----------------------------------------------------------------------------------#
#--- Generate analysis of module, class, datatype, and class instance structure ---#
Expand Down
9 changes: 7 additions & 2 deletions code/drasil-code-base/lib/Language/Drasil/Code/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ data LABinOp = Index
data OrdBinOp = Lt | Gt | LEq | GEq
deriving Eq

-- | @Vector x Vector -> Vector@ binary operations (cross product).
data VVVBinOp = Cross
-- | @Vector x Vector -> Vector@ binary operations (cross product, vector addition, vector sub).
data VVVBinOp = Cross | VAdd | VSub
deriving Eq

-- | @Vector x Vector -> Number@ binary operations (dot product).
Expand Down Expand Up @@ -304,6 +304,11 @@ instance ExprC CodeExpr where
-- | Smart constructor to cross product two expressions.
cross = VVVBinaryOp Cross

-- | Adding vectors
vAdd = VVVBinaryOp VAdd
-- | Subtracting vectors
vSub = VVVBinaryOp VSub

-- | Smart constructor for case statements with a complete set of cases.
completeCase = Case Complete

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ ordBinOp LD.GEq = GEq

vvvBinOp :: LD.VVVBinOp -> VVVBinOp
vvvBinOp LD.Cross = Cross
vvvBinOp LD.VAdd = VAdd
vvvBinOp LD.VSub = VSub

vvnBinOp :: LD.VVNBinOp -> VVNBinOp
vvnBinOp LD.Dot = Dot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ ordBfunc GEq = (?>=)
-- Maps a 'VVVBinOp' to it's corresponding GOOL binary function.
vecVecVecBfunc :: VVVBinOp -> (SValue r -> SValue r -> SValue r)
vecVecVecBfunc Cross = error "bfunc: Cross not implemented"
vecVecVecBfunc VAdd = error "bfunc: Vector addition not implemented"
vecVecVecBfunc VSub = error "bfunc: Vector subtraction not implemented"

-- Maps a 'VVNBinOp' to it's corresponding GOOL binary function.
vecVecNumBfunc :: VVNBinOp -> (SValue r -> SValue r -> SValue r)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ reVelInColl :: SimpleQDef
reVelInColl = mkQuantDef initRelVel reVelInCollEqn

reVelInCollEqn :: Expr
reVelInCollEqn = sy velAP $- sy velBP
reVelInCollEqn = sy velAP `vSub` sy velBP

reVelInCollDesc :: Sentence
reVelInCollDesc = foldlSent [S "In a collision, the", phraseNP (QP.velocity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,12 @@ forcej = ucs' (dccWDS "forcej" (compoundPhrase'

velAP = ucs' (dccWDS "v^AP" (compoundPhrase' (QP.velocity ^. term)
(cn "of the point of collision P in body A"))
(phrase QP.velocity)) (sup (eqSymb QP.velocity)(Concat [lBodyA, lPoint])) Real velU
(phrase QP.velocity)) (sup (eqSymb QP.velocity)(Concat [lBodyA, lPoint]))
(Vect Real) velU
velBP = ucs' (dccWDS "v^BP" (compoundPhrase' (QP.velocity ^. term)
(cn "of the point of collision P in body B"))
(phrase QP.velocity)) (sup (eqSymb QP.velocity)(Concat [lBodyB, lPoint])) Real velU
(phrase QP.velocity)) (sup (eqSymb QP.velocity)(Concat [lBodyB, lPoint]))
(Vect Real) velU

force_1 = forceParam "1" "first" label1
force_2 = forceParam "2" "second" label2
Expand Down
15 changes: 15 additions & 0 deletions code/drasil-lang/lib/Language/Drasil/Expr/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ class ExprC r where
-- | Smart constructor for vector scaling
vScale :: r -> r -> r

-- | Vector Addition
vAdd :: r -> r -> r

-- | Vector Subtraction
vSub :: r -> r -> r

-- | Smart constructor for case statements with a complete set of cases.
completeCase :: [(r, r)] -> r

Expand Down Expand Up @@ -365,6 +371,10 @@ instance ExprC Expr where

-- | Smart constructor to cross product two expressions.
cross = VVVBinaryOp Cross
-- | Adding vectors
vAdd = VVVBinaryOp VAdd
-- | Subtracting vectors
vSub = VVVBinaryOp VSub

-- | Smart constructor for case statements with a complete set of cases.
completeCase = Case Complete
Expand Down Expand Up @@ -535,6 +545,11 @@ instance ExprC M.ModelExpr where
-- | Smart constructor to cross product two expressions.
cross = M.VVVBinaryOp M.Cross

-- | Adding vectors
vAdd = M.VVVBinaryOp M.VAdd
-- | Subtracting vectors
vSub = M.VVVBinaryOp M.VSub

-- | Smart constructor for case statements with a complete set of cases.
completeCase = M.Case Complete

Expand Down
31 changes: 28 additions & 3 deletions code/drasil-lang/lib/Language/Drasil/Expr/Lang.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ data LABinOp = Index
data OrdBinOp = Lt | Gt | LEq | GEq
deriving Eq

-- | @Vector x Vector -> Vector@ binary operations (cross product).
data VVVBinOp = Cross
-- | @Vector x Vector -> Vector@ binary operations (cross product, addition, subtraction).
data VVVBinOp = Cross | VAdd | VSub
deriving Eq

-- | @Vector x Vector -> Number@ binary operations (dot product).
Expand Down Expand Up @@ -193,6 +193,16 @@ instance Eq Expr where
-- fromRational r = ArithBinaryOp Frac (fromInteger $ numerator r)
-- (fromInteger $ denominator r)

-- Helper class for pretty-printing errors (should move from here)
-- We don't want to (ab)use Show for this
class Pretty p where
pretty :: p -> String

instance Pretty VVVBinOp where
pretty Cross = "cross product"
pretty VAdd = "vector addition"
pretty VSub = "vector subtraction"

instance LiteralC Expr where
int = Lit . int
str = Lit . str
Expand All @@ -206,6 +216,19 @@ assocArithOperToTy MulI = S.Integer
assocArithOperToTy AddRe = S.Real
assocArithOperToTy MulRe = S.Real

-- helper function for typechecking to help reduce duplication
vvvInfer :: TypingContext Space -> VVVBinOp -> Expr -> Expr -> Either Space TypeError
vvvInfer ctx op l r = case (infer ctx l, infer ctx r) of
(Left lt@(S.Vect lsp), Left (S.Vect rsp)) ->
if lsp == rsp && S.isBasicNumSpace lsp then
if op == VSub && (lsp == S.Natural || rsp == S.Natural) then
Right $ "Vector subtraction expects both operands to be vectors of non-natural numbers. Received `" ++ show lsp ++ "` and `" ++ show rsp ++ "`."
else Left lt
else Right $ "Vector " ++ pretty op ++ " expects both operands to be vectors of non-natural numbers. Received `" ++ show lsp ++ "` and `" ++ show rsp ++ "`."
(Left lsp, Left rsp) -> Right $ "Vector operation " ++ pretty op ++ " expects vector operands. Received `" ++ show lsp ++ "` and `" ++ show rsp ++ "`."
(_ , Right re) -> Right re
(Right le, _ ) -> Right le

instance Typed Expr Space where
check :: TypingContext Space -> Expr -> Space -> Either Space TypeError
check = typeCheckByInfer
Expand Down Expand Up @@ -340,12 +363,14 @@ instance Typed Expr Space where
(_, Right re) -> Right re
(Right le, _) -> Right le

infer cxt (VVVBinaryOp Cross l r) = case (infer cxt l, infer cxt r) of
infer cxt (VVVBinaryOp o l r) = vvvInfer cxt o l r
{- case (infer cxt l, infer cxt r) of
(Left lTy, Left rTy) -> if lTy == rTy && S.isBasicNumSpace lTy && lTy /= S.Natural
then Left lTy
else Right $ "Vector cross product expects both operands to be vectors of non-natural numbers. Received `" ++ show lTy ++ "` X `" ++ show rTy ++ "`."
(_ , Right re) -> Right re
(Right le, _ ) -> Right le
-}

infer cxt (VVNBinaryOp Dot l r) = case (infer cxt l, infer cxt r) of
(Left lt@(S.Vect lsp), Left rt@(S.Vect rsp)) -> if lsp == rsp && S.isBasicNumSpace lsp
Expand Down
2 changes: 2 additions & 0 deletions code/drasil-lang/lib/Language/Drasil/ModelExpr/Convert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ ordBinOp E.GEq = GEq

vvvBinOp :: E.VVVBinOp -> VVVBinOp
vvvBinOp E.Cross = Cross
vvvBinOp E.VAdd = VAdd
vvvBinOp E.VSub = VSub

vvnBinOp :: E.VVNBinOp -> VVNBinOp
vvnBinOp E.Dot = Dot
Expand Down
4 changes: 2 additions & 2 deletions code/drasil-lang/lib/Language/Drasil/ModelExpr/Lang.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ data LABinOp = Index
data OrdBinOp = Lt | Gt | LEq | GEq
deriving Eq

-- | @Vector x Vector -> Vector@ binary operations (cross product).
data VVVBinOp = Cross
-- | @Vector x Vector -> Vector@ binary operations (cross product, vector addition, subtraction).
data VVVBinOp = Cross | VAdd | VSub
deriving Eq

-- | @Vector x Vector -> Number@ binary operations (dot product).
Expand Down
2 changes: 2 additions & 0 deletions code/drasil-printers/lib/Language/Drasil/HTML/Print.hs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ pOps Dim = "dim"
pOps Exp = "e"
pOps Neg = "−"
pOps Cross = "⨯"
pOps VAdd = "+"
pOps VSub = "−"
pOps Dot = "⋅"
pOps Scale = " " -- same as Mul
pOps Eq = " = " -- with spaces?
Expand Down
2 changes: 2 additions & 0 deletions code/drasil-printers/lib/Language/Drasil/JSON/Print.hs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ pOps Dim = "dim"
pOps Exp = "e"
pOps Neg = "-"
pOps Cross = "⨯"
pOps VAdd = " + "
pOps VSub = " - "
pOps Dot = "⋅"
pOps Scale = "" -- same as Mul
pOps Eq = " = " -- with spaces?
Expand Down
2 changes: 2 additions & 0 deletions code/drasil-printers/lib/Language/Drasil/Plain/Print.hs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ opsDoc Dim = text "dim"
opsDoc Exp = text "exp"
opsDoc Neg = text "-"
opsDoc Cross = text " cross "
opsDoc VAdd = text " + "
opsDoc VSub = text " - "
opsDoc Dot = text " dot "
opsDoc Scale = text " * "
opsDoc Eq = text " == "
Expand Down
1 change: 1 addition & 0 deletions code/drasil-printers/lib/Language/Drasil/Printing/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ data Ops = IsIn | Integer | Real | Rational | Natural | Boolean | Comma | Prime
| Ln | Sin | Cos | Tan | Sec | Csc | Cot | Arcsin | Arccos | Arctan | Not
| Dim | Exp | Neg | Cross | Dot | Scale | Eq | NEq | Lt | Gt | LEq | GEq | Impl | Iff
| Subt | And | Or | Add | Mul | Summ | Inte | Prod | Point | Perc | LArrow | RArrow | ForAll
| VAdd | VSub

-- | Holds the type of "text fencing" ("(), {}, |, ||").
data Fence = Paren | Curly | Norm | Abs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ codeExpr (OrdBinaryOp LEq a b) sm = mkBOp sm P.LEq a b
codeExpr (OrdBinaryOp GEq a b) sm = mkBOp sm P.GEq a b
codeExpr (VVNBinaryOp Dot a b) sm = mkBOp sm P.Dot a b
codeExpr (VVVBinaryOp Cross a b) sm = mkBOp sm P.Cross a b
codeExpr (VVVBinaryOp VAdd a b) sm = mkBOp sm P.VAdd a b
codeExpr (VVVBinaryOp VSub a b) sm = mkBOp sm P.VSub a b
codeExpr (NVVBinaryOp Scale a b) sm = mkBOp sm P.Scale a b
codeExpr (Operator o d e) sm = eop sm o d e
codeExpr (RealI c ri) sm = renderRealInt sm (lookupC (sm ^. stg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ expr (OrdBinaryOp Gt a b) sm = mkBOp sm P.Gt a b
expr (OrdBinaryOp LEq a b) sm = mkBOp sm P.LEq a b
expr (OrdBinaryOp GEq a b) sm = mkBOp sm P.GEq a b
expr (VVVBinaryOp Cross a b) sm = mkBOp sm P.Cross a b
expr (VVVBinaryOp VAdd a b) sm = mkBOp sm P.VAdd a b
expr (VVVBinaryOp VSub a b) sm = mkBOp sm P.VSub a b
expr (VVNBinaryOp Dot a b) sm = mkBOp sm P.Dot a b
expr (NVVBinaryOp Scale a b) sm = mkBOp sm P.Scale a b
expr (Operator o d e) sm = eop sm o d e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ modelExpr (OrdBinaryOp LEq a b) sm = mkBOp sm P.LEq a b
modelExpr (OrdBinaryOp GEq a b) sm = mkBOp sm P.GEq a b
modelExpr (VVNBinaryOp Dot a b) sm = mkBOp sm P.Dot a b
modelExpr (VVVBinaryOp Cross a b) sm = mkBOp sm P.Cross a b
modelExpr (VVVBinaryOp VAdd a b) sm = mkBOp sm P.VAdd a b
modelExpr (VVVBinaryOp VSub a b) sm = mkBOp sm P.VSub a b
modelExpr (NVVBinaryOp Scale a b) sm = mkBOp sm P.Scale a b
modelExpr (Operator o d e) sm = eop sm o d e
modelExpr (RealI c ri) sm = renderRealInt sm (lookupC (sm ^. stg)
Expand Down
2 changes: 2 additions & 0 deletions code/drasil-printers/lib/Language/Drasil/TeX/Print.hs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ pOps Dim = command "mathsf" "dim"
pOps Exp = pure $ text "e"
pOps Neg = pure hyph
pOps Cross = texSym "times"
pOps VAdd = pure pls
pOps VSub = pure hyph -- unfortunately, hyphen and - are the same
pOps Dot = commandD "cdot" empty
pOps Scale = pure $ text " "
pOps Eq = pure assign
Expand Down

0 comments on commit f7eaea7

Please sign in to comment.