Code Monkey home page Code Monkey logo

sequent.jl's Introduction

Sequent

Stable Dev Build Status Coverage

This package implements sequent calculus in Julia programming language, which gives you the capability of easily and formally describing semantics of your system.

To running the whole tests, you should add an extra package

pkg> add https://github.com/thautwarm/HMRowUnification.jl

Example: Natural Number

@data Nat begin
    Z
    S(Nat)
end

rule_z = @sequent FOLD_Z begin
    _ ₛ Z => 0
end

rule_s = @sequent FOLD_S begin
    _ ₛ term => n
    _ S(term) => n + 1 
end

@semantics [rule_z, rule_s]

(nothing ₛ Z) == 0
(nothing S(S(Z))) == 2

Example: Monomorphic Type Inference

@data ML begin
    LApp(ML, ML)
    LFun(Symbol, ML)
    LVar(Symbol)
    LVal(Int)
end


ImD = Base.ImmutableDict

struct Sigma{O}
    unbox :: ImD{Symbol, O}
end

Sigma{O}() where O = Sigma(ImD{Symbol, O}())

Base.getindex(s::Sigma{O}, x::Pair{Symbol, A}) where {O, A<:O} =
    Sigma(ImD(s.unbox, x))
Base.getindex(s::Sigma{O}, x::Symbol) where O = 
    s.unbox[x]

(s::Sigma{O})(x::Pair{Symbol, A}) where {O, A<:O} = ImD(s, x)

r_app = @sequent APP begin
    (Γ, σ) ʷ a => ta
    (Γ, σ) ʷ f => Arrow(ta′, tr)
    true = Γ.unify(ta, ta′)
    (Γ, σ) ʷ LApp(f, a) => tr
end

r_var = @sequent VAR begin
    (Γ, σ) ʷ LVar(s) => σ[s]
end

r_fun = @sequent FUN begin
    a′ = Γ.new_tvar()
    (Γ, σ[a => a′]) ʷ r => tr
    (Γ, σ) ʷ LFun(a, r) => Arrow(a′, tr)
end

r_val = @sequent VAL begin
    _ ʷ LVal(_) => Nom(:int)
end

@semantics [r_app, r_fun, r_val, r_var]

@testset "ML" begin
    tctx = HMT[]
    Γ = mk_tcstate(tctx)
    σ = Sigma{HMT}()
    int_t = Nom(:int)
    σ = σ[:add => Arrow(int_t, Arrow(int_t, int_t))]
    term = 
        LFun(:a,
            LApp(
                LApp(
                    LVar(:add),
                    LVar(:a)),
                LVar(:a)))
    infer_ty =  (Γ, σ) ʷ term
    @test Γ.prune(infer_ty) == Arrow(int_t, int_t)
end

sequent.jl's People

Contributors

thautwarm avatar github-actions[bot] avatar

Stargazers

ebigram avatar Elias Carvalho avatar Chad Scherrer avatar Alessandro Cheli avatar Alexey Nikolaev avatar Arthur Correnson avatar Anqur avatar Yueh-Hua Tu avatar Mesabloo avatar Chris Pressey avatar ChangXin Sun avatar Peter avatar Lîm Tsú-thuàn avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

hermetique

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.