haskell-jp / beginners #20 at 2022-05-23 01:05:07 +0900

takino takayuki
extencibleに関する質問です

scrapboxのプログラムを試していましたが、エラーの修正方法が見付かりませんので、御教授願います。


type Person = Record '[ "personId" :> Int, "name" :> String]
type Address = Record '[ "personId" :> Int, "address" :> String]

-- Associate??
getPersonId :: Associate "personId" Int xs => Record xs -> Int
getPersonId = view #personId


-- エラーメッセージ
-- [1 of 1] Compiling Main ( src/extensible.hs, interpreted )
--
-- src/extensible.hs:83:16: error:
-- Not in scope: type constructor or class ‘Associate’
-- Perhaps you meant one of these:
-- ‘Associated’ (imported from Data.Extensible),
-- ‘Associated'’ (imported from Data.Extensible)
-- |
-- 83 | getPersonId :: Associate "personId" Int xs => Record xs -> Int
-- | ^^^^^^^^^
-- Failed, no modules loaded.


-- 参考にしているサイト
https://scrapbox.io/haskell-shoen/extensible%2F%E5%88%9D%E5%BF%83%E8%80%85%E5%90%91%E3%81%91%E6%94%BB%E7%95%A5%E6%83%85%E5%A0%B1
https://hackage.haskell.org/package/extensible-0.8.3/changelog 曰く、バージョン0.8で Associate は削除されてしまったみたいで、件のページがその仕様変更に追いついていないようです。代わりに追加されたとおぼしき Associated のドキュメント https://hackage.haskell.org/package/extensible-0.8.3/docs/Data-Extensible-Inclusion.html#t:Associated 曰く
Associated xs (k ':> v) is equivalent to Associate k v xs
なので
Associated xs ("personId" :> Int) => Record xs -> Int

ならいかがでしょうか?
ちょっと試せてないので間違っていたらすみません :sweat_drops:
takino takayuki
ご回答の通りでうまくいきました。
懇切丁寧なご返答で痛みいります。
ありがとうございました。