2014-05-19 19:52:21 +00:00
|
|
|
local env = environment()
|
2014-05-18 02:30:43 +00:00
|
|
|
local l = mk_param_univ("l")
|
|
|
|
local U_l = mk_sort(l)
|
2014-06-30 16:14:55 +00:00
|
|
|
local A = Local("A", U_l)
|
2014-07-22 16:43:18 +00:00
|
|
|
local U_l1 = mk_sort(max_univ(l, 1)) -- Make sure U_l1 is not Prop
|
2014-05-18 02:30:43 +00:00
|
|
|
local list_l = Const("list", {l}) -- list.{l}
|
|
|
|
local Nat = Const("nat")
|
|
|
|
local vec_l = Const("vec", {l}) -- vec.{l}
|
|
|
|
local zero = Const("zero")
|
|
|
|
local succ = Const("succ")
|
2014-05-17 16:25:03 +00:00
|
|
|
local forest_l = Const("forest", {l})
|
|
|
|
local tree_l = Const("tree", {l})
|
2014-06-30 16:14:55 +00:00
|
|
|
local n = Local("n", Nat)
|
2014-05-18 21:17:57 +00:00
|
|
|
|
2014-06-13 15:26:05 +00:00
|
|
|
env = env:add_universe("u")
|
|
|
|
env = env:add_universe("v")
|
2014-05-18 21:17:57 +00:00
|
|
|
local u = global_univ("u")
|
|
|
|
local v = global_univ("v")
|
|
|
|
|
|
|
|
function display_type(env, t)
|
2014-05-19 19:52:21 +00:00
|
|
|
print(tostring(t) .. " : " .. tostring(type_checker(env):check(t)))
|
2014-05-18 21:17:57 +00:00
|
|
|
end
|
|
|
|
|
2014-05-18 02:30:43 +00:00
|
|
|
env = add_inductive(env,
|
|
|
|
"nat", Type,
|
|
|
|
"zero", Nat,
|
|
|
|
"succ", mk_arrow(Nat, Nat))
|
|
|
|
-- In the following inductive datatype, {l} is the list of universe level parameters.
|
|
|
|
-- 1 is the number of parameters.
|
2014-07-22 16:43:18 +00:00
|
|
|
-- The Propean true in {A, U_l, true} is marking that this argument is implicit.
|
2014-05-17 20:59:06 +00:00
|
|
|
env = add_inductive(env,
|
2014-06-30 16:14:55 +00:00
|
|
|
"list", {l}, 1, Pi(A, U_l1),
|
|
|
|
"nil", Pi(A, list_l(A)),
|
|
|
|
"cons", Pi(A, mk_arrow(A, list_l(A), list_l(A))))
|
2014-05-17 20:59:06 +00:00
|
|
|
env = add_inductive(env,
|
2014-06-30 16:14:55 +00:00
|
|
|
"vec", {l}, 1, Pi(A, n, U_l1),
|
|
|
|
"vnil", Pi(A, vec_l(A, zero)),
|
|
|
|
"vcons", Pi(A, n, mk_arrow(A, vec_l(A, n), vec_l(A, succ(n)))))
|
2014-05-17 16:25:03 +00:00
|
|
|
|
|
|
|
local And = Const("and")
|
|
|
|
local Or = Const("or")
|
2014-05-18 02:30:43 +00:00
|
|
|
-- Datatype without introduction rules (aka constructors). It is a uninhabited type.
|
2014-07-22 16:43:18 +00:00
|
|
|
env = add_inductive(env, "false", Prop)
|
2014-05-18 02:30:43 +00:00
|
|
|
-- Datatype with a single constructor.
|
2014-07-22 16:43:18 +00:00
|
|
|
env = add_inductive(env, "true", Prop, "trivial", Const("true"))
|
|
|
|
local A = Local("A", Prop)
|
|
|
|
local B = Local("B", Prop)
|
2014-05-17 20:59:06 +00:00
|
|
|
env = add_inductive(env,
|
2014-07-22 16:43:18 +00:00
|
|
|
"and", 2, Pi(A, B, Prop),
|
2014-06-30 16:14:55 +00:00
|
|
|
"and_intro", Pi(A, B, mk_arrow(A, B, And(A, B))))
|
2014-05-17 20:59:06 +00:00
|
|
|
env = add_inductive(env,
|
2014-07-22 16:43:18 +00:00
|
|
|
"or", 2, Pi(A, B, Prop),
|
2014-06-30 16:14:55 +00:00
|
|
|
"or_intro_left", Pi(A, B, mk_arrow(A, Or(A, B))),
|
|
|
|
"or_intro_right", Pi(A, B, mk_arrow(B, Or(A, B))))
|
|
|
|
local A = Local("A", U_l)
|
2014-07-22 16:43:18 +00:00
|
|
|
local P = Local("P", mk_arrow(A, Prop))
|
2014-06-30 16:14:55 +00:00
|
|
|
local a = Local("a", A)
|
2014-05-17 16:25:03 +00:00
|
|
|
local exists_l = Const("exists", {l})
|
2014-05-17 20:59:06 +00:00
|
|
|
env = add_inductive(env,
|
2014-07-22 16:43:18 +00:00
|
|
|
"exists", {l}, 2, Pi(A, P, Prop),
|
2014-06-30 16:14:55 +00:00
|
|
|
"exists_intro", Pi(A, P, a, mk_arrow(P(a), exists_l(A, P))))
|
2014-05-17 16:25:03 +00:00
|
|
|
|
2014-05-17 20:59:06 +00:00
|
|
|
env = add_inductive(env, {l}, 1,
|
2014-06-30 16:14:55 +00:00
|
|
|
{"tree", Pi(A, U_l1),
|
|
|
|
"node", Pi(A, mk_arrow(A, forest_l(A), tree_l(A)))
|
2014-05-17 20:59:06 +00:00
|
|
|
},
|
2014-06-30 16:14:55 +00:00
|
|
|
{"forest", Pi(A, U_l1),
|
|
|
|
"emptyf", Pi(A, forest_l(A)),
|
|
|
|
"consf", Pi(A, mk_arrow(tree_l(A), forest_l(A), forest_l(A)))})
|
2014-05-17 20:59:06 +00:00
|
|
|
local tc = type_checker(env)
|
2014-05-18 21:17:57 +00:00
|
|
|
|
2014-05-19 22:18:48 +00:00
|
|
|
display_type(env, Const("forest", {0}))
|
|
|
|
display_type(env, Const("vcons", {0}))
|
|
|
|
display_type(env, Const("consf", {0}))
|
2014-09-04 22:03:59 +00:00
|
|
|
display_type(env, Const({"forest", "rec"}, {v, u}))
|
|
|
|
display_type(env, Const({"nat", "rec"}, {v}))
|
|
|
|
display_type(env, Const({"or", "rec"}))
|
2014-05-18 21:17:57 +00:00
|
|
|
|
2014-05-18 02:19:32 +00:00
|
|
|
local Even = Const("Even")
|
|
|
|
local Odd = Const("Odd")
|
2014-06-30 16:14:55 +00:00
|
|
|
local b = Local("b", Nat)
|
2014-05-18 02:19:32 +00:00
|
|
|
env = add_inductive(env, {},
|
2014-07-22 16:43:18 +00:00
|
|
|
{"Even", mk_arrow(Nat, Prop),
|
2014-05-18 02:19:32 +00:00
|
|
|
"zero_is_even", Even(zero),
|
2014-06-30 16:14:55 +00:00
|
|
|
"succ_odd", Pi(b, mk_arrow(Odd(b), Even(succ(b))))},
|
2014-07-22 16:43:18 +00:00
|
|
|
{"Odd", mk_arrow(Nat, Prop),
|
2014-06-30 16:14:55 +00:00
|
|
|
"succ_even", Pi(b, mk_arrow(Even(b), Odd(succ(b))))})
|
2014-05-18 03:12:55 +00:00
|
|
|
|
|
|
|
local flist_l = Const("flist", {l})
|
|
|
|
env = add_inductive(env,
|
2014-06-30 16:14:55 +00:00
|
|
|
"flist", {l}, 1, Pi(A, U_l1),
|
|
|
|
"fnil", Pi(A, flist_l(A)),
|
2014-07-22 16:43:18 +00:00
|
|
|
"fcons", Pi(A, mk_arrow(mk_arrow(Nat, A), mk_arrow(Nat, Prop, flist_l(A)), flist_l(A))))
|
2014-05-18 22:39:48 +00:00
|
|
|
|
|
|
|
local eq_l = Const("eq", {l})
|
2014-06-30 16:14:55 +00:00
|
|
|
|
|
|
|
local A = Local("A", U_l)
|
|
|
|
local a = Local("a", A)
|
|
|
|
local b = Local("b", A)
|
2014-05-18 22:39:48 +00:00
|
|
|
env = add_inductive(env,
|
2014-07-22 16:43:18 +00:00
|
|
|
"eq", {l}, 2, Pi(A, a, b, Prop),
|
2014-06-30 16:14:55 +00:00
|
|
|
"refl", Pi(A, a, eq_l(A, a, a)))
|
2014-09-04 22:03:59 +00:00
|
|
|
display_type(env, Const({"eq", "rec"}, {v, u}))
|
|
|
|
display_type(env, Const({"exists", "rec"}, {u}))
|
|
|
|
display_type(env, Const({"list", "rec"}, {v, u}))
|
|
|
|
display_type(env, Const({"Even", "rec"}))
|
|
|
|
display_type(env, Const({"Odd", "rec"}))
|
|
|
|
display_type(env, Const({"and", "rec"}, {v}))
|
|
|
|
display_type(env, Const({"vec", "rec"}, {v, u}))
|
|
|
|
display_type(env, Const({"flist", "rec"}, {v, u}))
|
2014-05-19 19:52:21 +00:00
|
|
|
|
2014-09-04 22:03:59 +00:00
|
|
|
local nat_rec1 = Const({"nat", "rec"}, {1})
|
2014-06-30 16:14:55 +00:00
|
|
|
local a = Local("a", Nat)
|
|
|
|
local b = Local("b", Nat)
|
|
|
|
local n = Local("n", Nat)
|
|
|
|
local c = Local("c", Nat)
|
|
|
|
local add = Fun(a, b, nat_rec1(mk_lambda("_", Nat, Nat), b, Fun(n, c, succ(c)), a))
|
2014-05-19 19:52:21 +00:00
|
|
|
display_type(env, add)
|
|
|
|
local tc = type_checker(env)
|
|
|
|
assert(tc:is_def_eq(add(succ(succ(zero)), succ(zero)),
|
|
|
|
succ(succ(succ(zero)))))
|
|
|
|
assert(tc:is_def_eq(add(succ(succ(succ(zero))), succ(succ(zero))),
|
|
|
|
succ(succ(succ(succ(succ(zero)))))))
|
|
|
|
|
2014-05-19 22:18:48 +00:00
|
|
|
local list_nat = Const("list", {1})(Nat)
|
2014-09-04 22:03:59 +00:00
|
|
|
local list_nat_rec1 = Const({"list", "rec"}, {1, 1})(Nat)
|
2014-05-19 19:52:21 +00:00
|
|
|
display_type(env, list_nat_rec1)
|
2014-06-30 16:14:55 +00:00
|
|
|
local h = Local("h", Nat)
|
|
|
|
local t = Local("t", list_nat)
|
|
|
|
local c = Local("c", Nat)
|
|
|
|
local lst = Local("lst", list_nat)
|
|
|
|
local length = Fun(lst, list_nat_rec1(mk_lambda("_", list_nat, Nat), zero, Fun(h, t, c, succ(c)), lst))
|
2014-05-19 22:18:48 +00:00
|
|
|
local nil_nat = Const("nil", {1})(Nat)
|
|
|
|
local cons_nat = Const("cons", {1})(Nat)
|
2014-05-19 19:52:21 +00:00
|
|
|
print(tc:whnf(length(nil_nat)))
|
|
|
|
assert(tc:is_def_eq(length(nil_nat), zero))
|
|
|
|
assert(tc:is_def_eq(length(cons_nat(zero, nil_nat)), succ(zero)))
|
|
|
|
assert(tc:is_def_eq(length(cons_nat(zero, cons_nat(zero, nil_nat))), succ(succ(zero))))
|
2014-05-19 20:42:49 +00:00
|
|
|
|
2014-05-26 22:54:41 +00:00
|
|
|
env:export("ind1_mod.olean")
|
|
|
|
local env2 = import_modules("ind1_mod")
|
|
|
|
local tc = type_checker(env2)
|
|
|
|
assert(tc:is_def_eq(length(nil_nat), zero))
|
|
|
|
assert(tc:is_def_eq(length(cons_nat(zero, nil_nat)), succ(zero)))
|
|
|
|
assert(tc:is_def_eq(length(cons_nat(zero, cons_nat(zero, nil_nat))), succ(succ(zero))))
|
|
|
|
|
2014-05-19 20:42:49 +00:00
|
|
|
-- Martin-Lof style identity type
|
2014-05-21 23:29:25 +00:00
|
|
|
local env = hott_environment()
|
2014-05-19 20:42:49 +00:00
|
|
|
local Id_l = Const("Id", {l})
|
2014-06-30 16:14:55 +00:00
|
|
|
local A = Local("A", U_l)
|
|
|
|
local a = Local("a", A)
|
|
|
|
local b = Local("b", A)
|
2014-06-13 15:26:05 +00:00
|
|
|
env = env:add_universe("u")
|
|
|
|
env = env:add_universe("v")
|
2014-05-19 20:42:49 +00:00
|
|
|
env = add_inductive(env,
|
2014-06-30 16:14:55 +00:00
|
|
|
"Id", {l}, 1, Pi(A, a, b, U_l),
|
|
|
|
"Id_refl", Pi(A, b, Id_l(A, b, b)))
|
2014-09-04 22:03:59 +00:00
|
|
|
display_type(env, Const({"Id", "rec"}, {v, u}))
|