質問

genOut/String を起動するにはどうすればよいですか?

module IOStream where

import System.IO
import System.IO.Unsafe

class Out a where
  out :: a → String

instance Show a ⇒ Out a where
  out = show

outString :: String → String
outString = id
{-# RULES "genOut/String" out = outString #-}

infixl 9 <<, ≪
(≪), (<<) :: Out a ⇒ IO Handle → a → IO Handle
(<<)= (≪)
h ≪ a = do
  s ← h
  hPutStr s $ out a
  return s

cout, cin, cerr :: IO Handle
cout = return stdout
cin  = return stdin
cerr = return stderr

endl :: String
endl = "\n"

--infixr 9 ∘ °
(∘) = (.)
(°) = flip (∘)

module Main where

import System.IO
import IOStream

foreign import ccall "pi.h f_" f_ :: IO Double

main :: IO Int
main = do
  --putStrLn . show =<< f_

--  ((≪ endl) . (cout ≪)) =<< f_ 
  (cout ≪) ° (≪ endl) =<< f_
  return 0

コンパイルとリンク:

cetin@unique:~/lab/c/linking/demo$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.10.2
cetin@unique:~/lab/c/linking/demo$ ghc -fglasgow-exts -O2 -ddump-simpl-stats -XUndecidableInstances -O2 iostream.hs main.hs pi_v2.o -o hsex2

==================== FloatOut stats: ====================
0 Lets floated to top level; 1 Lets floated elsewhere; from 3 Lambda groups



==================== FloatOut stats: ====================
0 Lets floated to top level; 0 Lets floated elsewhere; from 5 Lambda groups



==================== Grand total simplifier statistics ====================
Total ticks:     184

40 PreInlineUnconditionally
45 PostInlineUnconditionally
24 UnfoldingDone
8 LetFloatFromLet
4 EtaReduction
57 BetaReduction
6 KnownBranch
11 SimplifierDone



==================== FloatOut stats: ====================
0 Lets floated to top level; 0 Lets floated elsewhere; from 1 Lambda groups



==================== FloatOut stats: ====================
3 Lets floated to top level; 0 Lets floated elsewhere; from 1 Lambda groups



==================== Grand total simplifier statistics ====================
Total ticks:     218

42 PreInlineUnconditionally
57 PostInlineUnconditionally
33 UnfoldingDone
9 LetFloatFromLet
1 EtaReduction
66 BetaReduction
10 KnownBranch
12 SimplifierDone

結果:

cetin@unique:~/lab/c/linking/demo$ ./hsex2
3.141592653589793"\n"cetin@unique:~/lab/c/linking/demo$ 

期待される:

cetin@unique:~/lab/c/linking/demo$ ./hsex2
3.141592653589793
cetin@unique:~/lab/c/linking/demo$ 

そのルールを有効にするにはどうすればよいですか?

役に立ちましたか?

解決

欲しいようですね 重複するインスタンス, 、あなたが望むように、 instance Out String とは違います instance Show a => Out a それと重なるもの。

{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
{-# LANGUAGE OverlappingInstances, UndecidableInstances #-}
class Out a where out :: a -> String
instance Out String where out = id
instance (Show a) => Out a where out = show

一般的な推奨事項は、以下の場合を除き、重複するインスタンスや決定不可能なインスタンスの使用を避けることです。 本当に 型チェックに対する変更は移植性がなく、他の問題を引き起こす可能性があるため、これは必要です。


編集

実装に関しては、クラスのインスタンスごとに辞書を考えてください。 << のインスタンスの辞書が与えられます Out 隠しパラメータとして使用することが期待されています。以来 out そこから見上げられている、あなたの入る余地はない RULE 発砲する。もし out タイプクラス内になく、非ポリモーフィック関数から呼び出されていた場合、私は次のことを期待します RULE 一致させますが、このままでは機能しないのも不思議ではありません。

RULE/SPECIALIZE これらは最適化のみを目的としており、起動するかどうかによってコードの動作が変わるべきではありません。

あなたが何をしているのか理解するのにしばらく時間がかかりました…Haskell が C++ ではないことは理解していますよね?ポリモーフィズムの利用方法はまったく異なります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top