Merge with cfg
This commit is contained in:
95
lib/miniFun/Lexer.mll
Normal file
95
lib/miniFun/Lexer.mll
Normal file
@ -0,0 +1,95 @@
|
||||
{
|
||||
open Parser
|
||||
exception LexingError of string
|
||||
|
||||
let create_hashtable size init =
|
||||
let tbl = Hashtbl.create size in
|
||||
List.iter (fun (key, data) -> Hashtbl.add tbl key data) init;
|
||||
tbl
|
||||
|
||||
let keyword_table =
|
||||
let mapping = [
|
||||
("bool", TYPEBOOL);
|
||||
("else", ELSE);
|
||||
("false", BOOL(false));
|
||||
("fst", FIRST);
|
||||
("snd", SECOND);
|
||||
("fun", LAMBDA);
|
||||
("if", IF);
|
||||
("in", IN);
|
||||
("int", TYPEINT);
|
||||
("lambda", LAMBDA);
|
||||
("let", LET);
|
||||
("not", BNOT);
|
||||
("powmod", POWERMOD);
|
||||
("rand", RAND);
|
||||
("rec", REC);
|
||||
("then", THEN);
|
||||
("true", BOOL(true));
|
||||
]
|
||||
in create_hashtable (List.length mapping) mapping
|
||||
}
|
||||
|
||||
|
||||
let digit = ['0'-'9']
|
||||
let alpha = ['a'-'z' 'A'-'Z']
|
||||
let white = [' ' '\t']+ | '\r' | '\n' | "\r\n"
|
||||
|
||||
let integer = ('-')?(digit)(digit*)
|
||||
let var = (alpha|'_') (alpha|digit|'_')*
|
||||
|
||||
let symbols = ['!'-'/' ':'-'?' '[' ']' '^' '{'-'}' '~']
|
||||
|
||||
(* lexing rules *)
|
||||
rule read = parse
|
||||
| white {read lexbuf}
|
||||
| var as v {
|
||||
match Hashtbl.find_opt keyword_table v with
|
||||
| Some keyword -> keyword
|
||||
| None -> VARIABLE(v)
|
||||
}
|
||||
| "%" {MODULO}
|
||||
| "&&" {BAND}
|
||||
| "(" {LEFTPAR}
|
||||
| ")" {RIGHTPAR}
|
||||
| "*" {TIMES}
|
||||
| "+" {PLUS}
|
||||
| "," {COMMA}
|
||||
| "-" {MINUS}
|
||||
| "->" {TYPEFUNCTION}
|
||||
| "/" {DIVISION}
|
||||
| ":" {COLUMN}
|
||||
| "<" {CMPLESS}
|
||||
| "<=" {CMPLESSEQ}
|
||||
| "=" {ASSIGNMENT}
|
||||
| "==" {CMP}
|
||||
| "=>" {RESULTS}
|
||||
| ">" {CMPGREATER}
|
||||
| ">=" {CMPGREATEREQ}
|
||||
| "\\" {LAMBDA}
|
||||
| "^" {POWER}
|
||||
| "||" {BOR}
|
||||
| integer as i {INT(int_of_string i)}
|
||||
| "(*" {comments 0 lexbuf}
|
||||
| eof {EOF}
|
||||
| _ {
|
||||
raise
|
||||
(LexingError
|
||||
(Printf.sprintf
|
||||
"Error scanning %s on line %d at char %d"
|
||||
(Lexing.lexeme lexbuf)
|
||||
(lexbuf.Lexing.lex_curr_p.Lexing.pos_lnum)
|
||||
(lexbuf.Lexing.lex_curr_p.Lexing.pos_lnum)
|
||||
))}
|
||||
and comments level = parse
|
||||
| "*)" {if level = 0
|
||||
then read lexbuf
|
||||
else comments (level-1) lexbuf}
|
||||
| "(*" {comments (level+1) lexbuf}
|
||||
| _ {comments level lexbuf}
|
||||
| eof {raise (LexingError ("Comment is not closed"))}
|
||||
|
||||
|
||||
{
|
||||
let lex = read
|
||||
}
|
||||
98
lib/miniFun/Parser.mly
Normal file
98
lib/miniFun/Parser.mly
Normal file
@ -0,0 +1,98 @@
|
||||
(* code to be copied in the scanner module *)
|
||||
(*
|
||||
*)
|
||||
%{
|
||||
open Types
|
||||
%}
|
||||
|
||||
(* tokens *)
|
||||
%token TYPEBOOL TYPEINT TYPEFUNCTION
|
||||
%token LAMBDA RAND IF IN THEN ELSE LET REC BNOT POWERMOD RESULTS
|
||||
%token <bool> BOOL
|
||||
%token <string> VARIABLE
|
||||
%token <int> INT
|
||||
%token COMMA COLUMN LEFTPAR RIGHTPAR CMPLESS CMPGREATER PLUS MINUS TIMES
|
||||
%token DIVISION MODULO POWER ASSIGNMENT BAND BOR CMP CMPLESSEQ CMPGREATEREQ
|
||||
%token FIRST SECOND
|
||||
%token EOF
|
||||
|
||||
%type <t_exp> prg
|
||||
%type <t_exp> texp
|
||||
%type <ftype> typeexp
|
||||
|
||||
(* start nonterminal *)
|
||||
%start prg
|
||||
|
||||
(* associativity in order of precedence *)
|
||||
/*%right rightlowest */
|
||||
%left lowest
|
||||
%right TYPEFUNCTION
|
||||
%left COMMA
|
||||
%nonassoc INT BOOL VARIABLE
|
||||
%left POWERMOD
|
||||
%left IF
|
||||
%left BOR BAND
|
||||
%left CMP CMPLESS CMPLESSEQ CMPGREATER CMPGREATEREQ
|
||||
%left PLUS MINUS
|
||||
%left TIMES DIVISION MODULO
|
||||
%left POWER
|
||||
%right BNOT RAND
|
||||
%left FIRST SECOND
|
||||
%left LAMBDA
|
||||
%left LET
|
||||
%left LEFTPAR
|
||||
%right righthighest
|
||||
|
||||
%%
|
||||
|
||||
(* grammar *)
|
||||
prg:
|
||||
| e = texp; EOF {e}
|
||||
texp:
|
||||
| i = INT {Integer (i)}
|
||||
| b = BOOL {Boolean (b)}
|
||||
| a = VARIABLE {Variable (a)}
|
||||
| LEFTPAR; a = texp; COMMA; b = texp; RIGHTPAR
|
||||
{Tuple (a, b)}
|
||||
| LAMBDA; v = VARIABLE; COLUMN; t = typeexp; RESULTS; body = texp
|
||||
%prec lowest {Function (v, t, body)}
|
||||
| a = texp; b = texp {Application (a, b)} %prec righthighest
|
||||
| a = texp; PLUS; b = texp {Plus (a, b)}
|
||||
| a = texp; MINUS; b = texp {Minus (a, b)}
|
||||
| a = texp; TIMES; b = texp {Times (a, b)}
|
||||
| a = texp; DIVISION; b = texp {Division (a, b)}
|
||||
| a = texp; MODULO; b = texp {Modulo (a, b)}
|
||||
| a = texp; POWER; b = texp {Power (a, b)}
|
||||
| a = texp; BAND; b = texp {BAnd (a, b)}
|
||||
| a = texp; BOR; b = texp {BOr (a, b)}
|
||||
|
||||
| FIRST; a = texp {First (a)}
|
||||
| SECOND; a = texp {Second (a)}
|
||||
|
||||
| a = texp; CMP; b = texp {Cmp (a, b)}
|
||||
| a = texp; CMPLESS; b = texp {CmpLess (a, b)}
|
||||
| a = texp; CMPLESSEQ; b = texp {CmpLessEq (a, b)}
|
||||
| a = texp; CMPGREATER; b = texp {CmpGreater (a, b)}
|
||||
| a = texp; CMPGREATEREQ; b = texp {CmpGreaterEq (a, b)}
|
||||
|
||||
| POWERMOD; LEFTPAR; t1 = texp; COMMA;
|
||||
t2 = texp; COMMA;
|
||||
t3 = texp; RIGHTPAR
|
||||
{PowerMod (t1, t2, t3)}
|
||||
| RAND; t = texp; {Rand (t)}
|
||||
| BNOT; b = texp {BNot (b)}
|
||||
| IF; b = texp; THEN; c1 = texp; ELSE; c2 = texp;
|
||||
%prec lowest {IfThenElse (b, c1, c2)}
|
||||
| LET; v = VARIABLE; ASSIGNMENT; c = texp; IN; rest = texp
|
||||
%prec lowest {LetIn (v, c, rest)}
|
||||
| LET; REC; f = VARIABLE; x = VARIABLE; COLUMN; t = typeexp; ASSIGNMENT; body = texp; IN; rest = texp
|
||||
%prec lowest {LetFun (f, x, t, body, rest)}
|
||||
| LEFTPAR; a = texp; RIGHTPAR {a}
|
||||
typeexp:
|
||||
| TYPEINT {IntegerType}
|
||||
| TYPEBOOL {BooleanType}
|
||||
| v = delimited(LEFTPAR, typeexp, RIGHTPAR)
|
||||
{v}
|
||||
| a = typeexp; COMMA; b = typeexp {TupleType (a, b)}
|
||||
| vin = typeexp; TYPEFUNCTION; vout = typeexp
|
||||
{FunctionType (vin, vout)}
|
||||
@ -5,7 +5,7 @@ Random.self_init ()
|
||||
|
||||
let (let*) = Result.bind
|
||||
|
||||
let rec evaluate (mem: memory) (command: t_exp) : (permittedValues, error) result =
|
||||
let rec evaluate (mem: memory) (command: t_exp) : (permittedValues, [> error]) result =
|
||||
match command with
|
||||
Integer n -> Ok (IntegerPermitted n)
|
||||
| Boolean b -> Ok (BooleanPermitted b)
|
||||
@ -14,14 +14,19 @@ let rec evaluate (mem: memory) (command: t_exp) : (permittedValues, error) resul
|
||||
None -> Error (`AbsentAssignment ("The variable " ^ v ^ " is not defined."))
|
||||
| Some a -> Ok a
|
||||
)
|
||||
| Function (xs, _, f) ->
|
||||
| Tuple (x, y) -> (
|
||||
let* xval = evaluate mem x in
|
||||
let* yval = evaluate mem y in
|
||||
Ok (TuplePermitted (xval, yval))
|
||||
)
|
||||
| Function (x, _, f) ->
|
||||
Ok (FunctionPermitted
|
||||
{inputList = xs;
|
||||
{input = x;
|
||||
body = f;
|
||||
assignments = mem.assignments;
|
||||
recursiveness = None}
|
||||
)
|
||||
| Application (f, xs) -> (
|
||||
| Application (f, x) -> (
|
||||
let* evalf = evaluate mem f in
|
||||
let* funcClosure = (
|
||||
match evalf with
|
||||
@ -30,45 +35,20 @@ let rec evaluate (mem: memory) (command: t_exp) : (permittedValues, error) resul
|
||||
^ " it's an integer"))
|
||||
| BooleanPermitted _ -> Error (`WrongType ("Function is not a function,"
|
||||
^ " it's a boolean"))
|
||||
| TuplePermitted _ -> Error (`WrongType ("Function is not a function,"
|
||||
^ " it's a tuple"))
|
||||
) in
|
||||
let parmList = List.map (fun k -> evaluate mem k) xs in
|
||||
let rec helper m params values =
|
||||
match (params, values) with
|
||||
(_, []) -> Ok (m, params)
|
||||
| ([], _) ->
|
||||
Error (`WrongArity ("Function application has arity " ^
|
||||
(List.length funcClosure.inputList
|
||||
|> string_of_int) ^
|
||||
", but was applied to " ^
|
||||
(List.length xs |> string_of_int) ^
|
||||
" parameters"))
|
||||
| (p::tlparams, (Ok v)::tlvalues) -> helper
|
||||
(VariableMap.add p v m)
|
||||
tlparams
|
||||
tlvalues
|
||||
| (_, (Error e)::_) -> Error e
|
||||
in
|
||||
let* (mem2assignments, params) = helper
|
||||
funcClosure.assignments
|
||||
funcClosure.inputList
|
||||
parmList
|
||||
in
|
||||
let mem2 = (
|
||||
let* param = evaluate mem x in
|
||||
let mem2 =
|
||||
match funcClosure.recursiveness with
|
||||
None -> {assignments = mem2assignments}
|
||||
| Some nameF -> {
|
||||
assignments =
|
||||
VariableMap.add
|
||||
nameF
|
||||
(FunctionPermitted funcClosure)
|
||||
mem2assignments
|
||||
}
|
||||
) in
|
||||
match params with
|
||||
[] -> evaluate mem2 funcClosure.body
|
||||
| _ -> (
|
||||
Ok (FunctionPermitted {funcClosure with inputList = params;
|
||||
assignments = mem2assignments}))
|
||||
None -> {assignments = (
|
||||
VariableMap.add funcClosure.input param funcClosure.assignments)}
|
||||
| Some nameF -> {assignments = (
|
||||
VariableMap.add funcClosure.input param funcClosure.assignments |>
|
||||
VariableMap.add nameF (FunctionPermitted funcClosure)
|
||||
)}
|
||||
in
|
||||
evaluate mem2 funcClosure.body
|
||||
)
|
||||
| Plus (a, b) ->
|
||||
let* aval = (
|
||||
@ -248,7 +228,24 @@ let rec evaluate (mem: memory) (command: t_exp) : (permittedValues, error) resul
|
||||
)
|
||||
in
|
||||
Ok (BooleanPermitted (not aval))
|
||||
|
||||
| First a ->
|
||||
let* aval = (
|
||||
match evaluate mem a with
|
||||
Ok TuplePermitted (x, _) -> Ok x
|
||||
| Error e -> Error e
|
||||
| _ -> Error (`WrongType ("Value is not a tuple"))
|
||||
)
|
||||
in
|
||||
Ok (aval)
|
||||
| Second a ->
|
||||
let* aval = (
|
||||
match evaluate mem a with
|
||||
Ok TuplePermitted (_, x) -> Ok x
|
||||
| Error e -> Error e
|
||||
| _ -> Error (`WrongType ("Value is not a tuple"))
|
||||
)
|
||||
in
|
||||
Ok (aval)
|
||||
| Cmp (exp_1, exp_2) ->
|
||||
let* exp_1val = match evaluate mem exp_1 with
|
||||
Ok IntegerPermitted x -> Ok x
|
||||
@ -329,13 +326,13 @@ let rec evaluate (mem: memory) (command: t_exp) : (permittedValues, error) resul
|
||||
let* evalxval = evaluate mem xval in
|
||||
let mem2 = {assignments = VariableMap.add x evalxval mem.assignments} in
|
||||
evaluate mem2 rest
|
||||
| LetFun (f, xs, _, fbody, rest) ->
|
||||
| LetFun (f, x, _, fbody, rest) ->
|
||||
let mem2 = {
|
||||
assignments =
|
||||
VariableMap.add
|
||||
f
|
||||
(FunctionPermitted
|
||||
{ inputList = xs;
|
||||
{ input = x;
|
||||
body = fbody;
|
||||
assignments = mem.assignments;
|
||||
recursiveness = Some f})
|
||||
@ -344,8 +341,8 @@ let rec evaluate (mem: memory) (command: t_exp) : (permittedValues, error) resul
|
||||
evaluate mem2 rest
|
||||
|
||||
|
||||
let reduce (program: t_exp) (iin : int) : (int, error) result =
|
||||
let program' = (Application (program, [(Integer iin)])) in
|
||||
let reduce (program: t_exp) (iin : int) : (int, [> error]) result =
|
||||
let program' = (Application (program, (Integer iin))) in
|
||||
let mem : memory = {assignments = VariableMap.empty} in
|
||||
match (evaluate mem program') with
|
||||
Ok IntegerPermitted a -> Ok a
|
||||
|
||||
@ -1 +1,3 @@
|
||||
val reduce : Types.t_exp -> int -> (int, Types.error) result
|
||||
val evaluate : Types.memory -> Types.t_exp -> (Types.permittedValues, [> Types.error]) result
|
||||
|
||||
val reduce : Types.t_exp -> int -> (int, [> Types.error]) result
|
||||
|
||||
@ -5,11 +5,10 @@ Random.self_init ()
|
||||
|
||||
let (let*) = Result.bind
|
||||
|
||||
|
||||
let rec principalTypings (D: ) (e: t_exp) : () result
|
||||
|
||||
let evaluate_type (_program: t_exp) (_context: typingshape) : (typingshape, error) result =
|
||||
failwith "asd"
|
||||
let evaluate_type_polimorphic (_program: t_exp) (_context: typingshape) : (typingshape, error) result =
|
||||
failwith "Not implemented"
|
||||
(* match program with *)
|
||||
(* Integer _ -> Ok (VariableMap.empty, IntegerType) *)
|
||||
(* | Boolean _ -> Ok (VariableMap.empty, BooleanType) *)
|
||||
@ -56,5 +55,155 @@ let evaluate_type (_program: t_exp) (_context: typingshape) : (typingshape, erro
|
||||
(* | LetIn (x, xval, rest) -> failwith "Not Implemented" *)
|
||||
(* | LetFun (f, xs, typef, fbody, rest) -> failwith "Not Implemented" *)
|
||||
|
||||
let typecheck (_program: t_exp) : (ftype, error) result =
|
||||
failwith "Not Implemented"
|
||||
let rec evaluate_type (program: t_exp) (context: ftype VariableMap.t) : (ftype, [> typechecking_error]) result =
|
||||
match program with
|
||||
Integer _ -> Ok IntegerType
|
||||
| Boolean _ -> Ok BooleanType
|
||||
| Variable x -> ( (* check for the type in the context *)
|
||||
match VariableMap.find_opt x context with
|
||||
None -> Error (`AbsentAssignment
|
||||
("The variable " ^ x ^ " is not defined."))
|
||||
| Some t -> Ok t
|
||||
)
|
||||
| Tuple (x, y) -> (
|
||||
let* xtype = evaluate_type x context in
|
||||
let* ytype = evaluate_type y context in
|
||||
Ok (TupleType (xtype, ytype))
|
||||
)
|
||||
| Function (x, typef, fbody) -> (
|
||||
(* first check that the function has the right specified type then check
|
||||
the type of the body using the bindings for the input *)
|
||||
match typef with
|
||||
FunctionType (tin, tout) -> (
|
||||
let* typefbody = evaluate_type fbody (VariableMap.add x tin context) in
|
||||
if (typefbody = tout) then
|
||||
Ok typef
|
||||
else
|
||||
Error (`WrongTypeSpecification
|
||||
("Function does not return specified type."))
|
||||
)
|
||||
| _ -> Error (`WrongTypeSpecification
|
||||
("Specification of function is not a function type."))
|
||||
)
|
||||
| Application (f, x) -> (
|
||||
let* evalf = evaluate_type f context in
|
||||
let* evalx = evaluate_type x context in
|
||||
match evalf with
|
||||
FunctionType (tin, tout) -> (
|
||||
if tin = evalx then
|
||||
Ok tout
|
||||
else
|
||||
Error (`WrongType "Appling function with wrong input type to value")
|
||||
)
|
||||
| _ -> Error (`WrongType "Applying to a non function type")
|
||||
)
|
||||
| Plus (x, y)
|
||||
| Minus (x, y)
|
||||
| Times (x, y)
|
||||
| Division (x, y)
|
||||
| Modulo (x, y)
|
||||
| Power (x, y) -> (
|
||||
let* typex = evaluate_type x context in
|
||||
let* typey = evaluate_type y context in
|
||||
match typex, typey with
|
||||
| (IntegerType, IntegerType) -> Ok IntegerType
|
||||
| (IntegerType, _) -> Error (`WrongType "Second term is not an integer.")
|
||||
| (_, _) -> Error (`WrongType "First term is not an integer.")
|
||||
)
|
||||
| PowerMod (x, y, z) -> (
|
||||
let* typex = evaluate_type x context in
|
||||
let* typey = evaluate_type y context in
|
||||
let* typez = evaluate_type z context in
|
||||
match typex, typey, typez with
|
||||
| (IntegerType, IntegerType, IntegerType) -> Ok IntegerType
|
||||
| (IntegerType, IntegerType, _) -> Error (`WrongType ("Third term is " ^
|
||||
"not an integer."))
|
||||
| (IntegerType, _, _) -> Error (`WrongType
|
||||
("Second term is not an integer."))
|
||||
| (_, _, _) -> Error (`WrongType "First term is not an integer.")
|
||||
)
|
||||
| Rand (x) -> (
|
||||
let* typex = evaluate_type x context in
|
||||
match typex with
|
||||
| (IntegerType) -> Ok IntegerType
|
||||
| (_) -> Error (`WrongType "Term is not an integer.")
|
||||
)
|
||||
| BAnd (x, y)
|
||||
| BOr (x, y) -> (
|
||||
let* typex = evaluate_type x context in
|
||||
let* typey = evaluate_type y context in
|
||||
match typex, typey with
|
||||
| (BooleanType, BooleanType) -> Ok BooleanType
|
||||
| (BooleanType, _) -> Error (`WrongType "Second term is not a boolean.")
|
||||
| (_, _) -> Error (`WrongType "First term is not a boolean.")
|
||||
)
|
||||
| BNot (x) -> (
|
||||
let* typex = evaluate_type x context in
|
||||
match typex with
|
||||
| (BooleanType) -> Ok BooleanType
|
||||
| (_) -> Error (`WrongType "Term is not a boolean.")
|
||||
)
|
||||
| First (x) -> (
|
||||
let* typex = evaluate_type x context in
|
||||
match typex with
|
||||
| (TupleType (x, _)) -> Ok x
|
||||
| (_) -> Error (`WrongType "Term is not a tuple.")
|
||||
)
|
||||
| Second (x) -> (
|
||||
let* typex = evaluate_type x context in
|
||||
match typex with
|
||||
| (TupleType (_, x)) -> Ok x
|
||||
| (_) -> Error (`WrongType "Term is not a tuple.")
|
||||
)
|
||||
| Cmp (x, y)
|
||||
| CmpLess (x, y)
|
||||
| CmpLessEq (x, y)
|
||||
| CmpGreater (x, y)
|
||||
| CmpGreaterEq (x, y) -> (
|
||||
let* typex = evaluate_type x context in
|
||||
let* typey = evaluate_type y context in
|
||||
match typex, typey with
|
||||
| (IntegerType, IntegerType) -> Ok BooleanType
|
||||
| (IntegerType, _) -> Error (`WrongType "Second term is not an integer.")
|
||||
| (_, _) -> Error (`WrongType "First term is not an integer.")
|
||||
)
|
||||
| IfThenElse (guard, if_exp, else_exp) -> (
|
||||
let* typeguard = evaluate_type guard context in
|
||||
let* typeif_exp = evaluate_type if_exp context in
|
||||
let* typeelse_exp = evaluate_type else_exp context in
|
||||
match typeguard, typeif_exp, typeelse_exp with
|
||||
(BooleanType, t1, t2) -> (
|
||||
if t1 = t2 then
|
||||
Ok t1
|
||||
else
|
||||
Error (`WrongType "If branches do not have the same type.")
|
||||
)
|
||||
| (_, _, _) -> Error (`WrongType "If guard is not a boolean.")
|
||||
)
|
||||
| LetIn (x, xval, rest) ->
|
||||
(* bind the type to the variable name in the context *)
|
||||
let* typex = evaluate_type xval context in
|
||||
evaluate_type rest (VariableMap.add x typex context)
|
||||
| LetFun (f, x, typef, fbody, rest) ->
|
||||
(* like with the function case, but also add f itself to the bindings *)
|
||||
match typef with
|
||||
FunctionType (tin, tout) -> (
|
||||
let newcontext = VariableMap.add f typef context in
|
||||
let newcontextwithx = VariableMap.add x tin newcontext in
|
||||
let* typefbody = evaluate_type fbody newcontextwithx in
|
||||
let* typerest = evaluate_type rest newcontext in
|
||||
match (typefbody = tout, typerest) with
|
||||
(false, _) -> Error (`WrongTypeSpecification
|
||||
"Function does not return specified type.")
|
||||
| (true, t) -> Ok t
|
||||
)
|
||||
| _ -> Error (`WrongTypeSpecification
|
||||
"Specification of function is not a function type.")
|
||||
|
||||
let typecheck (program: t_exp) : (ftype, [> typechecking_error]) result =
|
||||
let* typeprogram = evaluate_type program VariableMap.empty in
|
||||
match typeprogram with
|
||||
FunctionType (IntegerType, IntegerType) -> (
|
||||
Ok (typeprogram)
|
||||
)
|
||||
| _ -> Error (`WrongType "Program is not a function from int to int.")
|
||||
|
||||
@ -1 +1 @@
|
||||
val typecheck : Types.t_exp -> (Types.ftype, Types.error) result
|
||||
val typecheck : Types.t_exp -> (Types.ftype, [> Types.typechecking_error]) result
|
||||
|
||||
@ -8,8 +8,9 @@ module VariableSet = Set.Make(String)
|
||||
type ftype =
|
||||
IntegerType
|
||||
| BooleanType
|
||||
| TupleType of ftype * ftype
|
||||
| PolimorphicType of string
|
||||
| FunctionType of ftype list * ftype
|
||||
| FunctionType of ftype * ftype
|
||||
type fsubstitution = (* goes from polimorphic types to types *)
|
||||
ftype VariableMap.t
|
||||
type fenvironment = (* goes from variables to types *)
|
||||
@ -21,8 +22,9 @@ type t_exp =
|
||||
Integer of int (* x := a *)
|
||||
| Boolean of bool (* v *)
|
||||
| Variable of variable (* x *)
|
||||
| Function of variable list * ftype * t_exp (* lambda x: t. x *)
|
||||
| Application of t_exp * t_exp list (* x x *)
|
||||
| Tuple of t_exp * t_exp (* (a, b) *)
|
||||
| Function of variable * ftype * t_exp (* lambda x: t. x *)
|
||||
| Application of t_exp * t_exp (* x x *)
|
||||
| Plus of t_exp * t_exp (* x + x *)
|
||||
| Minus of t_exp * t_exp (* x - x *)
|
||||
| Times of t_exp * t_exp (* x * x *)
|
||||
@ -31,9 +33,11 @@ type t_exp =
|
||||
| Power of t_exp * t_exp (* x ^ x *)
|
||||
| PowerMod of t_exp * t_exp * t_exp (* (x ^ x) % x *)
|
||||
| Rand of t_exp (* rand(0, x) *)
|
||||
| BAnd of t_exp * t_exp (* x and x *)
|
||||
| BOr of t_exp * t_exp (* x or x *)
|
||||
| BAnd of t_exp * t_exp (* x && x *)
|
||||
| BOr of t_exp * t_exp (* x || x *)
|
||||
| BNot of t_exp (* not x *)
|
||||
| First of t_exp (* fst x *)
|
||||
| Second of t_exp (* scn x *)
|
||||
| Cmp of t_exp * t_exp (* x == x *)
|
||||
| CmpLess of t_exp * t_exp (* x < x *)
|
||||
| CmpLessEq of t_exp * t_exp (* x <= x *)
|
||||
@ -41,14 +45,15 @@ type t_exp =
|
||||
| CmpGreaterEq of t_exp * t_exp (* x >= x *)
|
||||
| IfThenElse of t_exp * t_exp * t_exp (* if b then c else c *)
|
||||
| LetIn of variable * t_exp * t_exp (* let x = x in x *)
|
||||
| LetFun of variable * variable list * ftype * t_exp * t_exp (* let rec x: t. x in x *)
|
||||
| LetFun of variable * variable * ftype * t_exp * t_exp (* let rec x. y: t. x in x*)
|
||||
|
||||
type permittedValues =
|
||||
IntegerPermitted of int
|
||||
| BooleanPermitted of bool
|
||||
IntegerPermitted of int
|
||||
| BooleanPermitted of bool
|
||||
| TuplePermitted of permittedValues * permittedValues
|
||||
| FunctionPermitted of closure
|
||||
and closure = {
|
||||
inputList: variable list;
|
||||
input: variable;
|
||||
body: t_exp;
|
||||
assignments: permittedValues VariableMap.t;
|
||||
recursiveness: variable option
|
||||
@ -58,10 +63,18 @@ type memory = {
|
||||
assignments: permittedValues VariableMap.t
|
||||
}
|
||||
|
||||
type error = [
|
||||
|
||||
type base_error = [
|
||||
`AbsentAssignment of string
|
||||
| `WrongType of string
|
||||
| `DivisionByZero of string
|
||||
| `WrongArity of string
|
||||
]
|
||||
|
||||
type typechecking_error = [
|
||||
| base_error
|
||||
| `WrongTypeSpecification of string
|
||||
]
|
||||
|
||||
type error = [
|
||||
| base_error
|
||||
| `DivisionByZero of string
|
||||
]
|
||||
|
||||
@ -8,8 +8,9 @@ module VariableSet : Set.S with type elt = string
|
||||
type ftype =
|
||||
IntegerType
|
||||
| BooleanType
|
||||
| TupleType of ftype * ftype
|
||||
| PolimorphicType of variable
|
||||
| FunctionType of ftype list * ftype
|
||||
| FunctionType of ftype * ftype
|
||||
type fsubstitution = (* goes from polimorphic types to types *)
|
||||
ftype VariableMap.t
|
||||
type fenvironment = (* goes from variables to types *)
|
||||
@ -47,8 +48,9 @@ type t_exp =
|
||||
Integer of int (* x := a *)
|
||||
| Boolean of bool (* v *)
|
||||
| Variable of variable (* x *)
|
||||
| Function of variable list * ftype * t_exp (* lambda x: t. x *)
|
||||
| Application of t_exp * t_exp list (* x x *)
|
||||
| Tuple of t_exp * t_exp (* (a, b) *)
|
||||
| Function of variable * ftype * t_exp (* lambda x: t. x *)
|
||||
| Application of t_exp * t_exp (* x x *)
|
||||
| Plus of t_exp * t_exp (* x + x *)
|
||||
| Minus of t_exp * t_exp (* x - x *)
|
||||
| Times of t_exp * t_exp (* x * x *)
|
||||
@ -57,9 +59,11 @@ type t_exp =
|
||||
| Power of t_exp * t_exp (* x ^ x *)
|
||||
| PowerMod of t_exp * t_exp * t_exp (* (x ^ x) % x *)
|
||||
| Rand of t_exp (* rand(0, x) *)
|
||||
| BAnd of t_exp * t_exp (* x and x *)
|
||||
| BOr of t_exp * t_exp (* x or x *)
|
||||
| BAnd of t_exp * t_exp (* x && x *)
|
||||
| BOr of t_exp * t_exp (* x || x *)
|
||||
| BNot of t_exp (* not x *)
|
||||
| First of t_exp (* fst x *)
|
||||
| Second of t_exp (* scn x *)
|
||||
| Cmp of t_exp * t_exp (* x == x *)
|
||||
| CmpLess of t_exp * t_exp (* x < x *)
|
||||
| CmpLessEq of t_exp * t_exp (* x <= x *)
|
||||
@ -67,14 +71,15 @@ type t_exp =
|
||||
| CmpGreaterEq of t_exp * t_exp (* x >= x *)
|
||||
| IfThenElse of t_exp * t_exp * t_exp (* if b then c else c *)
|
||||
| LetIn of variable * t_exp * t_exp (* let x = x in x *)
|
||||
| LetFun of variable * variable list * ftype * t_exp * t_exp (* let rec x: t. x in x *)
|
||||
| LetFun of variable * variable * ftype * t_exp * t_exp (* let rec x. y: t. x in x*)
|
||||
|
||||
type permittedValues =
|
||||
IntegerPermitted of int
|
||||
| BooleanPermitted of bool
|
||||
IntegerPermitted of int
|
||||
| BooleanPermitted of bool
|
||||
| TuplePermitted of permittedValues * permittedValues
|
||||
| FunctionPermitted of closure
|
||||
and closure = {
|
||||
inputList: variable list;
|
||||
input: variable;
|
||||
body: t_exp;
|
||||
assignments: permittedValues VariableMap.t;
|
||||
recursiveness: variable option
|
||||
@ -84,10 +89,18 @@ type memory = {
|
||||
assignments: permittedValues VariableMap.t
|
||||
}
|
||||
|
||||
type error = [
|
||||
|
||||
type base_error = [
|
||||
`AbsentAssignment of string
|
||||
| `WrongType of string
|
||||
| `DivisionByZero of string
|
||||
| `WrongArity of string
|
||||
]
|
||||
|
||||
type typechecking_error = [
|
||||
| base_error
|
||||
| `WrongTypeSpecification of string
|
||||
]
|
||||
|
||||
type error = [
|
||||
| base_error
|
||||
| `DivisionByZero of string
|
||||
]
|
||||
|
||||
16
lib/miniFun/dune
Normal file
16
lib/miniFun/dune
Normal file
@ -0,0 +1,16 @@
|
||||
(ocamllex Lexer)
|
||||
|
||||
(menhir
|
||||
(modules Parser)
|
||||
(explain true)
|
||||
(infer true)
|
||||
(flags --dump --table)
|
||||
)
|
||||
|
||||
(library
|
||||
(name miniFun)
|
||||
(public_name miniFun)
|
||||
(modules Lexer Parser Types Semantics TypeChecker)
|
||||
(libraries utility menhirLib))
|
||||
|
||||
(include_subdirs qualified)
|
||||
Reference in New Issue
Block a user