From 7d6bd327f02ab2e3ba5741766b13dc9d23f92b8a Mon Sep 17 00:00:00 2001 From: JosephBond Date: Mon, 2 Oct 2023 11:00:59 +0100 Subject: [PATCH 01/26] refactored input arrays to be input lists --- example/Example.purs | 5 +++-- example/Util/DTW.purs | 13 +++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/example/Example.purs b/example/Example.purs index a8983dc4f..8e1aa80dc 100644 --- a/example/Example.purs +++ b/example/Example.purs @@ -3,6 +3,7 @@ module Example.Example where import Prelude import Data.Array ((..)) +import Data.List ((:), List(..)) import Effect (Effect) import Effect.Class.Console (log, logShow) import Example.Util.DTW (distEuclid, distanceDTWWindow) @@ -22,8 +23,8 @@ main = do [ (i × j) ] logShow nextIndices let - x = [ 3.0, 1.0, 2.0, 2.0, 1.0 ] - y = [ 2.0, 0.0, 0.0, 3.0, 3.0, 1.0, 0.0 ] + x = (3.0: 1.0: 2.0: 2.0: 1.0 : Nil) + y = (2.0: 0.0: 0.0: 3.0: 3.0: 1.0: 0.0: Nil) m1 × m2 = unsafePartial $ distanceDTWWindow x y 2 distEuclid log "Finished DTW" logShow m2 diff --git a/example/Util/DTW.purs b/example/Util/DTW.purs index ef21f98da..b67c25638 100644 --- a/example/Util/DTW.purs +++ b/example/Util/DTW.purs @@ -6,9 +6,9 @@ module Example.Util.DTW import Prelude -import Data.Array (length, modifyAtIndices, range, (!!), (..)) +import Data.Array (modifyAtIndices, range, (!!), (..)) import Data.Foldable (foldl) -import Data.List (List(..)) +import Data.List (List(..), index, length) import Data.Maybe (fromJust) import Data.Ord (abs) import Partial.Unsafe (unsafePartial) @@ -29,7 +29,7 @@ costMatrixInit rows cols window = mapMatrix init indexMat init (_ × 0) = Infty init (x × y) = if (abs $ x - y) <= window then FNum 0.0 else Infty -distanceDTWWindow :: Partial => Array Number -> Array Number -> Int -> (Number -> Number -> NumInf) -> Matrix NumInf × List (Int × Int) +distanceDTWWindow :: Partial => List Number -> List Number -> Int -> (Number -> Number -> NumInf) -> Matrix NumInf × List (Int × Int) distanceDTWWindow seq1 seq2 window cost = result × (extractPath priorcells (n × m)) where n = length seq1 @@ -48,7 +48,7 @@ distanceDTWWindow seq1 seq2 window cost = result × (extractPath priorcells (n ijm1 = dists ! i' ! j' - 1 im1jm1 = dists ! i' - 1 ! j' - 1 minim × prev = costAndPrevD (i' × j') im1j ijm1 im1jm1 - costij = cost (seq1 ! i' - 1) (seq2 ! j' - 1) `plus` minim + costij = cost (seq1 ? i' - 1) (seq2 ? j' - 1) `plus` minim in updateAt i' j' dists (const costij) × updateAt i' j' inds (const prev) @@ -92,6 +92,11 @@ unsafeArrayInd arr ind = unsafePartial $ fromJust (arr !! ind) infixl 5 unsafeArrayInd as ! +unsafeListInd :: forall a. List a -> Int -> a +unsafeListInd list ind = unsafePartial $ fromJust (index list ind) + +infixl 5 unsafeListInd as ? + mapMatrix :: forall a b. (a -> b) -> Matrix a -> Matrix b mapMatrix f m = map (\row -> map f row) m From 679a8ac2f4176b043d300fb8d98590b7e1777610 Mon Sep 17 00:00:00 2001 From: JosephBond Date: Mon, 2 Oct 2023 11:01:11 +0100 Subject: [PATCH 02/26] Tidied up --- example/Example.purs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/Example.purs b/example/Example.purs index 8e1aa80dc..4ad9ae570 100644 --- a/example/Example.purs +++ b/example/Example.purs @@ -23,8 +23,8 @@ main = do [ (i × j) ] logShow nextIndices let - x = (3.0: 1.0: 2.0: 2.0: 1.0 : Nil) - y = (2.0: 0.0: 0.0: 3.0: 3.0: 1.0: 0.0: Nil) + x = (3.0 : 1.0 : 2.0 : 2.0 : 1.0 : Nil) + y = (2.0 : 0.0 : 0.0 : 3.0 : 3.0 : 1.0 : 0.0 : Nil) m1 × m2 = unsafePartial $ distanceDTWWindow x y 2 distEuclid log "Finished DTW" logShow m2 From d0ccd8719f6a061d9d508713b95e78f884501c01 Mon Sep 17 00:00:00 2001 From: JosephBond Date: Mon, 2 Oct 2023 11:37:26 +0100 Subject: [PATCH 03/26] nextIndices implemented in Fluid --- example/Example.purs | 9 +++------ example/Util/DTW.purs | 10 ++++++---- fluid/example/dtw/next-indices.fld | 5 +++++ test/Spec/Specs.purs | 1 + 4 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 fluid/example/dtw/next-indices.fld diff --git a/example/Example.purs b/example/Example.purs index 4ad9ae570..cb55051d3 100644 --- a/example/Example.purs +++ b/example/Example.purs @@ -6,7 +6,7 @@ import Data.Array ((..)) import Data.List ((:), List(..)) import Effect (Effect) import Effect.Class.Console (log, logShow) -import Example.Util.DTW (distEuclid, distanceDTWWindow) +import Example.Util.DTW (costMatrixInit, distEuclid, distanceDTWWindow) import Partial.Unsafe (unsafePartial) import Util ((×)) @@ -17,11 +17,8 @@ main = do n = 5 m = 7 window = 2 - nextIndices = do - i <- 1 .. n - j <- (max 1 (i - window)) .. (min m (i + window)) - [ (i × j) ] - logShow nextIndices + initMat = costMatrixInit 5 7 2 + logShow initMat let x = (3.0 : 1.0 : 2.0 : 2.0 : 1.0 : Nil) y = (2.0 : 0.0 : 0.0 : 3.0 : 3.0 : 1.0 : 0.0 : Nil) diff --git a/example/Util/DTW.purs b/example/Util/DTW.purs index b67c25638..082b09346 100644 --- a/example/Util/DTW.purs +++ b/example/Util/DTW.purs @@ -1,8 +1,10 @@ module Example.Util.DTW - ( NumInf(..) - , distEuclid - , distanceDTWWindow - ) where + ( NumInf(..) + , costMatrixInit + , distEuclid + , distanceDTWWindow + ) + where import Prelude diff --git a/fluid/example/dtw/next-indices.fld b/fluid/example/dtw/next-indices.fld new file mode 100644 index 000000000..9c62d1af8 --- /dev/null +++ b/fluid/example/dtw/next-indices.fld @@ -0,0 +1,5 @@ +let nextIndices n m window = + [(i, j) | i <- enumFromTo 1 n, + j <- enumFromTo (max 1 (i - window)) (min m (i + window))] +in + nextIndices 5 7 2 diff --git a/test/Spec/Specs.purs b/test/Spec/Specs.purs index 9493973d4..ca2509102 100644 --- a/test/Spec/Specs.purs +++ b/test/Spec/Specs.purs @@ -40,6 +40,7 @@ misc_cases = , { file: "range", fwd_expect: "((0, 0) : ((0, 1) : ((1, 0) : ((1, 1) : []))))" } , { file: "records", fwd_expect: "{a : 2, b : 6, c : 7, d : (5 : []), e : 7}" } , { file: "reverse", fwd_expect: "(2 : (1 : []))" } + , { file: "dtw/next-indices", fwd_expect: "((1, 1) : ((1, 2) : ((1, 3) : ((2, 1) : ((2, 2) : ((2, 3) : ((2, 4) : ((3, 1) : ((3, 2) : ((3, 3) : ((3, 4) : ((3, 5) : ((4, 2) : ((4, 3) : ((4, 4) : ((4, 5) : ((4, 6) : ((5, 3) : ((5, 4) : ((5, 5) : ((5, 6) : ((5, 7) : []))))))))))))))))))))))"} ] desugar_cases :: Array TestSpec From 82b5516020d9afbeb6e36c492f4898a0549b2a2a Mon Sep 17 00:00:00 2001 From: JosephBond Date: Mon, 2 Oct 2023 14:37:53 +0100 Subject: [PATCH 04/26] let binding structure in cost-matrix.fld --- example/Example.purs | 4 ++-- fluid/example/dtw/cost-matrix.fld | 8 ++++++++ src/DataType.purs | 4 ++++ test/Main.purs | 32 ++++++++++++------------------- test/Spec/Specs.purs | 1 + 5 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 fluid/example/dtw/cost-matrix.fld diff --git a/example/Example.purs b/example/Example.purs index cb55051d3..78425e2f1 100644 --- a/example/Example.purs +++ b/example/Example.purs @@ -2,7 +2,7 @@ module Example.Example where import Prelude -import Data.Array ((..)) +-- import Data.Array ((..)) import Data.List ((:), List(..)) import Effect (Effect) import Effect.Class.Console (log, logShow) @@ -17,7 +17,7 @@ main = do n = 5 m = 7 window = 2 - initMat = costMatrixInit 5 7 2 + initMat = costMatrixInit n m window logShow initMat let x = (3.0 : 1.0 : 2.0 : 2.0 : 1.0 : Nil) diff --git a/fluid/example/dtw/cost-matrix.fld b/fluid/example/dtw/cost-matrix.fld new file mode 100644 index 000000000..18bc5d441 --- /dev/null +++ b/fluid/example/dtw/cost-matrix.fld @@ -0,0 +1,8 @@ +let abs x y = if x - y < 0 then negate (x - y) else (x - y); + costMatrixInit rows cols window = + let ns = [0 .. rows]; + ms = [0 .. cols] + in + [| (n,m) | (n, m) in (ns, ms) |] +in costMatrixInit 3 4 1 + diff --git a/src/DataType.purs b/src/DataType.purs index 863b562cc..f101a9069 100644 --- a/src/DataType.purs +++ b/src/DataType.purs @@ -168,4 +168,8 @@ dataTypes = L.fromFoldable , dataType "Marker" [ "Arrowhead" × 0 ] + , dataType "InfNum" + [ "FNum" × 1 + , "Infty" × 0 + ] ] diff --git a/test/Main.purs b/test/Main.purs index bc30a4a5b..a2d0872f3 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -1,16 +1,8 @@ -module Test.Main - ( main - , test_bwd - , test_desugaring - , test_graphics - -- , test_linking - , test_misc - -- , test_scratchpad - ) where +module Test.Main where import Prelude hiding (add) -import Data.Array (concat) +-- import Data.Array (concat) import Data.Profunctor.Strong (second) import Effect (Effect) import Effect.Aff (Aff) @@ -23,16 +15,16 @@ main :: Effect Unit main = run tests tests :: Array (String × Aff Unit) -tests = concat - [ test_desugaring - , test_misc - , test_bwd - , test_graphics - , test_linking - ] - --- test_scratchpad :: Array (String × Aff Unit) --- test_scratchpad = [] +-- tests = concat +-- [ test_desugaring +-- , test_misc +-- , test_bwd +-- , test_graphics +-- , test_linking +-- ] +tests = test_scratchpad +test_scratchpad :: Array (String × Aff Unit) +test_scratchpad = second void <$> many [ { file: "dtw/cost-matrix", fwd_expect: "forced fail" }] 1 test_desugaring :: Array (String × Aff Unit) test_desugaring = second void <$> many desugar_cases 1 diff --git a/test/Spec/Specs.purs b/test/Spec/Specs.purs index ca2509102..f71a78558 100644 --- a/test/Spec/Specs.purs +++ b/test/Spec/Specs.purs @@ -41,6 +41,7 @@ misc_cases = , { file: "records", fwd_expect: "{a : 2, b : 6, c : 7, d : (5 : []), e : 7}" } , { file: "reverse", fwd_expect: "(2 : (1 : []))" } , { file: "dtw/next-indices", fwd_expect: "((1, 1) : ((1, 2) : ((1, 3) : ((2, 1) : ((2, 2) : ((2, 3) : ((2, 4) : ((3, 1) : ((3, 2) : ((3, 3) : ((3, 4) : ((3, 5) : ((4, 2) : ((4, 3) : ((4, 4) : ((4, 5) : ((4, 6) : ((5, 3) : ((5, 4) : ((5, 5) : ((5, 6) : ((5, 7) : []))))))))))))))))))))))"} + , { file: "dtw/cost-matrix", fwd_expect: "forced fail" } ] desugar_cases :: Array TestSpec From b367935b93a945b3dfe9e8d040c23f325de0e890 Mon Sep 17 00:00:00 2001 From: JosephBond Date: Mon, 2 Oct 2023 14:50:16 +0100 Subject: [PATCH 05/26] costMatrixInit in fluid in working yet flawed state. Fails to pass testParse? --- example/Util/DTW.purs | 11 +++++------ fluid/example/dtw/cost-matrix.fld | 3 ++- test/Main.purs | 3 ++- test/Spec/Specs.purs | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/example/Util/DTW.purs b/example/Util/DTW.purs index 082b09346..6524ddbfe 100644 --- a/example/Util/DTW.purs +++ b/example/Util/DTW.purs @@ -1,10 +1,9 @@ module Example.Util.DTW - ( NumInf(..) - , costMatrixInit - , distEuclid - , distanceDTWWindow - ) - where + ( NumInf(..) + , costMatrixInit + , distEuclid + , distanceDTWWindow + ) where import Prelude diff --git a/fluid/example/dtw/cost-matrix.fld b/fluid/example/dtw/cost-matrix.fld index 18bc5d441..703eea13f 100644 --- a/fluid/example/dtw/cost-matrix.fld +++ b/fluid/example/dtw/cost-matrix.fld @@ -1,8 +1,9 @@ let abs x y = if x - y < 0 then negate (x - y) else (x - y); + costMatrixInit rows cols window = let ns = [0 .. rows]; ms = [0 .. cols] in - [| (n,m) | (n, m) in (ns, ms) |] + [| let initV = if ((abs n m )<= window) then FNum (abs n m) else Infty in initV | (n, m) in (rows, cols) |] in costMatrixInit 3 4 1 diff --git a/test/Main.purs b/test/Main.purs index a2d0872f3..6dc5742e0 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -23,8 +23,9 @@ tests :: Array (String × Aff Unit) -- , test_linking -- ] tests = test_scratchpad + test_scratchpad :: Array (String × Aff Unit) -test_scratchpad = second void <$> many [ { file: "dtw/cost-matrix", fwd_expect: "forced fail" }] 1 +test_scratchpad = second void <$> many [ { file: "dtw/cost-matrix", fwd_expect: "forced fail" } ] 1 test_desugaring :: Array (String × Aff Unit) test_desugaring = second void <$> many desugar_cases 1 diff --git a/test/Spec/Specs.purs b/test/Spec/Specs.purs index f71a78558..7b74c5857 100644 --- a/test/Spec/Specs.purs +++ b/test/Spec/Specs.purs @@ -40,7 +40,7 @@ misc_cases = , { file: "range", fwd_expect: "((0, 0) : ((0, 1) : ((1, 0) : ((1, 1) : []))))" } , { file: "records", fwd_expect: "{a : 2, b : 6, c : 7, d : (5 : []), e : 7}" } , { file: "reverse", fwd_expect: "(2 : (1 : []))" } - , { file: "dtw/next-indices", fwd_expect: "((1, 1) : ((1, 2) : ((1, 3) : ((2, 1) : ((2, 2) : ((2, 3) : ((2, 4) : ((3, 1) : ((3, 2) : ((3, 3) : ((3, 4) : ((3, 5) : ((4, 2) : ((4, 3) : ((4, 4) : ((4, 5) : ((4, 6) : ((5, 3) : ((5, 4) : ((5, 5) : ((5, 6) : ((5, 7) : []))))))))))))))))))))))"} + , { file: "dtw/next-indices", fwd_expect: "((1, 1) : ((1, 2) : ((1, 3) : ((2, 1) : ((2, 2) : ((2, 3) : ((2, 4) : ((3, 1) : ((3, 2) : ((3, 3) : ((3, 4) : ((3, 5) : ((4, 2) : ((4, 3) : ((4, 4) : ((4, 5) : ((4, 6) : ((5, 3) : ((5, 4) : ((5, 5) : ((5, 6) : ((5, 7) : []))))))))))))))))))))))" } , { file: "dtw/cost-matrix", fwd_expect: "forced fail" } ] From abed919fa57f3e96f9dcbcbcdcad428549590c3b Mon Sep 17 00:00:00 2001 From: JosephBond Date: Mon, 2 Oct 2023 15:16:22 +0100 Subject: [PATCH 06/26] Cost-matrix almost right, but not quite, fails due to forced test failure --- fluid/example/dtw/cost-matrix.fld | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fluid/example/dtw/cost-matrix.fld b/fluid/example/dtw/cost-matrix.fld index 703eea13f..d1c0d6303 100644 --- a/fluid/example/dtw/cost-matrix.fld +++ b/fluid/example/dtw/cost-matrix.fld @@ -1,9 +1,9 @@ let abs x y = if x - y < 0 then negate (x - y) else (x - y); - costMatrixInit rows cols window = let ns = [0 .. rows]; ms = [0 .. cols] in - [| let initV = if ((abs n m )<= window) then FNum (abs n m) else Infty in initV | (n, m) in (rows, cols) |] + [| let initV = if ((abs n m )<= window) then FNum 0 else Infty + in initV | (n, m) in (rows, cols) |] in costMatrixInit 3 4 1 From 015b2623aebe7b809e1ed5ab28440d4732ff3e8e Mon Sep 17 00:00:00 2001 From: Joe Bond Date: Mon, 2 Oct 2023 16:12:50 +0100 Subject: [PATCH 07/26] Removing forced failing tests --- fluid/example/dtw/cost-matrix.fld | 2 +- test/Spec/Specs.purs | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/fluid/example/dtw/cost-matrix.fld b/fluid/example/dtw/cost-matrix.fld index d1c0d6303..c45a633ea 100644 --- a/fluid/example/dtw/cost-matrix.fld +++ b/fluid/example/dtw/cost-matrix.fld @@ -5,5 +5,5 @@ let abs x y = if x - y < 0 then negate (x - y) else (x - y); in [| let initV = if ((abs n m )<= window) then FNum 0 else Infty in initV | (n, m) in (rows, cols) |] -in costMatrixInit 3 4 1 +in costMatrixInit 5 7 1 diff --git a/test/Spec/Specs.purs b/test/Spec/Specs.purs index 7b74c5857..ac7529bff 100644 --- a/test/Spec/Specs.purs +++ b/test/Spec/Specs.purs @@ -41,7 +41,14 @@ misc_cases = , { file: "records", fwd_expect: "{a : 2, b : 6, c : 7, d : (5 : []), e : 7}" } , { file: "reverse", fwd_expect: "(2 : (1 : []))" } , { file: "dtw/next-indices", fwd_expect: "((1, 1) : ((1, 2) : ((1, 3) : ((2, 1) : ((2, 2) : ((2, 3) : ((2, 4) : ((3, 1) : ((3, 2) : ((3, 3) : ((3, 4) : ((3, 5) : ((4, 2) : ((4, 3) : ((4, 4) : ((4, 5) : ((4, 6) : ((5, 3) : ((5, 4) : ((5, 5) : ((5, 6) : ((5, 7) : []))))))))))))))))))))))" } - , { file: "dtw/cost-matrix", fwd_expect: "forced fail" } + , { file: "dtw/cost-matrix" + , fwd_expect: + "FNum 0, FNum 0, Infty, Infty, Infty, Infty, Infty,\n\ + \FNum 0, FNum 0, FNum 0, Infty, Infty, Infty, Infty,\n\ + \Infty, FNum 0, FNum 0, FNum 0, Infty, Infty, Infty,\n\ + \Infty, Infty, FNum 0, FNum 0, FNum 0, Infty, Infty,\n\ + \Infty, Infty, Infty, FNum 0, FNum 0, FNum 0, Infty" + } ] desugar_cases :: Array TestSpec From 78190e243a690b7a280c807a6ecfa3bb90e4337c Mon Sep 17 00:00:00 2001 From: Joe Bond Date: Mon, 2 Oct 2023 16:15:41 +0100 Subject: [PATCH 08/26] Actually fixed the borked output --- test/Main.purs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/Main.purs b/test/Main.purs index 6dc5742e0..c7f53ead0 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -25,7 +25,14 @@ tests :: Array (String × Aff Unit) tests = test_scratchpad test_scratchpad :: Array (String × Aff Unit) -test_scratchpad = second void <$> many [ { file: "dtw/cost-matrix", fwd_expect: "forced fail" } ] 1 +test_scratchpad = second void <$> many [ { file: "dtw/cost-matrix" + , fwd_expect: + "FNum 0, FNum 0, Infty, Infty, Infty, Infty, Infty,\n\ + \FNum 0, FNum 0, FNum 0, Infty, Infty, Infty, Infty,\n\ + \Infty, FNum 0, FNum 0, FNum 0, Infty, Infty, Infty,\n\ + \Infty, Infty, FNum 0, FNum 0, FNum 0, Infty, Infty,\n\ + \Infty, Infty, Infty, FNum 0, FNum 0, FNum 0, Infty" + } ] 1 test_desugaring :: Array (String × Aff Unit) test_desugaring = second void <$> many desugar_cases 1 From d21b87d6e956124af6f2510a368dc2cd806cec7e Mon Sep 17 00:00:00 2001 From: Joe Bond Date: Mon, 2 Oct 2023 16:26:23 +0100 Subject: [PATCH 09/26] minimum and previous direction example fixed --- fluid/example/dtw/min-prev.fld | 10 ++++++++++ test/Main.purs | 8 ++------ 2 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 fluid/example/dtw/min-prev.fld diff --git a/fluid/example/dtw/min-prev.fld b/fluid/example/dtw/min-prev.fld new file mode 100644 index 000000000..a313355e5 --- /dev/null +++ b/fluid/example/dtw/min-prev.fld @@ -0,0 +1,10 @@ +let minAndPrev (i, j) im1 jm1 ijm1 = + let minimal = minimum (im1:(jm1:(ijm1:[]))) + in + if minimal == im1 + then (((i - 1), j), minimal) + else if minimal == jm1 + then ((i, (j - 1)), minimal) + else + (((i - 1), (j - 1)), ijm1) +in minAndPrev (4,5) 3 4 5 \ No newline at end of file diff --git a/test/Main.purs b/test/Main.purs index c7f53ead0..096d61ff1 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -25,13 +25,9 @@ tests :: Array (String × Aff Unit) tests = test_scratchpad test_scratchpad :: Array (String × Aff Unit) -test_scratchpad = second void <$> many [ { file: "dtw/cost-matrix" +test_scratchpad = second void <$> many [ { file: "dtw/min-prev" , fwd_expect: - "FNum 0, FNum 0, Infty, Infty, Infty, Infty, Infty,\n\ - \FNum 0, FNum 0, FNum 0, Infty, Infty, Infty, Infty,\n\ - \Infty, FNum 0, FNum 0, FNum 0, Infty, Infty, Infty,\n\ - \Infty, Infty, FNum 0, FNum 0, FNum 0, Infty, Infty,\n\ - \Infty, Infty, Infty, FNum 0, FNum 0, FNum 0, Infty" + "((3, 5), 3)" } ] 1 test_desugaring :: Array (String × Aff Unit) From 2ac1b7f82c825af77fffee2208a5af2087ceef3f Mon Sep 17 00:00:00 2001 From: Joe Bond Date: Mon, 2 Oct 2023 16:29:44 +0100 Subject: [PATCH 10/26] Tidied previous commit --- test/Main.purs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/Main.purs b/test/Main.purs index 096d61ff1..f8052f125 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -25,10 +25,13 @@ tests :: Array (String × Aff Unit) tests = test_scratchpad test_scratchpad :: Array (String × Aff Unit) -test_scratchpad = second void <$> many [ { file: "dtw/min-prev" +test_scratchpad = second void <$> many + [ { file: "dtw/min-prev" , fwd_expect: "((3, 5), 3)" - } ] 1 + } + ] + 1 test_desugaring :: Array (String × Aff Unit) test_desugaring = second void <$> many desugar_cases 1 From a0e17629f9cc0e562401616321e5cb61ec045c43 Mon Sep 17 00:00:00 2001 From: Joe Bond Date: Tue, 3 Oct 2023 11:10:33 +0100 Subject: [PATCH 11/26] Added more components of dtw example, need some bug fixes --- fluid/example/dtw/cost-matrix.fld | 4 +++- fluid/example/dtw/cost-predicate.fld | 4 ++++ fluid/example/dtw/min-prev.fld | 2 +- fluid/example/dtw/next-indices.fld | 4 ++-- test/Main.purs | 2 +- 5 files changed, 11 insertions(+), 5 deletions(-) create mode 100644 fluid/example/dtw/cost-predicate.fld diff --git a/fluid/example/dtw/cost-matrix.fld b/fluid/example/dtw/cost-matrix.fld index c45a633ea..219d0235a 100644 --- a/fluid/example/dtw/cost-matrix.fld +++ b/fluid/example/dtw/cost-matrix.fld @@ -3,7 +3,9 @@ let abs x y = if x - y < 0 then negate (x - y) else (x - y); let ns = [0 .. rows]; ms = [0 .. cols] in - [| let initV = if ((abs n m )<= window) then FNum 0 else Infty + [| let initV = if (n == 0 `and` m == 0) `or` ((abs (n - m) <= window) `and` (not (n == 0 `or` m == 0))) + then FNum 0 + else Infty in initV | (n, m) in (rows, cols) |] in costMatrixInit 5 7 1 diff --git a/fluid/example/dtw/cost-predicate.fld b/fluid/example/dtw/cost-predicate.fld new file mode 100644 index 000000000..fa2d8ab9e --- /dev/null +++ b/fluid/example/dtw/cost-predicate.fld @@ -0,0 +1,4 @@ +let initV n m window = if n == 0 `or` m == 0 + then FNum 0 + else Infty +in initV 4 6 2 \ No newline at end of file diff --git a/fluid/example/dtw/min-prev.fld b/fluid/example/dtw/min-prev.fld index a313355e5..5e71f89f4 100644 --- a/fluid/example/dtw/min-prev.fld +++ b/fluid/example/dtw/min-prev.fld @@ -1,5 +1,5 @@ let minAndPrev (i, j) im1 jm1 ijm1 = - let minimal = minimum (im1:(jm1:(ijm1:[]))) + let minimal = minimum [im1,jm1,ijm1] in if minimal == im1 then (((i - 1), j), minimal) diff --git a/fluid/example/dtw/next-indices.fld b/fluid/example/dtw/next-indices.fld index 9c62d1af8..f7e7f85fb 100644 --- a/fluid/example/dtw/next-indices.fld +++ b/fluid/example/dtw/next-indices.fld @@ -1,5 +1,5 @@ let nextIndices n m window = - [(i, j) | i <- enumFromTo 1 n, - j <- enumFromTo (max 1 (i - window)) (min m (i + window))] + [(i, j) | i <- [1 .. n], + j <- [(max 1 (i - window)) .. (min m (i + window))]] in nextIndices 5 7 2 diff --git a/test/Main.purs b/test/Main.purs index f8052f125..705abeb12 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -28,7 +28,7 @@ test_scratchpad :: Array (String × Aff Unit) test_scratchpad = second void <$> many [ { file: "dtw/min-prev" , fwd_expect: - "((3, 5), 3)" + "fail" } ] 1 From 02b58515c73838e01b598e040560884442abfede Mon Sep 17 00:00:00 2001 From: Joe Bond Date: Tue, 3 Oct 2023 15:49:37 +0100 Subject: [PATCH 12/26] Fixing cost predicate, nearly done --- fluid/example/dtw/cost-matrix.fld | 2 +- fluid/example/dtw/cost-predicate.fld | 5 +++-- test/Main.purs | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/fluid/example/dtw/cost-matrix.fld b/fluid/example/dtw/cost-matrix.fld index 219d0235a..e99955fe4 100644 --- a/fluid/example/dtw/cost-matrix.fld +++ b/fluid/example/dtw/cost-matrix.fld @@ -3,7 +3,7 @@ let abs x y = if x - y < 0 then negate (x - y) else (x - y); let ns = [0 .. rows]; ms = [0 .. cols] in - [| let initV = if (n == 0 `and` m == 0) `or` ((abs (n - m) <= window) `and` (not (n == 0 `or` m == 0))) + [| let initV = if (and ((abs n m) <= window) (or (0 < n) (0 < m))) then FNum 0 else Infty in initV | (n, m) in (rows, cols) |] diff --git a/fluid/example/dtw/cost-predicate.fld b/fluid/example/dtw/cost-predicate.fld index fa2d8ab9e..e30181df2 100644 --- a/fluid/example/dtw/cost-predicate.fld +++ b/fluid/example/dtw/cost-predicate.fld @@ -1,4 +1,5 @@ -let initV n m window = if n == 0 `or` m == 0 +let abs x y = if x - y < 0 then negate (x - y) else (x - y); + initV n m window = if (or (and (n == 0) (m == 0))(and ((abs n m) <= window) (not (or (n == 0) (m == 0))))) then FNum 0 else Infty -in initV 4 6 2 \ No newline at end of file +in initV 0 0 2 \ No newline at end of file diff --git a/test/Main.purs b/test/Main.purs index 705abeb12..85fce825f 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -26,7 +26,7 @@ tests = test_scratchpad test_scratchpad :: Array (String × Aff Unit) test_scratchpad = second void <$> many - [ { file: "dtw/min-prev" + [ { file: "dtw/cost-predicate" , fwd_expect: "fail" } From dc5a4baa59208767cef6b199854c47b97ae209b8 Mon Sep 17 00:00:00 2001 From: Joe Bond Date: Tue, 3 Oct 2023 15:59:46 +0100 Subject: [PATCH 13/26] Pretty sure I fixed cost-matrix now --- fluid/example/dtw/cost-matrix.fld | 4 ++-- fluid/example/dtw/cost-predicate.fld | 4 ++-- test/Main.purs | 8 ++++++-- test/Spec/Specs.purs | 4 ++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/fluid/example/dtw/cost-matrix.fld b/fluid/example/dtw/cost-matrix.fld index e99955fe4..b383acd2e 100644 --- a/fluid/example/dtw/cost-matrix.fld +++ b/fluid/example/dtw/cost-matrix.fld @@ -3,9 +3,9 @@ let abs x y = if x - y < 0 then negate (x - y) else (x - y); let ns = [0 .. rows]; ms = [0 .. cols] in - [| let initV = if (and ((abs n m) <= window) (or (0 < n) (0 < m))) + [| let initV = if (or (and (n == 1) (m == 1)) (and ((abs n m) <= window) (not (or (n == 1) (m == 1))))) then FNum 0 else Infty in initV | (n, m) in (rows, cols) |] -in costMatrixInit 5 7 1 +in costMatrixInit 5 7 2 diff --git a/fluid/example/dtw/cost-predicate.fld b/fluid/example/dtw/cost-predicate.fld index e30181df2..dbbeddf3d 100644 --- a/fluid/example/dtw/cost-predicate.fld +++ b/fluid/example/dtw/cost-predicate.fld @@ -1,5 +1,5 @@ let abs x y = if x - y < 0 then negate (x - y) else (x - y); - initV n m window = if (or (and (n == 0) (m == 0))(and ((abs n m) <= window) (not (or (n == 0) (m == 0))))) + initV n m window = if (or (and (n == 0) (m == 0)) (and ((abs n m) <= window) (not (or (n == 0) (m == 0))))) then FNum 0 else Infty -in initV 0 0 2 \ No newline at end of file +in initV 1 0 2 \ No newline at end of file diff --git a/test/Main.purs b/test/Main.purs index 85fce825f..91def02a4 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -26,9 +26,13 @@ tests = test_scratchpad test_scratchpad :: Array (String × Aff Unit) test_scratchpad = second void <$> many - [ { file: "dtw/cost-predicate" + [ { file: "dtw/cost-matrix" , fwd_expect: - "fail" + "FNum 0, Infty, Infty, Infty, Infty, Infty, Infty,\n\ + \Infty, FNum 0, FNum 0, Infty, Infty, Infty, Infty,\n\ + \Infty, FNum 0, FNum 0, FNum 0, Infty, Infty, Infty,\n\ + \Infty, Infty, FNum 0, FNum 0, FNum 0, Infty, Infty,\n\ + \Infty, Infty, Infty, FNum 0, FNum 0, FNum 0, Infty" } ] 1 diff --git a/test/Spec/Specs.purs b/test/Spec/Specs.purs index ac7529bff..61f79dfce 100644 --- a/test/Spec/Specs.purs +++ b/test/Spec/Specs.purs @@ -43,8 +43,8 @@ misc_cases = , { file: "dtw/next-indices", fwd_expect: "((1, 1) : ((1, 2) : ((1, 3) : ((2, 1) : ((2, 2) : ((2, 3) : ((2, 4) : ((3, 1) : ((3, 2) : ((3, 3) : ((3, 4) : ((3, 5) : ((4, 2) : ((4, 3) : ((4, 4) : ((4, 5) : ((4, 6) : ((5, 3) : ((5, 4) : ((5, 5) : ((5, 6) : ((5, 7) : []))))))))))))))))))))))" } , { file: "dtw/cost-matrix" , fwd_expect: - "FNum 0, FNum 0, Infty, Infty, Infty, Infty, Infty,\n\ - \FNum 0, FNum 0, FNum 0, Infty, Infty, Infty, Infty,\n\ + "FNum 0, Infty, Infty, Infty, Infty, Infty, Infty,\n\ + \Infty, FNum 0, FNum 0, Infty, Infty, Infty, Infty,\n\ \Infty, FNum 0, FNum 0, FNum 0, Infty, Infty, Infty,\n\ \Infty, Infty, FNum 0, FNum 0, FNum 0, Infty, Infty,\n\ \Infty, Infty, Infty, FNum 0, FNum 0, FNum 0, Infty" From cd41424f877ae198b7683d08424e829ee936dd57 Mon Sep 17 00:00:00 2001 From: Joe Bond Date: Tue, 3 Oct 2023 16:01:47 +0100 Subject: [PATCH 14/26] Fixed cost-matrix function now :) --- fluid/example/dtw/cost-matrix.fld | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fluid/example/dtw/cost-matrix.fld b/fluid/example/dtw/cost-matrix.fld index b383acd2e..965d2781b 100644 --- a/fluid/example/dtw/cost-matrix.fld +++ b/fluid/example/dtw/cost-matrix.fld @@ -7,5 +7,5 @@ let abs x y = if x - y < 0 then negate (x - y) else (x - y); then FNum 0 else Infty in initV | (n, m) in (rows, cols) |] -in costMatrixInit 5 7 2 +in costMatrixInit 5 7 1 From 97eced835caf87c709c22386a7d503c03f743406 Mon Sep 17 00:00:00 2001 From: Joe Bond Date: Tue, 3 Oct 2023 16:26:43 +0100 Subject: [PATCH 15/26] Removed fast-vect dependency --- spago.dhall | 1 - 1 file changed, 1 deletion(-) diff --git a/spago.dhall b/spago.dhall index 5cabf820a..650afd297 100644 --- a/spago.dhall +++ b/spago.dhall @@ -16,7 +16,6 @@ You can edit this file as you like. , "either" , "exceptions" , "exists" - , "fast-vect" , "foldable-traversable" , "foreign-object" , "heterogeneous" From 025f31cd43c7c9761aeb020451b48a746023efa6 Mon Sep 17 00:00:00 2001 From: JosephBond Date: Wed, 4 Oct 2023 11:16:46 +0100 Subject: [PATCH 16/26] Inlined arraydata --- fluid/example/dtw/cost-matrix.fld | 2 +- src/Primitive/Defs.purs | 20 +++++++++++++++----- test/Main.purs | 18 +++++++++--------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/fluid/example/dtw/cost-matrix.fld b/fluid/example/dtw/cost-matrix.fld index 965d2781b..dfee539d2 100644 --- a/fluid/example/dtw/cost-matrix.fld +++ b/fluid/example/dtw/cost-matrix.fld @@ -3,7 +3,7 @@ let abs x y = if x - y < 0 then negate (x - y) else (x - y); let ns = [0 .. rows]; ms = [0 .. cols] in - [| let initV = if (or (and (n == 1) (m == 1)) (and ((abs n m) <= window) (not (or (n == 1) (m == 1))))) + [| let initV = if or (and (n == 1) (m == 1)) (and (abs n m <= window) (not (or (n == 1) (m == 1)))) then FNum 0 else Infty in initV | (n, m) in (rows, cols) |] diff --git a/src/Primitive/Defs.purs b/src/Primitive/Defs.purs index a24e82463..53e97fcf8 100644 --- a/src/Primitive/Defs.purs +++ b/src/Primitive/Defs.purs @@ -96,7 +96,6 @@ debugLog = mkExists $ ForeignOp' { arity: 1, op': op', op: fwd, op_bwd: unsafePa bwd :: OpBwd Unit bwd _ = error unimplemented -type ArrayData a = Array2 (Val a) dims :: ForeignOp dims = mkExists $ ForeignOp' { arity: 1, op': op, op: fwd, op_bwd: unsafePartial bwd } @@ -108,12 +107,12 @@ dims = mkExists $ ForeignOp' { arity: 1, op': op, op: fwd, op_bwd: unsafePartial Constr <$> new (singleton α) <@> cPair <@> (v1 : v2 : Nil) op _ = throw "Matrix expected" - fwd :: OpFwd (Raw ArrayData) + fwd :: OpFwd (Array2 (Raw Val)) fwd (Matrix α (MatrixRep (vss × (i × β1) × (j × β2))) : Nil) = pure $ (map erase <$> vss) × Constr α cPair (Int β1 i : Int β2 j : Nil) fwd _ = throw "Matrix expected" - bwd :: Partial => OpBwd (Raw ArrayData) + bwd :: Partial => OpBwd (Array2 (Raw Val)) bwd (vss × Constr α c (Int β1 i : Int β2 j : Nil)) | c == cPair = Matrix α (MatrixRep (((<$>) botOf <$> vss) × (i × β1) × (j × β2))) : Nil @@ -125,19 +124,30 @@ matrixLookup = mkExists $ ForeignOp' { arity: 2, op': op, op: fwd, op_bwd: bwd } | c == cPair = matrixGet i j r op _ = throw "Matrix and pair of integers expected" - fwd :: OpFwd (Raw ArrayData × (Int × Int) × (Int × Int)) + fwd :: OpFwd (Array2 (Raw Val) × (Int × Int) × (Int × Int)) fwd (Matrix _ r@(MatrixRep (vss × (i' × _) × (j' × _))) : Constr _ c (Int _ i : Int _ j : Nil) : Nil) | c == cPair = do v <- matrixGet i j r pure $ ((map erase <$> vss) × (i' × j') × (i × j)) × v fwd _ = throw "Matrix and pair of integers expected" - bwd :: OpBwd (Raw ArrayData × (Int × Int) × (Int × Int)) + bwd :: OpBwd (Array2 (Raw Val) × (Int × Int) × (Int × Int)) bwd ((vss × (i' × j') × (i × j)) × v) = Matrix bot (matrixUpdate i j (const v) (MatrixRep (((<$>) botOf <$> vss) × (i' × bot) × (j' × bot)))) : Constr bot cPair (Int bot i : Int bot j : Nil) : Nil +matrixMut :: ForeignOp +matrixMut = mkExists $ ForeignOp' {arity: 3, op': op, op: fwd, op_bwd: bwd} + where + op :: OpGraph + op = error "todo" + + fwd :: OpFwd (Array2 (Raw Val) × (Int × Int) × (Int × Int)) + fwd _ = throw "Matrix, pair of ints, and new val expected" + + bwd :: OpBwd (Array2 (Raw Val) × (Int × Int) × (Int × Int)) + bwd = error "todo" dict_difference :: ForeignOp dict_difference = mkExists $ ForeignOp' { arity: 2, op': op, op: fwd, op_bwd: unsafePartial bwd } where diff --git a/test/Main.purs b/test/Main.purs index 91def02a4..0e179ff34 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -2,7 +2,7 @@ module Test.Main where import Prelude hiding (add) --- import Data.Array (concat) +import Data.Array (concat) import Data.Profunctor.Strong (second) import Effect (Effect) import Effect.Aff (Aff) @@ -15,14 +15,14 @@ main :: Effect Unit main = run tests tests :: Array (String × Aff Unit) --- tests = concat --- [ test_desugaring --- , test_misc --- , test_bwd --- , test_graphics --- , test_linking --- ] -tests = test_scratchpad +tests = concat + [ test_desugaring + , test_misc + , test_bwd + , test_graphics + , test_linking + ] +-- tests = test_scratchpad test_scratchpad :: Array (String × Aff Unit) test_scratchpad = second void <$> many From 5594332b2fe9d38d36dafe3fa7b0a892f3c768ea Mon Sep 17 00:00:00 2001 From: JosephBond Date: Wed, 4 Oct 2023 11:16:58 +0100 Subject: [PATCH 17/26] tidy up --- src/Primitive/Defs.purs | 6 +++--- test/Main.purs | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Primitive/Defs.purs b/src/Primitive/Defs.purs index 53e97fcf8..39b17ecce 100644 --- a/src/Primitive/Defs.purs +++ b/src/Primitive/Defs.purs @@ -96,7 +96,6 @@ debugLog = mkExists $ ForeignOp' { arity: 1, op': op', op: fwd, op_bwd: unsafePa bwd :: OpBwd Unit bwd _ = error unimplemented - dims :: ForeignOp dims = mkExists $ ForeignOp' { arity: 1, op': op, op: fwd, op_bwd: unsafePartial bwd } where @@ -138,16 +137,17 @@ matrixLookup = mkExists $ ForeignOp' { arity: 2, op': op, op: fwd, op_bwd: bwd } : Nil matrixMut :: ForeignOp -matrixMut = mkExists $ ForeignOp' {arity: 3, op': op, op: fwd, op_bwd: bwd} +matrixMut = mkExists $ ForeignOp' { arity: 3, op': op, op: fwd, op_bwd: bwd } where op :: OpGraph op = error "todo" fwd :: OpFwd (Array2 (Raw Val) × (Int × Int) × (Int × Int)) - fwd _ = throw "Matrix, pair of ints, and new val expected" + fwd _ = throw "Matrix, pair of ints, and new val expected" bwd :: OpBwd (Array2 (Raw Val) × (Int × Int) × (Int × Int)) bwd = error "todo" + dict_difference :: ForeignOp dict_difference = mkExists $ ForeignOp' { arity: 2, op': op, op: fwd, op_bwd: unsafePartial bwd } where diff --git a/test/Main.purs b/test/Main.purs index 0e179ff34..818df672e 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -22,6 +22,7 @@ tests = concat , test_graphics , test_linking ] + -- tests = test_scratchpad test_scratchpad :: Array (String × Aff Unit) From b78a2d822059a61cb90c9c696657adeae1a76166 Mon Sep 17 00:00:00 2001 From: JosephBond Date: Wed, 4 Oct 2023 11:38:09 +0100 Subject: [PATCH 18/26] Used MatrixRep where possible --- src/Primitive/Defs.purs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Primitive/Defs.purs b/src/Primitive/Defs.purs index 39b17ecce..7b0661c0c 100644 --- a/src/Primitive/Defs.purs +++ b/src/Primitive/Defs.purs @@ -123,15 +123,15 @@ matrixLookup = mkExists $ ForeignOp' { arity: 2, op': op, op: fwd, op_bwd: bwd } | c == cPair = matrixGet i j r op _ = throw "Matrix and pair of integers expected" - fwd :: OpFwd (Array2 (Raw Val) × (Int × Int) × (Int × Int)) + fwd :: OpFwd (Raw MatrixRep × (Int × Int)) fwd (Matrix _ r@(MatrixRep (vss × (i' × _) × (j' × _))) : Constr _ c (Int _ i : Int _ j : Nil) : Nil) | c == cPair = do v <- matrixGet i j r - pure $ ((map erase <$> vss) × (i' × j') × (i × j)) × v + pure $ (MatrixRep ((map erase <$> vss) × ((i' × unit) × (j' × unit))) × (i × j)) × v fwd _ = throw "Matrix and pair of integers expected" - bwd :: OpBwd (Array2 (Raw Val) × (Int × Int) × (Int × Int)) - bwd ((vss × (i' × j') × (i × j)) × v) = + bwd :: OpBwd (Raw MatrixRep × (Int × Int)) + bwd (((MatrixRep (vss × (i'× _) × (j'× _))) × (i × j)) × v) = Matrix bot (matrixUpdate i j (const v) (MatrixRep (((<$>) botOf <$> vss) × (i' × bot) × (j' × bot)))) : Constr bot cPair (Int bot i : Int bot j : Nil) : Nil @@ -142,10 +142,10 @@ matrixMut = mkExists $ ForeignOp' { arity: 3, op': op, op: fwd, op_bwd: bwd } op :: OpGraph op = error "todo" - fwd :: OpFwd (Array2 (Raw Val) × (Int × Int) × (Int × Int)) + fwd :: OpFwd (Raw MatrixRep × (Int × Int)) fwd _ = throw "Matrix, pair of ints, and new val expected" - bwd :: OpBwd (Array2 (Raw Val) × (Int × Int) × (Int × Int)) + bwd :: OpBwd (Raw MatrixRep × (Int × Int)) bwd = error "todo" dict_difference :: ForeignOp From 0313ec899e812fe61de1e89f046bfa3a7556c6b9 Mon Sep 17 00:00:00 2001 From: JosephBond Date: Wed, 4 Oct 2023 11:38:19 +0100 Subject: [PATCH 19/26] tidied up --- src/Primitive/Defs.purs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Primitive/Defs.purs b/src/Primitive/Defs.purs index 7b0661c0c..8f8004b0e 100644 --- a/src/Primitive/Defs.purs +++ b/src/Primitive/Defs.purs @@ -131,7 +131,7 @@ matrixLookup = mkExists $ ForeignOp' { arity: 2, op': op, op: fwd, op_bwd: bwd } fwd _ = throw "Matrix and pair of integers expected" bwd :: OpBwd (Raw MatrixRep × (Int × Int)) - bwd (((MatrixRep (vss × (i'× _) × (j'× _))) × (i × j)) × v) = + bwd (((MatrixRep (vss × (i' × _) × (j' × _))) × (i × j)) × v) = Matrix bot (matrixUpdate i j (const v) (MatrixRep (((<$>) botOf <$> vss) × (i' × bot) × (j' × bot)))) : Constr bot cPair (Int bot i : Int bot j : Nil) : Nil From ecc9ede35b42b94b058b497aac97afd599160700 Mon Sep 17 00:00:00 2001 From: JosephBond Date: Wed, 4 Oct 2023 14:02:35 +0100 Subject: [PATCH 20/26] Got both functions typechecking? --- src/Primitive/Defs.purs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Primitive/Defs.purs b/src/Primitive/Defs.purs index 8f8004b0e..21da11955 100644 --- a/src/Primitive/Defs.purs +++ b/src/Primitive/Defs.purs @@ -142,12 +142,21 @@ matrixMut = mkExists $ ForeignOp' { arity: 3, op': op, op: fwd, op_bwd: bwd } op :: OpGraph op = error "todo" - fwd :: OpFwd (Raw MatrixRep × (Int × Int)) - fwd _ = throw "Matrix, pair of ints, and new val expected" + fwd :: OpFwd (Raw MatrixRep × (Int × Int) × Raw Val) + fwd (Matrix _ r@(MatrixRep (vss × (i' × _) × (j' × _))) : Constr _ c (Int _ i : Int _ j : Nil) : v : Nil) + | c == cPair = do + oldV <- matrixGet i j r + let newM = matrixUpdate i j (const v) r + pure $ ((erase newM) × (i × j) × (erase oldV)) × (Matrix bot newM) - bwd :: OpBwd (Raw MatrixRep × (Int × Int)) - bwd = error "todo" + fwd _ = throw "Matrix, pair of ints, and new val expected" + bwd :: OpBwd (Raw MatrixRep × (Int × Int) × Raw Val) + bwd ((((MatrixRep (vss × (i' × _) × (j' × _))) × (i × j) × v) × oldV)) = + Matrix bot (matrixUpdate i j (const oldV) (MatrixRep (((<$>) botOf <$> vss) × (i' × bot) × (j' × bot)))) + : Constr bot cPair (Int bot i : Int bot j : Nil) + : (map (const bot) v) + : Nil dict_difference :: ForeignOp dict_difference = mkExists $ ForeignOp' { arity: 2, op': op, op: fwd, op_bwd: unsafePartial bwd } where From 60d48f359dbbd5eb8a5e58c6b21191c86a56f549 Mon Sep 17 00:00:00 2001 From: JosephBond Date: Wed, 4 Oct 2023 14:02:46 +0100 Subject: [PATCH 21/26] tidy up --- src/Primitive/Defs.purs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Primitive/Defs.purs b/src/Primitive/Defs.purs index 21da11955..7449905e9 100644 --- a/src/Primitive/Defs.purs +++ b/src/Primitive/Defs.purs @@ -145,18 +145,19 @@ matrixMut = mkExists $ ForeignOp' { arity: 3, op': op, op: fwd, op_bwd: bwd } fwd :: OpFwd (Raw MatrixRep × (Int × Int) × Raw Val) fwd (Matrix _ r@(MatrixRep (vss × (i' × _) × (j' × _))) : Constr _ c (Int _ i : Int _ j : Nil) : v : Nil) | c == cPair = do - oldV <- matrixGet i j r - let newM = matrixUpdate i j (const v) r - pure $ ((erase newM) × (i × j) × (erase oldV)) × (Matrix bot newM) + oldV <- matrixGet i j r + let newM = matrixUpdate i j (const v) r + pure $ ((erase newM) × (i × j) × (erase oldV)) × (Matrix bot newM) fwd _ = throw "Matrix, pair of ints, and new val expected" bwd :: OpBwd (Raw MatrixRep × (Int × Int) × Raw Val) - bwd ((((MatrixRep (vss × (i' × _) × (j' × _))) × (i × j) × v) × oldV)) = - Matrix bot (matrixUpdate i j (const oldV) (MatrixRep (((<$>) botOf <$> vss) × (i' × bot) × (j' × bot)))) + bwd ((((MatrixRep (vss × (i' × _) × (j' × _))) × (i × j) × v) × oldV)) = + Matrix bot (matrixUpdate i j (const oldV) (MatrixRep (((<$>) botOf <$> vss) × (i' × bot) × (j' × bot)))) : Constr bot cPair (Int bot i : Int bot j : Nil) : (map (const bot) v) : Nil + dict_difference :: ForeignOp dict_difference = mkExists $ ForeignOp' { arity: 2, op': op, op: fwd, op_bwd: unsafePartial bwd } where From 5605fbd901de0fe24b7809e12d6ef169f7494dd4 Mon Sep 17 00:00:00 2001 From: JosephBond Date: Wed, 4 Oct 2023 14:11:59 +0100 Subject: [PATCH 22/26] testable matrix update now? --- src/Primitive/Defs.purs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Primitive/Defs.purs b/src/Primitive/Defs.purs index 7449905e9..dc95ae0bd 100644 --- a/src/Primitive/Defs.purs +++ b/src/Primitive/Defs.purs @@ -140,14 +140,16 @@ matrixMut :: ForeignOp matrixMut = mkExists $ ForeignOp' { arity: 3, op': op, op: fwd, op_bwd: bwd } where op :: OpGraph - op = error "todo" + op (Matrix α r : Constr _ c (Int _ i : Int _ j : Nil) : v : Nil) + | c == cPair = pure $ Matrix α (matrixUpdate i j (const v) r) + op _ = throw "Matrix, pair of ints, and new val expected" fwd :: OpFwd (Raw MatrixRep × (Int × Int) × Raw Val) - fwd (Matrix _ r@(MatrixRep (vss × (i' × _) × (j' × _))) : Constr _ c (Int _ i : Int _ j : Nil) : v : Nil) + fwd (Matrix _ r : Constr _ c (Int _ i : Int _ j : Nil) : v : Nil) | c == cPair = do oldV <- matrixGet i j r let newM = matrixUpdate i j (const v) r - pure $ ((erase newM) × (i × j) × (erase oldV)) × (Matrix bot newM) + pure $ ((erase newM) × (i × j) × (erase v)) × oldV fwd _ = throw "Matrix, pair of ints, and new val expected" From 14b2de2f953ef19ccc2b23bbcc5e36c51cc43f4c Mon Sep 17 00:00:00 2001 From: JosephBond Date: Wed, 4 Oct 2023 14:12:10 +0100 Subject: [PATCH 23/26] Another tidy up --- src/Primitive/Defs.purs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Primitive/Defs.purs b/src/Primitive/Defs.purs index dc95ae0bd..f1c984453 100644 --- a/src/Primitive/Defs.purs +++ b/src/Primitive/Defs.purs @@ -140,7 +140,7 @@ matrixMut :: ForeignOp matrixMut = mkExists $ ForeignOp' { arity: 3, op': op, op: fwd, op_bwd: bwd } where op :: OpGraph - op (Matrix α r : Constr _ c (Int _ i : Int _ j : Nil) : v : Nil) + op (Matrix α r : Constr _ c (Int _ i : Int _ j : Nil) : v : Nil) | c == cPair = pure $ Matrix α (matrixUpdate i j (const v) r) op _ = throw "Matrix, pair of ints, and new val expected" From cf7acabb85a10af495360c7c39a1e3e8229c7825 Mon Sep 17 00:00:00 2001 From: JosephBond Date: Wed, 4 Oct 2023 16:55:15 +0100 Subject: [PATCH 24/26] MatrixUpdate works if inefficient --- fluid/example/dtw/matrix-update.fld | 2 ++ src/Primitive/Defs.purs | 27 ++++++++++++++++----------- src/Val.purs | 7 +++---- test/Main.purs | 20 ++++++++++---------- 4 files changed, 31 insertions(+), 25 deletions(-) create mode 100644 fluid/example/dtw/matrix-update.fld diff --git a/fluid/example/dtw/matrix-update.fld b/fluid/example/dtw/matrix-update.fld new file mode 100644 index 000000000..e91b4b856 --- /dev/null +++ b/fluid/example/dtw/matrix-update.fld @@ -0,0 +1,2 @@ +let mat = [| (n, m) | (n,m) in (3, 3)|] +in matrixUpdate mat (1,1) 100 \ No newline at end of file diff --git a/src/Primitive/Defs.purs b/src/Primitive/Defs.purs index f1c984453..d627869aa 100644 --- a/src/Primitive/Defs.purs +++ b/src/Primitive/Defs.purs @@ -66,6 +66,7 @@ primitives = D.fromFoldable , "mod" × binaryZero { i: int, o: int, fwd: mod } , "quot" × binaryZero { i: int, o: int, fwd: quot } , "rem" × binaryZero { i: int, o: int, fwd: rem } + , "matrixUpdate" × extern matrixMut ] error_ :: ForeignOp @@ -120,13 +121,13 @@ matrixLookup = mkExists $ ForeignOp' { arity: 2, op': op, op: fwd, op_bwd: bwd } where op :: OpGraph op (Matrix _ r : Constr _ c (Int _ i : Int _ j : Nil) : Nil) - | c == cPair = matrixGet i j r + | c == cPair = pure $ matrixGet i j r op _ = throw "Matrix and pair of integers expected" fwd :: OpFwd (Raw MatrixRep × (Int × Int)) fwd (Matrix _ r@(MatrixRep (vss × (i' × _) × (j' × _))) : Constr _ c (Int _ i : Int _ j : Nil) : Nil) | c == cPair = do - v <- matrixGet i j r + let v = matrixGet i j r pure $ (MatrixRep ((map erase <$> vss) × ((i' × unit) × (j' × unit))) × (i × j)) × v fwd _ = throw "Matrix and pair of integers expected" @@ -141,24 +142,28 @@ matrixMut = mkExists $ ForeignOp' { arity: 3, op': op, op: fwd, op_bwd: bwd } where op :: OpGraph op (Matrix α r : Constr _ c (Int _ i : Int _ j : Nil) : v : Nil) - | c == cPair = pure $ Matrix α (matrixUpdate i j (const v) r) + | c == cPair = Matrix <$> new (singleton α) <@> (matrixUpdate i j (const v) r) op _ = throw "Matrix, pair of ints, and new val expected" fwd :: OpFwd (Raw MatrixRep × (Int × Int) × Raw Val) - fwd (Matrix _ r : Constr _ c (Int _ i : Int _ j : Nil) : v : Nil) - | c == cPair = do - oldV <- matrixGet i j r - let newM = matrixUpdate i j (const v) r - pure $ ((erase newM) × (i × j) × (erase v)) × oldV + fwd (Matrix α r@(MatrixRep (vss × (i' × _) × (j' × _))) : Constr _ c (Int _ i : Int _ j : Nil) : v : Nil) + | c == cPair = + let oldV = matrixGet i j r + newM = matrixUpdate i j (const v) r + in + pure $ (MatrixRep ((map erase <$> vss) × ((i' × unit) × (j' × unit))) × (i × j) × (erase oldV)) × (Matrix α newM) fwd _ = throw "Matrix, pair of ints, and new val expected" bwd :: OpBwd (Raw MatrixRep × (Int × Int) × Raw Val) - bwd ((((MatrixRep (vss × (i' × _) × (j' × _))) × (i × j) × v) × oldV)) = - Matrix bot (matrixUpdate i j (const oldV) (MatrixRep (((<$>) botOf <$> vss) × (i' × bot) × (j' × bot)))) + bwd ((((MatrixRep (vss × (i' × _) × (j' × _))) × (i × j) × oldV) × (Matrix α r))) = + let newV = matrixGet i j r + in + Matrix α (matrixUpdate i j (const (map (const bot) oldV)) (MatrixRep (((<$>) botOf <$> vss) × (i' × bot) × (j' × bot)))) : Constr bot cPair (Int bot i : Int bot j : Nil) - : (map (const bot) v) + : newV : Nil + bwd _ = error "absurd backwards!" dict_difference :: ForeignOp dict_difference = mkExists $ ForeignOp' { arity: 2, op': op, op: fwd, op_bwd: unsafePartial bwd } diff --git a/src/Val.purs b/src/Val.purs index a8c7e94f0..26e4c5b1f 100644 --- a/src/Val.purs +++ b/src/Val.purs @@ -23,7 +23,7 @@ import Foreign.Object (keys) as O import Graph (Vertex(..)) import Graph.GraphWriter (class MonadGraphAlloc) import Lattice (class BoundedJoinSemilattice, class BoundedLattice, class Expandable, class JoinSemilattice, class Neg, Raw, definedJoin, expand, maybeJoin, neg, (∨)) -import Util (type (×), Endo, error, orElse, throw, unsafeUpdateAt, (!), (×), (≜), (≞)) +import Util (type (×), Endo, definitely, error, orElse, throw, unsafeUpdateAt, (!), (×), (≜), (≞)) import Util.Pretty (Doc, beside, text) data Val a @@ -106,9 +106,8 @@ newtype MatrixRep a = MatrixRep (Array2 (Val a) × (Int × a) × (Int × a)) type Array2 a = Array (Array a) -matrixGet :: forall a m. MonadThrow Error m => Int -> Int -> MatrixRep a -> m (Val a) -matrixGet i j (MatrixRep (vss × _ × _)) = - orElse "Index out of bounds" $ do +matrixGet :: forall a. Int -> Int -> MatrixRep a -> Val a +matrixGet i j (MatrixRep (vss × _ × _)) = definitely "index out of bounds!" $ do us <- vss !! (i - 1) us !! (j - 1) diff --git a/test/Main.purs b/test/Main.purs index 818df672e..1143cf507 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -2,7 +2,7 @@ module Test.Main where import Prelude hiding (add) -import Data.Array (concat) +-- import Data.Array (concat) import Data.Profunctor.Strong (second) import Effect (Effect) import Effect.Aff (Aff) @@ -15,19 +15,19 @@ main :: Effect Unit main = run tests tests :: Array (String × Aff Unit) -tests = concat - [ test_desugaring - , test_misc - , test_bwd - , test_graphics - , test_linking - ] +-- tests = concat +-- [ test_desugaring +-- , test_misc +-- , test_bwd +-- , test_graphics +-- , test_linking +-- ] --- tests = test_scratchpad +tests = test_scratchpad test_scratchpad :: Array (String × Aff Unit) test_scratchpad = second void <$> many - [ { file: "dtw/cost-matrix" + [ { file: "dtw/matrix-update" , fwd_expect: "FNum 0, Infty, Infty, Infty, Infty, Infty, Infty,\n\ \Infty, FNum 0, FNum 0, Infty, Infty, Infty, Infty,\n\ From 751a740a0bab069edf19b00a15032e722a167a85 Mon Sep 17 00:00:00 2001 From: JosephBond Date: Wed, 4 Oct 2023 21:57:10 +0100 Subject: [PATCH 25/26] final tidy --- src/Primitive/Defs.purs | 20 +++++++++++--------- src/Val.purs | 4 ++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Primitive/Defs.purs b/src/Primitive/Defs.purs index d627869aa..9e1bd1e40 100644 --- a/src/Primitive/Defs.purs +++ b/src/Primitive/Defs.purs @@ -66,7 +66,7 @@ primitives = D.fromFoldable , "mod" × binaryZero { i: int, o: int, fwd: mod } , "quot" × binaryZero { i: int, o: int, fwd: quot } , "rem" × binaryZero { i: int, o: int, fwd: rem } - , "matrixUpdate" × extern matrixMut + , "matrixUpdate" × extern matrixMut ] error_ :: ForeignOp @@ -148,21 +148,23 @@ matrixMut = mkExists $ ForeignOp' { arity: 3, op': op, op: fwd, op_bwd: bwd } fwd :: OpFwd (Raw MatrixRep × (Int × Int) × Raw Val) fwd (Matrix α r@(MatrixRep (vss × (i' × _) × (j' × _))) : Constr _ c (Int _ i : Int _ j : Nil) : v : Nil) | c == cPair = - let oldV = matrixGet i j r - newM = matrixUpdate i j (const v) r + let + oldV = matrixGet i j r + newM = matrixUpdate i j (const v) r in - pure $ (MatrixRep ((map erase <$> vss) × ((i' × unit) × (j' × unit))) × (i × j) × (erase oldV)) × (Matrix α newM) + pure $ (MatrixRep ((map erase <$> vss) × ((i' × unit) × (j' × unit))) × (i × j) × (erase oldV)) × (Matrix α newM) fwd _ = throw "Matrix, pair of ints, and new val expected" bwd :: OpBwd (Raw MatrixRep × (Int × Int) × Raw Val) bwd ((((MatrixRep (vss × (i' × _) × (j' × _))) × (i × j) × oldV) × (Matrix α r))) = - let newV = matrixGet i j r + let + newV = matrixGet i j r in - Matrix α (matrixUpdate i j (const (map (const bot) oldV)) (MatrixRep (((<$>) botOf <$> vss) × (i' × bot) × (j' × bot)))) - : Constr bot cPair (Int bot i : Int bot j : Nil) - : newV - : Nil + Matrix α (matrixUpdate i j (const (map (const bot) oldV)) (MatrixRep (((<$>) botOf <$> vss) × (i' × bot) × (j' × bot)))) + : Constr bot cPair (Int bot i : Int bot j : Nil) + : newV + : Nil bwd _ = error "absurd backwards!" dict_difference :: ForeignOp diff --git a/src/Val.purs b/src/Val.purs index 26e4c5b1f..3095f8489 100644 --- a/src/Val.purs +++ b/src/Val.purs @@ -108,8 +108,8 @@ type Array2 a = Array (Array a) matrixGet :: forall a. Int -> Int -> MatrixRep a -> Val a matrixGet i j (MatrixRep (vss × _ × _)) = definitely "index out of bounds!" $ do - us <- vss !! (i - 1) - us !! (j - 1) + us <- vss !! (i - 1) + us !! (j - 1) matrixUpdate :: forall a. Int -> Int -> Endo (Val a) -> Endo (MatrixRep a) matrixUpdate i j δv (MatrixRep (vss × h × w)) = From 701a4abb15bcf5f0b5e7ad14f94d7bdb65dc04ed Mon Sep 17 00:00:00 2001 From: JosephBond Date: Wed, 4 Oct 2023 22:00:30 +0100 Subject: [PATCH 26/26] All tidied and working, now a test --- test/Main.purs | 20 ++++++++++---------- test/Spec/Specs.purs | 6 ++++++ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/test/Main.purs b/test/Main.purs index 1143cf507..de073564d 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -2,7 +2,7 @@ module Test.Main where import Prelude hiding (add) --- import Data.Array (concat) +import Data.Array (concat) import Data.Profunctor.Strong (second) import Effect (Effect) import Effect.Aff (Aff) @@ -15,15 +15,15 @@ main :: Effect Unit main = run tests tests :: Array (String × Aff Unit) --- tests = concat --- [ test_desugaring --- , test_misc --- , test_bwd --- , test_graphics --- , test_linking --- ] - -tests = test_scratchpad +tests = concat + [ test_desugaring + , test_misc + , test_bwd + , test_graphics + , test_linking + ] + +-- tests = test_scratchpad test_scratchpad :: Array (String × Aff Unit) test_scratchpad = second void <$> many diff --git a/test/Spec/Specs.purs b/test/Spec/Specs.purs index 61f79dfce..2b1dc8556 100644 --- a/test/Spec/Specs.purs +++ b/test/Spec/Specs.purs @@ -49,6 +49,12 @@ misc_cases = \Infty, Infty, FNum 0, FNum 0, FNum 0, Infty, Infty,\n\ \Infty, Infty, Infty, FNum 0, FNum 0, FNum 0, Infty" } + , { file: "dtw/matrix-update" + , fwd_expect: + "100, (1, 2), (1, 3),\n\ + \(2, 1), (2, 2), (2, 3),\n\ + \(3, 1), (3, 2), (3, 3)" + } ] desugar_cases :: Array TestSpec