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.


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

2021-01-30 Thread Jens Axel Søgaard
Den tor. 21. jan. 2021 kl. 05.06 skrev Stuart Hungerford <
stuart.hungerf...@gmail.com>:

> 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.
>

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
;;;

; This file contains a minimal implementation of traits.
; Overview:

;(define-trait trait (method ...)
;A trait is defined as list of methods names.

;(implementation trait structure body ...)
;An implementation of a trait for a given structure types,
;associates functions to each of the methods suitable
;for that structure type.
;Within body, the method names can be used.

;(with ([id trait structure expression] ...) . body)
;Similar to (let ([id expression] ...) . body),
;but within the body, one can use id.method
;to call a method.

; Expansion time helpers
(begin-for-syntax
  ; These functions produce new identifiers. The context is taken from stx.
  (define (identifier:structure-method stx s m)
(format-id stx "~a-~a" s m))
  (define (identifier:id.method stx id m)
(format-id #'stx "~a.~a" id m))

; get-methods : identifier -> list-of-identifiers
  (define (get-methods trait)
(syntax-local-value (format-id trait "~a-methods" trait


; SYNTAX  (define-trait Name (name ...))
;   This declares a new trait with the name Name having the methods name


(define-syntax (define-trait stx)
  (syntax-parse stx
[(_define-trait Name (id ...))
   (with-syntax ([trait-methods (format-id #'Name "~a-methods" #'Name)])
 (syntax/loc stx
   (define-syntax trait-methods (list #'id ...]

[(_define-trait Name (super-trait ...) (id ...))
 (displayln (list 'dt stx))
 (define ids(syntax->list #'(id ...)))
 (define supers (syntax->list #'(super-trait ...)))
 (with-syntax ([trait-methods(format-id #'Name "~a-methods"
#'Name)]
   [((super-method ...) ...) (map get-methods supers)])
   (syntax/loc stx
 (define-syntax trait-methods
   (list #'id ...
 #'super-method ... ...]))



; SYNTAX  (implementation trait structure . body)
;   Defines structure-method for each method of the trait.
;   The structure-method  is bound to  method.
;   If method is defined in body, then that binding is used.
;   If method is not bound in body, but bound outside, the outside binding
is used.
;   If method is not bound at all, an error is signaled.
(define-syntax (implementation stx)
  (syntax-parse stx
[(_implementation trait structure body ...)
 (define methods (get-methods #'trait))
 (with-syntax*
   ([(method ...); These short names are used
by the user
 (for/list ([method methods])
   (syntax-local-introduce
(format-id #'stx "~a" method)))]

[(structure-method ...)   ; Used in the output of the
`with` form.
 (for/list ([method methods])
   (identifier:structure-method #'trait #'structure method))])

   (syntax/loc stx
 (define-values (structure-method ...)
   (let ()
 body ...
 (values method ...)]))

(define-syntax (with stx)
  (syntax-parse stx
[(_with ([id trait structure expression] ...) . body)
 (define traits (syntax->list #'(trait ...)))
 (define ids(syntax->list #'(id ...)))
 (define structures (syntax->list #'(structure ...)))
 (define methodss   (map get-methods traits))

 (define (map-methods f id t s ms)
   (for/list ([m ms])
 (f id t s m)))

 (define (map-clauses f)
   (for/list ([id ids] [t traits] [s structures] [ms methodss])
 (map-methods f id t s ms)))

 (with-syntax
   ([((id.method ...) ...); names used inside `with`
 (map-clauses (λ (id t s m)
(syntax-local-introduce
 (identifier:id.method #'stx id m]

[((structure-method ...) ...) ; names used outside `with`
 (map-clauses (λ (id t s m)
  

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-24 Thread Jens Axel Søgaard
Den tor. 21. jan. 2021 kl. 05.06 skrev Stuart Hungerford <
stuart.hungerf...@gmail.com>:

> On Thursday, 21 January 2021 at 10:22:45 UTC+11 Jens Axel Søgaard wrote:
>


> > 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.
>

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?

/Jens Axel

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.

-- 
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/CABefVgzzBkGYb7qPziPGoOGpDZ9QbpO%3DJVyDRrV_DLx5JJFA-w%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-21 Thread Hendrik Boom
On Wed, Jan 20, 2021 at 08:06:45PM -0800, Stuart Hungerford wrote:
> 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.

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
on duckduckgo.

The software:
https://github.com/agda/agda-categories

Documents:
  Formalizing Category Theory in Agda  
pdf: https://arxiv.org/pdf/2005.07059.pdf
slides: https://hustmphrrr.github.io/asset/slides/cpp21.pdf

Agda itself is also worth a look.
As well as older proof assistants like coq.

There's definitely a trand towards constructivism in foundational 
mathamatics.

-- hendrik

> 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.

-- 
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/20210121135543.qi2rth7qkhlasrcc%40topoi.pooq.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-20 Thread Siddhartha Kasivajhula
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.



On Wed, Jan 20, 2021 at 3:22 PM Jens Axel Søgaard 
wrote:

> Den ons. 20. jan. 2021 kl. 08.43 skrev Stuart Hungerford <
> stuart.hungerf...@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:
>
>   prefix  x y z  will match symbols only
>   prefix  r s will match numbers only (not bigfloats)
>   prefix  p qwil match exact naturals only
>   prefix 훼 훽 will match exact numbers
>   prefix bool   will match booleans only
>
>   suffix .0  will match inexact numbers only
>   suffix .bf will match bigfloats only
>
> As an example, here is the part that implements the symbolic natural
> logarithm
> (the assumption is that the argument u is in normalized form):
>
> (define (Ln: u)
>   (math-match u
> [1  0] ; ln(1)=0
> [r. #:when (%positive? r.)  (%ln r.)]  ; here %ln is an ln
> that handles both reals and bigfloats
> [@e  1]; @e is the syntax
> matching Euler's e,  ln(e)=1
> [(Complex a b) #:when (not (equal? 0 b))   ; all complex numbers
> are handled here
>(⊕ (Ln (Sqrt (⊕ (Sqr a) (Sqr b
>   (⊗ @i (Angle (Complex a b]
> [(Expt @e v) v]; ln(e^v) = v
> [(Expt u α) #:when (= (%numerator (abs α)) 1)  ; ln( u^(/n) ) = 1/n
> ln(u)
> (⊗ α (Ln: u))]
> [(⊗ u v)  (⊕ (Ln: u) (Ln: v))] ; ln(u*v) = ln(u) +
> ln(v)
> [_ `(ln ,u)])) ; keep as is
>
> Note that the match pattern (⊗ u v) matches not only products of two
> factors, but general products.
> Matching (⊗ u v)  agains (* 2 x y z) will bind u to 2 and v to (* x y z).
> This convention turned out to be very convenient.
>
> I am happy about many aspects of racket-cas, but I wish I used structs to
> represent the expressions.
> Thanks to the custom matcher, it ought to be possible to change the
> underlying representation
> and still reuse most parts of the code. That's a future project.
>
>
> 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?
>
> /Jens Axel
>
>
>
>
>
>
>
>
>
> --
> 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/CABefVgwbdetq%2BWBa4h6MPLt1XxnhFQSiOACPC46xsx6cVA9imQ%40mail.gmail.com
> 

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

2021-01-20 Thread Jens Axel Søgaard
Den ons. 20. jan. 2021 kl. 08.43 skrev Stuart Hungerford <
stuart.hungerf...@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:

  prefix  x y z  will match symbols only
  prefix  r s will match numbers only (not bigfloats)
  prefix  p qwil match exact naturals only
  prefix 훼 훽 will match exact numbers
  prefix bool   will match booleans only

  suffix .0  will match inexact numbers only
  suffix .bf will match bigfloats only

As an example, here is the part that implements the symbolic natural
logarithm
(the assumption is that the argument u is in normalized form):

(define (Ln: u)
  (math-match u
[1  0] ; ln(1)=0
[r. #:when (%positive? r.)  (%ln r.)]  ; here %ln is an ln that
handles both reals and bigfloats
[@e  1]; @e is the syntax
matching Euler's e,  ln(e)=1
[(Complex a b) #:when (not (equal? 0 b))   ; all complex numbers
are handled here
   (⊕ (Ln (Sqrt (⊕ (Sqr a) (Sqr b
  (⊗ @i (Angle (Complex a b]
[(Expt @e v) v]; ln(e^v) = v
[(Expt u α) #:when (= (%numerator (abs α)) 1)  ; ln( u^(/n) ) = 1/n
ln(u)
(⊗ α (Ln: u))]
[(⊗ u v)  (⊕ (Ln: u) (Ln: v))] ; ln(u*v) = ln(u) + ln(v)
[_ `(ln ,u)])) ; keep as is

Note that the match pattern (⊗ u v) matches not only products of two
factors, but general products.
Matching (⊗ u v)  agains (* 2 x y z) will bind u to 2 and v to (* x y z).
This convention turned out to be very convenient.

I am happy about many aspects of racket-cas, but I wish I used structs to
represent the expressions.
Thanks to the custom matcher, it ought to be possible to change the
underlying representation
and still reuse most parts of the code. That's a future project.


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?

/Jens Axel

-- 
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/CABefVgwbdetq%2BWBa4h6MPLt1XxnhFQSiOACPC46xsx6cVA9imQ%40mail.gmail.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.


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

2021-01-19 Thread Robby Findler
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 ;).

Robby


On Tue, Jan 19, 2021 at 4:58 PM Stuart Hungerford <
stuart.hungerf...@gmail.com> wrote:

> 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
> 
> .
>

-- 
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/CAL3TdOM_pt6EfuXvov8jWb_%3D9u%2BqaKh2UN7bhaaYFYruaQmfJg%40mail.gmail.com.