Lecture de lambda-termes et reduce

This commit is contained in:
Adrien Vannson 2022-05-11 21:29:20 +02:00
parent 359d7333f5
commit f60d98ea78
No known key found for this signature in database
GPG Key ID: FE2E66FD978C1A55
3 changed files with 53 additions and 4 deletions

View File

@ -17,7 +17,12 @@ rule token = parse
| "elim" { ELIM }
| "cut" { CUT }
| "False" { FALSE }
| "fun" { FUN }
| "=>" { MAPS_TO }
| ':' { VDOTS }
| "exf" { EXF }
| ['A'-'Z']+['0'-'9']* as s { TYPE_NAME s }
| ['a'-'z']+['0'-'9']* as s { VAR_NAME s }
| '.' { DOT }

18
main.ml
View File

@ -37,6 +37,24 @@ let readline () = match file with
| Some f -> input_line f
in
if !reduce_option then (
let lexbuf = Lexing.from_channel (match file with
| None -> stdin
| Some file -> file
)
in
let lambda_term =
try
Parser.main_lambda Lexer.token lexbuf
with e -> (
Printf.printf "Can't read lambda term\n";
raise e
)
in
reduce lambda_term;
exit 0
);
(* Show a message only if the input is read from stdin *)
let show s = match file with
| None -> Printf.printf "%s" s

View File

@ -11,6 +11,7 @@
%token <string> TYPE_NAME
%token DOT INTRO ASSUMPTION APPLY ELIM CUT
%token FUN MAPS_TO VDOTS EXF
/* L'ordre de définition définit la priorité */
%right RARROW
@ -22,10 +23,20 @@
%start main_tactic
%type <Tactic.tactic> main_tactic
%start main_lambda
%type <Structs.lam> main_lambda
%%
main_type:
| ty EOF { $1 }
main_tactic:
| tactic EOF { $1 }
main_lambda:
| lambda EOF { $1 }
/* Types */
ty:
| LPAREN ty RPAREN { $2 }
| ty RARROW ty { TImpl ($1, $3) }
@ -33,12 +44,27 @@ ty:
| TILDE ty { TImpl ($2, TFalse) }
| FALSE { TFalse }
main_tactic:
| tactic EOF { $1 }
/* Tactiques */
tactic:
| INTRO VAR_NAME DOT { Intro $2 }
| ASSUMPTION DOT { Assumption }
| APPLY VAR_NAME DOT { Apply $2 }
| ELIM VAR_NAME DOT { Elim $2 }
| CUT ty DOT { Cut $2 }
/* Lambda-termes */
lambda_arg: /* Expression pouvant être en argument d'une fonction */
| VAR_NAME { LVar $1 }
| LPAREN lambda RPAREN { $2 }
/* Application d'une fonction */
lambda_app:
| lambda_app lambda_arg { LApp ($1, $2) }
| lambda_arg { $1 }
lambda:
| lambda_app { $1 }
| FUN VAR_NAME VDOTS ty MAPS_TO lambda { LFun ($2, $4, $6) }
| FUN LPAREN VAR_NAME VDOTS ty RPAREN MAPS_TO lambda
{ LFun ($3, $5, $8) }
| EXF LPAREN VAR_NAME VDOTS ty RPAREN { LExf (LVar $3, $5) }