Comments (2)
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.
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)
- Error in file ./tox.makam, line 3, character 1: Parse error HOT 2
- Confusing error with transitive qualified imports HOT 13
- %\n is a syntax error HOT 1
- When should we use `<-` vs `:-`? HOT 3
- Parse error when whitespace preceeds '>>' HOT 1
- Allow new lines in tests HOT 1
- Next parts of the tutorial? HOT 3
- Add bitwise operations for integers HOT 2
- Type aliases HOT 1
- Question: Makam vs. Teyjus HOT 3
- Report delayed constraints in the REPL HOT 2
- Unification failure HOT 3
- Syntax rules don't compile HOT 3
- Loading a file from REPL HOT 2
- Parse error for adjacent parenthesis
- More integer arithmetics
- Extending syntax.makam leads to error with non-existent variable nil HOT 3
- inverting open for binders
- Implementing IDE in browser using Datalog HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from makam.