Re: [racket-users] Moving a Rust/Haskell abstract algebra library to Racket?

2021-01-31 Thread Stuart Hungerford
On Sunday, 31 January 2021 at 08:07:27 UTC+11 Jens Axel Søgaard wrote:

Den tor. 21. jan. 2021 kl. 05.06 skrev Stuart Hungerford <
> stuart.h...@gmail.com>:
>
>> [...]
>> By using the Rust trait system (and later Haskell typeclasses) I could 
>> create structure traits/typeclasses that don't clash with the builtin 
>> numeric types or with the larger more production oriented libraries in 
>> those languages in the same general area of math.
>>
>> Once I added generative testing of the structure axioms I could 
>> experiment with, e.g. finite fields and ensure all the relevant axioms and 
>> laws were (at least probabilistically) met.
>>
>
> Not knowing Rust nor traits, I have amused myself writing a very simple 
> version of traits.
>  
>
> #lang racket
> (require (for-syntax syntax/parse racket/syntax))
>
> ;;;
> ;;; TRAITS
> ;;; 
>

Many thanks Jens.  This is an excellent example of Racket's 
build-your-own-abstractions philosophy at work.  (Although where I live I 
would have used bream or sharks instead of herring ;-)

If Hackett was still maintained I would probably use it too.

Thanks,

Stu




-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/4bd7fad3-0c68-46c0-9504-530b3d4e3ab4n%40googlegroups.com.


[racket-users] Typed racket and generics?

2021-01-30 Thread Stuart Hungerford
Hi Racketeers,

Is there any way to have Racket code using `define-generics` interact with 
typed Racket code?  (I think the answer is "no", but I thought I'd check 
for sure).

Thanks,

Stu

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/333b7744-85e8-4745-aad2-77272d1e20c5n%40googlegroups.com.


Re: [racket-users] Moving a Rust/Haskell abstract algebra library to Racket?

2021-01-24 Thread Stuart Hungerford
On Mon, Jan 25, 2021 at 6:52 AM Jens Axel Søgaard 
wrote:

That's a very interesting project. You are so to speak optimizing for
> readability.
> I immediately get a vision of a SICM-like book, but for algebra instead of
> classical mechanics.
>
> Racket will be a good choice, since macros give you the possibility
> of experimenting with suitable, easily understood syntax.
>
> A tricky choice is to be made: how are the concepts going to be
> represented
> as Racket values. Normal structs does not allow multiple inheritance.
>
> Looking at a diagram such as the one below, raises the question whether
> the
> relationship between the various concepts are to be modelled explicitly or
> implicitly.
>
> [image: image.png]
>
> Maybe some kind of interface for each concept is needed?
>

Thanks for pointing those relationships out -- in the earlier Rust and
Haskell libraries I used a trait or typeclass for each concept/structure. I
decided to make the interfaces simpler but more repetitive by having (for
example) separate traits/typeclasses for Additive, Multiplicative and
"abstract" groups. That allowed (for example) the real numbers to form
groups under both addition and multiplication.

I'm hoping I can do something similar with Racket generic interfaces, as
the other people in this thread have kindly pointed out to me in their
packages and examples.


> Link to SICM in case you haven't seen it already.
>
>
> https://mitpress.mit.edu/books/structure-and-interpretation-classical-mechanics
>
> Note that the authors of SICM wrote a CAS in Scheme that is used in the
> book.
>

One of the benefits of reading through this (with a non-physics background)
is the chance to re-examine the use of notation in mathematics and how it
can transfer to a computing environment.

Thanks,

Stu

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAG%2BkMrEZpC2WCCKfa8pL5Jvw-rLRyCs7-053S84N1rJAyBKUGA%40mail.gmail.com.


Re: [racket-users] Moving a Rust/Haskell abstract algebra library to Racket?

2021-01-21 Thread Stuart Hungerford
On Fri, Jan 22, 2021 at 12:56 AM Hendrik Boom  wrote:

> [...]
>
> You might also want to look at the implementations of category theory in Agda.
> Agda is a language which unifies execution and correctness proof to some
> extent.
>
> Not that you want to implement catagory theory, but category theory is a
> form of algebra, and you may find some useful ideas about syntax and
> semantics there.
>
> I attended an online seminar about this recently, but I haven't been
> able to find the details again.  The following links seem to refer to the
> same project.  (I found them looking for
> category theory in agda

Thanks Hendrik -- I had forgotten that Agda (and Idris, Coq, Lean) are
also good sources of inspiration in this general area.

Stu

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAG%2BkMrGTrbQuoFuYq%2BGWQKch0VZukQyuc6ngR0jhAwsxvidXVw%40mail.gmail.com.


Re: [racket-users] Moving a Rust/Haskell abstract algebra library to Racket?

2021-01-20 Thread Stuart Hungerford
On Thursday, 21 January 2021 at 14:49:19 UTC+11 Siddhartha Kasivajhula 
wrote:

Depending on what you're trying to accomplish, you may find the 
> relation/composition 
>  module 
> (which I authored) to be of interest. It doesn't model algebraic structures 
> explicitly but uses them to generalize common composition operators like + 
> (as a group), and .. (as a monoid). It also provides derived functions like 
> power 
> 
> .
>
> If this is the kind of thing you're trying to do, also take a look at generic 
> interfaces  
> (used 
> in the above library) which resemble typeclasses, and which could be used 
> to define monoids, groups, and so on as interfaces, which particular Racket 
> types could implement.
>
> There's also the Algebraic Racket 
>  library which I 
> believe uses classes and objects rather than generic interfaces, and has a 
> lot of other algebraic goodies as far as I can tell.
>

Thanks so much for pointing these out, there's a lot of inspiration there 
to draw from.

Stu

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/f64b0183-2669-4580-bd75-6cb998260790n%40googlegroups.com.


Re: [racket-users] Moving a Rust/Haskell abstract algebra library to Racket?

2021-01-20 Thread Stuart Hungerford
On Thursday, 21 January 2021 at 10:22:45 UTC+11 Jens Axel Søgaard wrote:

Den ons. 20. jan. 2021 kl. 08.43 skrev Stuart Hungerford <
> stuart.h...@gmail.com>:
>
>> On Wednesday, 20 January 2021 at 12:34:59 UTC+11 Robby Findler wrote:
>>
>> I'm no expert on algebras, but I think the way to work on this is not to 
>>> think "what Racket constructs are close that I might coopt to express what 
>>> I want?" but instead to think "what do I want my programs to look like" and 
>>> then design the language from there, reusing libraries as they seem helpful 
>>> or designing new ones that do what you want. Racket's 
>>> language-implementation facilities are pretty powerful (of course, if there 
>>> is nothing like what you end up needing, there will still be actual 
>>> programming to do ;).
>>>
>>
>> Thanks Robby -- that's a very interesting way to look at library design 
>> that seems to make particular sense in the Racket environment.
>>
>
> An example of such an approach is racket-cas, a simple general purpose 
> cas, which
> represents expressions as s-expression. 
>
> The polynomial 4x^2 + 3 is represented as '(+ 3 (* 4 (expt x 2))) 
> internally.
>
> The expressions are manipulated through pattern matching. Instead of
> using the standard `match`, I wanted my own version `math-match`.
> The idea is that `math-match` introduces the following conventions in 
> patterns:
>

This is also fascinating (and very useful) -- thanks.  This package 
illustrates the build-your-own-notation approach nicely.
[...]

> Back to your project - what is the goal of the project?
> Making something like GAP perhaps?
> Do you want your users to supply types - or do you want to go a more 
dynamic route?

My project is really aimed at supporting self-directed learning of concepts 
from abstract algebra. I was taught many years ago that to really 
understand something to try implementing it in a high level language. That 
will soon expose any hidden assumptions or misunderstandings.

An early attempt (in Rust) is at: https://gitlab.com/ornamentist/un-algebra

By using the Rust trait system (and later Haskell typeclasses) I could 
create structure traits/typeclasses that don't clash with the builtin 
numeric types or with the larger more production oriented libraries in 
those languages in the same general area of math.

Once I added generative testing of the structure axioms I could experiment 
with, e.g. finite fields and ensure all the relevant axioms and laws were 
(at least probabilistically) met.

Thanks again Jens.


Stu


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/b14e56eb-71ef-49f4-8e98-fea4ced6e3adn%40googlegroups.com.


Re: [racket-users] Moving a Rust/Haskell abstract algebra library to Racket?

2021-01-19 Thread Stuart Hungerford
On Wednesday, 20 January 2021 at 12:34:59 UTC+11 Robby Findler wrote:

I'm no expert on algebras, but I think the way to work on this is not to 
> think "what Racket constructs are close that I might coopt to express what 
> I want?" but instead to think "what do I want my programs to look like" and 
> then design the language from there, reusing libraries as they seem helpful 
> or designing new ones that do what you want. Racket's 
> language-implementation facilities are pretty powerful (of course, if there 
> is nothing like what you end up needing, there will still be actual 
> programming to do ;).
>

Thanks Robby -- that's a very interesting way to look at library design 
that seems to make particular sense in the Racket environment.

Stu

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/b56bd80d-dbc3-4bda-9aa2-07e2545a9ca7n%40googlegroups.com.


[racket-users] Moving a Rust/Haskell abstract algebra library to Racket?

2021-01-19 Thread Stuart Hungerford
Hi Racketeers,

I'd like to try re-implementing a library for experimenting with abstract 
algebraic structures in Racket (that is groups, fields, rings etc, not 
algebraic data types like sum or product types). With Racket's strong 
numeric hierarchy and programmable programming language philosophy it seems 
like a good environment for experimenting with mathematical structures.

This library was originally developed in Rust and more recently Haskell and 
made heavy use of Rust traits and Haskell typeclasses to implement these 
structures for the builtin numeric types as a well as my own derived 
numeric types (e.g. finite fields). I understand Racket is not Haskell (or 
Rust) and naively trying to emulate typeclasses or traits in Racket will 
likely lead to un-idiomatic code. Having said that, what Racket idioms 
would be most useful for this kind of project?

A typical typeclass (from the Haskell version) would be:

```
class Monoid a => Group a where

  inverse :: a -> a

  power :: Int -> a -> a
```

Thanks,

Stu

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/a1ce47b2-9dab-4b33-b50b-557bfef7e331n%40googlegroups.com.


[racket-users] Interaction of Typed Racket with define/match?

2017-11-13 Thread Stuart Hungerford
Hi,

I'm trying to create a Typed Racket function that compares to 2-vectors of 
integers with an optional error term that defaults to 1:

#lang typed/racket


(define-type Reading (Vector Integer Integer))


(: close? (->* (Reading Reading) (Integer) Boolean))
(define/match (close? r1 r2 [err 1])
  [((vector a b) (vector c d) e)
(and (= a (+ c e)) (= b (+ d e)))])


In untyped Racket the function compiles and runs as I expected. In Typed 
Racket the pattern-matched values all seem to have type "Any". 

I think there's some interaction between Typed Racket's typing rules and 
the define/match macro that I haven't understood properly? As a complete 
Racket newb I may also be going about this the wrong way.  Any advice much 
appreciated.

Thanks,

Stu

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Idiomatic Racket approach for handling collisions in library provides?

2017-11-08 Thread Stuart Hungerford
Hi,

A newbie question about idiomatic Racket code: 

Suppose I wanted to create several numeric modules that all had their own 
interpretation of what addition means.  If each of them uses a (provide ... 
+ ...) form, the various "+" exports are going to collide in client modules 
that (require ...) more than one of them.

Is it idiomatic Racket usage to prefix the individual "+" functions inside 
each module like the built-in floating point  modules do (e.g. "fl+") or to 
assume client modules will use something like (require (rename-in ...)...) 
or (require (prefix-in ...) ...) to handle any collisions themselves?

If anyone can point me to example Racket code where this issue has been 
idiomatically handled that would be very useful.

This is also a good opportunity to say how impressive the level of 
documentation and support is in the Racket community. Thanks!

Stu


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Typed racket equivalent of Haskell newtype?

2015-05-24 Thread Stuart Hungerford
On Monday, 25 May 2015 02:43:58 UTC+10, Alex Knauth  wrote:

 Also opaque types might help you:
 http://docs.racket-lang.org/ts-reference/special-forms.html#%28form._%28%28lib._typed-racket%2Fbase-env%2Fprims..rkt%29._require%2Ftyped%29%29
 http://docs.racket-lang.org/ts-reference/type-ref.html#%28form._%28%28lib._typed-racket%2Fbase-env%2Fbase-types-extra..rkt%29._.Opaque%29%29
 
 If you wanted sub-typing too, then refinement types would help, except I 
 don’t think they work yet.
 
 An example of using them for radians and degrees:

Also many thanks for pointing this out.

Stu

-- 
You received this message because you are subscribed to the Google Groups 
Racket Users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Typed racket equivalent of Haskell newtype?

2015-05-23 Thread Stuart Hungerford
Hi,

Is there an idiomatic Typed Racket equivalent to the Haskell concept of 
newtypes?

https://www.haskell.org/tutorial/moretypes.html

That is, some way to have separate types that share a common structure? 

For example I'd like to define Radians and Degrees types that are both 
implemented as Flonums but have Typed Racket raise a type error when a Radians 
value is used where a Degrees value is expected. I can see using single field 
structs might work:

(struct: Radians ([a : Flonum]))

(struct: Degrees ([a : Flonum]))

; type checks
(: d-90 : Degrees)
(define d-90 (Degrees 90.0))

; type mismatch!
(: r-PI : Radians)
(define r-PI (Degrees 180.0))


Is there a better way of working with the Typed Racket idioms to do this kind 
of thing?

Thanks,

Stu

-- 
You received this message because you are subscribed to the Google Groups 
Racket Users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Typed racket equivalent of Haskell newtype?

2015-05-23 Thread Stuart Hungerford
On Sunday, 24 May 2015 12:50:48 UTC+10, Matthias Felleisen  wrote:

 Stuart, would a predicated refinement type work for you: 
 
 -- 
 http://docs.racket-lang.org/ts-reference/special-forms.html?q=define-predicate#%28form._%28%28lib._typed-racket%2Fbase-env%2Fprims..rkt%29._make-predicate%29%29

Looking into that now--many thanks.

Stu  



-- 
You received this message because you are subscribed to the Google Groups 
Racket Users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Typed racket equivalent of Haskell newtype?

2015-05-23 Thread Stuart Hungerford
On Sunday, 24 May 2015 08:14:30 UTC+10, Alex Knauth  wrote:

 As far as I know, this doesn’t exist in typed racket currently, but I’m 
 wondering:  (I’m thinking of seeing if I can implement this when I have time 
 this summer)
 
 How should sub-typing interact with this?
 Should it be a completely separate type, not related by sub-typing at all, or 
 should the new type be a subtype of the representation type?
 What should happen if values with these types crossed into untyped code? 
 What should happen if an untyped program passes a normal flonum to a function 
 that was supposed to accept Radians?
 Would you want to be able to supply a predicate to check properties of the 
 values of the new type? 

As a Racket newb I'm hugely under-qualified to respond to these issues--I'll 
defer to the more experienced Racket community.

I'll also take the hint that since the wonderful typed racket math library does 
not do this with angles I should avoid it (for now) too.

Stu


-- 
You received this message because you are subscribed to the Google Groups 
Racket Users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Tunable flonum comparison using math library

2015-05-19 Thread Stuart Hungerford
Hi,

I've been trying out Racket for 2D graphics tasks and have come across the  
Racket math library. Firstly just wanted to say a big thank you to the 
developers for such a well thought out and documented library.

I've had problems in the past with floating point comparison, especially around 
catastrophic cancellation issues.  What I need as a result is a tunable 
floating point (Flonum) comparison function.

Following these authors:

http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
https://www.ualberta.ca/~kbeach/phys420_580_2010/docs/ACM-Goldberg.pdf
http://stackoverflow.com/questions/10334688/how-dangerous-is-it-to-compare-floating-point-values/10335601#10335601

I've been testing this approach that provides an epsilon value for two 
flonums near zero and an ULP difference otherwise.

(: close? (- Flonum Flonum Real Real Boolean))

(define (close? x y eps ulps)
  (cond
[(or (nan? x) (nan? y)) #f]
[(= (abs (- x y)) (fl eps) #t]
[(not (= (sgn x) (sgn y))) #f]
[else (= (flulp-error x y) (fl ulps))]))

I'm almost certainly misunderstanding something in those references and not 
making the best use of the facilities the math library has to offer. Has anyone 
else gone down this path with Racket and can share their experiences?

Thanks,

Stu

-- 
You received this message because you are subscribed to the Google Groups 
Racket Users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.