関数型プログラミング言語Haskell Part34 (667レス)
関数型プログラミング言語Haskell Part34 http://mevius.5ch.net/test/read.cgi/tech/1639713446/
上
下
前次
1-
新
通常表示
512バイト分割
レス栞
抽出解除
必死チェッカー(本家)
(べ)
自ID
レス栞
あぼーん
220: デフォルトの名無しさん [sage] 2023/06/25(日) 14:24:09.04 ID:H+Ij4nqZ 質問にさせていただきます 違う標数での有限体での計算を必要とするコードを考えてます ある変数に𝔽₃や𝔽₅の値を混在して代入することはないので𝔽₃の元を生成するconstructor F3や𝔽₅の元を生成すconstructor F5を定義して x = F3 $ 1+2 y = F5 $ 2-5 のような記述ができればいいなと思ってます そのためには data FiniteFieldCh3 = F3 Int instance Num F3 where... のようにF3,F5両方にNumを定義しないといけません 面倒なのでFiniteFieldと言うクラスを作って以下のようにできないかやってみたところ class FiniteField t where modulus :: t -> Int toInt :: t -> Int fromInt :: Int -> t instance ( FiniteField a ) => ( Num a ) where x + y = fromInt $ mod ( toInt x ) + ( toInt y ) ) ( char x ) http://mevius.5ch.net/test/read.cgi/tech/1639713446/220
221: デフォルトの名無しさん [sage] 2023/06/25(日) 14:24:14.71 ID:H+Ij4nqZ 以下のようなerror が出てきます prog.hs:22:10: error: • The constraint ‘FiniteField a’ is no smaller than the instance head (Use UndecidableInstances to permit this) • In the instance declaration for ‘(Num a)’ | 22 | instance ( FiniteField a ) => ( Num a ) where | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ コレはなんですか? ちなみにFlexibleInstancesというのが必要かとの事なので冒頭に {-# LANGUAGE FlexibleInstances #-} を入れています コンパイラはghc8.4.4です よろしくお願い致します http://mevius.5ch.net/test/read.cgi/tech/1639713446/221
222: デフォルトの名無しさん [sage] 2023/06/25(日) 14:43:47.12 ID:H+Ij4nqZ すいません、最後の行間違って修正前のやつコピペしました class FiniteField t where modulus :: t -> Int toInt :: t -> Int fromInt :: Int -> t instance ( FiniteField a ) => ( Num a ) where x + y = fromInt $ mod ( toInt x ) + ( toInt y ) ) ( modulus x ) です よろしくお願い致します http://mevius.5ch.net/test/read.cgi/tech/1639713446/222
メモ帳
(0/65535文字)
上
下
前次
1-
新
書
関
写
板
覧
索
設
栞
歴
スレ情報
赤レス抽出
画像レス抽出
歴の未読スレ
AAサムネイル
Google検索
Wikipedia
ぬこの手
ぬこTOP
0.036s