Giter Site home page Giter Site logo

k's Introduction

k

Test Coverage Status Documentation Note

Warning This is a developing project

K is a theorem prover based on Racket ecosystem, interact with Racket in useful way is the major purpose of the project.

Installation

  1. only want core language
    raco pkg install --auto k-core
  2. wants standard library
    raco pkg install --auto k-core
    raco pkg install --auto k-lib

Usage

NOTE: implicit parameter haven't implemented, the following is a bit pseudo code

(def (symm {A : Type} {x y : A}
           [P : (≡ x y)])
  : (≡ y x)
  [refl => refl])

(def (trans {A : Type}
            {x y z : A}
            [P1 : (≡ x y)]
            [P2 : (≡ y z)])
  : (≡ x z)
  [refl refl => refl])

For developer

The following commands might be helpful for your development

raco pkg install --auto ./k-core
raco pkg install --auto ./k-lib
raco pkg install --auto ./k-doc
raco pkg install --auto ./k-test
raco pkg install --auto ./k-example
# apply git config of the project
git config commit.template $(pwd)/.gitmessage

k's People

Contributors

cybai avatar dannypsnl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

cybai

k's Issues

def identity

Currently, the following code cannot work

(def (identity [a : A]) : A
  [a => a])

define Congruence

We should be able to define the following code:

(def (cong {A B : Type}
           {x y : A}
           [f : (-> A B)]
           [P : (≡ x y)])
  : (≡ (f x) (f y))
  [f refl => refl])

Blocked by #21

improve pattern transformation

The idea is helping constructor expanded as it's name when no parameters, then no transformation would make pattern reference to constructor syntax, then syntax-property should be accessible.

universe polymorphism

When type checking, unified universe level, and ensure it's a total order. We can build a directed graph that preserves <= relationship by direction, then a graph without any rings is a valid type-construction.

references

doc initial

Need more description for our forms and libraries:

  1. data
  2. def
  3. check
  4. typeof
  5. libraries:
    • k/data/nat
    • k/data/bool
    • k/data/list
    • k/data/vec
    • k/equality

postulate definition for later reuse it in `data`

It's easy to find data's definition is a list of postulate definitions, with such reusing implicit parameters can be done in one place! So I propose the following syntax.

(def f : T
  #:postulate)
(def (f [p : P] ...) : T
  #:postulate)

And for constructors, syntax will be

(def ...
  #:constructor)

Because overlapped binding from racket/base makes current bounded as constructors is not reliable.

Example

Nat

(data Nat : Type
      [z : Nat]
      [s (n : Nat) : Nat])

to

(def Nat : Type
  #:postulate)
(def z : Nat
  #:constructor)
(def (s [n : Nat]) : Nat
  #:constructor)

equality

(data (≡ {A : Type} [a b : A]) : Type
      [refl : (≡ a a)])

to

(def (≡ {A : Type} [a b : A]) : Type
  #:postulate)
#|
notice a hard problem here, how to know how many meta came from data type should run into here?
|#
(def (refl {A : Type} {a : A}) : (≡ a a)
  #:constructor)

equivalence type

The following program shows problem:

(data (≡ [a : A] [b : A]) : Type
      [refl : (≡ a a)])

(check (≡ z z)
       (refl))
(check (≡ z (s z))
       (refl))

Our unification didn't figure out this program has type mismatching.

bind many name with type

The current bind is like

(def (foo [x : A] [y : A]) : A
  [x => y])

would be good to have

(def (foo [x y : A]) : A
  [x => y])
  • extract bindings
  • update data type bindings
  • update data type constructor bindings
  • update def bindings

path

Cubical/HoTT variant language, e.g. #lang k/ube

  1. left, right
  2. idp?

Proof examples

(def (refl {A : Type}
           {a : A})
  ; `(≡ x y)` is short for `(Path _ x y)`, `Path` is equality in cubical
  : (≡ a a)
  [{A} {a} => (lambda (i) a)])

; invert is symmetric in cubical
(def (invert [A : Type]
             [a b : A]
             [p : (≡ a b)])
  : (≡ b a)
  ; The path lambda contains current interval `i`
  ; to prove reverse result
  ; we invert the current interval `i` by using `~`
  [A a b => (lambda (i) (p (~ i)))])

Bad type should work without strict positivity

Since now strict positivity check hasn't been enabled, the following program should be type-checked, but not(sadly).

(define-syntax-parser →
  [(_ A B) #'(Pi ([x : A]) B)])

(data Bad : Type
      [bad [f : (→ ⊥ Bad)] : Bad])

(def (self-app [b : Bad]) : ⊥
  [(bad f) (f (bad f))])

(def (absurd) : ⊥
  (self-app (bad self-app)))

Error message:

racket.tw/k/k-core/k/core.rkt:77:10: dict-ref: no value found for key
  key: #<syntax:3-unsaved-editor:11:12 f>
  all keys: '(#<syntax:3-unsaved-editor:10:6 self-app>)

related #3

finite set

Example from Agda

open import Agda.Builtin.Equality
open import Data.Nat

data Fin : Set where
  fzero :  {n}  Fin (suc n)
  fsuc :  {n}  Fin n  Fin (suc n)

_ : Fin 2
_ = fsuc fzero
_ : Fin 2
_ = fzero

Translated to k

(require k/data/nat)

(data (Fin [n : Nat]) : Type
  [fzero : (Fin (s n))]
  [fsuc (x : (Fin n)) : (Fin (s n))])

(check (Fin (s (s z)))
       fzero)
(check (Fin (s (s z)))
       (fsuc fzero))

data type with dependencies(PI type): like Vec, List

(data (List [A : Type]) : Type
     [nil : (List A)]
     [:: (a : A) : (List A)])
(data (Vec [E : Type] [LEN : Nat]) : Type
     [vnil : (Vec E z)]
     [v:: (e : E) (Vec E N) : (Vec E (s N))])

ensure the above program works, and introduce implicit arguments for type automatically(that is, no need to write out {A : Type} -> (List A), but just (List A)).

add failed test

To ensure our type checker works well with ill-form, we need a test set for cases that shall fail.

implement pi type correctly

To now, we still using a simulated pi type(of course make it weaker) that cannot be spread properly. I think now it's a good time to make it correct since it's all things' foundation.

Abbreviation for `Pi`

(define-syntax-parser →
  [(_ A B) #'(Pi ([_ : A]) B)])

I guess also rename it out as ->

without-K

The very basic idea of without-K is that x = x can't be pattern matched as refl. The rest of the work of without-K is preventing you from doing the same thing indirectly.

On this, questions are

  1. Should we implement cubical/homotopy based on this?
  2. How to do it correctly?

covering check

We have to ensure a definition is covering(no paths get ignored).

depends on term's type cannot work

#lang k

(require k/data/nat
         k/data/bool)

(def (Nat-or-Bool [x : Bool]) : Type
  [true => Nat]
  [false => Bool])

(def a : (Nat-or-Bool true) z)
(def b : (Nat-or-Bool false) true)

The above program shows a simple type depends on the term sample, but it will get:

example/test2.rkt:10:28: type-mismatch: expect: `(Nat-or-Bool true)`, get: `Nat`

test is not working

test.rkt example is not working. Maybe related to #8

; /Users/ar/work/k/example/test.rkt:6:31: x: unbound identifier
;   in: x
; Context (plain; to see better errortrace context, re-run with C-u prefix):
;   /Users/ar/work/k/core.rkt:58:0 check-type
;   .../parse/define.rkt:20:4
;   /Users/ar/.emacs.d/elpa/racket-mode-20210727.1545/racket/syntax.rkt:66:0

Do you have more documentation about the package?

simpler index type

We can have different data type https://arxiv.org/pdf/2103.15408.pdf by @ice1000

A challenge will be how to encode the following behavior.

This pattern matching is not traditional pattern matching, say, it does not need
to be covering (although in the Vec example it is) and it can contain seemingly un-
reachable patterns (like duplicated patterns).

For example, Fin type:

(data (Fin [n : Nat]) : Type
  [(s n) => fzero]
  [(s n) => (fsuc [x : (Fin n)])])

There might also have covering type, for example, Vec type:

(data (Vec [A : Type] [N : Nat]) : Type
  [A z => nil]
  [A (s n) => (:: [a : A] [v : (Vec A n)])])

Fix implicit definition's expansion

(def (symm {A : Type}
           [x y : A]
           [p : (≡ x y)])
  : (≡ A y x)
  [A x y refl => refl])

When we have different length of definitions (e.g. (≡ x y) vs (≡ A y x)), we'll get following errors:

→ racket k-lib/k/equality.rkt
andmap: all lists must have same size
  first list length: 4
  other list length: 3
  procedure: #<procedure:unify?>
  context...:
   /Applications/Racket v8.3/collects/racket/private/map.rkt:275:2: gen-andmap
   /Users/cybai/codespace/racket/k/k-core/k/core.rkt:57:0: check-type
   /Applications/Racket v8.3/collects/syntax/parse/private/parse.rkt:1020:13: dots-loop
   /Applications/Racket v8.3/collects/syntax/wrap-modbeg.rkt:46:4

however, we should be able to generate the type automatically to do pattern matching.

with a proper implementation, we should be able to see (≡ x y) and (≡ A x y) as same thing in the symm function.

normalize term in type properly

(check (≡ (+ (s z) (s z)) (s (s z)))
       (refl))
; error
; cannot-unified: (+ (s z) (s z)) with (s (s z))

The above program cannot work properly, because our naive unification algorithm unifies (+ (s z) (s z)) with (s (s z)) directly. To fix this, we have to check if a term is not normalized, we shall normalize it.

bug(`Nat=?` cannot type check now)

ref: https://github.com/racket-tw/k/blob/develop/k-lib/k/data/nat.rkt#L21-L25

(def (Nat=? [n m : Nat]) : Bool
  [z z => true]
  [z (s m) => false]
  [(s n) z => false]
  [(s n) (s m) => (Nat=? n m)])

Error message:

dict-ref: no value found for key
  key: #<syntax:k-lib/k/data/nat.rkt:25:19 Nat=?>
  all keys: '(#<syntax:k-lib/k/data/nat.rkt:25:6 n> #<syntax:k-lib/k/data/nat.rkt:25:12 m>)
  context...:
   /Users/dannypsnl/racket.tw/k/k-core/k/core.rkt:73:0: typeof
   /Users/dannypsnl/racket.tw/k/k-core/k/core.rkt:56:0: check-type
   /Applications/Racket v8.4/collects/syntax/parse/private/parse.rkt:1020:13: dots-loop
   /Applications/Racket v8.4/collects/syntax/wrap-modbeg.rkt:46:4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.