2022-03-02 22:02:51 +00:00
{- # OPTIONS - - rewriting # -}
2022-02-09 00:26:58 +00:00
module FFI.Data.Aeson where
2022-03-02 22:02:51 +00:00
open import Agda.Builtin.Equality using ( _≡_)
open import Agda.Builtin.Equality.Rewrite using ( )
2022-02-09 00:26:58 +00:00
open import Agda.Builtin.Bool using ( Bool)
open import Agda.Builtin.String using ( String)
open import FFI.Data.ByteString using ( ByteString)
open import FFI.Data.HaskellString using ( HaskellString; pack)
2022-03-02 22:02:51 +00:00
open import FFI.Data.Maybe using ( Maybe; just; nothing)
2022-03-23 20:02:57 +00:00
open import FFI.Data.Either using ( Either; mapL)
2022-02-09 00:26:58 +00:00
open import FFI.Data.Scientific using ( Scientific)
open import FFI.Data.Vector using ( Vector)
2022-03-02 22:02:51 +00:00
open import Properties.Equality using ( _≢_)
2022-02-09 00:26:58 +00:00
{- # FOREIGN GHC import qualified Data.Aeson # -}
{- # FOREIGN GHC import qualified Data.Aeson.Key # -}
{- # FOREIGN GHC import qualified Data.Aeson.KeyMap # -}
postulate
KeyMap : Set → Set
Key : Set
fromString : String → Key
toString : Key → String
2022-03-02 22:02:51 +00:00
empty : ∀ { A} → KeyMap A
singleton : ∀ { A} → Key → A → ( KeyMap A)
insert : ∀ { A} → Key → A → ( KeyMap A) → ( KeyMap A)
delete : ∀ { A} → Key → ( KeyMap A) → ( KeyMap A)
unionWith : ∀ { A} → ( A → A → A) → ( KeyMap A) → ( KeyMap A) → ( KeyMap A)
2022-02-09 00:26:58 +00:00
lookup : ∀ { A} → Key -> KeyMap A -> Maybe A
{- # POLARITY KeyMap ++ # -}
{- # COMPILE GHC KeyMap = type Data.Aeson.KeyMap.KeyMap # -}
{- # COMPILE GHC Key = type Data.Aeson.Key.Key # -}
{- # COMPILE GHC fromString = Data.Aeson.Key.fromText # -}
{- # COMPILE GHC toString = Data.Aeson.Key.toText # -}
2022-03-02 22:02:51 +00:00
{- # COMPILE GHC empty = \_ - > Data.Aeson.KeyMap.empty # -}
{- # COMPILE GHC singleton = \_ - > Data.Aeson.KeyMap.singleton # -}
{- # COMPILE GHC insert = \_ - > Data.Aeson.KeyMap.insert # -}
{- # COMPILE GHC delete = \_ - > Data.Aeson.KeyMap.delete # -}
{- # COMPILE GHC unionWith = \_ - > Data.Aeson.KeyMap.unionWith # -}
2022-02-09 00:26:58 +00:00
{- # COMPILE GHC lookup = \_ - > Data.Aeson.KeyMap.lookup # -}
2022-03-02 22:02:51 +00:00
postulate lookup-insert : ∀ { A} k v ( m : KeyMap A) → ( lookup k ( insert k v m) ≡ just v)
postulate lookup-empty : ∀ { A} k → ( lookup { A} k empty ≡ nothing)
postulate lookup-insert-not : ∀ { A} j k v ( m : KeyMap A) → ( j ≢ k) → ( lookup k m ≡ lookup k ( insert j v m) )
postulate singleton-insert-empty : ∀ { A} k ( v : A) → ( singleton k v ≡ insert k v empty)
postulate insert-swap : ∀ { A} j k ( v w : A) m → ( j ≢ k) → insert j v ( insert k w m) ≡ insert k w ( insert j v m)
postulate insert-over : ∀ { A} j k ( v w : A) m → ( j ≡ k) → insert j v ( insert k w m) ≡ insert j v m
postulate to-from : ∀ k → toString( fromString k) ≡ k
postulate from-to : ∀ k → fromString( toString k) ≡ k
{- # REWRITE lookup - insert lookup - empty singleton - insert - empty # -}
2022-02-09 00:26:58 +00:00
data Value : Set where
object : KeyMap Value → Value
array : Vector Value → Value
string : String → Value
number : Scientific → Value
bool : Bool → Value
null : Value
{- # COMPILE GHC Value = data Data.Aeson.Value (Data.Aeson.Object|Data.Aeson.Array|Data.Aeson.String|Data.Aeson.Number|Data.Aeson.Bool|Data.Aeson.Null) # -}
Object = KeyMap Value
Array = Vector Value
postulate
decode : ByteString → Maybe Value
eitherHDecode : ByteString → Either HaskellString Value
{- # COMPILE GHC decode = Data.Aeson.decodeStrict # -}
{- # COMPILE GHC eitherHDecode = Data.Aeson.eitherDecodeStrict # -}
eitherDecode : ByteString → Either String Value
2022-03-23 20:02:57 +00:00
eitherDecode bytes = mapL pack ( eitherHDecode bytes)
2022-02-09 00:26:58 +00:00