From 51de0d65710152d3a83c037d1de2768e8c2f4a3e Mon Sep 17 00:00:00 2001 From: Andrei Mikhailov Date: Sat, 4 Jan 2025 13:54:34 -0300 Subject: [PATCH 1/2] updated documentation strings to include the case of empty tag in a union --- dhall/src/Dhall/Marshal/Encode.hs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/dhall/src/Dhall/Marshal/Encode.hs b/dhall/src/Dhall/Marshal/Encode.hs index 4ba9e0055..5ae18cf4f 100644 --- a/dhall/src/Dhall/Marshal/Encode.hs +++ b/dhall/src/Dhall/Marshal/Encode.hs @@ -943,6 +943,7 @@ encodeFieldWith name encodeType = RecordEncoder $ Dhall.Map.singleton name encod data Status = Queued Natural | Result Text | Errored Text + | Unreachable () :} And assume that we have the following Dhall union that we would like to @@ -951,6 +952,7 @@ data Status = Queued Natural > < Result : Text > | Queued : Natural > | Errored : Text +> | Unreachable > >.Result "Finish successfully" Our encoder has type 'Encoder' @Status@, but we can't build that out of any @@ -963,11 +965,13 @@ injectStatus = adapt >$< unionEncoder ( encodeConstructorWith "Queued" inject >|< encodeConstructorWith "Result" inject >|< encodeConstructorWith "Errored" inject + >|< encodeConstructorWith "Unreachable" inject ) where - adapt (Queued n) = Left n - adapt (Result t) = Right (Left t) - adapt (Errored e) = Right (Right e) + adapt (Queued n) = Just (Left n) + adapt (Result t) = Just (Right (Left t)) + adapt (Errored e) = Just (Right (Right e)) + adapt Unreachable = Nothing :} Or, since we are simply using the `ToDhall` instance to inject each branch, we could write @@ -978,11 +982,13 @@ injectStatus = adapt >$< unionEncoder ( encodeConstructor "Queued" >|< encodeConstructor "Result" >|< encodeConstructor "Errored" + >|< encodeConstructor "Unreachable" ) where - adapt (Queued n) = Left n - adapt (Result t) = Right (Left t) - adapt (Errored e) = Right (Right e) + adapt (Queued n) = Just (Left n) + adapt (Result t) = Just (Right (Left t)) + adapt (Errored e) = Just (Right (Right e)) + adapt Unreachable = Nothing :} -} From ad2f8f9f5b580f4b23f290cdbf20e3f5ca668aa2 Mon Sep 17 00:00:00 2001 From: Andrei Mikhailov Date: Sun, 5 Jan 2025 11:41:07 -0300 Subject: [PATCH 2/2] docs for union decoder: changed Maybe to Either and the type of Unreachable --- dhall/src/Dhall/Marshal/Encode.hs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/dhall/src/Dhall/Marshal/Encode.hs b/dhall/src/Dhall/Marshal/Encode.hs index 5ae18cf4f..b62644422 100644 --- a/dhall/src/Dhall/Marshal/Encode.hs +++ b/dhall/src/Dhall/Marshal/Encode.hs @@ -943,7 +943,7 @@ encodeFieldWith name encodeType = RecordEncoder $ Dhall.Map.singleton name encod data Status = Queued Natural | Result Text | Errored Text - | Unreachable () + | Unreachable :} And assume that we have the following Dhall union that we would like to @@ -952,7 +952,7 @@ data Status = Queued Natural > < Result : Text > | Queued : Natural > | Errored : Text -> | Unreachable +> | Unreachable > >.Result "Finish successfully" Our encoder has type 'Encoder' @Status@, but we can't build that out of any @@ -968,10 +968,10 @@ injectStatus = adapt >$< unionEncoder >|< encodeConstructorWith "Unreachable" inject ) where - adapt (Queued n) = Just (Left n) - adapt (Result t) = Just (Right (Left t)) - adapt (Errored e) = Just (Right (Right e)) - adapt Unreachable = Nothing + adapt (Queued n) = Left n + adapt (Result t) = Right (Left t) + adapt (Errored e) = Right (Right (Left e)) + adapt Unreachable = Right (Right (Right ())) :} Or, since we are simply using the `ToDhall` instance to inject each branch, we could write @@ -985,10 +985,10 @@ injectStatus = adapt >$< unionEncoder >|< encodeConstructor "Unreachable" ) where - adapt (Queued n) = Just (Left n) - adapt (Result t) = Just (Right (Left t)) - adapt (Errored e) = Just (Right (Right e)) - adapt Unreachable = Nothing + adapt (Queued n) = Left n + adapt (Result t) = Right (Left t) + adapt (Errored e) = Right (Right (Left e)) + adapt Unreachable = Right (Right (Right ())) :} -}