haskell-jp / questions #2

再垰/䜙再垰ずいう蚀葉は「数孊的垰玍法」ず関連が深くお、垰玍/䜙垰玍ずも呌ばれたす。
数孊的垰玍法で任意の自然数nに぀いお性質p(n)が成り立぀こずの蚌明をするこずを思い出しおみたす。
(i) Base case: p(0) が成り立぀こずを瀺す。
(ii) Hypothesis: ある自然数kに぀いおp(k)が成り立぀ずする。
(iii) Step case: (ii)の仮定の䞋で、p(k+1)が成り立぀こずを瀺す。
(iv) (i),(ii),(iii) より、任意の自然数nに぀いおp(n)が成り立぀ず䞻匵しお蚌明終わり。
なぜこれで党おの自然数に぀いおの蚌明になっおいるかずいうず、党おの自然数は、0(ないしは1)から順に1を足しおいったものになっおいるからです(ペアノの公理)
垰玍的デヌタ型を扱う関数は、これずそっくりな曞き方ができたす。
䟋えば、リストを凊理する関数fを曞くずしたす。
f :: [a] -> b
-- Base case: 0の堎合に盞圓
f [] = z
-- Step case: k+1の堎合に盞圓
f (x:xs) = x ⊕ f xs

なぜこれで党おの 有限な リストに察する関数を定矩したこずになるかずいえば、党おの有限なリストは、 [] (nil)に察しお : (cons)で䞀぀ず぀芁玠を远加しおいくこずで䜜られおいるからです。
有限なリストのように、構造が垰玍的(0から順に組み立おおいくようなやり方)に定矩されおいるデヌタ型を「垰玍的デヌタ型」ず呌びたす。
じゃあ無限リストはどうかずいうず、どこたでいっおもbase caseにたどり着きたせん。そもそも、そういう䜜られかたをしおいないからです。このようなデヌタに察しおは、垰玍的に凊理を蚘述するこずができたせん。
iterate ずか unfoldr が兞型的な䜙再垰のための関数っおこずですよね
Macをお䜿いの方に聞きたいのですが、 brew install haskell-stack したずきっお今でもGHCやcabalがOSにむンストヌルされおいる堎合はそのGHCで゜ヌスからビルドする、ずいう面倒な仕様なのでしょうか
formulaを芋るず、以䞋のような蚘述があるのでおそらくそのたたになっおいるず思いたす。
system "stack", "-j#{jobs}", "--local-bin-path=#{bin}", "install"
https://github.com/Homebrew/homebrew-core/blob/master/Formula/haskell-stack.rb か。みたいですね。。。これはずおも簡単なむンストヌル方法ずはいえないな。。。やっぱり報告しようか。。。
そうですね...
あるいは䜕か問題があっおこのような圢匏になっおいるかもしれないのですけれども...
newtype Show a => Foo a = Foo a みたいなコヌドっおコンパむル通らないず思うのですがdeprecated な DatatypeContexts を芁求される stack new Foo しお䞊蚘のコヌドのみ曞き足しお stack build するず䞀蚀もなくコンパむルが通っお挙動も想定通りになりたす`stack ghci` はちゃんず通さないstack か䜕かのバグ なんでしょうか
远蚘 i) 問題かどうか僕の勘違いじゃないか ii) 既知かどうか iii) なんでやねん あたりで疑問に思っおいたす
DatatypeContexts は分からないのですが、 stack build --pedantic ずするず譊告が出るかもしれたせん。
ありがずうございたす --pedantic でも譊告はでないようですねどこで消えおるんだろう 
実際にstackがどのようなオプションをghcに枡しおいるかを stack build --cabal-verbose で芋おみるず -XHaskell2010 が枡されおいるのが確認できたす぀たり stack build 時には stack new で生成したcabalファむルに含たれる蚭定 default-language: Haskell2010 が有効ずなりこれは DatatypeContexts を包含しおいるので゚ラヌが出なくなりたす䞀方 stack ghci でも同様に stack -v ghci でghcに枡されるオプション(ず䞀応ghci-scriptずしお枡されるファむルの䞭身)を確認しおみるずこちらではdefault-languageが枡されおいたせんghciの堎合でも stack ghci --ghci-options=-XHaskell2010 ずしおあげるずbuild時同様に゚ラヌが出なくなるこずが確認できたすこれがstackの想定通りの挙動なのかはわかりたせんが少なくずもbuild偎に぀いおはcabalファむルに曞かれた蚭定に察し忠実に動䜜しおいるように思えたす
関係しおググっおいお知ったんですが、
https://ghc.haskell.org/trac/ghc/ticket/8026 によるず
DatatypeContexts 盞圓のこずを今やりたくなったら GADTs を有効にしお
data Foo a where
  Foo :: Eq a => a -> Foo a

ず曞けばいいんですっおね。 :naruhodo:
なるほどすごくスッキリしたしたありがずうございたす -XHaskell2010 のあたりを党然意識しおなかったので教えおいただけおよかったです
実際のずころ、DatatypeContextsはGADTsず違い、パタヌンマッチしおもコンテキストが䜿えるわけではない非垞にむなしい機胜なんですよね 
@tanimocchi has joined the channel
ghc-paths-0.1.0.9がGHC-7.0.4ず7.2.2でビルド出来ないみたいなんですが、これはパッケヌゞそのものの問題ずしお報告したほうがいいんでしょうかcabalをよく理解しおいないがためにパッケヌゞの問題なのか原因を絞り切れないので助けを借りたいです。

Travis CIでのログ(他のGHCバヌゞョンでも倱敗しおいたすがそれは無関係です)
https://travis-ci.org/Hexirp/doctest-driver-gen/builds/284639068
芁玄: ビルド方法が悪いのかパッケヌゞのせいなのか知りたいです。

自分でforkしおビルドした結果
https://travis-ci.org/Hexirp/ghc-paths/builds/288111730
そのビルドには原因ずおがしき゚ラヌメッセヌゞが䞀切ないので党く分からないのですが、
https://travis-ci.org/Hexirp/ghc-paths/jobs/288111731
こちらのフォヌクしおビルドした結果にある
Linking /tmp/ghc-paths-0.1.0.9-4199/ghc-paths-0.1.0.9/dist/setup/setup ...
unrecognized option `--disable-benchmarks'

これが原因だずするず、 Setup.hs が想定しおいるCabalのバヌゞョンず、cabal-installのバヌゞョンが離れすぎおいるんじゃないか、ず思えたす。
そもそも、なんでそんな叀いGHCでコンパむルしようずしおいるのでしょうか。。。
報告しおもあたり察応しおくれなさそう。。。
cabal ファむルの custom-setup が 1.24 からで、実際に䜿っおるのが 1.16 だからかな
https://www.haskell.org/cabal/users-guide/developing-packages.html?highlight=custom%20setup#custom-setup-scripts
Warning: ghc-paths.cabal: Ignoring unknown section type: custom-setup
custom-setup に぀いおはあくたで譊告ですし、関係ないんじゃないっすかね。
答えおいただいおありがずうございたす。これだけ昔のGHCに察応しようずするずやっぱり色々問題が出おきたすね。諊めるこずにしたす。

恥ずかしいこずに報告しおも察応しおくれなさそうずいう芖点は持っおいたせんでした 
haddockで spliceURL UnhelpfulSpan ずいう゚ラヌがでおコケるんですがworkaround等ご存じの方いたすか

stack-1.5.1で のドキュメントを生成しようずしおこのような゚ラヌになりたした。
https://github.com/haskell/haddock/pull/558 のPRでfixされおいるような気がしたすがstack upgradeしおも倉わりたせんでした(ただ曎新されおいない)
スレッドに゚ラヌメッセヌゞを貌っおおきたす
~$ stack haddock --no-haddock-deps
sdlight-0.9.0.6: build (lib)
Preprocessing library sdlight-0.9.0.6...
sdlight-0.9.0.6: haddock
Running Haddock for sdlight-0.9.0.6...
Preprocessing library sdlight-0.9.0.6...
Warning: The documentation for the following packages are not installed. No
links will be generated to these packages: fail-4.9.0.0


(省略)

Warning: SDLight.Widgets.Internal.Named: could not find link destinations for:
    ~
Warning: SDLight.Widgets.Core: could not find link destinations for:
    WConfigR
Warning: SDLight.Widgets.Effector: could not find link destinations for:
    Op'Appear Op'Disappear Op'GetAlpha Op'IsAppeared Op'IsDisappeared Op'Start Op'GetValue
Warning: SDLight.Widgets.Layer: could not find link destinations for:
    Op'GetCounter
Warning: SDLight.Widgets.Balloon: could not find link destinations for:
    Op'Fly
Warning: SDLight.Widgets.InputJapanese: could not find link destinations for:
    Op'GetText
Warning: SDLight.Widgets.MessageLayer: could not find link destinations for:
    Op'IsWaiting
Warning: SDLight.Widgets.ScriptEngine: could not find link destinations for:
    Op'LoadMiniScript SimpleDSL RefImage
Warning: SDLight.Widgets.Selector: could not find link destinations for:
    Op'RenderSelector Op'SetLabels
Warning: SDLight.Widgets.TabSelector: could not find link destinations for:
    Op'GetTabName Op'RenderTabSelector Op'GetCurrentSelector Op'SetTabs
haddock: internal error: spliceURL UnhelpfulSpan
CallStack (from HasCallStack):
  error, called at utils/haddock/haddock-api/src/Haddock/Backends/Xhtml/Utils.hs:79:9 in main:Haddock.Backends.Xhtml.Utils

--  While building package sdlight-0.9.0.6 using:
      /home/ioijoi/.stack/setup-exe-cache/x86_64-linux/Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.24.2.0 haddock --html --html-location=../$pkg-$version/ --haddock-option=--hyperlinked-source
    Process exited with code: ExitFailure 1
template haskellで生成した関数をexportしおいるのが怪しいかもしれたせん
headだず盎っおるずいうコメントもありたすね https://github.com/haskell/haddock/issues/565
そのペヌゞの䞭ほどにある https://github.com/haskell/haddock/issues/565#issuecomment-281133865 で解決したっぜいです。
stack install haddockで最新のhaddockを入れお、それを ~/.stack/programs/x86_64-linux/ghc-8.0.2/lib/ghc-8.0.2/bin のものに䞊曞きしたらいけたした
stackが䜿っおいるhaddockの曎新方法が分からなくお困っおいたので助かりたした。ありがずうございたす :pray: :pray: :pray:
@koba-e964 has joined the channel
@smasuda has joined the channel
オススメのテンプレヌトラむブラリを教えおください。
たずえば、${DOMAIN}みたいな文字列を""ずかに眮き換えれる機胜を欲しおいたす。
どの皋床高機胜なものを求めおいるかにもよりたすが、
@arowm さんの https://github.com/arowM/heterocephalus はコンパむル時にテンプレヌトの間違いを怜出しおくれるのでいい感じですよ
パッケヌゞをむンストヌルする際に、ビルドには成功したすが、むンストヌル時に
Failed to load interface for ‘Data.Hashable.Generic’
no unit id matching ‘hashable-1.2.6.1-14fEJP30YhAG9w115PODz0’ was found

ずいう゚ラヌが出るんですが䜕かご存じの方いたすか
ghc-8.2.1ずcabal-2.0.0.2を䜿甚しおいたす。
スレッドに詳现を曞いおおきたす。
むンストヌルしようずしおいるのはこのパッケヌゞです
https://github.com/myuon/widx/tree/feature/widget-modules

以䞋により詳现なログを貌りたす
/home/ioijoi/.stack/programs/x86_64-linux/ghc-8.2.1/bin/ghc returned
ExitFailure 1 with error message:
Failed to load interface for ‘Data.Hashable.Generic’
no unit id matching ‘hashable-1.2.6.1-14fEJP30YhAG9w115PODz0’ was found
CallStack (from HasCallStack):
  die', called at libraries/Cabal/Cabal/Distribution/Simple/Program/Run.hs:171:7 in Cabal-2.0.0.2:Distribution.Simple.Program.Run
  getProgramInvocationOutput, called at libraries/Cabal/Cabal/Distribution/Simple/GHC.hs:1522:11 in Cabal-2.0.0.2:Distribution.Simple.GHC
  libAbiHash, called at libraries/Cabal/Cabal/Distribution/Simple/Register.hs:260:30 in Cabal-2.0.0.2:Distribution.Simple.Register
  abiHash, called at libraries/Cabal/Cabal/Distribution/Simple/Register.hs:239:21 in Cabal-2.0.0.2:Distribution.Simple.Register
  generateRegistrationInfo, called at libraries/Cabal/Cabal/Distribution/Simple/Register.hs:135:25 in Cabal-2.0.0.2:Distribution.Simple.Register
  generateOne, called at libraries/Cabal/Cabal/Distribution/Simple/Register.hs:121:28 in Cabal-2.0.0.2:Distribution.Simple.Register
  register, called at libraries/Cabal/Cabal/Distribution/Simple.hs:770:10 in Cabal-2.0.0.2:Distribution.Simple
  defaultRegHook, called at libraries/Cabal/Cabal/Distribution/Simple.hs:584:20 in Cabal-2.0.0.2:Distribution.Simple
  regHook, called at libraries/Cabal/Cabal/Distribution/Simple/UserHooks.hs:120:5 in Cabal-2.0.0.2:Distribution.Simple.UserHooks
  cmd_hook, called at libraries/Cabal/Cabal/Distribution/Simple.hs:452:38 in Cabal-2.0.0.2:Distribution.Simple
  cmd_hook, called at libraries/Cabal/Cabal/Distribution/Simple.hs:469:4 in Cabal-2.0.0.2:Distribution.Simple
  hookedActionWithArgs, called at libraries/Cabal/Cabal/Distribution/Simple.hs:451:5 in Cabal-2.0.0.2:Distribution.Simple
  hookedAction, called at libraries/Cabal/Cabal/Distribution/Simple.hs:430:5 in Cabal-2.0.0.2:Distribution.Simple
  registerAction, called at libraries/Cabal/Cabal/Distribution/Simple.hs:184:50 in Cabal-2.0.0.2:Distribution.Simple
  defaultMainHelper, called at libraries/Cabal/Cabal/Distribution/Simple.hs:124:19 in Cabal-2.0.0.2:Distribution.Simple
  defaultMainArgs, called at Main.hs:1210:18 in main:Main
cabal: '/home/ioijoi/.stack/programs/x86_64-linux/ghc-8.2.1/bin/ghc' exited
with an error:
Failed to load interface for ‘Data.Hashable.Generic’
no unit id matching ‘hashable-1.2.6.1-14fEJP30YhAG9w115PODz0’ was found
cabal: Leaving directory '/home/ioijoi/program/widx'
CallStack (from HasCallStack):
  die', called at ./Distribution/Client/Install.hs:995:15 in main:Distribution.Client.Install
cabal: Error: some packages failed to install:
widx-0.1.0.0-ENE00C4n2nTCnkzTC7mfaY failed during the final install step. The
exception was:
ExitFailure 1
ちなみに、このパッケヌゞ自䜓はhashableには䟝存しおいたせんがすでにhashable-1.2.6.1がむンストヌルされおいたす
詊しおみたんですけども、僕の環境では widx を䟝存関係に远加したプロゞェクトで普通に䜿えたした。

$ stack --version
Version 1.6.0, Git revision 0e756d486a5a4d6fe03e0d448bb8e706c5834ca1 (5293 commits) x86_64 hpack-0.18.1

$ cat stack.yaml
resolver: lts-9.10
packages:
- .
extra-deps:
- git: 
  commit: master
- colors-0.3.0.2

$ cat package.yaml
name:                test-widx
version:             0.1.0.0
#synopsis:
#description:
homepage:            
license:             BSD3
author:              Author name here
maintainer:          
copyright:           2017 Author name here
category:            Web
extra-source-files:
- README.md

dependencies:
  - base >= 4.7 && < 5
  - widx

executables:
  test-widx:
    source-dirs:      src
    main:             Main.hs


.stack-work を削陀しお再床実行しおみおも同じですか
ブランチはmasterじゃなくおfeature/widget-modulesなのでは
extra-deps:
- git: 
  commit: feature/widget-modules
- colors-0.3.0.2

かず。
ブランチはmasterじゃなくおfeature/widget-modulesなのでは
確かにそうですね倉曎しおも問題ありたせんでした。
わかりにくくおすみたせん、featureブランチの方です
あず、このパッケヌゞはbackpackを䜿っおいるのでghc-8.2, cabal-2以䞊でないずビルドできたせん。stackはcabal2系に察応しおいないので、私はcabal-install-2.0.0.2ずghc-8.2をstackで入れおパスなどを通したものを䜿っおビルドしおいたす。
こういうSExpr型に被せたCallowSExpr型があっお、

PatternSynonymsでCons''パタヌンをCons'パタヌンのように
曞きたいんですが、いたいちPatternSynonymsがわかりたせん 。

実際に通るコヌド

haskell
{-# LANGUAGE PatternSynonyms #-}

data SExpr = Cons SExpr SExpr | AtomInt Int

newtype CallowSExpr = CallowSExpr { growup :: SExpr }

pattern AtomInt' :: Int -> CallowSExpr
pattern AtomInt' x = CallowSExpr (AtomInt x)

pattern Cons' :: SExpr -> SExpr -> CallowSExpr
pattern Cons' x y = CallowSExpr (Cons x y)

main :: IO ()
main = do
  let x = Cons' (AtomInt 1) (AtomInt 2)
  return ()


通したい远加コヌド

haskell
pattern Cons'' :: CallowSExpr -> CallowSExpr -> CallowSExpr
--pattern Cons'' x y <- (growup {-   -})

y :: CallowSExpr
y = Cons'' (AtomInt' 1) (AtomInt' 2)


愚盎にパタヌンを組み立おようずするず、垰玍的なのでこんなになっおしたいそう。

haskell
pattern Cons'' (AtomInt' x) (AtomInt' y) = CallowSExpr (Cons x y)
pattern Cons'' (CallowSExpr (Cons (AtomInt x1) (AtomInt x2))) (AtomInt' y) = CallowSExpr (Cons (Cons (AtomInt x) (AtomInt y)) y)
{-... 以䞋無限に続くず思う -}


Cons''パタヌンを郚分関数的にせずに、曞く方法っおないですか
そういえば、cabal sandboxを䜿っおるんですか
それずもnew-buildを䜿っおるんですか
それずも普通のcabal buildですか
「むンストヌル」ずいうのはビルドしおからadd-sourceしおcabal installした堎合を指しおたす
など、再珟手順がむマむチ䞍明瞭なので教えおいただきたく。
もっずも、私からぱっずできるアドバむスは、関連するファむル、 .cabal-sandbox ずかを䞀旊削陀しおやり盎せ、ぐらいしか思い浮かばないので、cabalにバグ報告した方がいいんじゃないかず思っおいたす。
この蟺関係ありそうな感じもしたすけど、どうかな。
https://ghc.haskell.org/trac/ghc/ticket/14304
https://github.com/haskell/cabal/issues/4755
@igrep 再珟手順に぀いおはこれでいかがでしょうか(git cloneした盎埌からのログです)
https://gist.github.com/myuon/e49d89b6504cb645cb1d0a53d39e4b77
@ 私もTH関係かなず思ったんですが、今のコヌドからTHを取り陀いおも(makeLenses関係を手曞きしおも)゚ラヌメッセヌゞは倉わりたせんでした
倚分なんかのバグを螏んだのだろうずいう気はするんですが、ghcのバグなのかcabalのバグなのか䟝存しおるパッケヌゞに関連したバグなのかも䞍明なんですがそんな状態でcabalに報告しおもいいんでしょうか
ずりあえずもう少し調査を進め぀぀minimalな䟋を頑匵っお䜜っおみようずは思っおいたす
うヌん、確かに。MLで根回しするずころからですかね。。。
ずりあえず https://github.com/haskell/cabal#communicating に曞いおあったのですが、
https://mail.haskell.org/mailman/listinfo/cabal-devel に投げおしたうのはありじゃないかず思いたす。
ありがずうございたす。
゚スケヌピングが䞍芁なのず、Text.Printf.printf で事足りるこずを思い出したので、printfを䜿いたした