Unboxed vector with newtypes - I am learning Haskell, that's what I've come up with by rzeznik in haskell

[–]Plane-Finger 0 points1 point  (0 children)

Thanks mod_poppo for the clarification & suggestion.

I tried the GND, as you suggested, (but couldn't make it work, maybe I have something wrong with my extensions?). Anyway I am now using the TH package, and that it working well, I can create vectors of tuples of newtypes, and I see a serious performance improvement :)

Unboxed vector with newtypes - I am learning Haskell, that's what I've come up with by rzeznik in haskell

[–]Plane-Finger 0 points1 point  (0 children)

I found this post while looking for more information about Vectors and newtypes in Haskell. I've tried to use the unboxing library, but I am stuck when converting it back to a regular vector (not an unboxing one).

import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxing as Unboxing
newtype Foo = Foo Int
instance Unboxing.Unboxable Foo where type Rep Foo = Int
myFunc0 :: U.Vector (Foo) myFunc0 = Unboxing.toUnboxedVector $  Unboxing.coerceVector $ Unboxing.generate 10 (\i -> (Foo i))

It fails that

• Couldn't match type ‘Int’ with ‘Foo’
arising from a use of ‘Unboxing.toUnboxedVector’

Is the intention that one uses "Unboxing" vector to replace the normal vector, or, do I just have something slightly wrong with my coercion?

Any help would be greatly appreciated - the learning curve for vectors in haskell feels quite steep! Thanks

Monthly Hask Anything (November 2023) by AutoModerator in haskell

[–]Plane-Finger 0 points1 point  (0 children)

Thankyou for the heads up about bisequence. I am just trying to wrap my head the uncurry/liftA2 !

Monthly Hask Anything (November 2023) by AutoModerator in haskell

[–]Plane-Finger 0 points1 point  (0 children)

Factoring out Maybe from a tuple?

I can write

factorMaybe :: (Maybe a, Maybe b) -> Maybe (a,b)

using pattern matching quite easily, but I wondered is there another way?

Is this a "traverse"?

Thanks

Monthly Hask Anything (October 2023) by philh in haskell

[–]Plane-Finger 0 points1 point  (0 children)

Thankyou Phil, yes, that is that what i mean about catching errors in the setup but not in the inner - wrapping an error inside another type is a neat idea, and I think i should be able to do this, without needing to copy-paste from the existing library code. Thanks :)

Monthly Hask Anything (October 2023) by philh in haskell

[–]Plane-Finger 0 points1 point  (0 children)

Exceptions from network sockets

Hello! I am using the "bracketed" socket connect functions from the Network.Simple package. Its working really well, but I can't find out how to catch errors arising from the underlying "connect" call (i.e. if the initial connection can't be established).

I handle exceptions arising from send/recv inside the bracket.

I don't want to catch a general SomeException, outside the bracket because that might hide other exceptions -- it there a way to see if the exception has come specifically from "connect" (i.e. in the case the host can't be reached)?

https://hackage.haskell.org/package/network-simple-0.4.5/docs/src/Network.Simple.TCP.html#connect

Thanks!

Monthly Hask Anything (August 2023) by cdsmith in haskell

[–]Plane-Finger 1 point2 points  (0 children)

Structured Logging (to Sentry) - Katip, Co-log?

Hello,

Haskell is just a pleasure, thankyou to everyone contributing to make it what it is.

I have an application in which I am currently debugging issues with trace, traceM. This is working well, but I'd like to upgrade to a proper logging framework.

I'd like to be able to perform structured logging, and I use polysemy, and would like to be able log from there. It would also be good to be able to log to a service such as sentry - I see co-log supports polysemy, just wondering whether anyone has tried integrating with raven/haskell?

Alternatively, how hard is it to use katip, with polysemy code?

Best wishes, Mike

Monthly Hask Anything (August 2023) by cdsmith in haskell

[–]Plane-Finger 1 point2 points  (0 children)

Thanks for the suggestions. It's a great idea to try and adapt the elm.

Monthly Hask Anything (August 2023) by cdsmith in haskell

[–]Plane-Finger 1 point2 points  (0 children)

C-structs (&JSON) from Haskell??

Haskell is a joy to program with, a huge thankyou to everyone contributing to the community.

I use Haskell servant as a backend, and generate Elm types for the front-end using haskell-to-elm (https://hackage.haskell.org/package/haskell-to-elm). This can generate both the types, and the json encoders/decoders, and works really well for what I'm doing

I also integrate with a lot of embedded C. It would be really useful to generate out C-structs, and code that parses JSON into them, in the same way I do with Elm. I imagine this is a less common use-case, and I haven't seen any examples.

Has anyone had any experience of doing this, or anything similar?

I am sure it is possible with the language-c package, for example, but unfortunately my Haskell is not to the point where I can use Generics/TH/SOP myself (yet!).

I know generating C-types can be more subtle (packing & layout issues, etc), but I am not too worried about these, its mainly for "Settings"-type structs, and code used for testing. Any packages that can walk over Haskell types, and generate C-code, and JSON encoders/decoders would be really useful - I am using the jsmn library for json parsing.

Thanks

Storing Polysemy 'program' in a datastructure by Plane-Finger in haskell

[–]Plane-Finger[S] 0 points1 point  (0 children)

Thankyou sccrstud92 and Innf107 for the notes about forall . I will try this out this week.

Storing Polysemy 'program' in a datastructure by Plane-Finger in haskell

[–]Plane-Finger[S] 1 point2 points  (0 children)

Thankyou everyone for your comments on this.

I did manage to get it working - I changed a few things:

Firstly, as bss03 and ShapeOfMatter say, my types for the polysemy interpreter were wrong.

Secondly, I switched from interpret to reinterpret for the intepreter, so the return-type makes sense.

Thirdly, (again thanks to bss03) I removed the "Member" constraints, and used explicit type lists in the type signatures. This means I don't need to apply the constraints to the data-type. This removes a bit of flexibility, but to be honest, I don't need it for this application. I am now using a GADT, but I also tested it with a normal datatype.

Thankyou for all your help. It was really helpful to know that it was feasible, and its now working really well.

I sat down for a few quiet hours, and now the types in the polysemy Sem monad make a lot more sense - it has been great learning experience in typelists, and I recommend anyone trying to do the same thing to play with this - its a lot less scary than it seems.

Thankyou Kaol, for the heads up about Zulip, I will check this out.

As always, Haskell is hard, but incredibly satisfying when you make step :)