Browse Source

Lot of stuff, m'kay.

Nicolas Lenz 3 months ago
parent
commit
c6e362d414
8 changed files with 330 additions and 58 deletions
  1. 24
    0
      .gitignore
  2. 186
    0
      example.katrin
  3. 24
    58
      ideas.md
  4. 51
    0
      katrin.cabal
  5. 32
    0
      package.yaml
  6. 3
    0
      src/Katrin.hs
  7. 6
    0
      src/Main.hs
  8. 4
    0
      stack.yaml

+ 24
- 0
.gitignore View File

@@ -0,0 +1,24 @@
1
+# Config file
2
+config.yaml
3
+
4
+# ---> Haskell
5
+dist
6
+dist-*
7
+cabal-dev
8
+*.o
9
+*.hi
10
+*.chi
11
+*.chs.h
12
+*.dyn_o
13
+*.dyn_hi
14
+.hpc
15
+.hsenv
16
+.cabal-sandbox/
17
+cabal.sandbox.config
18
+*.prof
19
+*.aux
20
+*.hp
21
+*.eventlog
22
+.stack-work/
23
+cabal.project.local
24
+.HTF/

+ 186
- 0
example.katrin View File

@@ -0,0 +1,186 @@
1
+// This file contains a wild collection examples of how Katrin code might look.
2
+
3
+// No "module" or "package" specification. Only the file name is used.
4
+
5
+// I might be clinging to Haskell syntax too much. I might need to get more independent.
6
+
7
+Type = Set Universe  // The backend of this needs a lot of thought.
8
+
9
+Maybe _ : Type -> Type
10
+Maybe a = type
11
+    Nothing : Maybe a
12
+    Just : a → Maybe a
13
+
14
+(_,_) : Type → Type → Type  // Underscore syntax shamelssly stolen from Agda. Very flexible.
15
+(a,b) = type
16
+    (_,_) : a -> b -> (a,b)
17
+
18
+ℕ : Type
19
+ℕ = type
20
+    One : ℕ
21
+    Succ : ℕ → ℕ
22
+
23
+ℤ : Type
24
+ℤ = type
25
+    Zero : ℤ
26
+    Plus : ℕ → ℤ
27
+    Minus : ℕ → ℤ
28
+
29
+Coℕ : Type
30
+Coℕ = cotype
31
+    Pred : Coℕ → Maybe Coℕ
32
+
33
+Three : ℕ
34
+Three = Succ (Succ One)
35
+
36
+CoTwo : Coℕ
37
+CoTwo = {Pred = Just {Pred = Nothing}}  // Provisional syntax borrowed from Haskell, I'll need to think of something better.
38
+
39
+
40
+_ coPlus _ : Coℕ → Coℕ → Coℕ  // Defining pure text functions infix should probably give a warning.
41
+x coPlus y = case Pred y
42
+    Just y' -> {Pred = Just x} coPlus y'
43
+
44
+// Lambda syntax ideas
45
+f _ : ℕ → ℕ
46
+f = x ↦ x + 10
47
+f = λx . x + 10
48
+f = \x . x + 10
49
+f = \x -> x + 10
50
+f = \x => x + 10
51
+
52
+List _ : Type → Type
53
+List a = type
54
+    Empty : List a
55
+    Cons : a → List a → List a
56
+
57
+CoList _ : Type → Type
58
+CoList a = cotype
59
+    Split : CoList a → Maybe (a, CoList a)
60
+
61
+OneTwoThree : List Int
62
+OneTwoThree = Cons 1 (Cons 2 (Cons 3 Empty))
63
+
64
+CoOneTwoThree : CoList Int
65
+CoOneTwoThree = Just (1, Just (2, (Just 3, Nothing)))
66
+
67
+Nat : Type
68
+Nat = ℕ
69
+
70
+Pair _ _ : Set → Set → Set
71
+Pair a b = type
72
+    Pair : (first : a) → (second : b) → Pair a b
73
+
74
+
75
+// Rethink this together with the Set/Type stuff
76
+
77
+Person : Set
78
+Person = {Alice, Bob}
79
+
80
+bestPair : Pair Person Person
81
+bestPair = Pair Alice Bob
82
+
83
+test : Set
84
+test = {Kek, Lel, Topkek}
85
+
86
+
87
+// Traits for algebraic structures for calculating.
88
+
89
+trait Magma a
90
+    * : a → a → a
91
+
92
+trait (Magma a) ⇒ Semigroup a
93
+    (x * y) * z == x * (y * z)
94
+
95
+trait (Semigroup a) ⇒ Monoid a
96
+    1 : a
97
+    1 * x == x
98
+    x * 1 == x
99
+
100
+trait (Monoid a) ⇒ Group a
101
+    1/ _ : a → a
102
+
103
+trait (Group a) ⇒ AbelianGroup a
104
+    x * y == y * x
105
+
106
+
107
+// Define all those analogously, but named "Co" and with + instead of *.
108
+// Is there a more elegant way?
109
+
110
+trait CoMagma a
111
+    _ + _ : a → a → a
112
+
113
+trait (CoMagma a) ⇒ CoSemigroup a
114
+    (x + y) + z == x + (y + z)
115
+
116
+trait (CoSemigroup a) ⇒ CoMonoid a
117
+    0 : a
118
+    0 + x == x
119
+    x + 0 == x
120
+
121
+trait (CoMonoid a) ⇒ CoAbelianMonoid a
122
+    x + y == y + x
123
+
124
+trait (CoMonoid a) ⇒ CoGroup a
125
+    - _ : a → a
126
+
127
+trait (CoGroup a, CoAbelianMonoid a) ⇒ CoAbelianGroup a
128
+
129
+trait (Monoid a, CoAbelianMonoid a) ⇒ Semiring a
130
+    x * (y + z) = (x * y) + (x * z)
131
+    (y + z) * x = (y * x) + (z * x)
132
+
133
+trait (Semiring a, CoAbelianGroup a) ⇒ Ring a
134
+
135
+trait (Ring a, AbelianGroup a/0) ⇒ Field a
136
+
137
+// Probably using less mathematical and more concrete names would be a good idea.
138
+
139
+
140
+// Instances for ℕ and ℤ. This should make normale calculations using those types possible.
141
+
142
+instance CoMagma ℕ
143
+    x + Zero = x
144
+    x + (Succ y) = (Succ x) + y
145
+
146
+instance CoMagma ℤ
147
+    x + Zero = x
148
+    Zero + y = y
149
+    Plus x + Plus y = x + y
150
+    Minus x + Minus y = Minus (x + y)
151
+
152
+    Plus (Succ x) + Minus One = Plus x
153
+    Plus (Succ x) + Minus (Succ y) = Plus x + Minus y
154
+    Plus One + Minus (Succ y) = Minus y
155
+
156
+    Minus x + Plus y = Plus y + Minus x
157
+
158
+instance CoSemigroup ℕ
159
+
160
+instance CoSemigroup ℤ
161
+
162
+instance CoMonoid ℤ
163
+    0 = Zero
164
+
165
+instance CoGroup ℤ
166
+    - Plus x = Minus x
167
+    - Minus x = Plus y
168
+    - Zero = Zero
169
+
170
+instance CoAbelianGroup ℤ
171
+
172
+
173
+instance Magma ℤ
174
+    x * Zero = Zero
175
+
176
+    x * Plus One = x
177
+    x * Plus (Succ y) = x + (x * y)
178
+
179
+    x * Minus y = - (x * Plus y)
180
+
181
+instance Semigroup ℤ
182
+
183
+instance Monoid ℤ
184
+    1 = Plus One
185
+
186
+instance Ring ℤ

+ 24
- 58
ideas.md View File

@@ -1,6 +1,6 @@
1 1
 # Ideas for Katrin
2 2
 
3
-Please note that at this point this document is just a collection of quite random notes and ideas.
3
+Please be aware that at this point this document is just a collection of quite random notes and ideas.
4 4
 
5 5
 ## Paradigms
6 6
 
@@ -25,15 +25,21 @@ Interaction (with users, windows, servers, ...) is a very important thing in pro
25 25
     - Could this be used for multithreading (besides automatic parallelism of function calculations via dependencies)?
26 26
 - Event-driven: Things happening on the outside are represented by events with handlers, maybe IO monads
27 27
 - Look into Idris Effects
28
+- Look into Functional Reactive Programming
28 29
 - Usable as full command shell (replacing Bash etc.)
30
+    - Things like piping etc. possible with type-safety
31
+    - How to most elegantly integrate non-Katrin programs?
29 32
 
30 33
 
31 34
 ### Data-centric
32 35
 
33 36
 - Communication with databases/files/servers/users should easy and type-safe
34
-- If possible: simultaneously markup-/data-language?
35
-- Integration with CrystalOS file/return type system
36
-- *Languages* (regular, context-free) at the language heart
37
+- ~~If possible: simultaneously markup-/data-language?~~
38
+    - Bad idea. Better stick to YAML as encouraged default. It can represent record types very well and is already established and less awkward to write.
39
+    - Deriving of serialization traits should be generically possible.
40
+- Integration with CrystalOS file/return type system, one day
41
+- ~~*Languages* (regular, context-free) at the language heart~~
42
+    - Probably nonsense
37 43
 
38 44
 
39 45
 ## Influenced by
@@ -42,72 +48,32 @@ Interaction (with users, windows, servers, ...) is a very important thing in pro
42 48
 - Idris
43 49
 - Agda
44 50
 - Python
51
+- Go
45 52
 
53
+Languages to look into: Lisp, ML, Clojure, F#, Scale, Nix, Nim
46 54
 
47
-## Types
48 55
 
49
-*Atom*: set of all atoms. An atom is a single part literal. Can be any string starting with a capital letter, a number and no blanks.
56
+## Tooling
50 57
 
51
-*Universe* = *Atom^\** (Kleene closure of *Atom*, i.e. any number of arbitrary atoms)
58
+Good tooling is very important for a language.
52 59
 
53
-*Type* = PowerSet(*Universe*) i.e. any subset of the universe is a type [Context free?]
60
+One tool for everything: `katrin` command line tool is compiler, interpreter, shell, build system, formatter etc. in a single convenient place, like with Go.
54 61
 
55
-*Universe* ∈ *Type*!
62
+- `katrin compile test.katrin` compiles a single file into a binary.
63
+- `katrin shell test.katrin` starts an interactive shell with the file loaded.
64
+- `katrin run test.katrin` or simply `katrin test.katrin` executes the file.
65
+- `katrin build` builds a project following the directions in the `project.yaml` file.
66
+- `katrin test` runs a KaTest test suite.
67
+- `katrin fmt test.katrin` formats a source file following the conventions.
68
+- `katrin lint test.katrin` gives hints on how to improve a source file.
69
+- `katrin language-server` starts a language server for editors supporting the protocol.
56 70
 
57
-```
58
-ℕ ∈ Type
59
-```
60
-
61
-`→` (or `->`) takes two types and »returns« a type containing all functions from the first into the second type.
62
-`=` defines the value of an identifier. Identifiers can be any string starting with a small letter.
63
-
64
-→ **Any** string (including single character ones) denotes **either** an atom **or** an identifier.
65
-
66
-`↦` (or `|>`) can be used to write anonymous (i.e. lambda) functions.
67
-
68
-```
69
-f ∈ ℕ → ℕ
70
-f = x ↦ x*2
71
-```
72
-
73
-```
74
-g ∈ ℕ → ℕ → ℕ → ℕ
75
-g = x ↦ y ↦ z ↦ x*2 + y*4 + z*3
76
-```
77
-
78
-Definition of simple enum-like types:
79
-
80
-```
81
-oneToFour ∈ Type
82
-oneToFour = {One, Two, Three, Four}
83
-
84
-thing ∈ oneToFour
85
-thing = Two
86
-```
87
-
88
-```
89
-vect ∈ ℕ → Type → Type
90
-```
91
-
92
-Note that type names are lowercased, as they are no literals but rather identifiers for the actual type, which is a set. However, the type members (`One`, `Two`...) *are* capitalized as they are actual literals/atoms.
93
-
94
-
95
-## Core language features
96
-
97
-A minimal implementation would need:
98
-
99
-- `→` and `↦` for functions
100
-- `∈`, `=`
101
-- Atoms, Tuples (for making literals from atoms), Sets
102
-- pattern matching
103
--
104
-- ...
105 71
 
106 72
 Everything else should be possible to be directly implemented in the language itself (which doesn't mean that that would be the best choice performance-wise).
107 73
 
108 74
 ## Stuff
109 75
 
110
-Katrin has no exceptions, functions should be total → `Maybe` return type
76
+Katrin is by default a total language.
111 77
 
112 78
 Lists *could* be implemented as sets of pairs of the index in the list and the actual value.
113 79
 

+ 51
- 0
katrin.cabal View File

@@ -0,0 +1,51 @@
1
+cabal-version: 1.12
2
+
3
+-- This file has been generated from package.yaml by hpack version 0.31.1.
4
+--
5
+-- see: https://github.com/sol/hpack
6
+--
7
+-- hash: 3f1f9a4db7b01dd6dab03afa5875f5a4383fefec1176837dd482d66e518bcc04
8
+
9
+name:           katrin
10
+version:        0.0.0
11
+synopsis:       Experimental functional and dependently-typet programming language
12
+description:    Please see the README at <https://git.eisfunke.com/research/katrin#readme>
13
+category:       Language
14
+author:         Nicolas Lenz
15
+maintainer:     nicolas@eisfunke.com
16
+copyright:      2018 Nicolas Lenz
17
+license:        Apache-2.0
18
+build-type:     Simple
19
+extra-source-files:
20
+    README.md
21
+
22
+source-repository head
23
+  type: git
24
+  location: https://git.eisfunke.com/research/katrin
25
+
26
+library
27
+  exposed-modules:
28
+      Katrin
29
+      Main
30
+  other-modules:
31
+      Paths_katrin
32
+  hs-source-dirs:
33
+      src
34
+  build-depends:
35
+      base >=4.7 && <5
36
+    , text
37
+  default-language: Haskell2010
38
+
39
+executable katrin
40
+  main-is: Main.hs
41
+  other-modules:
42
+      Katrin
43
+      Paths_katrin
44
+  hs-source-dirs:
45
+      src
46
+  ghc-options: -threaded -rtsopts -with-rtsopts=-N
47
+  build-depends:
48
+      base >=4.7 && <5
49
+    , katrin
50
+    , text
51
+  default-language: Haskell2010

+ 32
- 0
package.yaml View File

@@ -0,0 +1,32 @@
1
+name:                katrin
2
+version:             0.0.0
3
+license:             Apache-2.0
4
+git:                 "https://git.eisfunke.com/research/katrin"
5
+author:              "Nicolas Lenz"
6
+maintainer:          "nicolas@eisfunke.com"
7
+copyright:           "2018 Nicolas Lenz"
8
+
9
+extra-source-files:
10
+- README.md
11
+
12
+synopsis:            Experimental functional and dependently-typet programming language
13
+category:            Language
14
+description:         Please see the README at <https://git.eisfunke.com/research/katrin#readme>
15
+
16
+dependencies:
17
+- base >= 4.7 && < 5
18
+- text
19
+
20
+library:
21
+  source-dirs: src
22
+
23
+executables:
24
+  katrin:
25
+    main: Main.hs
26
+    source-dirs: src
27
+    ghc-options:
28
+    - -threaded
29
+    - -rtsopts
30
+    - -with-rtsopts=-N
31
+    dependencies:
32
+    - katrin

+ 3
- 0
src/Katrin.hs View File

@@ -0,0 +1,3 @@
1
+module Katrin where
2
+
3
+-- well.

+ 6
- 0
src/Main.hs View File

@@ -0,0 +1,6 @@
1
+module Main where
2
+
3
+-- | An excellent main function.
4
+-- | ~ Chef Excellence
5
+main :: IO ()
6
+main = putStrLn "Nothing to see here."

+ 4
- 0
stack.yaml View File

@@ -0,0 +1,4 @@
1
+resolver: lts-12.23
2
+
3
+packages:
4
+- .

Loading…
Cancel
Save