How do I combine lenses which contain context? - haskell

I'm starting from authorityL via authorityHostL to hostBSL - I know you can combine lenses via
(authorityL . authorityHostL . hostBSL)
but that fails with Couldn't match type ‘Authority’ with ‘Maybe Authority’. How do I properly deal with the Maybe here?

You can add a _Just in between to only focus in on the successful results.
(authorityL . _Just . authorityHostL . hostBSL)
does the trick like it's said in the comments.

Related

Is there identity conduit?

The title says it all. I've seen that some people apparently use Data.Conduit.List.map id as identity conduit, but is this the recommended way to stream data unchanged?
The simplest way to write an identity conduit is probably:
awaitForever yield
because this doesn't require an extra import.
The definition of Data.Conduit.List.map is very similar:
mapC f = awaitForever $ yield . f
(The difference between mapC and map has something to do with CPP macros to define fusion.)
When optimization is on (-O1), it appears both options result in identical code, so it's just a matter of taste.

Just and dot in winghci

Why does this work...
Just.(+3) $ 6.7
Just $ truncate 8.9
...but not this?
Just.truncate $ 8.9
I tried resolving truncate to a simple Double -> Int:
let f :: Double -> Int; f = (\ x -> truncate x);
...but that doesn't appear to be the problem...
Just.f $ 5.6
<interactive>:41:1:
Failed to load interface for `Just'
Use -v to see a list of the files searched for.
Many thanks!
When you mean to compose functions, it's better to write f . g than f.g. It's a little more readable, and you avoid a bunch of problems like this one.
When you have something of the form Foo.bar or Foo.Bar in Haskell, it is parsed as a qualified name. That's why Just.f doesn't work: Just isn't a module, so the 'interface' for Just can't be loaded.
Why Just.(+3) does work as intended: (+3) is a right section, not an identifier, so the dot can't be part of a qualified name. The only way to interpret it is to assume that . is an infix application of the operator (.), so it must be Just . (+3).
A dot between a capitalized identifier and another identifier is parsed as a qualified name (eg. Data.Map.insert), so the error is telling you that it couldn't find a module named Just. You can simply add spaces around the dot to fix this.

Printing Text.Pandoc.writers into ghci : No Show instance

What am I trying to do ?
Print under ghci the association list of formats and writers.
See doc :
writers :: [(String, Writer)]
Association list of formats and writers.
What has been tried
zurgl>>>import Text.Pandoc as P
zurgl>>>P.writers
<interactive>:20:1:
No instance for (Show (WriterOptions -> Pandoc -> [Char]))
arising from a use of `print'
Possible fix:
add an instance declaration for
(Show (WriterOptions -> Pandoc -> [Char]))
In a stmt of an interactive GHCi command: print it
I expected the corresponding show instance to be imported automatically, but it seems that's not the case. And I must admit I don't have any clue how to define an instance declaration for (Show (WriterOptions -> Pandoc -> [Char]). As a workaround, I've tried to import additional module of the Pandoc library, but still no Show instance available.
Then Should I define this instance by myself ?
If yes, have you any tips to share with me to complete this task.
If I shouldn't what's the issue ?
Thanks in advance for your help.
EDIT
Ok, I guess I saw my missunderstanding :
Doing :
zurgl>>>map (\x-> fst x) P.writers
["native","json","html","html5","html+lhs","html5+lhs","s5","slidy","slideous","dzslides","docbook","opendocument","latex","latex+lhs","beamer","beamer+lhs","context","texinfo","man","markdown","markdown+lhs","plain","rst","rst+lhs","mediawiki","textile","rtf","org","asciidoc"]
I think it make no sens to try to Show the second stuff in my tuples. It souhld be something like a function then we can't show it.
I guess it should be the problem.
What I tried to do make no sense as the tuple contain two different type.
The first one being an identifier (of type string) for a specific writer, the second one being the writer itself (then a function). For sure, if I try to print all of them it will fail as there is no Show instance for function.
Then to retrieve the list of available writer in Pandoc (with the aims to call the corresponding function dynamically), we just have to retrieve the list of identifier, as :
zurgl>>>map fst P.writers
["native","json","html","html5","html+lhs","html5+lhs","s5","slidy","slideous","dzslides","docbook","opendocument","latex","latex+lhs","beamer","beamer+lhs","context","texinfo","man","markdown","markdown+lhs","plain","rst","rst+lhs","mediawiki","textile","rtf","org","asciidoc"]

How do I specify language literals in hsparql?

If the raw SPARQL is rdfs:label "D (programming language)"#en, what would the hsparql syntax be?
I'm the maintainer of the hsparql DSL that you are having trouble with. A `simpleSelectWithLiteral' function has been added as an example:
https://github.com/robstewart57/hsparql/blob/master/tests/DBPedia.hs#L51
Does this solve your problem?
Looking at the DSL definition (line 251) on the HSparql Github page it looks like the following DSL rule applies:
instance TermLike ([Char], [Char]) where
varOrTerm (s, lang') = Term . RDFLiteralTerm $ RDFLiteralLang s lang'
It's been far too long since I wrote any Haskell so I'm not 100% certain what that translates to into a term but I assume it means use a tuple of two strings:
("value", "en")

Error with functions inside tuples in Haskell

i'm playing a little with Haskell and i'm stuck with this error, using the snd function with a tuple (String, list).
snd ("Felix Felices",[("Escarabajos Machacados",52,[f1,f2]),("Ojo de Tigre Sucio",2,[f2])])
ERROR - Cannot find "show" function for:
*** Expression : snd ("Felix Felices",[("Escarabajos Machacados",52,[f1,f2]),("Ojo de Tigre Sucio",2,[f2])])
*** Of type : [([Char],Integer,[(Integer,Integer,Integer) -> (Integer,Integer,Integer)])]
The thing is, if i remove f1, f2 and f3 (they are functions) the code works just fine, it returns the list. Why is this happening, why can't i just put a function inside the tuple's second position?
You can put functions into tuples. But you can't display them - ghci wants to show the result and print it. How should it show (convert to string) the functions? It can't, or at least nobody felt like choosing one way of doing it (which would propably be flawed anyway - at least I can't think of any approach that doesn't have holes to huge even I can see them). Therefore, you can't evaluate something that returns functions or collections of functions in ghci.
The problem isn't really the functions f1 f2 or f3, the problem is that you are trying to print these functions, but functions don't have an instance of Show, so they cannot be printed. However if you try:
Prelude> snd ("Felix Felices",[("Escarabajos Machacados",52,["f1","f2"]),("Ojo de Tigre Sucio",2,["f2"])])
you get the result:
[("Escarabajos Machacados",52,["f1","f2"]),("Ojo de Tigre Sucio",2,["f2"])]
So the problem isn't that you cannot have a function in a tuple, the problem is that you cannot convert functions to strings so they can be printed.
module Text.Show.Functions provides an instance Show (a -> b).
ghci> :m +Text.Show.Functions
ghci> [(*), (/)]
[<function>,<function>]
It's not useful for actually figuring out what the functions are, but there's no good way to do that anyway (well, the debugger and vacuum aside). But if you just want some Show instances for convenience, this is in the standard library.
You cannot print bare functions in Haskell as there is no "show" function defined for them.
You get the same type of error, if you type
Hugs> sqrt
for example
The system tells the type of the expression, which in your case is ([Char],Integer,[(Integer,Integer,Integer) -> (Integer,Integer,Integer)])], but cannot print it, because it is a function.

Resources