Giter Club home page Giter Club logo

Comments (2)

brendanzab avatar brendanzab commented on June 28, 2024

I've been wishing for this too - eg. for having a default typing context for my language. This is a bit related to #100 I think?

from makam.

astampoulis avatar astampoulis commented on June 28, 2024

Hi @d4hines and @brendanzab !
There isn't a built-in way to define constants right now unfortunately. The situation is a little easier to work-around though compared to type-level constants: you could define a predicate to expand definitions in a term, before doing any other kind of computation (e.g. type-checking, evaluation, etc.).

Here's some code that you could use for now, but I'll see if I can add something like this to the standard library, or to the core eventually.

define : [A] A -> A -> prop.

expand_definitions, expand_definitions_aux : [A] A -> A -> prop.
expand_definitions X Y :-
  demand.case_otherwise
    (expand_definitions_aux X Y)
    (structural @expand_definitions X Y).

expand_definitions_aux D E when (define D E).


(* STLC *)

term : type.
lam : (term -> term) -> term.
app : term -> term -> term.

typ : type.
arrow : typ -> typ -> typ.

typecheck : term -> typ -> prop.

typecheck (lam E) (arrow T T') :-
  (x:term -> typecheck x T -> typecheck (E x) T').

typecheck (app E1 E2) T' :-
  typecheck E1 (arrow T T'), typecheck E2 T.

(* church numerals as example of definitions *)

intconst : int -> term.
define (intconst 0) (lam (fun z => lam (fun s => z))).
define (intconst N) (lam (fun z => lam (fun s => app s (Pred z s))))
    when lessthan 0 N true :-
  minus N 1 N',
  define (intconst N') (lam (fun z => lam (fun s => Pred z s))).

(expand_definitions (intconst 2) X, typecheck X Y) ?

There is an alternative approach, which is not common in textbooks: you could extend the base language with a constructor for using a definition, and then add a typing rule for those, that "looks up" the definition itself. Here's one way to do that:

(* STLC *)

term : type.
lam : (term -> term) -> term.
app : term -> term -> term.

typ : type.
arrow : typ -> typ -> typ.

typecheck : term -> typ -> prop.

typecheck (lam E) (arrow T T') :-
  (x:term -> typecheck x T -> typecheck (E x) T').

typecheck (app E1 E2) T' :-
  typecheck E1 (arrow T T'), typecheck E2 T.

(* add definitions *)

def : term -> term.
define : term -> term -> prop.

typecheck (def X) T :-
  define X Y, typecheck Y T.

intconst : int -> term.
define (intconst 0) (lam (fun z => lam (fun s => z))).
define (intconst N) (lam (fun z => lam (fun s => app s (Pred z s))))
    when lessthan 0 N true :-
  minus N 1 N',
  define (intconst N') (lam (fun z => lam (fun s => Pred z s))).

(typecheck (def (intconst 0)) Y) ?

from makam.

Related Issues (20)

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.