Tools for generating Haskell types in Haskell ("second-order Haskell")?

Sorry in advance if this question is a bit vague. This is the result of some days off.

Thanks to a wonderful system like Haskell, it delightfully expresses the mathematical (especially algebraic) structure as cool classes. I mean, just take a look at numeric-prelude ! But using such a beautiful structure in practice has always seemed difficult to me.

You have a good, type of system, way of expressing that v1both v2are elements of a vector space Vand that are wan element of a vector space w. The type system allows you to write a program that adds v1and v2, but v1and w. Big! But in practice, you can play with potentially hundreds of vector spaces, and you certainly do not want to create types v1, v2..., V100and declare them copies of the vector space typeclass! Or maybe you read some data from the real world, as a result of which symbols appeared a, bandc - you can express that the free vector space above these symbols is really a vector space!

So you're stuck, right? To do many of the things you would like to do with vector spaces in a scientific computational parameter, you must abandon your type system by specifying a typeclass vector space and having functions to perform compatibility checks at run time. Do you have to? Is it possible to use the fact that Haskell is purely functional for writing a program that generates all the types you need and inserts them into a real program? Is there such a technique? By all means indicate if I just miss something basic here (I probably) :-)

Edit: I just discovered fundeps . I will have to think a little about how they relate to my question (commendable comments on this are appreciated).

+3
2

Haskell . wiki ; .

- , . :

mkFoo = [d| data Foo = Foo Int |]

Haskell (, ), data Foo = Foo Int, $(mkFoo).

, mkFoo , . $(mkFoo 100) 100 . TH . adaptive-tuple - , Template Haskell - .

Derive, . , .

+8

Haskell . :

-- A family of types for the natural numbers
data Zero
data Succ n

-- A family of vectors parameterized over the naturals (using GADTs extension)
data Vector :: * -> * -> * where
    -- empty is a vector with length zero
    Empty :: Vector Zero a
    -- given a vector of length n and an a, produce a vector of length n+1
    Cons  :: a -> Vector n a -> Vector (Succ n) a

-- A type-level adder for natural numbers (using TypeFamilies extension)
type family Plus n m :: *
type instance Plus Zero n = n
type instance Plus (Succ m) n = Succ (Plus m n)

-- Typesafe concatenation of vectors:
concatV :: Vector n a -> Vector m a -> Vector (Plus n m) a
concatV Empty ys = ys
concatV (Cons x xs) ys = Cons x (concatV xs ys)

, . , , .

Haskell - , , . , Agda, Coq, Epigram .

Haskell LISP -macro. , "ok insert, ". , , concatV .

, , . , - , . .: -)

+5

Source: https://habr.com/ru/post/1772435/


All Articles